Files
GameServer/MistoxServer/Client/UDPClient.cs
T

40 lines
1.4 KiB
C#

using System;
using System.Net;
using System.Numerics;
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 WithinRange { get; set; } = true; // Is the user close enough?
public Vector3 Position { get; set; } = new Vector3(0, 0, 0); // The remote location of the player
private 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();
}
}
}