/// <summary> /// Process the player requests. /// This method fires the <see cref="OnProcessPacket"/> event to be hooked up by plugins. /// </summary> public EPlayerRequestResult ProcessPlayerRequest(CPlayer player) { EPlayerRequestResult result = EPlayerRequestResult.NO_ERROR; foreach (DProcessPacket target in OnProcessPacket.GetInvocationList()) { result = target(this, player); if (result != EPlayerRequestResult.NO_ERROR && result != EPlayerRequestResult.PACKET_NOT_HANDLED) { W2Log.Write("eita", ELogType.CRITICAL_ERROR); break; } } return(result); }
/// <summary> /// Process the newly connected client. /// </summary> /// <param name="client">The newly connected client.</param> private async void ProcessClient_Channel1(TcpClient client) { using (client) using (NetworkStream stream = client.GetStream()) { CPlayer player = new CPlayer(stream); CCompoundBuffer packet = player.RecvPacket; try { // Iterate processing incoming player packets until he disconnects. while (player.State != EPlayerState.CLOSED) { int readCount = await stream.ReadAsync(packet.RawBuffer, 0, NetworkBasics.MAXL_PACKET); if (readCount != 4 && (readCount < 12 || readCount > NetworkBasics.MAXL_PACKET)) // Invalid game packet. { gameController.DisconnectPlayer(player); break; } else // Possible valid game packet chunk. { unsafe { packet.Offset = 0; fixed(byte *PinnedPacketChunk = &packet.RawBuffer[packet.Offset]) { // Check for the init code. if (*(uint *)&PinnedPacketChunk[packet.Offset] == NetworkBasics.INIT_CODE) { packet.Offset = 4; // If a valid index can't be assigned to the player, disconnect him if (!gameController.TryInsertPlayer(player)) { player.SendPacket(MTextMessagePacket.Create("O servidor está lotado. Tente novamente mais tarde.")); gameController.DisconnectPlayer(player); continue; } // If all the received chunk resumes to the INIT_CODE, read the next packet. if (readCount == 4) { continue; } } // Process all possible packets that were possibly sent together. while (packet.Offset < readCount && player.State != EPlayerState.CLOSED) { // Check if the game packet size is bigger than the remaining received chunk. if (packet.ReadNextUShort(0) > readCount - packet.Offset || packet.ReadNextUShort(0) < 12) { throw new Exception("Invalid received packet. Reading packet is bigger than the remaining chunk."); } // Tries to decrypt the packet. if (!PacketSecurity.Decrypt(packet)) { throw new Exception($"Can't decrypt a packet received from {client.Client.RemoteEndPoint}."); } W2Log.Write(String.Format("Processing recv packet {{0x{0:X}/{1}}} from {2}.", packet.ReadNextUShort(4), packet.ReadNextUShort(0), client.Client.RemoteEndPoint), ELogType.NETWORK); // Process the incoming packet. EPlayerRequestResult requestResult = gameController.ProcessPlayerRequest(player); // Treat the processing packet return. switch (requestResult) { case EPlayerRequestResult.PACKET_NOT_HANDLED: { W2Log.Write(String.Format("Recv packet {{0x{0:X}/{1}}} from {2} didn't was processed.", packet.ReadNextUShort(4), packet.ReadNextUShort(0), client.Client.RemoteEndPoint), ELogType.NETWORK); byte[] rawPacket = new byte[packet.ReadNextUShort(0)]; for (int i = 0; i < rawPacket.Length; i++) { rawPacket[i] = PinnedPacketChunk[i + packet.Offset]; } File.WriteAllBytes($@"..\..\Dumped Packets\Inner Packets\{packet.ReadNextUShort(4)}.bin", rawPacket); break; } case EPlayerRequestResult.CHECKSUM_FAIL: { W2Log.Write($"Recv packet from {client.Client.RemoteEndPoint} have invalid checksum.", ELogType.CRITICAL_ERROR); gameController.DisconnectPlayer(player); break; } case EPlayerRequestResult.PLAYER_INCONSISTENT_STATE: { W2Log.Write($"A player was disconnected due to inconsistent PlayerState.", ELogType.CRITICAL_ERROR); gameController.DisconnectPlayer(player); break; } } // Correct the offset to process the next packet in the received chunk. player.RecvPacket.Offset += packet.ReadNextUShort(0); } } } } } } catch (Exception ex) { W2Log.Write($"An unhandled exception happened processing the player {player.Index}. MSG: {ex.Message}", ELogType.CRITICAL_ERROR); } finally { gameController.DisconnectPlayer(player); } } }