Esempio n. 1
0
        public void AsyncReceive(IAsyncResult ar)
        {
            ReceiveBuffer = (byte[])ar.AsyncState;
            try
            {
                RecvSize = Socket.EndReceive(ar, out var error);

                if (error == SocketError.Success && RecvSize > 4)
                {
                    if (Crypto != null)
                    {
                        ReceiveBuffer = Crypto.Decrypt(ReceiveBuffer, RecvSize);
                    }


                    ushort size;
                    for (var i = 0; i < RecvSize; i += size)
                    {
                        size = BitConverter.ToUInt16(ReceiveBuffer, i);

                        if (size == RecvSize)
                        {
                            var packet = new IncommingPacket(this, BufferPool.Clone(ReceiveBuffer));
                            IncommingPacketQueue.Add(packet);
                        }
                        else if (size < RecvSize)
                        {
                            var firstPacket = new byte[size];
                            Buffer.BlockCopy(ReceiveBuffer, i, firstPacket, 0, size);


                            var packet = new IncommingPacket(this, BufferPool.Clone(firstPacket));
                            IncommingPacketQueue.Add(packet);
                        }
                        else
                        {
                            Output.WriteLine($"Packet: {BitConverter.ToUInt16(ReceiveBuffer, 2)} Len: {BitConverter.ToUInt16(ReceiveBuffer, 0)} Actual: {size}");
                            Output.WriteLine(ReceiveBuffer.HexDump());
                        }
                    }

                    Socket.BeginReceive(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, AsyncReceive, ReceiveBuffer);
                }
                else
                {
                    Disconnect();
                }
            }
            catch (Exception ex)
            {
                if (!(ex is ObjectDisposedException))
                {
                    Output.WriteLine($"Disconnect Reason: {ex.Message} \r\n {ex.StackTrace}");
                    Output.WriteLine(ex);
                }
                Disconnect();
            }
        }
Esempio n. 2
0
 public static void Add(IncommingPacket packet)
 {
     Queue.Enqueue(packet);
     SyncBlock.Set();
 }