mirror of
https://github.com/rafi1212122/PemukulPaku
synced 2025-12-13 01:14:35 +01:00
logger & tcp server
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson;
|
|
||||||
using Common.Resources.Proto;
|
using Common.Resources.Proto;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
@@ -8,7 +7,7 @@ namespace Common.Database
|
|||||||
public class User
|
public class User
|
||||||
{
|
{
|
||||||
public static readonly IMongoCollection<UserScheme> collection = Global.db.GetCollection<UserScheme>("Users");
|
public static readonly IMongoCollection<UserScheme> collection = Global.db.GetCollection<UserScheme>("Users");
|
||||||
|
|
||||||
public static UserScheme CreateUser(string name)
|
public static UserScheme CreateUser(string name)
|
||||||
{
|
{
|
||||||
UserScheme user = new()
|
UserScheme user = new()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Config.Net;
|
using Common.Utils;
|
||||||
|
using Config.Net;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
@@ -6,6 +7,7 @@ namespace Common
|
|||||||
public static class Global
|
public static class Global
|
||||||
{
|
{
|
||||||
public static IConfig config = new ConfigurationBuilder<IConfig>().UseJsonFile("config.json").Build();
|
public static IConfig config = new ConfigurationBuilder<IConfig>().UseJsonFile("config.json").Build();
|
||||||
|
public static Logger c = new("Global");
|
||||||
|
|
||||||
public static MongoClient MongoClient = new MongoClient(config.DatabaseUri);
|
public static MongoClient MongoClient = new MongoClient(config.DatabaseUri);
|
||||||
public static IMongoDatabase db = MongoClient.GetDatabase("PemukulPaku");
|
public static IMongoDatabase db = MongoClient.GetDatabase("PemukulPaku");
|
||||||
@@ -14,7 +16,7 @@ namespace Common
|
|||||||
|
|
||||||
public interface IConfig
|
public interface IConfig
|
||||||
{
|
{
|
||||||
[Option(DefaultValue = VerboseLevel.Warns)]
|
[Option(DefaultValue = VerboseLevel.Normal)]
|
||||||
VerboseLevel VerboseLevel { get; }
|
VerboseLevel VerboseLevel { get; }
|
||||||
|
|
||||||
[Option(DefaultValue = false)]
|
[Option(DefaultValue = false)]
|
||||||
@@ -57,8 +59,8 @@ namespace Common
|
|||||||
|
|
||||||
public enum VerboseLevel
|
public enum VerboseLevel
|
||||||
{
|
{
|
||||||
Errors = 0,
|
Silent = 0,
|
||||||
Warns = 1,
|
Normal = 1,
|
||||||
Debug = 2
|
Debug = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
58
Common/Utils/Logger.cs
Normal file
58
Common/Utils/Logger.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
namespace Common.Utils
|
||||||
|
{
|
||||||
|
public class Logger
|
||||||
|
{
|
||||||
|
private readonly string _name;
|
||||||
|
private readonly ConsoleColor _color;
|
||||||
|
|
||||||
|
public Logger(string name, ConsoleColor color = ConsoleColor.Cyan)
|
||||||
|
{
|
||||||
|
_name = name;
|
||||||
|
_color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Log(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = _color;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Warn(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(params string[] message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write(DateTime.Now.ToString("HH:mm:ss "));
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("<");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.Write(_name);
|
||||||
|
Console.ResetColor();
|
||||||
|
Console.Write("> ");
|
||||||
|
Console.BackgroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.BackgroundColor = ConsoleColor.DarkRed;
|
||||||
|
Console.WriteLine(string.Join("\t", message));
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
Gameserver/Server.cs
Normal file
34
Gameserver/Server.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Net;
|
||||||
|
using Common;
|
||||||
|
using Common.Utils;
|
||||||
|
|
||||||
|
namespace PemukulPaku.Gameserver
|
||||||
|
{
|
||||||
|
public class Server
|
||||||
|
{
|
||||||
|
public static Logger c = new("TCP", ConsoleColor.Blue);
|
||||||
|
|
||||||
|
public static void Start()
|
||||||
|
{
|
||||||
|
TcpListener Listener = new(IPAddress.Parse("0.0.0.0"), (int)Global.config.Gameserver.Port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Listener.Start();
|
||||||
|
c.Log($"TCP server started on port {Global.config.Gameserver.Port}");
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
TcpClient Client = Listener.AcceptTcpClient();
|
||||||
|
c.Warn($"{Client.Client.RemoteEndPoint} connected!");
|
||||||
|
NetworkStream stream = Client.GetStream();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
c.Error("TCP server error: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
using Common;
|
using Common;
|
||||||
|
using Common.Utils;
|
||||||
using HttpServer.Controllers;
|
using HttpServer.Controllers;
|
||||||
|
|
||||||
namespace HttpServer
|
namespace HttpServer
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
private static readonly Logger c = new("HTTP", ConsoleColor.Green);
|
||||||
|
|
||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
Thread.CurrentThread.IsBackground = true;
|
Thread.CurrentThread.IsBackground = true;
|
||||||
@@ -20,7 +23,38 @@ namespace HttpServer
|
|||||||
AccountController.AddHandlers(app);
|
AccountController.AddHandlers(app);
|
||||||
ConfigController.AddHandlers(app);
|
ConfigController.AddHandlers(app);
|
||||||
|
|
||||||
|
app.UseMiddleware<RequestLoggingMiddleware>();
|
||||||
|
c.Log($"HTTP server started on port 80 & 443"); // A lie
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class RequestLoggingMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
private static readonly string[] SurpressedRoutes = new string[] { "/report", "/sdk/dataUpload" };
|
||||||
|
|
||||||
|
public RequestLoggingMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if ((int)Global.config.VerboseLevel > (int)VerboseLevel.Normal)
|
||||||
|
{
|
||||||
|
c.Log($"{context.Response.StatusCode} {context.Request.Method.ToUpper()} {context.Request.Path}");
|
||||||
|
}else if(((int)Global.config.VerboseLevel > (int)VerboseLevel.Silent) && (Array.IndexOf(SurpressedRoutes, context.Request.Path.ToString()) == -1))
|
||||||
|
{
|
||||||
|
c.Log($"{context.Response.StatusCode} {context.Request.Method.ToUpper()} {context.Request.Path}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
Program.cs
11
Program.cs
@@ -1,6 +1,7 @@
|
|||||||
using Common.Resources.Proto;
|
using Common.Resources.Proto;
|
||||||
using Common;
|
using Common;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
|
using PemukulPaku.Gameserver;
|
||||||
|
|
||||||
namespace PemukulPaku
|
namespace PemukulPaku
|
||||||
{
|
{
|
||||||
@@ -8,15 +9,17 @@ namespace PemukulPaku
|
|||||||
{
|
{
|
||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello!");
|
Global.c.Log("Starting...");
|
||||||
|
|
||||||
|
Global.config.Gameserver.Host = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.OperationalStatus == OperationalStatus.Up).First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().Address.ToString();
|
||||||
|
|
||||||
GetPlayerTokenRsp getPlayerTokenRsp = new()
|
GetPlayerTokenRsp getPlayerTokenRsp = new()
|
||||||
{
|
{
|
||||||
Msg = "Hello!"
|
Msg = "Hello!"
|
||||||
};
|
};
|
||||||
new Thread(HttpServer.Program.Main).Start();
|
|
||||||
|
|
||||||
Global.config.Gameserver.Host = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.OperationalStatus == OperationalStatus.Up).First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().Address.ToString();
|
new Thread(HttpServer.Program.Main).Start();
|
||||||
Console.WriteLine(Global.config.Gameserver.Host);
|
new Thread(Server.Start).Start();
|
||||||
|
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user