public void OnPacket(object sender, PacketReader reader) { RecvOp op = (RecvOp)reader.Read <ushort>(); IPacketHandler <T> handler = Handlers.GetValueOrDefault(op); handler?.Handle(sender as T, reader); }
public bool HandlePacket(Client client, string packet) { try { IPacket deserialized = _deserializer.Deserialize(packet); if (deserialized == null || deserialized is UnknownPacket) { return(true); } if (deserialized is CommandPacket commandPacket) { // TODO : Command manager return(true); } IPacketHandler handler = _handlers.GetValueOrDefault(deserialized.GetType()); if (handler == null) { return(true); } handler.Handle(client, deserialized); return(true); } catch (Exception e) { _logger.Error(e.Message, e); } return(true); }
protected override void HandleReceived(ITcpSocket socket, byte[] data) { if (!socket.IsAlive) { return; } if (!_clients[socket.UnitOfOrder].ContainsKey(socket)) { return; } BafClient client = _clients[socket.UnitOfOrder][socket]; List <BafPacket> packets = client.Receive(data); foreach (BafPacket packet in packets) { if (!_packetHandlers.ContainsKey(packet.Id)) { Logger.Error(client, $"HandleReceived: no packet handler registered for: {packet.Id}"); continue; } IPacketHandler packetHandler = _packetHandlers[packet.Id]; try { packetHandler.Handle(client, packet); } catch (Exception ex) { Logger.Exception(client, ex); } } }
public void OnPacket(object sender, Packet packet) { PacketReader reader = packet.Reader(); ushort op = reader.ReadUShort(); IPacketHandler <T> handler = handlers.GetValueOrDefault((RecvOp)op); handler?.Handle(sender as T, reader); }
public static void HandleAll(this IPacketHandler h, IEntity p, Func<int, PacketMessage> translate, Action action = null) { foreach (var op in h.Handles) { h.Handle(p, translate(op)); action?.Invoke(); } }
public static void SpamTrash(this IPacketHandler h, IEntity ent) { const int defaultShortSize = 1024; // size used for short size opcodes var s = Server(); var p = Player(s); void Spam(int size, bool randomSize, int op) { var iterations = (int)Math.Pow(2, 13); for (var i = 0; i < iterations; i++) { Blob b = randomSize ? RandomData(Rng.Next(0, size + 1)) : RandomData(size); h.Handle(ent, PacketMessage.Success((byte)op, b)); } } if (PacketDb == null) { var dirBuild = Path.GetDirectoryName(typeof(MockServer).GetTypeInfo().Assembly.Location); PacketDb = new JsonPacketDatabase(Path.Combine(dirBuild, "packet-lengths.json")); } foreach (var op in h.Handles) { // chose size var size = PacketDb.GetIncoming(op); switch (size) { case PacketLength.NextByte: Spam(byte.MaxValue, true, op); break; case PacketLength.NextShort: Spam(defaultShortSize, true, op); break; case PacketLength.Undefined: throw new NotSupportedException($"Undefined packet in {h.GetType().Name}"); default: var finalSize = (int) size; if (finalSize == 0) return; Spam(finalSize, false, op); break; } } }
public void ProcessCommands(ConnectManager connMan) { // 一次最多执行10个 for (int i = 0; i < 10; i++) { if (!m_InputStream.HasData()) { return; } int size = m_InputStream.TryReadBuffer(m_temp); //parse packet id and length if (size < 4) { return; } ushort len = BitConverter.ToUInt16(m_temp, 0); ushort packetId = BitConverter.ToUInt16(m_temp, 2); if (size < len) { //还没接收完或者包不完整 return; } m_InputStream.SetReadIndex(len); try { // 调用handle IPacketFactory factory = connMan.GetPacketFactory((Packets.PacketIdDefine)packetId); if (factory != null) { m_tempStream.Seek(0, SeekOrigin.Begin); m_tempStream.SetLength(len - c_headSize); object data = Serializer.Deserialize(factory.GetDataType(), m_tempStream);//GC狂魔 IPacketHandler handler = connMan.GetPacketHandler((Packets.PacketIdDefine)packetId); if (handler != null) { handler.Handle(this, data); } } } catch (Exception e) { LogModule.LogInfo("Handle packet error, Packet id : {0}, len : {1}, msg : {2}", packetId, len, e.Message); Shutdown(); break; } } }
private void HandlePacket(Client client, Packet packet) { Logger.LogUnknownIncomingPacket(client, packet); if (!_packetHandlerLookup.ContainsKey(packet.Id)) { //Logger.LogUnknownIncomingPacket(client, packet); return; } IPacketHandler packetHandler = _packetHandlerLookup[packet.Id]; //Logger.LogIncomingPacket(client, packet); packet.Data.SetPositionStart(); try { packetHandler.Handle(client, packet); } catch (Exception ex) { Logger.Exception(client, ex); } }
protected void Next(object request) { nextHandler?.Handle(request); }