diff --git a/GameServer/Handlers/GetActivityMainDataReqHandler.cs b/GameServer/Handlers/GetActivityMainDataReqHandler.cs new file mode 100644 index 0000000..1fe0233 --- /dev/null +++ b/GameServer/Handlers/GetActivityMainDataReqHandler.cs @@ -0,0 +1,18 @@ +using Common.Resources.Proto; + +namespace PemukulPaku.GameServer.Handlers +{ + + [PacketCmdId(CmdId.GetActivityMainDataReq)] + internal class GetActivityMainDataReqHandler : IPacketHandler + { + public void Handle(Session session, Packet _packet) + { + session.Send(Packet.FromProto(new GetActivityMainDataRsp() + { + retcode = GetActivityMainDataRsp.Retcode.Succ, + ActivityModuleTypeLists = new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 72 } + }, CmdId.GetActivityMainDataRsp)); + } + } +} diff --git a/GameServer/Packet.cs b/GameServer/Packet.cs index 9b4cbec..0c3580b 100644 --- a/GameServer/Packet.cs +++ b/GameServer/Packet.cs @@ -54,6 +54,13 @@ namespace PemukulPaku.GameServer return SerializedBody; } + public static bool IsValid(byte[] data) + { + string hexString = BitConverter.ToString(data).Replace("-", ""); + return hexString.StartsWith("01234567", StringComparison.OrdinalIgnoreCase) && + hexString.EndsWith("89ABCDEF", StringComparison.OrdinalIgnoreCase); + } + public static Packet FromProto(T proto, CmdId cmdId) { MemoryStream stream = new (); diff --git a/GameServer/Session.cs b/GameServer/Session.cs index 0eca479..a028026 100644 --- a/GameServer/Session.cs +++ b/GameServer/Session.cs @@ -1,4 +1,7 @@ -using System.Net.Sockets; +using System.Data.SqlTypes; +using System; +using System.IO; +using System.Net.Sockets; using Common; using Common.Resources.Proto; using Common.Utils; @@ -18,84 +21,65 @@ namespace PemukulPaku.GameServer Id = id; Client = client; c = new Logger(Id); - Task.Run(() => ClientLoop(client)); + Task.Run(ClientLoop); } - private void ClientLoop(TcpClient client) + private void ClientLoop() { - NetworkStream stream = client.GetStream(); + NetworkStream stream = Client.GetStream(); byte[] packetMagic = { 0x01, 0x23, 0x45, 0x67 }; // Magic start pattern byte[] packetEnd = { 0x89, 0xAB, 0xCD, 0xEF }; // Magic end pattern - byte[] buffer = new byte[4096]; - int bytesRead; + byte[] msg = new byte[1 << 16]; - try + while (Client.Connected) { - while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) + Array.Clear(msg, 0, msg.Length); + int len = stream.Read(msg, 0, msg.Length); + + if(len > 0) { - // Process the received bytes - for (int i = 0; i < bytesRead; i++) + List packets = new (); + + string CursedMsg = BitConverter.ToString(msg).Replace("-", ""); + string CursedMagic = BitConverter.ToString(packetMagic).Replace("-", ""); + string CursedEnd = BitConverter.ToString(packetEnd).Replace("-", ""); + + int MagicIndex = 0; + int EndIndex = 0; + + while ((MagicIndex = CursedMsg.IndexOf(CursedMagic, MagicIndex)) != -1 && (EndIndex = CursedMsg.IndexOf(CursedEnd, EndIndex)) != -1) { - if (buffer[i] == packetMagic[0]) + EndIndex += 8; + byte[] packet = new byte[EndIndex / 2 - MagicIndex / 2]; + Array.Copy(msg, MagicIndex / 2, packet, 0, EndIndex / 2 - MagicIndex / 2); + packets.Add(packet); + MagicIndex += MagicIndex; + EndIndex += EndIndex; + } + + c.Debug($"Found {packets.Count} packet"); + + foreach (byte[] packet in packets) + { + if (Packet.IsValid(packet)) { - bool found = true; - for (int j = 1; j < packetMagic.Length; j++) - { - if (buffer[i + j] != packetMagic[j]) - { - found = false; - break; - } - } - - if (found) - { - // Magic start pattern found - int endIndex = Array.IndexOf(buffer, packetEnd[0], i + packetMagic.Length); - if (endIndex != -1) - { - // Magic end pattern found - int packetLength = endIndex - i + packetEnd.Length; - byte[] packet = new byte[packetLength]; - Array.Copy(buffer, i, packet, 0, packetLength); - - // Process the packet - ProcessPacket(packet); - - // Update the buffer - int remainingBytes = bytesRead - (i + packetLength); - Array.Copy(buffer, i + packetLength, buffer, 0, remainingBytes); - - // Adjust the bytesRead value accordingly - bytesRead = remainingBytes; - i = -1; // Start processing from the beginning of the buffer again - } - else - { - // End pattern not found, break the loop and wait for more data - break; - } - } + ProcessPacket(packet); + } + else + { + c.Error("Invalid packet received:", BitConverter.ToString(packet).Replace("-", "")); } } } } - catch (Exception ex) - { - if (Client != null) - { - c.Warn($"{Id} disconnected"); - Server.GetInstance().Sessions.Remove(Id); - c.Debug("TCP client disconnect reason: " + ex.Message); - } - else - { - c.Error("TCP client error: " + ex.Message); - } - } - finally { Server.GetInstance().LogClients(); }; + + c.Debug("ClientLoop ends"); + Server.GetInstance().LogClients(); + + c.Warn($"{Id} disconnected"); + Server.GetInstance().Sessions.Remove(Id); } public void ProcessPacket(byte[] packet)