Skip to content

Commit

Permalink
First iteration improved multiplatform support
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lidgren committed Mar 24, 2015
1 parent 8033a73 commit fa8b1e9
Show file tree
Hide file tree
Showing 25 changed files with 674 additions and 401 deletions.
12 changes: 0 additions & 12 deletions Lidgren.Network/Encryption/NetAESEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,18 @@ namespace Lidgren.Network
public class NetAESEncryption : NetCryptoProviderBase
{
public NetAESEncryption(NetPeer peer)
#if UNITY_WEBPLAYER
: base(peer, new RijndaelManaged())
#else
: base(peer, new AesCryptoServiceProvider())
#endif
{
}

public NetAESEncryption(NetPeer peer, string key)
#if UNITY_WEBPLAYER
: base(peer, new RijndaelManaged())
#else
: base(peer, new AesCryptoServiceProvider())
#endif
{
SetKey(key);
}

public NetAESEncryption(NetPeer peer, byte[] data, int offset, int count)
#if UNITY_WEBPLAYER
: base(peer, new RijndaelManaged())
#else
: base(peer, new AesCryptoServiceProvider())
#endif
{
SetKey(data, offset, count);
}
Expand Down
59 changes: 59 additions & 0 deletions Lidgren.Network/Encryption/NetCryptoProviderEncryption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;
using System.Security.Cryptography;

namespace Lidgren.Network
{
public abstract class NetCryptoProviderEncryption : NetEncryption
{
public NetCryptoProviderEncryption(NetPeer peer)
: base(peer)
{
}

protected abstract CryptoStream GetEncryptStream(MemoryStream ms);

protected abstract CryptoStream GetDecryptStream(MemoryStream ms);

public override bool Encrypt(NetOutgoingMessage msg)
{
int unEncLenBits = msg.LengthBits;

var ms = new MemoryStream();
var cs = GetEncryptStream(ms);
cs.Write(msg.m_data, 0, msg.LengthBytes);
cs.Close();

// get results
var arr = ms.ToArray();
ms.Close();

msg.EnsureBufferSize((arr.Length + 4) * 8);
msg.LengthBits = 0; // reset write pointer
msg.Write((uint)unEncLenBits);
msg.Write(arr);
msg.LengthBits = (arr.Length + 4) * 8;

return true;
}

public override bool Decrypt(NetIncomingMessage msg)
{
int unEncLenBits = (int)msg.ReadUInt32();

var ms = new MemoryStream(msg.m_data, 4, msg.LengthBytes - 4);
var cs = GetDecryptStream(ms);

var result = m_peer.GetStorage(unEncLenBits);
cs.Read(result, 0, NetUtility.BytesToHoldBits(unEncLenBits));
cs.Close();

// TODO: recycle existing msg

msg.m_data = result;
msg.m_bitLength = unEncLenBits;

return true;
}
}
}
6 changes: 3 additions & 3 deletions Lidgren.Network/Encryption/NetXteaEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public NetXtea(NetPeer peer, byte[] key)
/// String to hash for key
/// </summary>
public NetXtea(NetPeer peer, string key)
: this(peer, NetUtility.CreateSHA1Hash(key), 32)
: this(peer, NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(key)), 32)
{
}

public override void SetKey(byte[] data, int offset, int length)
{
var key = NetUtility.CreateSHA1Hash(data, offset, length);
NetException.Assert(key.Length == 16);
var key = NetUtility.ComputeSHAHash(data, offset, length);
NetException.Assert(key.Length >= 16);
SetKey(key, 0, 16);
}

Expand Down
4 changes: 4 additions & 0 deletions Lidgren.Network/Lidgren.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
<Compile Include="NetUnreliableUnorderedReceiver.cs" />
<Compile Include="NetUPnP.cs" />
<Compile Include="NetUtility.cs" />
<Compile Include="Platform\PlatformAndroid.cs" />
<Compile Include="Platform\PlatformConstrained.cs" />
<Compile Include="Platform\PlatformWin32.cs" />
<Compile Include="Platform\PlatformWinRT.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions Lidgren.Network/NetBigInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,4 +2334,19 @@ public int GetLowestSetBit()
return ((word >> (n % 32)) & 1) > 0;
}
}

