Skip to content

Commit e9838fd

Browse files
committed
Unity compatibility patch (Aes to RijndaelManaged)
Cert. Validation callback fix
1 parent 1cde5f1 commit e9838fd

File tree

13 files changed

+67
-40
lines changed

13 files changed

+67
-40
lines changed

NetworkLibrary/Components/AesAlgorithm.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NetworkLibrary.Components
88
{
99
public class AesAlgorithm:IDisposable
1010
{
11-
private readonly Aes algorithm;
11+
private readonly SymmetricAlgorithm algorithm;
1212
private readonly ICryptoTransform encryptor;
1313
private readonly ICryptoTransform decryptor;
1414

@@ -22,7 +22,8 @@ public AesAlgorithm(byte[] Key, byte[] IV,string algorithmName = null)
2222
{
2323
if(algorithmName == null)
2424
{
25-
algorithm = Aes.Create();
25+
// AES.create is not compatible with Unity...
26+
algorithm = new System.Security.Cryptography.RijndaelManaged();//Aes.Create();
2627
}
2728
else
2829
{

NetworkLibrary/NetworkLibrary.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<Authors>RefrenceType</Authors>
1414
<PackageLicenseFile>Licence.txt</PackageLicenseFile>
1515
<Copyright>Apache 2.0</Copyright>
16-
<Version>1.0.3</Version>
16+
<Version>1.0.6</Version>
17+
<AssemblyVersion>1.0.5.0</AssemblyVersion>
18+
<FileVersion>1.0.5.0</FileVersion>
1719
</PropertyGroup>
1820

1921
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

NetworkLibrary/TCP/Base/AsyncTcpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public AsyncTpcClient() {}
2929

3030
public override void Connect(string IP, int port)
3131
{
32-
ConnectAsyncAwaitable(IP, port).Wait();
32+
_ = ConnectAsyncAwaitable(IP, port).Result;
3333
}
3434

3535
public override async Task<bool> ConnectAsyncAwaitable(string IP, int port)

NetworkLibrary/UDP/AsyncUdpClient.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using NetworkLibrary.Utils;
2+
using System;
23
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using 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

NetworkLibrary/UDP/AsyncUdpServer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ protected ConcurrentDictionary<IPEndPoint, UdpStatistics> Statistics
5555

5656
public AsyncUdpServer(int port = 20008)
5757
{
58-
ServerSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);
58+
// IPV6 is not compatible with Unity.
59+
ServerSocket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
5960
ServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true);
61+
// Not compatible with Unity..
62+
//ServerSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
63+
6064

6165
ServerSocket.ReceiveBufferSize = SocketReceiveBufferSize;
6266
ServerSocket.SendBufferSize = SocketSendBufferSize;

NetworkLibrary/UDP/Secure/SecureUdpClient.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ protected override void HandleBytesReceived(byte[] buffer, int offset, int count
3232
var decryptBuffer = BufferPool.RentBuffer(count+256);
3333
try
3434
{
35-
var decriptedAmount = algorithm.DecryptInto(buffer, offset, count, decryptBuffer, 0);
36-
HandleDecrypedBytes(decryptBuffer, 0, decriptedAmount);
35+
if (algorithm != null)
36+
{
37+
var decriptedAmount = algorithm.DecryptInto(buffer, offset, count, decryptBuffer, 0);
38+
HandleDecrypedBytes(decryptBuffer, 0, decriptedAmount);
39+
}
40+
else
41+
{
42+
HandleDecrypedBytes(buffer, offset, count);
43+
}
44+
3745
}
3846
catch (Exception e)
3947
{
@@ -55,8 +63,16 @@ public override void SendAsync(byte[] bytes, int offset, int count)
5563
var buffer = BufferPool.RentBuffer(count+256);
5664
try
5765
{
58-
int amount = algorithm.EncryptInto(bytes, offset, count, buffer, 0);
59-
base.SendAsync(buffer, 0, amount);
66+
if (algorithm != null)
67+
{
68+
int amount = algorithm.EncryptInto(bytes, offset, count, buffer, 0);
69+
base.SendAsync(buffer, 0, amount);
70+
}
71+
else
72+
{
73+
base.SendAsync(bytes, offset, count);
74+
}
75+
6076

6177
}
6278
catch(Exception ex)

Protobuff/P2P/HolePunch/ClientHolepunchState.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public ClientHolepunchState(RelayClient client, Guid stateId, Guid To)
3232

3333
public void HandleMessage(MessageEnvelope message)
3434
{
35-
MiniLogger.Log(MiniLogger.LogLevel.Info, "ClientStateGotMsg");
3635
switch (message.Header)
3736
{
3837
case HolepunchHeaders.CreateChannel:
@@ -143,8 +142,8 @@ private void HandleSuccess(MessageEnvelope message)
143142
{
144143
MiniLogger.Log(MiniLogger.LogLevel.Info, "success!!");
145144
message.LockBytes();
146-
holepunchClient.SwapAlgorith(new ConcurrentAesAlgorithm(message.Payload, message.Payload));
147-
//holepunchClient.Connect((IPEndPoint)holepunchClient.RemoteEndPoint);
145+
holepunchClient.SwapAlgorith(new ConcurrentAesAlgorithm(message.Payload, message.Payload));
146+
// holepunchClient.SwapAlgorith(null);
148147
Completion.TrySetResult(holepunchClient);
149148
}
150149

Protobuff/P2P/RelayClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ private void ResendUdpInitMessage(MessageEnvelope message)
548548
message.Payload = null;
549549
message.IsInternal = true;
550550

551+
MiniLogger.Log(MiniLogger.LogLevel.Info, "Resending Udp Init to " + udpRelayClient.RemoteEndPoint.ToString());
551552
udpRelayClient.SendAsyncMessage(message);
552553
protoClient.SendAsyncMessage(message);
553554
}

Protobuff/P2P/RelayServer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,11 @@ private void SendEndpoint(Guid clientId, MessageEnvelope message)
328328

329329
private void UdpClientAccepted(SocketAsyncEventArgs ClientSocket)
330330
{
331-
332331
}
333332

334333

335334
private void HandleUdpBytesReceived(IPEndPoint adress, byte[] bytes, int offset, int count)
336335
{
337-
338336
// Client is trying to register its Udp endpoint here
339337
if (!UdpCryptos.ContainsKey(adress))
340338
{
@@ -365,9 +363,9 @@ private void HandleUdpBytesReceived(IPEndPoint adress, byte[] bytes, int offset,
365363
}
366364
}
367365
}
368-
catch
366+
catch(Exception e)
369367
{
370-
MiniLogger.Log(MiniLogger.LogLevel.Error, "Udp Relay failed to deserialise envelope message ");
368+
MiniLogger.Log(MiniLogger.LogLevel.Error, "Udp Relay failed to deserialise envelope message "+e.Message);
371369
return;
372370
}
373371
finally

Protobuff/ProtoServer.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class ProtoServer
3838
internal readonly ProtoServerInternal server;
3939
private ConcurrentProtoSerialiser serialiser = new ConcurrentProtoSerialiser();
4040
private MessageAwaiter awaiter;
41-
private SharerdMemoryStreamPool streamPool = new SharerdMemoryStreamPool();
4241

4342

4443
public ProtoServer(int port)
@@ -89,8 +88,6 @@ public async Task<MessageEnvelope> SendMessageAndWaitResponse<T>(Guid clientId,
8988
message.MessageId = Guid.NewGuid();
9089

9190
var result = awaiter.RegisterWait(message.MessageId, timeoutMs);
92-
message.MessageId = Guid.NewGuid();
93-
9491
SendAsyncMessage(clientId, message, buffer, offset, count);
9592
return await result;
9693
}
@@ -101,8 +98,6 @@ public async Task<MessageEnvelope> SendMessageAndWaitResponse<T>(Guid clientId,
10198
message.MessageId = Guid.NewGuid();
10299

103100
var result = awaiter.RegisterWait(message.MessageId, timeoutMs);
104-
message.MessageId = Guid.NewGuid();
105-
106101
SendAsyncMessage(clientId, message, payload);
107102
return await result;
108103
}
@@ -112,9 +107,7 @@ public async Task<MessageEnvelope> SendMessageAndWaitResponse(Guid clientId, Mes
112107
if (message.MessageId == Guid.Empty)
113108
message.MessageId = Guid.NewGuid();
114109

115-
message.MessageId = Guid.NewGuid();
116110
var result = awaiter.RegisterWait(message.MessageId, timeoutMs);
117-
118111
SendAsyncMessage(clientId, message);
119112
return await result;
120113
}

0 commit comments

Comments
 (0)