2023-10-10 09:56:08 +00:00
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
using System.Net.Sockets;
|
2023-10-11 20:41:57 +00:00
|
|
|
|
using System.Text.Json;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
using AscNet.Common.Util;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
using MessagePack;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
|
|
|
|
namespace AscNet.GameServer
|
|
|
|
|
{
|
|
|
|
|
public class Session
|
|
|
|
|
{
|
|
|
|
|
public readonly string id;
|
|
|
|
|
public readonly TcpClient client;
|
|
|
|
|
public readonly Logger c;
|
|
|
|
|
private long lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
2023-10-10 10:10:29 +00:00
|
|
|
|
private ushort packetNo = 1;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
private readonly MessagePackSerializerOptions lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
|
|
|
|
public Session(string id, TcpClient tcpClient)
|
|
|
|
|
{
|
|
|
|
|
this.id = id;
|
|
|
|
|
client = tcpClient;
|
|
|
|
|
c = new(id, ConsoleColor.DarkGray);
|
|
|
|
|
|
|
|
|
|
Task.Run(ClientLoop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void ClientLoop()
|
|
|
|
|
{
|
|
|
|
|
NetworkStream stream = client.GetStream();
|
|
|
|
|
byte[] msg = new byte[1 << 16];
|
|
|
|
|
|
|
|
|
|
while (client.Connected)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Array.Clear(msg, 0, msg.Length);
|
|
|
|
|
int len = stream.Read(msg, 0, msg.Length);
|
|
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
|
|
|
|
lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
2023-10-10 09:56:08 +00:00
|
|
|
|
List<Packet> packets = new();
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
2023-10-10 09:56:08 +00:00
|
|
|
|
int readbytes = 0;
|
|
|
|
|
while (readbytes < len)
|
|
|
|
|
{
|
|
|
|
|
int packetLen = BinaryPrimitives.ReadInt32LittleEndian(msg.AsSpan()[readbytes..]);
|
|
|
|
|
readbytes += 4;
|
|
|
|
|
if (packetLen < 4)
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] packet = GC.AllocateUninitializedArray<byte>(packetLen);
|
|
|
|
|
Array.Copy(msg, readbytes, packet, 0, packetLen);
|
|
|
|
|
readbytes += packetLen;
|
|
|
|
|
Crypto.HaruCrypt.Decrypt(packet);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
packets.Add(MessagePackSerializer.Deserialize<Packet>(packet, lz4Options));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
c.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var packet in packets)
|
|
|
|
|
{
|
2023-10-11 13:20:26 +00:00
|
|
|
|
byte[] debugContent = packet.Content;
|
|
|
|
|
try
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-11 13:20:26 +00:00
|
|
|
|
switch (packet.Type)
|
|
|
|
|
{
|
|
|
|
|
case Packet.ContentType.Request:
|
|
|
|
|
Packet.Request request = MessagePackSerializer.Deserialize<Packet.Request>(packet.Content);
|
|
|
|
|
c.Log(request.Name);
|
|
|
|
|
debugContent = request.Content;
|
|
|
|
|
PacketFactory.GetPacketHandler(request.Name)?.Invoke(this, request.Content);
|
|
|
|
|
break;
|
|
|
|
|
case Packet.ContentType.Push:
|
|
|
|
|
Packet.Push push = MessagePackSerializer.Deserialize<Packet.Push>(packet.Content);
|
|
|
|
|
c.Log(push.Name);
|
|
|
|
|
debugContent = push.Content;
|
|
|
|
|
PacketFactory.GetPacketHandler(push.Name)?.Invoke(this, push.Content);
|
|
|
|
|
break;
|
|
|
|
|
case Packet.ContentType.Exception:
|
|
|
|
|
Packet.Exception exception = MessagePackSerializer.Deserialize<Packet.Exception>(packet.Content);
|
|
|
|
|
c.Error($"Exception packet received: {exception.Code}, {exception.Message}");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
c.Error($"Unknown packet received: {packet}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
c.Error("Failed to invoke handler: " + ex.Message + $", Raw {packet.Type} packet: " + BitConverter.ToString(debugContent).Replace("-", ""));
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-08 14:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
await Task.Delay(10);
|
|
|
|
|
// 10 sec timeout
|
|
|
|
|
if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - lastPacketTime > 10000)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisconnectProtocol();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 09:56:08 +00:00
|
|
|
|
public void SendPush<T>(T push)
|
|
|
|
|
{
|
2023-10-11 20:42:26 +00:00
|
|
|
|
try
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-11 20:42:26 +00:00
|
|
|
|
Packet.Push packet = new()
|
|
|
|
|
{
|
|
|
|
|
Name = typeof(T).Name,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(push)
|
|
|
|
|
};
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
2023-10-11 20:42:26 +00:00
|
|
|
|
Packet pushPacket = new Packet()
|
|
|
|
|
{
|
|
|
|
|
No = packetNo,
|
|
|
|
|
Type = Packet.ContentType.Push,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(packet)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Send(pushPacket);
|
|
|
|
|
packetNo++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-11 20:42:26 +00:00
|
|
|
|
c.Error(ex.Message);
|
|
|
|
|
}
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendResponse<T>(T response)
|
|
|
|
|
{
|
|
|
|
|
Packet.Response packet = new()
|
|
|
|
|
{
|
2023-10-10 10:10:29 +00:00
|
|
|
|
Id = 1,
|
2023-10-10 09:56:08 +00:00
|
|
|
|
Name = typeof(T).Name,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(response)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Send(new Packet()
|
|
|
|
|
{
|
|
|
|
|
No = packetNo,
|
|
|
|
|
Type = Packet.ContentType.Response,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(packet)
|
|
|
|
|
});
|
|
|
|
|
c.Log(packet.Name);
|
|
|
|
|
packetNo++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Send(Packet packet)
|
|
|
|
|
{
|
|
|
|
|
byte[] serializedPacket = MessagePackSerializer.Serialize(packet, lz4Options);
|
2023-10-10 10:10:29 +00:00
|
|
|
|
Crypto.HaruCrypt.Encrypt(serializedPacket);
|
|
|
|
|
|
2023-10-10 09:56:08 +00:00
|
|
|
|
byte[] sendBytes = GC.AllocateUninitializedArray<byte>(serializedPacket.Length + 4);
|
|
|
|
|
|
2023-10-10 10:10:29 +00:00
|
|
|
|
BinaryPrimitives.WriteInt32LittleEndian(sendBytes.AsSpan()[0..4], serializedPacket.Length);
|
2023-10-10 09:56:08 +00:00
|
|
|
|
Array.Copy(serializedPacket, 0, sendBytes, 4, serializedPacket.Length);
|
|
|
|
|
|
|
|
|
|
client.GetStream().Write(sendBytes);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 14:12:01 +00:00
|
|
|
|
public void DisconnectProtocol()
|
|
|
|
|
{
|
|
|
|
|
if (Server.Instance.Sessions.GetValueOrDefault(id) is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
c.Warn($"{id} disconnected");
|
|
|
|
|
client.Close();
|
|
|
|
|
Server.Instance.Sessions.Remove(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|