44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Mistox.Client {
|
|
public class mUDPClient : IDisposable {
|
|
|
|
public string UserName { get; set; } = ""; // The remote players UserName
|
|
public bool Alive { get; set; } = true; // Is the connection alive
|
|
public IPEndPoint IPAddress { get; set; } // The remote IP Address
|
|
|
|
IPEndPoint _remoteHost;
|
|
MistoxClient _parent;
|
|
|
|
public mUDPClient( MistoxClient parent, IPAddress RemoteIP, int Port ) {
|
|
_parent = parent;
|
|
_remoteHost = new IPEndPoint( RemoteIP, Port );
|
|
_parent.Alive = true;
|
|
}
|
|
|
|
public async Task Send<T>( T Data ) {
|
|
byte[] byteData = DataPacket.Serialize( Data );
|
|
await _parent.ClientSocket.SendAsync( byteData, byteData.Length, _remoteHost );
|
|
}
|
|
|
|
public static UInt128 GetConnectionKey(IPAddress ipAddress) {
|
|
byte[] bytes = ipAddress.GetAddressBytes();
|
|
if (bytes.Length == 4) {
|
|
bytes = ipAddress.MapToIPv6().GetAddressBytes();
|
|
}
|
|
return BitConverter.ToUInt128(bytes, 0);
|
|
}
|
|
|
|
public UInt128 GetConnectionKey() {
|
|
return GetConnectionKey( IPAddress.Address );
|
|
}
|
|
|
|
public void Dispose() {
|
|
_parent.Alive = false;
|
|
_parent.ClientSocket.Close();
|
|
_parent.ClientSocket = null;
|
|
}
|
|
}
|
|
} |