Exemplo n.º 1
0
        private void ProcessPacket(byte type, int size)
        {
            byte[] buffer   = new byte[size];
            int    received = 0;

            while (received < size)
            {
                int read = _socket.Receive(buffer, received, size - received, SocketFlags.None);
                if (read == 0)
                {
                    // eof
                    Disconnect();
                    return;
                }
                received += read;
            }
            _incomingEncryption.Cipher(buffer, 0);
            PacketType     packetType = (PacketType)type;
            IncomingPacket packet     = IncomingPacket.Create(packetType);

            if (packet != null && _hooks.ContainsKey(packetType))
            {
                MemoryStream ms = new MemoryStream(buffer);
                using (PacketInput pi = new PacketInput(ms))
                {
                    packet.Read(pi);
                }
                foreach (var hook in _hooks[packetType].ToArray())
                {
                    try
                    {
                        hook(packet);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ProcessPacket(byte type, int size)
        {
            try
            {
                byte[] buffer   = new byte[size];
                int    received = 0;
                while (received < size)
                {
                    int read = _socket.Receive(buffer, received, size - received, SocketFlags.None);
                    if (read == 0)
                    {
                        // eof
                        Disconnect(DisconnectReason.EofBody.SetDetails("Read was 0."));
                        return;
                    }

                    received += read;
                }

                _incomingEncryption.Cipher(buffer, 0);
                PacketType packetType = type.ToPacketType();

                if (packetType == PacketType.UNKNOWN)
                {
                    return;
                }

                IncomingPacket packet = IncomingPacket.Create(packetType);
                if (packet != null)
                {
                    MemoryStream ms = new MemoryStream(buffer);
                    using (PacketInput pi = new PacketInput(ms))
                    {
                        packet.Read(pi);

                        if (DebugPackets)
                        {
                            Log.Info("Received " + packet.GetPacketType());
                        }
                    }

                    foreach (Action <IncomingPacket> action in _anyPacketHook)
                    {
                        action.Invoke(packet);
                    }

                    if (_hooks.ContainsKey(packetType))
                    {
                        foreach (var hook in _hooks[packetType].ToArray())
                        {
                            try
                            {
                                hook(packet);
                            }
                            catch (Exception e)
                            {
                                Log.Info(e);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Disconnect(DisconnectReason.ExceptionOnProcessPacket.SetDetails(e.Message));
            }
        }