Exemplo n.º 1
0
        public override void HandlePacket(IPacketStream packet)
        {
            if (this.Socket == null)
            {
                _logger.LogTrace("Skip to handle packet. Reason: client is no more connected.");
                return;
            }

            try
            {
                PacketDeserializeHandler handler;

                if (PacketHandlers.TryGetValue(packet.PacketType, out handler))
                {
                    var deserializedPacket = handler.Invoke(packet);
                    OnPacketArrived?.Invoke(this, deserializedPacket);
                }
                else
                {
                    if (Enum.IsDefined(typeof(PacketType), packet.PacketType))
                    {
                        _logger.LogWarning("Received an unimplemented packet {0} from {1}.", packet.PacketType, this.RemoteEndPoint);
                    }
                    else
                    {
                        _logger.LogWarning("Received an unknown packet 0x{0} from {1}.", ((ushort)packet.PacketType).ToString("X2"), this.RemoteEndPoint);
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.LogError("Packet handle error from {0}. {1}", this.RemoteEndPoint, exception.Message);
                _logger.LogDebug(exception.InnerException?.StackTrace);
            }
        }
        public NetPacketHandler GetPacketHandler(ClientPacketId id)
        {
            if (!PacketHandlers.TryGetValue(id, out var packetHandler))
            {
                throw new Exception($"Missing packet handler for \"{id}\".");
            }

            return(packetHandler);
        }
Exemplo n.º 3
0
        private void HandlePacket(CoCProxyClient client, IPacket packet)
        {
            var handler = (PacketHandler)null;

            if (!PacketHandlers.TryGetValue(packet.ID, out handler))
            {
                return;
            }
            handler(this, client, packet);
        }
Exemplo n.º 4
0
        private void OnMessage(SockChatConnection conn, string msg)
        {
            bool hasSession = conn.Session != null;

            RateLimitState rateLimit = RateLimitState.None;

            if (!hasSession || !Context.RateLimiter.HasRankException(conn.Session.User))
            {
                rateLimit = Context.RateLimiter.BumpConnection(conn);
            }

            Logger.Debug($@"[{conn}] {rateLimit}");
            if (!hasSession && rateLimit == RateLimitState.Drop)
            {
                conn.Close();
                return;
            }

            IEnumerable <string> args = msg.Split(IServerPacket.SEPARATOR);

            if (!Enum.TryParse(args.ElementAtOrDefault(0), out ClientPacketId packetId))
            {
                return;
            }

            if (packetId != ClientPacketId.Authenticate)
            {
                if (!hasSession)
                {
                    return;
                }

                if (rateLimit == RateLimitState.Drop)
                {
                    Context.BanUser(conn.Session.User, Context.RateLimiter.BanDuration, UserDisconnectReason.Flood);
                    return;
                } /*else if(rateLimit == RateLimitState.Warn)
                   * sess.SendPacket(new FloodWarningPacket(Context.Bot));*/
            }

            if (PacketHandlers.TryGetValue(packetId, out IPacketHandler handler))
            {
                handler.HandlePacket(new PacketHandlerContext(args, conn));
            }
        }
Exemplo n.º 5
0
    public void HandleServerPacket(byte[] PacketData)
    {
        //We need to essentially keep reading information from the packet until there is nothing left, at the start of each section of data will be the packet type identifier so you know what
        //comes next and how it should be handled

        //Open up the network packet
        PacketReader Reader = new PacketReader(PacketData);

        //Display how much information there is to be read from this packet
        //Log.Print(Reader.BytesLeft() + " bytes of data left to read from the packet");

        while (!Reader.FinishedReading())
        {
            int PacketType = Reader.ReadInt();
            if (PacketHandlers.TryGetValue(PacketType, out PacketHandle PacketHandle))
            {
                PacketHandle.Invoke(Reader);
            }
        }
    }
Exemplo n.º 6
0
        public override void HandlePacket(IPacketStream packet)
        {
            PacketDeserializeHandler handler;

            if (PacketHandlers.TryGetValue(packet.PacketType, out handler))
            {
                var deserializedPacket = handler.Invoke(packet);
                OnPacketArrived?.Invoke(deserializedPacket);
            }
            else
            {
                if (Enum.IsDefined(typeof(PacketType), packet.PacketType))
                {
                    this.logger.LogWarning("Received an unimplemented packet {0}.", packet.PacketType);
                }
                else
                {
                    this.logger.LogWarning("Received an unknown packet 0x{0}.", ((ushort)packet.PacketType).ToString("X2"));
                }
            }
        }
Exemplo n.º 7
0
        private void OnPacketReceived(object sender, PacketReceivedEventArgs e)
        {
            if (e.Exception == null)
            {
                PacketLog.LogData(e.Packet, PacketDirection.Client);
                var defaultHandler = (PacketHandler)null;
                var handler        = (PacketHandler)null;

                if (DefaultPacketHandlers.TryGetValue(e.Packet.ID, out defaultHandler))
                {
                    defaultHandler(this, e.Packet); // use default handler
                }
                if (PacketHandlers.TryGetValue(e.Packet.ID, out handler))
                {
                    handler(this, e.Packet); // use custom handler
                }
            }
            else
            {
                Console.WriteLine("Failed to read packet: {0}", e.Exception.Message);
            }
        }
        /// <summary>
        ///     Try to process a packet using registered packet handlers
        /// </summary>
        /// <param name="packet"></param>
        /// <returns>True if a handler was found which successfully processed the packet.</returns>
        bool ProcessPacketMethod(Packet packet)
        {
            bool            result = false;
            PacketProcessor pprocessor;

            bool localHandler;

            lock (m_packetHandlers) {
                localHandler = m_packetHandlers.TryGetValue(packet.Type, out pprocessor);
            }
            if (localHandler)
            {
                //there is a local handler for this packet type
                if (pprocessor.Async)
                {
                    object obj = new AsyncPacketProcess(this, pprocessor.method, packet);
                    m_udpServer.FireAndForget(ProcessSpecificPacketAsync, obj);
                    result = true;
                }
                else
                {
                    result = pprocessor.method(this, packet);
                }
                return(result);
            }

            //there is not a local handler so see if there is a Global handler
            PacketMethod method = null;
            bool         found;

            lock (PacketHandlers) {
                found = PacketHandlers.TryGetValue(packet.Type, out method);
            }
            if (found)
            {
                result = method(this, packet);
            }
            return(result);
        }