Пример #1
0
 public static void BeginReadPacket(Socket skt, ClientProcessor psr, Action <Packet> callback, Action failure)
 {
     byte[] n = new byte[5];
     skt.BeginReceive(n, 0, 5, 0, ar =>
     {
         try
         {
             byte[] x = (byte[])ar.AsyncState;
             int len  = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(x, 0));
             if (len == 0)
             {
                 failure();
                 return;
             }
             len           -= 5;
             byte id        = x[4];
             Packet packet  = Packets[(PacketID)id].CreateInstance();
             byte[] content = new byte[len];
             if (len > 0)
             {
                 skt.Receive(content);
             }
             packet.Read(psr, new NReader(new MemoryStream(packet.Crypt(psr, content))));
             callback(packet);
         }
         catch { failure(); }
     }, n);
 }
Пример #2
0
        public static Packet ReadPacket(NReader rdr, ClientProcessor psr)
        {
            int    len    = rdr.ReadInt32() - 5;
            byte   id     = rdr.ReadByte();
            Packet packet = Packets[(PacketID)id].CreateInstance();

            byte[] content = rdr.ReadBytes(len);
            packet.Read(psr, new NReader(new MemoryStream(packet.Crypt(psr, content))));
            return(packet);
        }
Пример #3
0
        private void IOCompleted(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                bool repeat;
                do
                {
                    repeat = false;

                    if (e.SocketError != SocketError.Success)
                    {
                        throw new SocketException((int)e.SocketError);
                    }

                    if (e.LastOperation == SocketAsyncOperation.Receive)
                    {
                        switch (receiveState)
                        {
                        case ReceiveState.ReceivingHdr:
                            if (debug)
                            {
                                Console.WriteLine(@"test");
                            }

                            if (e.BytesTransferred < 5)
                            {
                                parent.Disconnect();
                                return;
                            }

                            if (debug)
                            {
                                Console.WriteLine(@"test2");
                            }

                            if (e.Buffer[0] == 0x3c && e.Buffer[1] == 0x70 &&
                                e.Buffer[2] == 0x6f && e.Buffer[3] == 0x6c && e.Buffer[4] == 0x69)
                            {
                                ProcessPolicyFile();
                                return;
                            }

                            int len = (e.UserToken as ReceiveToken).Length =
                                IPAddress.NetworkToHostOrder(BitConverter.ToInt32(e.Buffer, 0)) - 5;
                            if (len < 0 || len > BUFFER_SIZE)
                            {
                                throw new InternalBufferOverflowException();
                            }
                            (e.UserToken as ReceiveToken).Packet =
                                Packet.Packets[(PacketID)e.Buffer[4]].CreateInstance();
                            if (debug)
                            {
                                Console.WriteLine("test3 - " + (e.UserToken as ReceiveToken).Packet.GetType().Name);
                            }

                            receiveState = ReceiveState.ReceivingBody;
                            e.SetBuffer(0, len);
                            if (!skt.ReceiveAsync(e))
                            {
                                repeat = true;
                            }
                            break;

                        case ReceiveState.ReceivingBody:
                            if (e.BytesTransferred < (e.UserToken as ReceiveToken).Length)
                            {
                                parent.Disconnect();
                                return;
                            }

                            Packet pkt = (e.UserToken as ReceiveToken).Packet;
                            pkt.Read(parent, e.Buffer, (e.UserToken as ReceiveToken).Length);

                            receiveState = ReceiveState.Processing;
                            bool cont = OnPacketReceived(pkt);

                            if (cont && skt.Connected)
                            {
                                receiveState = ReceiveState.ReceivingHdr;
                                e.SetBuffer(0, 5);
                                if (!skt.ReceiveAsync(e))
                                {
                                    repeat = true;
                                }
                            }
                            break;

                        default:
                            throw new InvalidOperationException(e.LastOperation.ToString());
                        }
                    }
                    else if (e.LastOperation == SocketAsyncOperation.Send)
                    {
                        switch (sendState)
                        {
                        case SendState.Ready:
                            byte[] dat = (e.UserToken as SendToken).Packet.Write(parent);

                            sendState = SendState.Sending;
                            e.SetBuffer(dat, 0, dat.Length);
                            if (!skt.SendAsync(e))
                            {
                                repeat = true;
                            }
                            break;

                        case SendState.Sending:
                            (e.UserToken as SendToken).Packet = null;

                            if (CanSendPacket(e, true))
                            {
                                repeat = true;
                            }
                            break;

                        default:
                            throw new InvalidOperationException(e.LastOperation.ToString());
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(e.LastOperation.ToString());
                    }
                } while (repeat);
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
        }