2023-10-10 09:56:08 +00:00
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Net.Sockets;
|
2023-12-11 13:13:37 +00:00
|
|
|
|
using System.Reflection.Emit;
|
2023-10-14 15:01:49 +00:00
|
|
|
|
using AscNet.Common;
|
2023-10-16 09:53:53 +00:00
|
|
|
|
using AscNet.Common.Database;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
using AscNet.Common.Util;
|
2023-12-07 11:37:38 +00:00
|
|
|
|
using AscNet.GameServer.Game;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
using AscNet.Logging;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
using MessagePack;
|
2023-10-11 21:15:35 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
using Logger = AscNet.Logging.Logger;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
|
|
|
|
namespace AscNet.GameServer
|
|
|
|
|
{
|
|
|
|
|
public class Session
|
|
|
|
|
{
|
|
|
|
|
public readonly string id;
|
|
|
|
|
public readonly TcpClient client;
|
2023-10-16 09:53:53 +00:00
|
|
|
|
public Player player = default!;
|
2023-10-19 07:07:48 +00:00
|
|
|
|
public Character character = default!;
|
|
|
|
|
public Stage stage = default!;
|
2023-12-07 11:37:38 +00:00
|
|
|
|
public Fight? fight;
|
2023-11-15 01:01:16 +00:00
|
|
|
|
public Inventory inventory = default!;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
public readonly Logger log;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
private long lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
2023-10-14 13:00:56 +00:00
|
|
|
|
private ushort packetNo = 0;
|
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;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
// TODO: add session based configuration? maybe from database?
|
2023-10-14 18:02:51 +00:00
|
|
|
|
log = new(typeof(Session), id, LogLevel.DEBUG, LogLevel.DEBUG);
|
|
|
|
|
log.LogLevelColor[LogLevel.INFO] = ConsoleColor.Cyan;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
Task.Run(ClientLoop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void ClientLoop()
|
|
|
|
|
{
|
|
|
|
|
NetworkStream stream = client.GetStream();
|
2023-11-12 01:58:59 +00:00
|
|
|
|
int prevBuf = 0;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
byte[] msg = new byte[1 << 16];
|
|
|
|
|
|
|
|
|
|
while (client.Connected)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-12-11 13:13:37 +00:00
|
|
|
|
int len = 0;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
2023-12-11 13:13:37 +00:00
|
|
|
|
read:
|
|
|
|
|
while (stream.DataAvailable)
|
|
|
|
|
{
|
|
|
|
|
len = stream.Read(msg, prevBuf, msg.Length - prevBuf);
|
|
|
|
|
prevBuf += len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevBuf > 0)
|
2023-10-08 14:12:01 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
2023-12-11 13:13:37 +00:00
|
|
|
|
while (readbytes < prevBuf)
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
|
|
|
|
int packetLen = BinaryPrimitives.ReadInt32LittleEndian(msg.AsSpan()[readbytes..]);
|
2023-12-11 13:13:37 +00:00
|
|
|
|
if (prevBuf > 0)
|
2023-11-24 13:01:26 +00:00
|
|
|
|
readbytes += 4;
|
2023-11-12 01:58:59 +00:00
|
|
|
|
if (packetLen < 1)
|
|
|
|
|
{
|
2023-12-11 13:13:37 +00:00
|
|
|
|
prevBuf = 0;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
break;
|
2023-11-12 01:58:59 +00:00
|
|
|
|
}
|
2023-12-11 13:13:37 +00:00
|
|
|
|
if (packetLen > prevBuf)
|
2023-11-12 01:58:59 +00:00
|
|
|
|
{
|
2023-12-11 13:13:37 +00:00
|
|
|
|
goto read;
|
2023-11-12 01:58:59 +00:00
|
|
|
|
}
|
2023-10-10 09:56:08 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-12 01:58:59 +00:00
|
|
|
|
log.Debug(BitConverter.ToString(msg).Replace("-", ""));
|
2023-12-11 13:13:37 +00:00
|
|
|
|
log.Debug($"PacketLen = {packetLen}, ReadLen = {prevBuf}");
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", ""));
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-11 13:13:37 +00:00
|
|
|
|
prevBuf = 0;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
debugContent = request.Content;
|
2023-10-14 13:00:56 +00:00
|
|
|
|
|
|
|
|
|
RequestPacketHandlerDelegate? requestPacketHandler = PacketFactory.GetRequestPacketHandler(request.Name);
|
|
|
|
|
if (requestPacketHandler is not null)
|
|
|
|
|
{
|
2023-10-14 17:39:26 +00:00
|
|
|
|
// TODO: with new logger this will be unnecessary
|
2023-10-14 15:01:49 +00:00
|
|
|
|
if (Common.Common.config.VerboseLevel > VerboseLevel.Silent)
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Info($"{request.Name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
2023-10-14 13:00:56 +00:00
|
|
|
|
requestPacketHandler.Invoke(this, request);
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-10-14 15:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
if (Common.Common.config.VerboseLevel > VerboseLevel.Silent)
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Warn($"{request.Name} handler not found!{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
2023-10-14 15:01:49 +00:00
|
|
|
|
}
|
2023-10-11 13:20:26 +00:00
|
|
|
|
break;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
|
2023-10-11 13:20:26 +00:00
|
|
|
|
case Packet.ContentType.Push:
|
|
|
|
|
Packet.Push push = MessagePackSerializer.Deserialize<Packet.Push>(packet.Content);
|
|
|
|
|
debugContent = push.Content;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Info(push.Name);
|
2023-10-14 13:00:56 +00:00
|
|
|
|
throw new NotImplementedException($"Packet push handlers not implemented ({push.Name})");
|
2023-10-14 17:39:26 +00:00
|
|
|
|
|
2023-10-11 13:20:26 +00:00
|
|
|
|
case Packet.ContentType.Exception:
|
|
|
|
|
Packet.Exception exception = MessagePackSerializer.Deserialize<Packet.Exception>(packet.Content);
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Error($"Exception packet received: {exception.Code}, {exception.Message}");
|
2023-10-11 13:20:26 +00:00
|
|
|
|
break;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
|
2023-10-11 13:20:26 +00:00
|
|
|
|
default:
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Error($"Unknown packet received: {packet}");
|
2023-10-11 13:20:26 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-11-24 13:01:26 +00:00
|
|
|
|
log.Error("Failed to invoke handler: " + ex.ToString() + $", 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-15 05:43:20 +00:00
|
|
|
|
public void SendPush<T>(T push) where T : new()
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-11 21:15:35 +00:00
|
|
|
|
Packet.Push packet = new()
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-14 13:07:07 +00:00
|
|
|
|
Name = typeof(T).Name,
|
2023-10-11 21:15:35 +00:00
|
|
|
|
Content = MessagePackSerializer.Serialize(push)
|
|
|
|
|
};
|
|
|
|
|
Send(new Packet()
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
No = ++packetNo,
|
2023-10-11 21:15:35 +00:00
|
|
|
|
Type = Packet.ContentType.Push,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(packet)
|
|
|
|
|
});
|
2023-10-15 05:43:20 +00:00
|
|
|
|
log.Info($"{packet.Name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(push)) : "")}");
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public void SendPush(string name, byte[] push)
|
|
|
|
|
{
|
|
|
|
|
Packet.Push packet = new()
|
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
Content = push
|
|
|
|
|
};
|
|
|
|
|
Send(new Packet()
|
|
|
|
|
{
|
|
|
|
|
No = ++packetNo,
|
|
|
|
|
Type = Packet.ContentType.Push,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(packet)
|
|
|
|
|
});
|
2023-10-15 05:43:20 +00:00
|
|
|
|
log.Info($"{name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(push))) : "")}");
|
2023-10-14 13:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 17:34:14 +00:00
|
|
|
|
public void SendResponse<T>(T response, int clientSeq = 0) where T : new()
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
|
|
|
|
Packet.Response packet = new()
|
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
Id = clientSeq,
|
2023-10-14 13:07:07 +00:00
|
|
|
|
Name = typeof(T).Name,
|
2023-10-10 09:56:08 +00:00
|
|
|
|
Content = MessagePackSerializer.Serialize(response)
|
|
|
|
|
};
|
|
|
|
|
Send(new Packet()
|
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
No = 0,
|
2023-10-10 09:56:08 +00:00
|
|
|
|
Type = Packet.ContentType.Response,
|
|
|
|
|
Content = MessagePackSerializer.Serialize(packet)
|
|
|
|
|
});
|
2023-10-15 05:43:20 +00:00
|
|
|
|
log.Info($"{packet.Name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(response)) : "")}");
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-11-11 01:12:11 +00:00
|
|
|
|
// DB save on disconnect
|
2024-04-11 09:11:34 +00:00
|
|
|
|
Save();
|
2023-11-11 01:12:11 +00:00
|
|
|
|
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Warn($"{id} disconnected");
|
2023-10-08 14:12:01 +00:00
|
|
|
|
client.Close();
|
|
|
|
|
Server.Instance.Sessions.Remove(id);
|
|
|
|
|
}
|
2024-04-11 09:11:34 +00:00
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
player?.Save();
|
|
|
|
|
character?.Save();
|
|
|
|
|
stage?.Save();
|
|
|
|
|
inventory?.Save();
|
|
|
|
|
|
|
|
|
|
log.Info($"Saving session state...");
|
|
|
|
|
}
|
2023-10-08 14:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|