cursed but proper packet handling

This commit is contained in:
rafi1212122
2023-05-27 19:09:32 +07:00
parent 55b943c6f7
commit aa6f0e08c9
3 changed files with 72 additions and 63 deletions

View File

@@ -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));
}
}
}

View File

@@ -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>(T proto, CmdId cmdId)
{
MemoryStream stream = new ();

View File

@@ -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<byte[]> 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])
{
bool found = true;
for (int j = 1; j < packetMagic.Length; j++)
{
if (buffer[i + j] != packetMagic[j])
{
found = false;
break;
}
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;
}
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);
c.Debug($"Found {packets.Count} packet");
// Process the packet
foreach (byte[] packet in packets)
{
if (Packet.IsValid(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;
c.Error("Invalid packet received:", BitConverter.ToString(packet).Replace("-", ""));
}
}
}
}
}
}
catch (Exception ex)
{
if (Client != null)
{
c.Debug("ClientLoop ends");
Server.GetInstance().LogClients();
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(); };
}
public void ProcessPacket(byte[] packet)