示例#1
0
        /// <summary>
        /// Reads the bytes from the stream.
        /// </summary>
        private void ReadWork()
        {
            try
            {
                while (true)
                {
                    ushort packetType   = BitConverter.ToUInt16(ReadBytes(2), 0);
                    int    packetLength = BitConverter.ToInt32(ReadBytes(4), 0);
                    byte[] packetData   = ReadBytes(packetLength);

                    if (!typeByte.ContainsKeyB(packetType))
                    {
                        //Theoretically it is not possible that we receive a packet
                        //which is not known, since all the packets need to pass a certification.
                        //But if the UDP connection sends something and the AddPacketTypeRequest get lost
                        //this case is going to happen.
                        HandleUnknownPacket();
                        continue;
                    }
                    Packet receivedPacket = packetConverter.GetPacket(typeByte[packetType], packetData);
                    receivedPackets.Enqueue(receivedPacket);
                    receivedPacket.Size = packetLength;
                    packetAvailableEvent.Set();

                    logger.LogInComingPacket(packetData, receivedPacket);
                }
            }
            catch (ThreadAbortException) { return; }
            catch (Exception exception)
            {
                logger.Log("Reading packet from stream", exception, LogLevel.Exception);
            }

            CloseHandler(CloseReason.ReadPacketThreadException);
        }
示例#2
0
        /// <summary> Reads <see cref="Packet"/> objects from the network and queues them in the <see cref="receivedPackets"/> queue. </summary>
        private void ReadWork()
        {
            try
            {
                while (true)
                {
                    ushort packetType   = BitConverter.ToUInt16(ReadBytes(2), 0);
                    int    packetLength = BitConverter.ToInt32(ReadBytes(4), 0);
                    byte[] packetData   = ReadBytes(packetLength);

                    if (!typeByte.ContainsValue(packetType))
                    {
                        //Theoretically it is not possible that we receive a packet
                        //which is not known, since all the packets need to pass a certification.
                        //But if the UDP connection sends something and the AddPacketTypeRequest get lost
                        //this case is going to happen.
                        //从理论上讲,我们不可能收到未知的数据包,因为所有数据包都需要通过认证。但是,如果UDP连接发送了一些消息,而AddPacketTypeRequest丢失了,则将发生这种情况。
                        HandleUnknownPacket();
                        continue;
                    }

                    Packet receivedPacket = packetConverter.GetPacket(typeByte[packetType], packetData);
                    receivedPackets.Enqueue(receivedPacket);
                    receivedPacket.Size = packetLength;

                    Logger.LogInComingPacket(packetData, receivedPacket);

                    packetAvailableEvent.Set();
                }
            }
            catch (Exception exception)
            {
                if (!threadCancellationTokenSource.IsCancellationRequested)
                {
                    Logger.Log("Reading packet from stream", exception, LogLevel.Exception);
                }
                else
                {
                    return;
                }
            }

            CloseHandler(CloseReason.ReadPacketThreadException);
        }