Major overall of data
This commit is contained in:
1 parent
64dbd02874
commit
6126b8259c
12 files changed
+160
-126
No files matched your search
@@ -0,0 +1,27 @@
|
||||
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 EventHandler onUserConnected;
|
||||
public void InvokeOnUserConnected(TcpClientConnection ConnectedUser){
|
||||
onUserConnected?.Invoke(ConnectedUser, new EventArgs());
|
||||
}
|
||||
public event EventHandler onReceived;
|
||||
public void InvokeOnReceived(dynamic DataPacket){
|
||||
onReceived?.Invoke(DataPacket, new EventArgs());
|
||||
}
|
||||
public event EventHandler onUserDisconnected;
|
||||
public void InvokeOnUserDisconnected(TcpClientConnection DisconnectedUser){
|
||||
onUserDisconnected?.Invoke(DisconnectedUser, new EventArgs());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -9,45 +9,39 @@ using System.Threading.Tasks;
|
||||
namespace Mistox.Server {
|
||||
public class ServerListener : IDisposable {
|
||||
|
||||
public event EventHandler onConnected;
|
||||
public event EventHandler onDisconnected;
|
||||
public event EventHandler onReceive;
|
||||
TcpListener _Listener;
|
||||
|
||||
MistoxServer _MistoxServer;
|
||||
TcpListener Listener;
|
||||
|
||||
public ServerListener( MistoxServer ServerInstance ) {
|
||||
_MistoxServer = ServerInstance;
|
||||
public ServerListener() {
|
||||
Thread ConnectionThread = new Thread(async () => await ListenerThread() );
|
||||
ConnectionThread.Start();
|
||||
}
|
||||
|
||||
async Task ListenerThread() {
|
||||
Listener = new TcpListener( _MistoxServer.BindAddress, _MistoxServer.Port );
|
||||
Listener.Start();
|
||||
while( _MistoxServer.Alive ) {
|
||||
TcpClient client = await Listener.AcceptTcpClientAsync();
|
||||
|
||||
if (_MistoxServer.Connections.Count + 1 > _MistoxServer.MaxClients) {
|
||||
_Listener = new TcpListener( ServerInterface.ServerInstance.BindAddress, ServerInterface.ServerInstance.Port );
|
||||
_Listener.Start();
|
||||
while( ServerInterface.ServerInstance.Alive ) {
|
||||
TcpClient client = await _Listener.AcceptTcpClientAsync();
|
||||
|
||||
if (ServerInterface.Connections.Count + 1 > ServerInterface.ServerInstance.MaxClients) {
|
||||
continue;
|
||||
}
|
||||
|
||||
IPEndPoint remote = client.Client.RemoteEndPoint as IPEndPoint;
|
||||
string connectionID = Guid.NewGuid().ToString();
|
||||
|
||||
_MistoxServer.Connections[connectionID] = new TcpClientConnection() {
|
||||
Connection = new TCPConnection( client, _MistoxServer, connectionID ),
|
||||
ServerInterface.Connections[connectionID] = new TcpClientConnection() {
|
||||
Connection = new TCPConnection( client, connectionID ),
|
||||
RemoteAddress = remote.Address
|
||||
};
|
||||
|
||||
Console.WriteLine( "New User Connected" );
|
||||
onConnected?.Invoke( _MistoxServer.Connections[connectionID], new EventArgs() );
|
||||
ServerInterface.ServerInstance.InvokeOnUserConnected( ServerInterface.Connections[connectionID] );
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Listener.Stop();
|
||||
Listener = null;
|
||||
_Listener.Stop();
|
||||
_Listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,10 @@ namespace Mistox.Server {
|
||||
public class TCPConnection : IDisposable {
|
||||
|
||||
TcpClient _client;
|
||||
MistoxServer _instance;
|
||||
string _connectionID;
|
||||
|
||||
public TCPConnection( TcpClient tcpClient, MistoxServer ServerInstance, string ConnectionID ) {
|
||||
public TCPConnection( TcpClient tcpClient, string ConnectionID ) {
|
||||
_client = tcpClient;
|
||||
_instance = ServerInstance;
|
||||
_connectionID = ConnectionID;
|
||||
_client.NoDelay = true;
|
||||
|
||||
@@ -22,15 +20,15 @@ namespace Mistox.Server {
|
||||
}
|
||||
|
||||
public async Task ReceiveThread() {
|
||||
while( _client.Connected && _instance.Connections[_connectionID].Connected ) {
|
||||
while( _client.Connected && ServerInterface.Connections[_connectionID].Connected ) {
|
||||
try {
|
||||
_instance.Connections[_connectionID].LastHeartbeat = DateTime.UtcNow;
|
||||
ServerInterface.Connections[_connectionID].LastHeartbeat = DateTime.UtcNow;
|
||||
byte[] StreamData = new byte[1024];
|
||||
int bytesRead = await _client.GetStream().ReadAsync(StreamData, 0, StreamData.Length);
|
||||
if( bytesRead > 0 ) {
|
||||
dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) );
|
||||
if( data != null ) {
|
||||
_instance.onReceived?.Invoke( data, new EventArgs() );
|
||||
ServerInterface.ServerInstance.InvokeOnReceived( data );
|
||||
}
|
||||
}
|
||||
} catch( Exception ) {
|
||||
@@ -46,9 +44,9 @@ namespace Mistox.Server {
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_instance.Connections[_connectionID].Connected = false;
|
||||
_instance.onUserDisconnected?.Invoke( _instance.Connections[_connectionID], new EventArgs() );
|
||||
_instance.Connections.TryRemove( _connectionID, out _ );
|
||||
ServerInterface.Connections[_connectionID].Connected = false;
|
||||
ServerInterface.ServerInstance.InvokeOnUserDisconnected( ServerInterface.Connections[_connectionID] );
|
||||
ServerInterface.Connections.TryRemove( _connectionID, out _ );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
|
||||
namespace Mistox.Server {
|
||||
|
||||
public class MistoxServer {
|
||||
public string ServerName { get; set; } = "";
|
||||
public IPAddress BindAddress { get; set; } = IPAddress.Any;
|
||||
public int Port { get; set; } = 9126;
|
||||
public int MaxClients { get; set; } = 10;
|
||||
public bool Alive { get; set; } = true;
|
||||
public ServerListener serverListener;
|
||||
public ConcurrentDictionary<string, TcpClientConnection> Connections { get; set; }
|
||||
|
||||
public EventHandler onUserConnected;
|
||||
public EventHandler onReceived;
|
||||
public EventHandler onUserDisconnected;
|
||||
}
|
||||
|
||||
public class TcpClientConnection {
|
||||
public string UserName { get; set; } = "";
|
||||
public bool IsAuthenticated { get; set; } = false;
|
||||
public DateTime LastHeartbeat { get; set; } = DateTime.UtcNow;
|
||||
public bool Connected { get; set; } = true;
|
||||
public IPAddress RemoteAddress { get; set; }
|
||||
public TCPConnection Connection { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user