CTFBot.Program.msgthread C# (CSharp) Method

msgthread() static private method

Thread function that runs continuously in the background, sending messages
static private msgthread ( ) : void
return void
        static void msgthread()
        {
            Thread.CurrentThread.Name = "Messaging";
            Thread.CurrentThread.IsBackground = true; //Allow runtime to close this thread at shutdown
            //Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
            logger.Info("Started messaging");

            while (irc.IsConnected) {
                QueuedMessage qm;

                //Console.WriteLine("Lag is " + irc.Lag.ToString());

                // First check for any priority messages to send
                if (priQueue.Count > 0)
                {
                    // We have priority messages to handle
                    lock (priQueue)
                        qm = (QueuedMessage)priQueue.Dequeue();
                }
                else
                {
                    // No priority messages; let's handle the regular-class messages
                    // Do we have any messages to handle?
                    if (fcQueue.Count == 0)
                    {
                        // No messages at all to handle
                        Thread.Sleep(50); // Sleep for 50 miliseconds
                        continue; // Start the loop over
                    }

                    // We do have a message to dequeue, so dequeue it
                    lock (fcQueue)
                        qm = (QueuedMessage)fcQueue.Dequeue();

                    //logger.Info(fcQueue.Count.ToString() + " in normal. sentLength: " + sentLength.ToString());
                }

                // Okay, we now have a message to handle in qm

                // Is our message too old?
                if (qm.IsDroppable && (DateTime.Now.Ticks - qm.SentTime > maxlag))
                {
                    //logger.Info("Lost packet");
                    continue; // Start the loop over
                }

                // If it's okay to send now, but we would exceed the bufflen if we were to send it
                if (!dontSendNow && (sentLength + calculateByteLength(qm) + 2 > bufflen))
                {
                    // Ping the server and wait for a reply
                    irc.RfcPing(targetServerName); //Removed Priority.Critical
                    irc.LastPingSent = DateTime.Now; //Hacked SmartIrc4net
                    sendlock.Reset();
                    dontSendNow = true;
                    //logger.Info("Waiting for artificial PONG");
                }

                // Sleep while it's not okay to send
                while (dontSendNow)
                    Thread.Sleep(1000);
                //sendlock.WaitOne();

                // Okay, we can carry on now. Is our message still fresh?
                if (qm.IsDroppable && (DateTime.Now.Ticks - qm.SentTime > maxlag))
                // Oops, sowwy. Our message has rotten.
                {
                    //logger.Info("Lost packet");
                    continue; // Start the loop over
                }

                // At last! Send the damn thing!
                // ...but only if we're still connected
                if (irc.IsConnected)
                {
                    sentLength = sentLength + calculateByteLength(qm) + 2;
                    irc.SendMessage(qm.type, qm.destination, qm.message);
                }

                //logger.Info("Lag was " + (DateTime.Now.Ticks - qm.SentTime));

                // Throttle on our part
                Thread.Sleep(300);
            }

            logger.Info("Thread ended");
        }