Wire up the event handlers
This commit is contained in:
1 parent
a04516c470
commit
a689f2656f
11 files changed
+90
-76
No files matched your search
@@ -11,39 +11,41 @@ namespace Mistox.Client {
|
||||
|
||||
Socket udpClient;
|
||||
|
||||
public mUDPClient( IPEndPoint ServerAddress, string ConnectionID ) {
|
||||
IPEndPoint remoteHost;
|
||||
|
||||
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, ServerAddress.Port ) );
|
||||
Alive = true;
|
||||
Port = ServerAddress.Port;
|
||||
udpClient.Bind( new IPEndPoint( IPAddress.Any, Port ) );
|
||||
ClientInterface.ServerClientInstance.Alive = true;
|
||||
Thread Client = new Thread(async() => { await ReceiveThread(); });
|
||||
Client.Start();
|
||||
}
|
||||
|
||||
async Task ReceiveThread() {
|
||||
while( Alive ) {
|
||||
while( ClientInterface.ServerClientInstance.Alive ) {
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
||||
dynamic data = mSerialize.uReceive( buffer.Sub(0, bytesRead) );
|
||||
dynamic data = Serializer.uReceive( buffer.Sub(0, bytesRead) );
|
||||
if( data != null ) {
|
||||
onReceived?.Invoke( data, new EventArgs() );
|
||||
ClientInterface.ServerClientInstance.InvokeOnReceived( data );
|
||||
}
|
||||
} catch( Exception ) {
|
||||
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "UDP Receive exception : " + e.Message );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Send<T>( T Data, IPEndPoint remoteHost ) {
|
||||
public async Task Send<T>( T Data ) {
|
||||
byte[] byteData = DataPacket.Serialize( Data );
|
||||
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
|
||||
await udpClient.SendToAsync( byteData, SocketFlags.None, remoteHost );
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Alive = false;
|
||||
ClientInterface.ServerClientInstance.Alive = false;
|
||||
udpClient.Close();
|
||||
udpClient = null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user