Rework server to be non static
This commit is contained in:
1 parent
875a5ae3e9
commit
6f910e969a
6 files changed
+97
-79
No files matched your search
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
@@ -8,44 +7,36 @@ using Mistox.Server;
|
||||
namespace Mistox {
|
||||
public class ServerInterface {
|
||||
|
||||
public static MistoxServer ServerInstance; // Server Data
|
||||
public static ServerListener Listener; // Server Listener
|
||||
public static ConcurrentDictionary<string, TcpClientConnection> Connections; // Server Connections
|
||||
public static ServerEntity ServerInstance; // Server Data
|
||||
|
||||
public event Action<TcpClientConnection> onUserConnected; // User connected event handler
|
||||
public event Action<dynamic> onDataReceived; // Data received event handler
|
||||
public event Action<TcpClientConnection> onUserDisconnected; // User disconnected event handler
|
||||
public event Action<ServerConnection> onUserConnected; // User connected event handler
|
||||
public event Action<dynamic> onDataReceived; // Data received event handler
|
||||
public event Action<ServerConnection> onUserDisconnected; // User disconnected event handler
|
||||
|
||||
public ServerInterface( int port, string serverName ) {
|
||||
|
||||
IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port );
|
||||
|
||||
// Initilize the server
|
||||
ServerInstance = new MistoxServer() {
|
||||
ServerName = serverName,
|
||||
BindAddress = IPAddress.Any,
|
||||
Port = port,
|
||||
MaxClients = 10
|
||||
};
|
||||
Connections = new ConcurrentDictionary<string, TcpClientConnection>();
|
||||
ServerInstance = new ServerEntity(listenerAddress);
|
||||
ServerInstance.ServerName = serverName;
|
||||
|
||||
// Setup the event handlers
|
||||
ServerInstance.onUserConnected += (TcpClientConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); };
|
||||
ServerInstance.onUserConnected += (ServerConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); };
|
||||
ServerInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
ServerInstance.onUserDisconnected += (TcpClientConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); };
|
||||
|
||||
// Start listening for connections and wire up the server
|
||||
Listener = new ServerListener();
|
||||
ServerInstance.onUserDisconnected += (ServerConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); };
|
||||
|
||||
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
|
||||
}
|
||||
|
||||
public async Task SendAllClients<Packet>(Packet data) {
|
||||
foreach( KeyValuePair<string, TcpClientConnection> cur in Connections ) {
|
||||
await cur.Value.Connection.Send(data);
|
||||
foreach( KeyValuePair<string, ServerConnection> cur in ServerInstance.Connections ) {
|
||||
await cur.Value.Send(data);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||
await Connections[UserName].Connection.Send( data );
|
||||
await ServerInstance.Connections[UserName].Send( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace Mistox.Server {
|
||||
|
||||
public class MistoxServer {
|
||||
public string ServerName { get; set; } = ""; // The name of the server
|
||||
public IPAddress BindAddress { get; set; } = IPAddress.Any; // What IP to bind on
|
||||
public int Port { get; set; } = 9126; // What Port to bind on
|
||||
public int MaxClients { get; set; } = 10; // The max players per server
|
||||
public bool Alive { get; set; } = true; // Is the server running
|
||||
|
||||
public event Action<TcpClientConnection> onUserConnected;
|
||||
public void InvokeUserConnected( TcpClientConnection user ){ onUserConnected?.Invoke( user ); }
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeDataReceived( dynamic data ){ onDataReceived?.Invoke( data ); }
|
||||
public event Action<TcpClientConnection> onUserDisconnected;
|
||||
public void InvokeUserDisconnected( TcpClientConnection user ){ onUserDisconnected?.Invoke( user ); }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Mistox.Server {
|
||||
public class TcpClientConnection {
|
||||
public string UserName { get; set; } = ""; // The Players UserName
|
||||
public bool IsAuthenticated { get; set; } = false; // Has the player authenticated
|
||||
public Vector3 Position { get; set; } // The ingame position of the player
|
||||
public float ConnectionRange { get; set; } // The ingame distance players need to be to be connected together
|
||||
public DateTime LastHeartbeat { get; set; } = DateTime.UtcNow; // Connection heartbeat for integrity
|
||||
public bool Connected { get; set; } = true; // Is the connection valid
|
||||
public IPAddress RemoteAddress { get; set; } // The remote users IP Address
|
||||
public TCPConnection Connection { get; set; } // The TCP Server Object
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
|
||||
namespace Mistox.Server {
|
||||
|
||||
public class ServerEntity {
|
||||
|
||||
// Class Definitions
|
||||
|
||||
public string ServerName { get; set; } = ""; // The name of the server
|
||||
public int MaxClients { get; set; } = 10; // The max players per server
|
||||
public float ConnectionRange { get; set; } = 40; // InGame distance for valid conections
|
||||
public int ConnectionTimeoutSeconds { get; set; } = 10; // If no reply in time kill the connection
|
||||
public bool Alive { get; set; } = true; // Is the server running
|
||||
|
||||
ServerListener Listener { get; set;}
|
||||
public ConcurrentDictionary<string, ServerConnection> Connections;
|
||||
|
||||
public ServerEntity(IPEndPoint ListenerAddress) {
|
||||
Listener = new ServerListener(ListenerAddress, this);
|
||||
Connections = new ConcurrentDictionary<string, ServerConnection>();
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
public event Action<ServerConnection> onUserConnected;
|
||||
public void InvokeUserConnected( ServerConnection user ){
|
||||
onUserConnected?.Invoke( user );
|
||||
}
|
||||
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeDataReceived( dynamic data ){
|
||||
onDataReceived?.Invoke( data );
|
||||
}
|
||||
|
||||
public event Action<ServerConnection> onUserDisconnected;
|
||||
public void InvokeUserDisconnected( ServerConnection user ){
|
||||
onUserDisconnected?.Invoke( user );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +1,30 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Numerics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Mistox.Attributes;
|
||||
|
||||
namespace Mistox.Server {
|
||||
public class TCPConnection : IDisposable {
|
||||
public class ServerConnection : IDisposable {
|
||||
|
||||
public string UserName { get; set; } = ""; // The Players UserName
|
||||
public bool IsAuthenticated { get; set; } = false; // Has the player authenticated
|
||||
public Vector3 Position { get; set; } // The ingame position of the player
|
||||
public DateTime LastHeartbeat { get; set; } = DateTime.UtcNow; // Connection heartbeat for integrity
|
||||
public bool Connected { get; set; } = true; // Is the connection valid
|
||||
|
||||
ServerEntity _parent;
|
||||
TcpClient _client;
|
||||
TcpClientConnection _connection;
|
||||
IPEndPoint _remoteAddress;
|
||||
|
||||
public TCPConnection( TcpClient tcpClient, string ConnectionID ) {
|
||||
public ServerConnection( TcpClient tcpClient, ServerEntity parent, IPEndPoint remote ) {
|
||||
_parent = parent;
|
||||
_client = tcpClient;
|
||||
_remoteAddress = remote;
|
||||
_client.NoDelay = true;
|
||||
_connection = ServerInterface.Connections[ConnectionID];
|
||||
ServerInterface.ServerInstance.InvokeUserConnected( ServerInterface.Connections[ConnectionID] );
|
||||
|
||||
ServerInterface.ServerInstance.InvokeUserConnected( this );
|
||||
// Start the receive thread
|
||||
Thread receiveThread = new Thread(async () => await ReceiveThread() );
|
||||
receiveThread.Start();
|
||||
@@ -25,7 +35,16 @@ namespace Mistox.Server {
|
||||
try {
|
||||
byte[] headerBuffer = new byte[DataPacket.HeaderLength];
|
||||
byte[] payloadBuffer = new byte[65535]; // Upper limit for safety
|
||||
while( _connection.Connected ) {
|
||||
while( Connected && _parent.Alive ) {
|
||||
|
||||
// Tick the last heartbeat or drop
|
||||
DateTime now = DateTime.UtcNow;
|
||||
if (LastHeartbeat.AddSeconds(_parent.ConnectionTimeoutSeconds) >= now) {
|
||||
Connected = false;
|
||||
Dispose();
|
||||
break;
|
||||
}
|
||||
LastHeartbeat = DateTime.UtcNow;
|
||||
|
||||
// Read the header off
|
||||
await ns.ReadExactlyAsync(headerBuffer, 0, 3);
|
||||
@@ -46,7 +65,7 @@ namespace Mistox.Server {
|
||||
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
||||
|
||||
// Return the object
|
||||
ServerInterface.ServerInstance.InvokeDataReceived(dataobject);
|
||||
_parent.InvokeDataReceived(dataobject);
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -63,10 +82,11 @@ namespace Mistox.Server {
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_connection.Connected = false;
|
||||
ServerInterface.ServerInstance.InvokeUserDisconnected( _connection );
|
||||
ServerInterface.ServerInstance.Alive = false;
|
||||
ServerInterface.Connections.TryRemove( _connection.UserName, out _ );
|
||||
Connected = false;
|
||||
_parent.InvokeUserDisconnected( this );
|
||||
Connected = false;
|
||||
_parent.Alive = false;
|
||||
// Remove this item from the Dictionary on the Server for garbage collection
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,30 +9,31 @@ using System.Threading.Tasks;
|
||||
namespace Mistox.Server {
|
||||
public class ServerListener : IDisposable {
|
||||
|
||||
ServerEntity _parent;
|
||||
IPEndPoint _bindAddress;
|
||||
TcpListener _Listener;
|
||||
|
||||
public ServerListener() {
|
||||
public ServerListener(IPEndPoint listenerAddress, ServerEntity parent) {
|
||||
_bindAddress = listenerAddress;
|
||||
_parent = parent;
|
||||
Thread ConnectionThread = new Thread(async () => await ListenerThread() );
|
||||
ConnectionThread.Start();
|
||||
}
|
||||
|
||||
async Task ListenerThread() {
|
||||
_Listener = new TcpListener( ServerInterface.ServerInstance.BindAddress, ServerInterface.ServerInstance.Port );
|
||||
_Listener = new TcpListener( _bindAddress );
|
||||
_Listener.Start();
|
||||
while( ServerInterface.ServerInstance.Alive ) {
|
||||
while( _parent.Alive ) {
|
||||
TcpClient client = await _Listener.AcceptTcpClientAsync();
|
||||
|
||||
if (ServerInterface.Connections.Count + 1 > ServerInterface.ServerInstance.MaxClients) {
|
||||
if (_parent.Connections.Count + 1 > _parent.MaxClients) {
|
||||
continue;
|
||||
}
|
||||
|
||||
IPEndPoint remote = client.Client.RemoteEndPoint as IPEndPoint;
|
||||
string connectionID = Guid.NewGuid().ToString();
|
||||
|
||||
ServerInterface.Connections[connectionID] = new TcpClientConnection() {
|
||||
Connection = new TCPConnection( client, connectionID ),
|
||||
RemoteAddress = remote.Address
|
||||
};
|
||||
_parent.Connections[connectionID] = new ServerConnection( client, _parent, remote );
|
||||
|
||||
Console.WriteLine( "New User Connected" );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user