more temporary handlers

This commit is contained in:
rfi
2024-02-19 06:18:44 +07:00
parent 11382f9d20
commit d2552b74b7
6 changed files with 165 additions and 16 deletions

View File

@@ -5,14 +5,16 @@ using ProtoBuf;
using System.Buffers.Binary;
using System.Net;
using System.Net.Sockets;
using System.Security.AccessControl;
using System.Text;
using System.Text.Json;
namespace BLHX.Server.Game
{
public class Connection
{
public readonly Logger c;
readonly TcpClient tcpClient;
readonly Logger c;
readonly CancellationTokenSource cts = new();
readonly Task loopTask;
ushort packetIdx = 0;
@@ -32,6 +34,7 @@ namespace BLHX.Server.Game
{
var ns = tcpClient.GetStream();
var buf = GC.AllocateUninitializedArray<byte>(ushort.MaxValue);
// TODO: pos isn't actually doing anything
var pos = 0;
while (!cts.Token.IsCancellationRequested)
@@ -47,32 +50,51 @@ namespace BLHX.Server.Game
int readLen = 0;
while (readLen < len)
{
if (buf.AsSpan(0, 22).SequenceEqual(Encoding.UTF8.GetBytes("GET /?cmd=load_server?")))
{
string svrList = @"[{""id"":1,""name"":""BLHX.Server"",""state"":0,""flag"":0,""sort"":0}]";
SendHttpResponse(svrList, "application/json");
readLen = len;
cts.Cancel();
break;
}
if (len - readLen < Packet.HEADER_SIZE + Packet.LENGTH_SIZE)
break;
var packet = new Packet(buf[readLen..]);
if (packet.command == Command.Cs10800)
{
var dec = packet.Decode<Cs10800>();
c.Debug(JsonSerializer.Serialize(dec, jsonSerializerOptions));
}
c.Log(packet.command.ToString());
var handler = PacketFactory.GetPacketHandler(packet.command);
if (handler is not null)
handler(this, packet);
else
c.Warn($"{packet.command} unhandled!"
#if DEBUG
, Enum.IsDefined(packet.command) ? BitConverter.ToString(packet.bytes).Replace("-", "") : BitConverter.ToString(buf[readLen..]).Replace("-", "")
#endif
);
readLen += packet.length + Packet.LENGTH_SIZE;
}
pos += len - readLen;
if (pos > 0)
Array.Copy(buf, readLen, buf, 0, pos);
if (len == readLen)
pos = 0;
}
catch (Exception ex)
{
c.Error($"An error occured while reading packets {ex}");
break;
}
}
EndProtocol();
}
public void Send<T>(T packet) where T : IExtensible
{
Command command = Enum.Parse<Command>(packet.GetType().Name);
c.Log(command.ToString());
var ns = tcpClient.GetStream();
using var ms = new MemoryStream();
@@ -85,10 +107,21 @@ namespace BLHX.Server.Game
BinaryPrimitives.WriteUInt16BigEndian(sendBuf.AsSpan(Packet.HEADER_SIZE), NextPacketIdx);
ms.ToArray().CopyTo(sendBuf.AsSpan(Packet.LENGTH_SIZE + Packet.HEADER_SIZE));
c.Debug(BitConverter.ToString(sendBuf).Replace("-", ""));
// c.Debug(BitConverter.ToString(sendBuf).Replace("-", ""));
ns.Write(sendBuf);
}
public void SendHttpResponse(string rsp, string type = "text/plain")
{
tcpClient.GetStream().Write(Encoding.UTF8.GetBytes(
"HTTP/1.1 200 OK" + Environment.NewLine
+ "Content-Length: " + rsp.Length + Environment.NewLine
+ "Content-Type: " + type + Environment.NewLine
+ Environment.NewLine
+ rsp
+ Environment.NewLine + Environment.NewLine));
}
public void EndProtocol()
{
cts.Cancel();