Files
GameServer/MistoxServer/Client/UDPClient.cs
T
2026-08-03 10:01:31 -07:00

38 lines
1.3 KiB
C#

using System;
using System.Net;
using System.Threading;
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 _remoteHost { get; set; } // The remote IP Address
private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
private readonly MistoxClient _parent;
public mUDPClient( MistoxClient parent, IPEndPoint remoteHost ) {
_parent = parent;
_remoteHost = remoteHost;
_parent.Alive = true;
}
public async Task Send<T>( T Data ) {
byte[] byteData = DataPacket.Serialize( Data );
await _sendLock.WaitAsync();
try {
await _parent.ClientSocket.SendAsync( byteData, byteData.Length, _remoteHost );
} finally {
_sendLock.Release();
}
}
public void Dispose() {
_parent.Alive = false;
_parent.ClientSocket.Close();
_parent.ClientSocket.Dispose();
}
}
}