#if WINDOWS_RUNTIME
internal sealed class Stack
{
private System.Collections.Generic.List<object> m_list = new System.Collections.Generic.List<object>();
public int Count { get { return m_list.Count; } }
public void Push(object item) { m_list.Add(item); }
public object Pop()
{
var item = m_list[m_list.Count - 1];
m_list.RemoveAt(m_list.Count - 1);
return item;
}
}
#endif
}
10 changes: 7 additions & 3 deletions Lidgren.Network/NetBuffer.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using System.Reflection;
using System.Net;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
/// <summary>
Expand Down Expand Up @@ -638,14 +642,14 @@ public double ReadTime(NetConnection connection, bool highPrecision)
/// <summary>
/// Reads a stored IPv4 endpoint description
/// </summary>
public IPEndPoint ReadIPEndPoint()
public NetEndPoint ReadIPEndPoint()
{
byte len = ReadByte();
byte[] addressBytes = ReadBytes(len);
int port = (int)ReadUInt16();

IPAddress address = new IPAddress(addressBytes);
return new IPEndPoint(address, port);
var address = NetUtility.CreateAddressFromBytes(addressBytes);
return new NetEndPoint(address, port);
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion Lidgren.Network/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
using System;
using System.Net;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
/// <summary>
Expand Down Expand Up @@ -80,7 +84,7 @@ public NetClient(NetPeerConfiguration config)
/// <param name="remoteEndPoint">The remote endpoint to connect to</param>
/// <param name="hailMessage">The hail message to pass</param>
/// <returns>server connection, or null if already connected</returns>
public override NetConnection Connect(IPEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
public override NetConnection Connect(NetEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
{
lock (m_connections)
{
Expand Down
8 changes: 6 additions & 2 deletions Lidgren.Network/NetConnection.Handshake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using System.Text;
using System.Threading;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
public partial class NetConnection
Expand Down Expand Up @@ -167,7 +171,7 @@ internal void SendConnectResponse(double now, bool onLibraryThread)
if (onLibraryThread)
m_peer.SendLibrary(om, m_remoteEndPoint);
else
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<System.Net.IPEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));

m_lastHandshakeSendTime = now;
m_handshakeAttempts++;
Expand All @@ -189,7 +193,7 @@ internal void SendDisconnect(string reason, bool onLibraryThread)
if (onLibraryThread)
m_peer.SendLibrary(om, m_remoteEndPoint);
else
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<System.Net.IPEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));
}

private void WriteLocalHail(NetOutgoingMessage om)
Expand Down
12 changes: 8 additions & 4 deletions Lidgren.Network/NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using System.Threading;
using System.Diagnostics;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
/// <summary>
Expand All @@ -18,7 +22,7 @@ public partial class NetConnection
internal NetPeerConfiguration m_peerConfiguration;
internal NetConnectionStatus m_status;
internal NetConnectionStatus m_visibleStatus;
internal IPEndPoint m_remoteEndPoint;
internal NetEndPoint m_remoteEndPoint;
internal NetSenderChannelBase[] m_sendChannels;
internal NetReceiverChannelBase[] m_receiveChannels;
internal NetOutgoingMessage m_localHailMessage;
Expand Down Expand Up @@ -57,7 +61,7 @@ public object Tag
/// <summary>
/// Gets the remote endpoint for the connection
/// </summary>
public IPEndPoint RemoteEndPoint { get { return m_remoteEndPoint; } }
public NetEndPoint RemoteEndPoint { get { return m_remoteEndPoint; } }

/// <summary>
/// Gets the unique identifier of the remote NetPeer for this connection
Expand All @@ -78,7 +82,7 @@ internal double GetResendDelay()
return 0.025 + (avgRtt * 2.1); // 25 ms + double rtt
}

