Rework client to be non static
This commit is contained in:
1 parent
6f910e969a
commit
b439805f3e
7 files changed
+107
-108
No files matched your search
@@ -9,28 +9,33 @@ using System.Threading.Tasks;
|
||||
namespace Mistox.Client {
|
||||
public class mUDPClient : IDisposable {
|
||||
|
||||
Socket udpClient;
|
||||
IPEndPoint remoteHost;
|
||||
public string UserName { get; set; } = ""; // The remote players UserName
|
||||
public bool Alive { get; set; } = true; // Is the connection alive
|
||||
|
||||
public mUDPClient( long RemoteIP, int Port ) {
|
||||
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 ) );
|
||||
ClientInterface.ServerClientInstance.Alive = true;
|
||||
Socket _udpClient;
|
||||
IPEndPoint _remoteHost;
|
||||
MistoxClient _parent;
|
||||
|
||||
public mUDPClient( MistoxClient parent, long RemoteIP, int Port ) {
|
||||
_parent = parent;
|
||||
_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(); });
|
||||
Client.Start();
|
||||
}
|
||||
|
||||
async Task ReceiveThread() {
|
||||
byte[] buffer = new byte[65535];
|
||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
||||
while( _parent.Alive ) {
|
||||
try {
|
||||
|
||||
// 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
|
||||
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
||||
int bytesRead = await _udpClient.ReceiveAsync( buffer );
|
||||
|
||||
// Create the data readers
|
||||
ReadOnlyMemory<byte> dataMemory = buffer.AsMemory(0, bytesRead);
|
||||
@@ -45,7 +50,7 @@ namespace Mistox.Client {
|
||||
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data );
|
||||
|
||||
// Return the object
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
|
||||
_parent.InvokeOnReceived( buildObj );
|
||||
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "UDP Receive exception : " + e.Message );
|
||||
@@ -55,13 +60,13 @@ namespace Mistox.Client {
|
||||
|
||||
public async Task Send<T>( T Data ) {
|
||||
byte[] byteData = DataPacket.Serialize( Data );
|
||||
await udpClient.SendToAsync( byteData, SocketFlags.None, remoteHost );
|
||||
await _udpClient.SendToAsync( byteData, SocketFlags.None, _remoteHost );
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
ClientInterface.ServerClientInstance.Alive = false;
|
||||
udpClient.Close();
|
||||
udpClient = null;
|
||||
_parent.Alive = false;
|
||||
_udpClient.Close();
|
||||
_udpClient = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user