Rework client to be non static
This commit is contained in:
1 parent
6f910e969a
commit
b439805f3e
7 files changed
+106
-107
No files matched your search
@@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Mistox.Client {
|
|
||||||
|
|
||||||
public class MistoxServerClient {
|
|
||||||
|
|
||||||
public string ServerName { get; set; } = ""; // The Name of the server
|
|
||||||
public bool Alive { get; set; } = true; // Is the server running
|
|
||||||
public int Port { get; set; } = 9126; // What Port to bind on
|
|
||||||
|
|
||||||
|
|
||||||
public event Action onServerConnected;
|
|
||||||
public void InvokeOnServerConnected(){
|
|
||||||
Alive = true;
|
|
||||||
onServerConnected?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action<UDPEndpoint> onUserConnected;
|
|
||||||
public void InvokeOnUserConnected(UDPEndpoint ConnectedUser){
|
|
||||||
onUserConnected?.Invoke(ConnectedUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action<dynamic> onDataReceived;
|
|
||||||
public void InvokeOnReceived(dynamic DataPacket){
|
|
||||||
onDataReceived?.Invoke(DataPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action<UDPEndpoint> onUserDisconnected;
|
|
||||||
public void InvokeOnUserDisconnected(UDPEndpoint DisconnectedUser){
|
|
||||||
onUserDisconnected?.Invoke(DisconnectedUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action onServerDisconnected;
|
|
||||||
public void InvokeOnServerDisconnected(){
|
|
||||||
Alive = false;
|
|
||||||
onServerDisconnected?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Mistox.Client {
|
|
||||||
public class UDPEndpoint {
|
|
||||||
public string UserName { get; set; } = ""; // The Players UserName
|
|
||||||
public bool Alive { get; set; } = true; // Is the connection alive
|
|
||||||
public mUDPClient Connection { get; set; } // The UDP Client Object
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace Mistox.Client {
|
||||||
|
|
||||||
|
public class MistoxClient {
|
||||||
|
|
||||||
|
public string ServerName { get; set; } = ""; // The Name of the server
|
||||||
|
public bool Alive { get; set; } = true; // Is the server running
|
||||||
|
public mTCPClient ServerConnection;
|
||||||
|
public ConcurrentDictionary<string, mUDPClient> clientConnections;
|
||||||
|
|
||||||
|
public MistoxClient(IPEndPoint Server) {
|
||||||
|
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address );
|
||||||
|
try {
|
||||||
|
ServerConnection = new mTCPClient( this, Server );
|
||||||
|
Console.WriteLine( "Connected to the server sucessfully" );
|
||||||
|
} catch( Exception e ) {
|
||||||
|
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
||||||
|
Console.WriteLine( e.ToString() );
|
||||||
|
Console.WriteLine( "}" );
|
||||||
|
}
|
||||||
|
Alive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action onServerConnected;
|
||||||
|
public void InvokeOnServerConnected(){
|
||||||
|
Alive = true;
|
||||||
|
onServerConnected?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action<mUDPClient> onUserConnected;
|
||||||
|
public void InvokeOnUserConnected(mUDPClient ConnectedUser){
|
||||||
|
onUserConnected?.Invoke(ConnectedUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action<dynamic> onDataReceived;
|
||||||
|
public void InvokeOnReceived(dynamic DataPacket){
|
||||||
|
onDataReceived?.Invoke(DataPacket);
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action<mUDPClient> onUserDisconnected;
|
||||||
|
public void InvokeOnUserDisconnected(mUDPClient DisconnectedUser){
|
||||||
|
onUserDisconnected?.Invoke(DisconnectedUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action onServerDisconnected;
|
||||||
|
public void InvokeOnServerDisconnected(){
|
||||||
|
Alive = false;
|
||||||
|
onServerDisconnected?.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,23 +9,24 @@ using System.Threading.Tasks;
|
|||||||
namespace Mistox.Client {
|
namespace Mistox.Client {
|
||||||
public class mTCPClient : IDisposable {
|
public class mTCPClient : IDisposable {
|
||||||
|
|
||||||
TcpClient Server;
|
TcpClient _connection;
|
||||||
|
MistoxClient _parent;
|
||||||
|
|
||||||
public mTCPClient( IPEndPoint ServerAddress ) {
|
public mTCPClient( MistoxClient parent, IPEndPoint ServerAddress ) {
|
||||||
Server = new TcpClient( AddressFamily.InterNetwork );
|
_connection = new TcpClient( AddressFamily.InterNetwork );
|
||||||
Server.NoDelay = true;
|
_connection.NoDelay = true;
|
||||||
Server.Connect( ServerAddress );
|
_connection.Connect( ServerAddress );
|
||||||
ClientInterface.ServerClientInstance.InvokeOnServerConnected();
|
_parent.InvokeOnServerConnected();
|
||||||
Thread RThread = new Thread(async () => { await ReceiveThread(); });
|
Thread RThread = new Thread(async () => { await ReceiveThread(); });
|
||||||
RThread.Start();
|
RThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task ReceiveThread() {
|
async Task ReceiveThread() {
|
||||||
using( NetworkStream ns = Server.GetStream() ) {
|
using( NetworkStream ns = _connection.GetStream() ) {
|
||||||
try {
|
try {
|
||||||
byte[] headerBuffer = new byte[DataPacket.HeaderLength];
|
byte[] headerBuffer = new byte[DataPacket.HeaderLength];
|
||||||
byte[] payloadBuffer = new byte[65535]; // Upper limit for safety
|
byte[] payloadBuffer = new byte[65535]; // Upper limit for safety
|
||||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
while( _parent.Alive ) {
|
||||||
|
|
||||||
// Read the header off
|
// Read the header off
|
||||||
await ns.ReadExactlyAsync(headerBuffer, 0, 3);
|
await ns.ReadExactlyAsync(headerBuffer, 0, 3);
|
||||||
@@ -46,11 +47,11 @@ namespace Mistox.Client {
|
|||||||
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject);
|
_parent.InvokeOnReceived(dataobject);
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ClientInterface.ServerClientInstance.Alive = false;
|
_parent.Alive = false;
|
||||||
Dispose();
|
Dispose();
|
||||||
Console.WriteLine("TCP Receive exception: " + e.Message);
|
Console.WriteLine("TCP Receive exception: " + e.Message);
|
||||||
}
|
}
|
||||||
@@ -59,13 +60,13 @@ namespace Mistox.Client {
|
|||||||
|
|
||||||
public async Task Send<T>(T packet) {
|
public async Task Send<T>(T packet) {
|
||||||
byte[] data = DataPacket.Serialize( packet );
|
byte[] data = DataPacket.Serialize( packet );
|
||||||
await Server.GetStream().WriteAsync( data, 0, data.Length );
|
await _connection.GetStream().WriteAsync( data, 0, data.Length );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
ClientInterface.ServerClientInstance.InvokeOnServerDisconnected();
|
_parent.InvokeOnServerDisconnected();
|
||||||
Server.Close();
|
_connection.Close();
|
||||||
Server = null;
|
_connection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,28 +9,33 @@ using System.Threading.Tasks;
|
|||||||
namespace Mistox.Client {
|
namespace Mistox.Client {
|
||||||
public class mUDPClient : IDisposable {
|
public class mUDPClient : IDisposable {
|
||||||
|
|
||||||
Socket udpClient;
|
public string UserName { get; set; } = ""; // The remote players UserName
|
||||||
IPEndPoint remoteHost;
|
public bool Alive { get; set; } = true; // Is the connection alive
|
||||||
|
|
||||||
public mUDPClient( long RemoteIP, int Port ) {
|
Socket _udpClient;
|
||||||
remoteHost = new IPEndPoint( RemoteIP, Port );
|
IPEndPoint _remoteHost;
|
||||||
udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
|
MistoxClient _parent;
|
||||||
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true );
|
|
||||||
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 );
|
public mUDPClient( MistoxClient parent, long RemoteIP, int Port ) {
|
||||||
udpClient.Bind( new IPEndPoint( IPAddress.Any, Port ) );
|
_parent = parent;
|
||||||
ClientInterface.ServerClientInstance.Alive = true;
|
_remoteHost = new IPEndPoint( RemoteIP, Port );
|
||||||
|
_udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
|
||||||
|
_udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true );
|
||||||
|
_udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 );
|
||||||
|
_udpClient.Bind( new IPEndPoint( IPAddress.Any, Port ) );
|
||||||
|
_parent.Alive = true;
|
||||||
Thread Client = new Thread(async() => { await ReceiveThread(); });
|
Thread Client = new Thread(async() => { await ReceiveThread(); });
|
||||||
Client.Start();
|
Client.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task ReceiveThread() {
|
async Task ReceiveThread() {
|
||||||
byte[] buffer = new byte[65535];
|
byte[] buffer = new byte[65535];
|
||||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
while( _parent.Alive ) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Read the data out of the UDP packet with a buffer that has the maximum possible UDP packet size
|
// Read the data out of the UDP packet with a buffer that has the maximum possible UDP packet size
|
||||||
// As UDP reads the whole next packet then drops. Im guaranteed to receive 1 packet at a time
|
// As UDP reads the whole next packet then drops. Im guaranteed to receive 1 packet at a time
|
||||||
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
int bytesRead = await _udpClient.ReceiveAsync( buffer );
|
||||||
|
|
||||||
// Create the data readers
|
// Create the data readers
|
||||||
ReadOnlyMemory<byte> dataMemory = buffer.AsMemory(0, bytesRead);
|
ReadOnlyMemory<byte> dataMemory = buffer.AsMemory(0, bytesRead);
|
||||||
@@ -45,7 +50,7 @@ namespace Mistox.Client {
|
|||||||
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data );
|
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data );
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
|
_parent.InvokeOnReceived( buildObj );
|
||||||
|
|
||||||
} catch( Exception e ) {
|
} catch( Exception e ) {
|
||||||
Console.WriteLine( "UDP Receive exception : " + e.Message );
|
Console.WriteLine( "UDP Receive exception : " + e.Message );
|
||||||
@@ -55,13 +60,13 @@ namespace Mistox.Client {
|
|||||||
|
|
||||||
public async Task Send<T>( T Data ) {
|
public async Task Send<T>( T Data ) {
|
||||||
byte[] byteData = DataPacket.Serialize( Data );
|
byte[] byteData = DataPacket.Serialize( Data );
|
||||||
await udpClient.SendToAsync( byteData, SocketFlags.None, remoteHost );
|
await _udpClient.SendToAsync( byteData, SocketFlags.None, _remoteHost );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
ClientInterface.ServerClientInstance.Alive = false;
|
_parent.Alive = false;
|
||||||
udpClient.Close();
|
_udpClient.Close();
|
||||||
udpClient = null;
|
_udpClient = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -8,28 +7,16 @@ using Mistox.Client;
|
|||||||
namespace Mistox {
|
namespace Mistox {
|
||||||
public class ClientInterface {
|
public class ClientInterface {
|
||||||
|
|
||||||
public static MistoxServerClient ServerClientInstance;
|
public static MistoxClient serverInstance;
|
||||||
public static mTCPClient serverConnection;
|
|
||||||
public static ConcurrentDictionary<string, UDPEndpoint> clientConnections;
|
|
||||||
|
|
||||||
public event Action onServerConnected;
|
public event Action onServerConnected;
|
||||||
public event Action<UDPEndpoint> onUserConnected;
|
public event Action<mUDPClient> onUserConnected;
|
||||||
public event Action<dynamic> onDataReceived;
|
public event Action<dynamic> onDataReceived;
|
||||||
public event Action<UDPEndpoint> onUserDisconnected;
|
public event Action<mUDPClient> onUserDisconnected;
|
||||||
public event Action onServerDisconnected;
|
public event Action onServerDisconnected;
|
||||||
|
|
||||||
public ClientInterface( string IpOrHostName, int Port ) {
|
public ClientInterface( string IpOrHostName, int Port ) {
|
||||||
|
|
||||||
// Wire up the Mistox server client
|
|
||||||
ServerClientInstance = new MistoxServerClient() {
|
|
||||||
Port = Port
|
|
||||||
};
|
|
||||||
ServerClientInstance.onServerConnected += onServerConnected;
|
|
||||||
ServerClientInstance.onUserConnected += (UDPEndpoint endpoint) => { onUserConnected?.Invoke(endpoint); };
|
|
||||||
ServerClientInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
|
||||||
ServerClientInstance.onUserDisconnected += (UDPEndpoint endpoint) => { onUserDisconnected?.Invoke(endpoint); };
|
|
||||||
ServerClientInstance.onServerDisconnected += onServerDisconnected;
|
|
||||||
|
|
||||||
// Get Server IP
|
// Get Server IP
|
||||||
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
|
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
|
||||||
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
||||||
@@ -38,30 +25,28 @@ namespace Mistox {
|
|||||||
ServerDenstination = new IPEndPoint( entry, Port );
|
ServerDenstination = new IPEndPoint( entry, Port );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + ServerDenstination.Address );
|
|
||||||
|
|
||||||
// Make a TCP connection to the server
|
// Start and wire up the Mistox server client
|
||||||
try {
|
serverInstance = new MistoxClient( ServerDenstination );
|
||||||
serverConnection = new mTCPClient( ServerDenstination );
|
serverInstance.onServerConnected += onServerConnected;
|
||||||
} catch( Exception e ) {
|
serverInstance.onUserConnected += (mUDPClient endpoint) => { onUserConnected?.Invoke(endpoint); };
|
||||||
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
serverInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||||
Console.WriteLine( e.ToString() );
|
serverInstance.onUserDisconnected += (mUDPClient endpoint) => { onUserDisconnected?.Invoke(endpoint); };
|
||||||
Console.WriteLine( "}" );
|
serverInstance.onServerDisconnected += onServerDisconnected;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendAllClients<Packet>(Packet data) {
|
public async Task SendAllClients<Packet>(Packet data) {
|
||||||
foreach(KeyValuePair<string, UDPEndpoint> cur in clientConnections){
|
foreach(KeyValuePair<string, mUDPClient> cur in serverInstance.clientConnections){
|
||||||
await cur.Value.Connection.Send( data );
|
await cur.Value.Send( data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||||
await clientConnections[UserName].Connection.Send( data );
|
await serverInstance.clientConnections[UserName].Send( data );
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendServer<Packet>(Packet data) {
|
public async Task SendServer<Packet>(Packet data) {
|
||||||
await serverConnection.Send( data );
|
await serverInstance.ServerConnection.Send( data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ namespace Mistox.Server {
|
|||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ClientInterface.ServerClientInstance.Alive = false;
|
_parent.Alive = false;
|
||||||
Dispose();
|
Dispose();
|
||||||
Console.WriteLine("TCP Receive exception: " + e.Message);
|
Console.WriteLine("TCP Receive exception: " + e.Message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user