Exemple #1
0
        /// <summary>
        /// Main Thread when we are in server mode.
        /// Here we always monitor to see if there is any thing ready to receive and if yes, we will pick it up.
        /// NOTE: If the connection become faulty, it will return and stop working
        /// </summary>
        private void WorkerThread()
        {
            while (!m_FaultyFlag && !m_evStop.WaitOne(100, false))
            {
                try
                {
                    if (Client.Available > 0)
                    {
                        // Data available!
                        // We have to process received command
                        Packet receivedData = ReadOnePacket(3000);   // 3 seconds as timeout
                        if (receivedData == null)
                        {
                            continue;
                        }

                        if (receivedData.Command == HANDSHAKERESPONSE_CMDCODE)
                        {
                            m_dtLastHandshake = DateTime.Now;
                        }
                        else if (receivedData.Command == HANDSHAKEREQUEST_CMDCODE)
                        {
                            SendHandshakeResponse();
                        }
                        else
                        {
                            if (OnNewPacketReceived != null)
                            {
                                OnNewPacketReceived.Invoke(receivedData, this);
                            }
                            else
                            {
                                // We have to queue all received packets
                                m_ReceivedPackets.Enqueue(receivedData);
                            }
                        }
                    }
                }
                catch
                {
                    m_FaultyFlag = true;
                }
            }

            return;
        }
 protected virtual void OnNewPacket(Packet packet)
 {
     OnNewPacketReceived?.Invoke(this, packet);
 }