internal NetConnection(NetPeer peer, IPEndPoint remoteEndPoint)
internal NetConnection(NetPeer peer, NetEndPoint remoteEndPoint)
{
m_peer = peer;
m_peerConfiguration = m_peer.Configuration;
Expand All @@ -97,7 +101,7 @@ internal NetConnection(NetPeer peer, IPEndPoint remoteEndPoint)
/// <summary>
/// Change the internal endpoint to this new one. Used when, during handshake, a switch in port is detected (due to NAT)
/// </summary>
internal void MutateEndPoint(IPEndPoint endPoint)
internal void MutateEndPoint(NetEndPoint endPoint)
{
m_remoteEndPoint = endPoint;
}
Expand Down
10 changes: 7 additions & 3 deletions Lidgren.Network/NetIncomingMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
using System.Net;
using System.Diagnostics;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
/// <summary>
Expand All @@ -29,7 +33,7 @@ namespace Lidgren.Network
public sealed class NetIncomingMessage : NetBuffer
{
internal NetIncomingMessageType m_incomingMessageType;
internal IPEndPoint m_senderEndPoint;
internal NetEndPoint m_senderEndPoint;
internal NetConnection m_senderConnection;
internal int m_sequenceNumber;
internal NetMessageType m_receivedMessageType;
Expand All @@ -52,9 +56,9 @@ public sealed class NetIncomingMessage : NetBuffer
public int SequenceChannel { get { return (int)m_receivedMessageType - (int)NetUtility.GetDeliveryMethod(m_receivedMessageType); } }

/// <summary>
/// IPEndPoint of sender, if any
/// endpoint of sender, if any
/// </summary>
public IPEndPoint SenderEndPoint { get { return m_senderEndPoint; } }
public NetEndPoint SenderEndPoint { get { return m_senderEndPoint; } }

/// <summary>
/// NetConnection of sender, if any
Expand Down
30 changes: 17 additions & 13 deletions Lidgren.Network/NetNatIntroduction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using System.Net;
using System.Threading;

#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif

namespace Lidgren.Network
{
public partial class NetPeer
Expand All @@ -11,10 +15,10 @@ public partial class NetPeer
/// Send NetIntroduction to hostExternal and clientExternal; introducing client to host
/// </summary>
public void Introduce(
IPEndPoint hostInternal,
IPEndPoint hostExternal,
IPEndPoint clientInternal,
IPEndPoint clientExternal,
NetEndPoint hostInternal,
NetEndPoint hostExternal,
NetEndPoint clientInternal,
NetEndPoint clientExternal,
string token)
{
// send message to client
Expand All @@ -25,7 +29,7 @@ public partial class NetPeer
um.Write(hostExternal);
um.Write(token);
Interlocked.Increment(ref um.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<IPEndPoint, NetOutgoingMessage>(clientExternal, um));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(clientExternal, um));

// send message to host
um = CreateMessage(10 + token.Length + 1);
Expand All @@ -35,7 +39,7 @@ public partial class NetPeer
um.Write(clientExternal);
um.Write(token);
Interlocked.Increment(ref um.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<IPEndPoint, NetOutgoingMessage>(hostExternal, um));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(hostExternal, um));
}

/// <summary>
Expand All @@ -49,8 +53,8 @@ internal void HandleNatIntroduction(int ptr)
NetIncomingMessage tmp = SetupReadHelperMessage(ptr, 1000); // never mind length

byte hostByte = tmp.ReadByte();
IPEndPoint remoteInternal = tmp.ReadIPEndPoint();
IPEndPoint remoteExternal = tmp.ReadIPEndPoint();
NetEndPoint remoteInternal = tmp.ReadIPEndPoint();
NetEndPoint remoteExternal = tmp.ReadIPEndPoint();
string token = tmp.ReadString();
bool isHost = (hostByte != 0);

Expand All @@ -67,7 +71,7 @@ internal void HandleNatIntroduction(int ptr)
punch.Write(hostByte);
punch.Write(token);
Interlocked.Increment(ref punch.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<IPEndPoint, NetOutgoingMessage>(remoteInternal, punch));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(remoteInternal, punch));
LogDebug("NAT punch sent to " + remoteInternal);

// send external punch
Expand All @@ -76,15 +80,15 @@ internal void HandleNatIntroduction(int ptr)
punch.Write(hostByte);
punch.Write(token);
Interlocked.Increment(ref punch.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<IPEndPoint, NetOutgoingMessage>(remoteExternal, punch));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(remoteExternal, punch));
LogDebug("NAT punch sent to " + remoteExternal);

}

/// <summary>
/// Called when receiving a NatPunchMessage from a remote endpoint
/// </summary>
private void HandleNatPunch(int ptr, IPEndPoint senderEndPoint)
private void HandleNatPunch(int ptr, NetEndPoint senderEndPoint)
{
NetIncomingMessage tmp = SetupReadHelperMessage(ptr, 1000); // never mind length

Expand All @@ -100,7 +104,7 @@ private void HandleNatPunch(int ptr, IPEndPoint senderEndPoint)
LogDebug("NAT punch received from " + senderEndPoint + " we're client, so we've succeeded - token is " + token);

//
// Release punch success to client; enabling him to Connect() to msg.SenderIPEndPoint if token is ok
// Release punch success to client; enabling him to Connect() to msg.Sender if token is ok
//
NetIncomingMessage punchSuccess = CreateIncomingMessage(NetIncomingMessageType.NatIntroductionSuccess, 10);
punchSuccess.m_senderEndPoint = senderEndPoint;
Expand All @@ -113,7 +117,7 @@ private void HandleNatPunch(int ptr, IPEndPoint senderEndPoint)
punch.Write((byte)0);
punch.Write(token);
Interlocked.Increment(ref punch.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<IPEndPoint, NetOutgoingMessage>(senderEndPoint, punch));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(senderEndPoint, punch));
}
}
}
Loading

0 comments on commit fa8b1e9

Please sign in to comment.