using System.Net.Sockets; using System.Text.Json; using DX86.Modules; using MySqlX.XDevAPI; namespace Server; class JsonMessage { public string cmd { get; set; } public string cid { get; set; } } public class BackendServer : DX86.TcpServer { CommandManager commandManager; public BackendServer(string address, int port, MessageSender ms) : base(address, port, ms) { ms.Log("[BACKENDSERVER] Setting up server."); commandManager = new CommandManager(); ms.Log("[BACKENDSERVER] Server Setup Complete."); } public override string ToggleOption(string option, string value) { throw new NotImplementedException(); } public override List> ConfigureOptions(string option) { throw new NotImplementedException(); } protected override void ClientConnectEvent(TcpClient client) { ms.Log("[BACKENDSERVER] Client connected: " + client.Client.RemoteEndPoint); } protected override void MessageReceivedEvent(TcpClient client, string message) { ms.Log("[BACKENDSERVER] Message received from client: " + message); // format json recieved JsonMessage? jsonMessage; if (message.StartsWith("{") && message.EndsWith("}")) { //this.SendMessageAsync(client, "Command received\n"); try { jsonMessage = JsonSerializer.Deserialize(message); if (jsonMessage != null) { ms.Log("[BACKENDSERVER] JSON Message received: cmd=" + jsonMessage.cmd + ", cid=" + jsonMessage.cid); handleCommand(cmd: jsonMessage.cmd, cid: jsonMessage.cid, client: client); } } catch (JsonException ex) { ms.Log("[BACKENDSERVER] Error parsing JSON: " + ex.Message); } } } protected override void ClientDisconnectEvent(TcpClient client) { throw new NotImplementedException(); } private void handleCommand(string cmd, string cid, TcpClient client) { string[] args = cmd.Split(' '); string command = args[0]; // Remove the leading dot string[] commandArgs = args.Skip(1).ToArray(); // Get the arguments after the command foreach (var commandArg in commandArgs) { ms.Debug("[BACKENDSERVER] Command argument: " + commandArg); } commandManager.ExecuteCommand(executor:command, args:commandArgs, client:client, clientSocket:this, cid: cid); } }