mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-12-13 22:31:38 +01:00
Initial Commit
This commit is contained in:
107
DX86/TcpServer.cs
Normal file
107
DX86/TcpServer.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using DX86.Modules;
|
||||
|
||||
namespace DX86;
|
||||
using System.Net.Sockets;
|
||||
|
||||
public abstract class TcpServer
|
||||
{
|
||||
private string address;
|
||||
private int port;
|
||||
protected MessageSender ms;
|
||||
private DX86 dx86;
|
||||
public List<string> optionsList;
|
||||
|
||||
|
||||
public TcpServer(string address, int port, MessageSender ms)
|
||||
{
|
||||
ms.Log("[DX86] Setting up server.");
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.ms = ms;
|
||||
optionsList = new List<string>();
|
||||
this.dx86 = new DX86(ms, ServerType.TCP);
|
||||
optionsList.Add("cancel");
|
||||
ms.Log("[DX86] Server Setup Complete.");
|
||||
_ = RunServer();
|
||||
}
|
||||
|
||||
|
||||
private async Task RunServer()
|
||||
{
|
||||
ms.Log("[DX86] Starting server.");
|
||||
// Subscribe to events
|
||||
dx86.ClientConnected += ClientConnectEvent;
|
||||
dx86.MessageReceived += MessageReceivedEvent;
|
||||
dx86.ClientDisconnected += ClientDisconnectEvent;
|
||||
|
||||
// Start the server
|
||||
dx86.StartServer(address, port);
|
||||
ms.Log("[DX86] Server started.");
|
||||
|
||||
// Keep the server running indefinitely
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
}
|
||||
|
||||
public void StopServer()
|
||||
{
|
||||
ms.Log("[DX86] Got Signal to Stop Server.");
|
||||
dx86.StopServer();
|
||||
ms.Log("[DX86] Server stopped.");
|
||||
}
|
||||
|
||||
public string GetAddress()
|
||||
{
|
||||
return address;
|
||||
}
|
||||
|
||||
public int GetPort()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
public void SendMessageAsync(TcpClient client, string message)
|
||||
{
|
||||
dx86.SendMessageAsync(client, message);
|
||||
}
|
||||
|
||||
public void BroadcastMessageAsync(TcpClient sender, string message)
|
||||
{
|
||||
dx86.BroadcastMessage(message, sender);
|
||||
}
|
||||
|
||||
public IPEndPoint GetClientEndPoint(TcpClient client)
|
||||
{
|
||||
if (client == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!client.Connected)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the remote endpoint, cast it to IPEndPoint
|
||||
IPEndPoint remoteEndPoint = client.Client.RemoteEndPoint as IPEndPoint;
|
||||
|
||||
if (remoteEndPoint != null)
|
||||
{
|
||||
// Return the IP address and port
|
||||
return remoteEndPoint;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public abstract string ToggleOption(string option, string value);
|
||||
public abstract List<List<string>> ConfigureOptions(string option);
|
||||
// Define your event handlers
|
||||
protected abstract void ClientConnectEvent(TcpClient client);
|
||||
|
||||
protected abstract void MessageReceivedEvent(TcpClient client, string message);
|
||||
|
||||
protected abstract void ClientDisconnectEvent(TcpClient client);
|
||||
}
|
||||
Reference in New Issue
Block a user