public static void GameUpdateHandler(IPEndPoint ep, byte[] bytesReceived) { Console.WriteLine("Received 'Game Update' Packet from {0}:{1}", bytesReceived.Length, ep.Address, ep.Port); ClientGamePacket packet = GetGamePacketFromBytes(bytesReceived); bool accepted = true; for (int i = 0; i < players.Count; i++) { if (packet.ID == players[i].ID) { if (players[i].ep == null) { players[i] = SetPlayerEP(players[i], ep); } if (players[i].dead) { accepted = false; } break; } } if (CheckPlayerLogged(packet.ID) && accepted) { Broadcast(ConnectionType.UDP, GetBytesFromPacket(packet)); } }
protected override void OnData(byte[] data) { using (var stream = new MemoryStream(data)) using (var reader = new GamePacketReader(stream)) { while (stream.Remaining() != 0) { // no packet on deck waiting for additional information, new data will be part of a new packet if (onDeck == null) { uint size = reader.ReadUInt(); onDeck = new FragmentedBuffer(size - sizeof(uint)); } onDeck.Populate(reader); if (onDeck.IsComplete) { var packet = new ClientGamePacket(onDeck.Data); incomingPackets.Enqueue(packet); onDeck = null; } } } }
public void HandleEncryptedPacket(ClientEncrypted encrypted) { byte[] data = encryption.Decrypt(encrypted.Data, encrypted.Data.Length); var packet = new ClientGamePacket(data); HandlePacket(packet); }
protected void HandlePacket(ClientGamePacket packet) { IReadable message = MessageManager.GetMessage(packet.Opcode); if (message == null) { log.Warn($"Received unknown packet {packet.Opcode:X}"); return; } MessageHandlerDelegate handlerInfo = MessageManager.GetMessageHandler(packet.Opcode); if (handlerInfo == null) { log.Warn($"Received unhandled packet {packet.Opcode}(0x{packet.Opcode:X})."); return; } if (packet.Opcode != GameMessageOpcode.ClientEncrypted && packet.Opcode != GameMessageOpcode.ClientPacked && packet.Opcode != GameMessageOpcode.ClientPackedWorld && packet.Opcode != GameMessageOpcode.ClientEntityCommand) { log.Trace($"Received packet {packet.Opcode}(0x{packet.Opcode:X})."); } // FIXME workaround for now. possible performance impact. // ClientPing does not currently work and the session times out after 300s -> this keeps the session alive if -any- client packet is received Heartbeat.OnHeartbeat(); using (var stream = new MemoryStream(packet.Data)) using (var reader = new GamePacketReader(stream)) { message.Read(reader); if (reader.BytesRemaining > 0) { log.Warn($"Failed to read entire contents of packet {packet.Opcode}"); } try { handlerInfo.Invoke(this, message); } catch (InvalidPacketValueException exception) { log.Error(exception); RequestedDisconnect = true; } catch (Exception exception) { log.Error(exception); } } }
static ClientGamePacket GetGamePacketFromBytes(byte[] buffer) { ClientGamePacket packet = new ClientGamePacket(); int size = Marshal.SizeOf(packet); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(buffer, 0, ptr, size); packet = (ClientGamePacket)Marshal.PtrToStructure(ptr, packet.GetType()); Marshal.FreeHGlobal(ptr); return(packet); }
static byte[] GetBytesFromPacket(ClientGamePacket packet) { int size = Marshal.SizeOf(packet); byte[] bytes = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(packet, ptr, true); Marshal.Copy(ptr, bytes, 0, size); Marshal.FreeHGlobal(ptr); return(bytes); }
private void HandlePacket(ClientGamePacket packet) { IReadable message = MessageManager.GetMessage(packet.Opcode); if (message == null) { log.Warn($"Received unknown packet {packet.Opcode:X}"); return; } MessageHandlerDelegate handlerInfo = MessageManager.GetMessageHandler(packet.Opcode); if (handlerInfo == null) { log.Warn($"Received unhandled packet {packet.Opcode}(0x{packet.Opcode:X})."); return; } if (packet.Opcode != GameMessageOpcode.ClientEncrypted && packet.Opcode != GameMessageOpcode.ClientPacked && packet.Opcode != GameMessageOpcode.ClientEntityCommand) { log.Trace($"Received packet {packet.Opcode}(0x{packet.Opcode:X})."); } using (var stream = new MemoryStream(packet.Data)) using (var reader = new GamePacketReader(stream)) { message.Read(reader); if (reader.BytesRemaining > 0) { log.Warn($"Failed to read entire contents of packet {packet.Opcode}"); } try { handlerInfo.Invoke(this, message); } catch (InvalidPacketValueException exception) { log.Error(exception); OnDisconnect(); } catch (Exception exception) { log.Error(exception); } } }
public void HandleEncryptedPacket(ClientEncrypted encrypted) { byte[] data = encryption.Decrypt(encrypted.Data, encrypted.Data.Length); // TODO: research this... if (data[0] == 0x8C) { byte[] dataHack = new byte[data.Length - 7]; Buffer.BlockCopy(data, 7, dataHack, 0, dataHack.Length); var packet = new ClientGamePacket(dataHack); HandlePacket(packet); } else { var packet = new ClientGamePacket(data); HandlePacket(packet); } }
public void HandlePacked(ClientPacked packed) { var packet = new ClientGamePacket(packed.Data); HandlePacket(packet); }