1- using System ;
1+ using NetworkLibrary . Utils ;
2+ using System ;
23using System . Collections . Concurrent ;
34using System . Collections . Generic ;
45using System . Diagnostics ;
@@ -49,15 +50,15 @@ public int SocketSendBufferSize
4950 public AsyncUdpClient ( )
5051 {
5152 recieveBuffer = new byte [ 65500 ] ;
52- clientSocket = new Socket ( SocketType . Dgram , ProtocolType . Udp ) ;
53+ clientSocket = new Socket ( AddressFamily . InterNetwork , SocketType . Dgram , ProtocolType . Udp ) ;
5354
54- clientSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . ExclusiveAddressUse , false ) ;
55+ clientSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . ExclusiveAddressUse , true ) ;
5556 //clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
56- clientSocket . SetSocketOption ( SocketOptionLevel . IP , SocketOptionName . PacketInformation , true ) ;
57+ // clientSocket.SetSocketOption(SocketOptionLevel.Udp , SocketOptionName.PacketInformation, true);
5758
5859 clientSocket . ReceiveBufferSize = ReceiveBufferSize ;
5960 clientSocket . SendBufferSize = SocketSendBufferSize ;
60- clientSocket . Blocking = false ;
61+ clientSocket . Blocking = true ;
6162
6263 RemoteEndPoint = new IPEndPoint ( IPAddress . Any , 0 ) ;
6364 }
@@ -85,7 +86,9 @@ public void SetRemoteEnd(string ip, int port, bool receive = true)
8586 {
8687 if ( IPAddress . TryParse ( ip , out var ipAdress ) )
8788 {
88- RemoteEndPoint = new IPEndPoint ( ipAdress , port ) ;
89+ // IPV6 is not compatible with Unity..
90+ var ep = new IPEndPoint ( ipAdress . MapToIPv4 ( ) , port ) ;
91+ RemoteEndPoint = ep ;
8992 if ( receive )
9093 Receive ( ) ;
9194 }
@@ -103,7 +106,7 @@ public void Connect(string IP, int port)
103106 RemoteEndPoint = new IPEndPoint ( IPAddress . Parse ( IP ) , port ) ;
104107 clientSocket . Connect ( RemoteEndPoint ) ;
105108 Connected = true ;
106- clientSocket . Blocking = false ;
109+ // clientSocket.Blocking = false;
107110 clientSocket . BeginReceive ( recieveBuffer , 0 , recieveBuffer . Length , SocketFlags . None , EndRecieve , null ) ;
108111 }
109112
@@ -146,7 +149,7 @@ private void EndRecieve(IAsyncResult ar)
146149 {
147150 amount = clientSocket . EndReceive ( ar ) ;
148151 }
149- catch ( SocketException e )
152+ catch ( Exception e )
150153 {
151154 OnError ? . Invoke ( e ) ;
152155 return ;
@@ -186,13 +189,22 @@ protected virtual void HandleBytesReceived(byte[] buffer, int offset, int count)
186189
187190 public virtual void SendAsync ( byte [ ] bytes , int offset , int count )
188191 {
192+
189193 try
190194 {
191- clientSocket . SendTo ( bytes , offset , count , SocketFlags . None , RemoteEndPoint ) ;
195+ if ( Connected )
196+ {
197+ clientSocket . Send ( bytes , offset , count , SocketFlags . None ) ;
198+ }
199+ else
200+ {
201+ clientSocket . SendTo ( bytes , offset , count , SocketFlags . None , RemoteEndPoint ) ;
202+ }
192203
193204 }
194205 catch ( Exception e )
195206 {
207+ MiniLogger . Log ( MiniLogger . LogLevel . Error , "Unable to send the Udp Datagram due to : " + e . Message ) ;
196208 }
197209 }
198210
0 commit comments