mirror of
https://git.lewd.wtf/PGR/ascnet
synced 2026-02-04 15:35:04 +01:00
add proper logger, saves to file as well
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AscNet.Common\AscNet.Common.csproj" />
|
||||
<ProjectReference Include="..\AscNet.Logging\AscNet.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Reflection;
|
||||
using AscNet.Common.Util;
|
||||
using AscNet.Logging;
|
||||
using MessagePack;
|
||||
|
||||
namespace AscNet.GameServer
|
||||
@@ -92,7 +92,9 @@ namespace AscNet.GameServer
|
||||
public static class PacketFactory
|
||||
{
|
||||
public static readonly Dictionary<string, RequestPacketHandlerDelegate> ReqHandlers = new();
|
||||
static readonly Logger c = new("Factory", ConsoleColor.Yellow);
|
||||
|
||||
// TODO: Configure based on session?
|
||||
static readonly Logger log = new(typeof(PacketFactory), LogLevel.DEBUG, LogLevel.DEBUG);
|
||||
|
||||
public static void LoadPacketHandlers()
|
||||
{
|
||||
@@ -101,7 +103,7 @@ namespace AscNet.GameServer
|
||||
|
||||
private static void LoadRequestPacketHandlers()
|
||||
{
|
||||
c.Log("Loading Packet Handlers...");
|
||||
log.Info("Loading Packet Handlers...");
|
||||
|
||||
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||
select t;
|
||||
@@ -112,11 +114,11 @@ namespace AscNet.GameServer
|
||||
if (attr == null || ReqHandlers.ContainsKey(attr.Name)) continue;
|
||||
ReqHandlers.Add(attr.Name, (RequestPacketHandlerDelegate)Delegate.CreateDelegate(typeof(RequestPacketHandlerDelegate), method));
|
||||
#if DEBUG
|
||||
c.Log($"Loaded {method.Name}");
|
||||
log.Info($"Loaded {method.Name}");
|
||||
#endif
|
||||
}
|
||||
|
||||
c.Log("Finished Loading Packet Handlers");
|
||||
log.Info("Finished Loading Packet Handlers");
|
||||
}
|
||||
|
||||
public static RequestPacketHandlerDelegate? GetRequestPacketHandler(string name)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using AscNet.Common.Util;
|
||||
using AscNet.Logging;
|
||||
|
||||
namespace AscNet.GameServer
|
||||
{
|
||||
public class Server
|
||||
{
|
||||
public static readonly Logger c = new(nameof(GameServer), ConsoleColor.Cyan);
|
||||
public static Logger log;
|
||||
public readonly Dictionary<string, Session> Sessions = new();
|
||||
private static Server? _instance;
|
||||
private readonly TcpListener listener;
|
||||
@@ -21,6 +21,10 @@ namespace AscNet.GameServer
|
||||
|
||||
public Server()
|
||||
{
|
||||
// TODO: add loglevel based on appsettings
|
||||
LogLevel logLevel = LogLevel.DEBUG;
|
||||
LogLevel fileLogLevel = LogLevel.DEBUG;
|
||||
log = new(typeof(Server), logLevel, fileLogLevel);
|
||||
listener = new(IPAddress.Parse("0.0.0.0"), Common.Common.config.GameServer.Port);
|
||||
}
|
||||
|
||||
@@ -31,21 +35,21 @@ namespace AscNet.GameServer
|
||||
try
|
||||
{
|
||||
listener.Start();
|
||||
c.Log($"{nameof(GameServer)} started and listening on port {Common.Common.config.GameServer.Port}");
|
||||
log.Info($"{nameof(GameServer)} started and listening on port {Common.Common.config.GameServer.Port}");
|
||||
|
||||
while (true)
|
||||
{
|
||||
TcpClient tcpClient = listener.AcceptTcpClient();
|
||||
string id = tcpClient.Client.RemoteEndPoint!.ToString()!;
|
||||
|
||||
c.Warn($"{id} connected");
|
||||
log.Warn($"{id} connected");
|
||||
Sessions.Add(id, new Session(id, tcpClient));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
c.Error("TCP listener error: " + ex.Message);
|
||||
c.Log("Waiting 3 seconds before restarting...");
|
||||
log.Error("TCP listener error: " + ex.Message);
|
||||
log.Info("Waiting 3 seconds before restarting...");
|
||||
Thread.Sleep(3000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
using System.Net.Sockets;
|
||||
using AscNet.Common;
|
||||
using AscNet.Common.Util;
|
||||
using AscNet.Logging;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Logger = AscNet.Logging.Logger;
|
||||
|
||||
namespace AscNet.GameServer
|
||||
{
|
||||
@@ -11,7 +13,7 @@ namespace AscNet.GameServer
|
||||
{
|
||||
public readonly string id;
|
||||
public readonly TcpClient client;
|
||||
public readonly Logger c;
|
||||
public readonly Logger log;
|
||||
private long lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
private ushort packetNo = 0;
|
||||
private readonly MessagePackSerializerOptions lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
|
||||
@@ -20,7 +22,8 @@ namespace AscNet.GameServer
|
||||
{
|
||||
this.id = id;
|
||||
client = tcpClient;
|
||||
c = new(id, ConsoleColor.DarkGray);
|
||||
// TODO: add session based configuration? maybe from database?
|
||||
log = new(typeof(Session), LogLevel.DEBUG, LogLevel.DEBUG);
|
||||
|
||||
Task.Run(ClientLoop);
|
||||
}
|
||||
@@ -62,7 +65,7 @@ namespace AscNet.GameServer
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
c.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", ""));
|
||||
log.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,34 +84,37 @@ namespace AscNet.GameServer
|
||||
RequestPacketHandlerDelegate? requestPacketHandler = PacketFactory.GetRequestPacketHandler(request.Name);
|
||||
if (requestPacketHandler is not null)
|
||||
{
|
||||
// TODO: with new logger this will be unnecessary
|
||||
if (Common.Common.config.VerboseLevel > VerboseLevel.Silent)
|
||||
c.Log($"{request.Name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
||||
log.Info($"{request.Name}{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
||||
requestPacketHandler.Invoke(this, request);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Common.Common.config.VerboseLevel > VerboseLevel.Silent)
|
||||
c.Warn($"{request.Name} handler not found!{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
||||
log.Warn($"{request.Name} handler not found!{(Common.Common.config.VerboseLevel >= VerboseLevel.Debug ? (", " + JsonConvert.SerializeObject(MessagePackSerializer.Typeless.Deserialize(request.Content))) : "")}");
|
||||
}
|
||||
break;
|
||||
|
||||
case Packet.ContentType.Push:
|
||||
Packet.Push push = MessagePackSerializer.Deserialize<Packet.Push>(packet.Content);
|
||||
debugContent = push.Content;
|
||||
c.Log(push.Name);
|
||||
log.Info(push.Name);
|
||||
throw new NotImplementedException($"Packet push handlers not implemented ({push.Name})");
|
||||
break;
|
||||
|
||||
case Packet.ContentType.Exception:
|
||||
Packet.Exception exception = MessagePackSerializer.Deserialize<Packet.Exception>(packet.Content);
|
||||
c.Error($"Exception packet received: {exception.Code}, {exception.Message}");
|
||||
log.Error($"Exception packet received: {exception.Code}, {exception.Message}");
|
||||
break;
|
||||
|
||||
default:
|
||||
c.Error($"Unknown packet received: {packet}");
|
||||
log.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("-", ""));
|
||||
log.Error("Failed to invoke handler: " + ex.Message + $", Raw {packet.Type} packet: " + BitConverter.ToString(debugContent).Replace("-", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,7 +146,7 @@ namespace AscNet.GameServer
|
||||
Type = Packet.ContentType.Push,
|
||||
Content = MessagePackSerializer.Serialize(packet)
|
||||
});
|
||||
c.Log(packet.Name);
|
||||
log.Info(packet.Name);
|
||||
}
|
||||
|
||||
public void SendPush(string name, byte[] push)
|
||||
@@ -156,7 +162,7 @@ namespace AscNet.GameServer
|
||||
Type = Packet.ContentType.Push,
|
||||
Content = MessagePackSerializer.Serialize(packet)
|
||||
});
|
||||
c.Log(packet.Name);
|
||||
log.Info(packet.Name);
|
||||
}
|
||||
|
||||
public void SendResponse<T>(T response, int clientSeq = 0)
|
||||
@@ -173,7 +179,7 @@ namespace AscNet.GameServer
|
||||
Type = Packet.ContentType.Response,
|
||||
Content = MessagePackSerializer.Serialize(packet)
|
||||
});
|
||||
c.Log(packet.Name);
|
||||
log.Info(packet.Name);
|
||||
}
|
||||
|
||||
private void Send(Packet packet)
|
||||
@@ -194,7 +200,7 @@ namespace AscNet.GameServer
|
||||
if (Server.Instance.Sessions.GetValueOrDefault(id) is null)
|
||||
return;
|
||||
|
||||
c.Warn($"{id} disconnected");
|
||||
log.Warn($"{id} disconnected");
|
||||
client.Close();
|
||||
Server.Instance.Sessions.Remove(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user