Skip to content

Commit

Permalink
Fixing LiteNet to 1.2.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidumanskiy committed Feb 12, 2024
1 parent d6204cb commit 84a76b1
Show file tree
Hide file tree
Showing 23 changed files with 900 additions and 495 deletions.
2 changes: 1 addition & 1 deletion source/Fenrir.Multiplayer/Fenrir.Multiplayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Fenrir Multiplayer Library</Description>
<Version>1.0.22</Version>
<Version>1.0.23</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Crc32cLayer() : base(CRC32C.ChecksumSize)

}

public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length)
{
if (length < NetConstants.HeaderSize + CRC32C.ChecksumSize)
{
Expand All @@ -22,7 +22,7 @@ public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] da
}

int checksumPoint = length - CRC32C.ChecksumSize;
if (CRC32C.Compute(data, offset, checksumPoint) != BitConverter.ToUInt32(data, checksumPoint))
if (CRC32C.Compute(data, 0, checksumPoint) != BitConverter.ToUInt32(data, checksumPoint))
{
NetDebug.Write("[NM] DataReceived checksum: bad!");
//Set length to 0 to have netManager drop the packet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected PacketLayerBase(int extraPacketSizeForLayer)
ExtraPacketSizeForLayer = extraPacketSizeForLayer;
}

public abstract void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length);
public abstract void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length);
public abstract void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,22 @@ public void SetKey(byte[] key)
Buffer.BlockCopy(key, 0, _byteKey, 0, key.Length);
}

public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length)
{
if (_byteKey == null)
return;
var cur = offset;
for (var i = 0; i < length; i++, cur++)
for (int i = 0; i < length; i++)
{
data[cur] = (byte)(data[cur] ^ _byteKey[i % _byteKey.Length]);
data[i] = (byte)(data[i] ^ _byteKey[i % _byteKey.Length]);
}
}

public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
{
if (_byteKey == null)
return;
var cur = offset;
for (var i = 0; i < length; i++, cur++)
int cur = offset;
for (int i = 0; i < length; i++, cur++)
{
data[cur] = (byte)(data[cur] ^ _byteKey[i % _byteKey.Length]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1705;1591</NoWarn>
<PackageVersion>1.1.0</PackageVersion>
<PackageVersion>1.2.0</PackageVersion>
<Title>Lite reliable UDP library for Mono and .NET </Title>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<AssemblyVersion>1.2.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand All @@ -27,7 +30,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);LITENETLIB_UNSAFE</DefineConstants>
<PackageTags>udp reliable-udp network</PackageTags>
<PackageReleaseNotes>https://github.com/RevenantX/LiteNetLib/releases/tag/v1.1.0</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/RevenantX/LiteNetLib/releases/tag/v1.2.0</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/RevenantX/LiteNetLib</RepositoryUrl>
<PackageProjectUrl>https://github.com/RevenantX/LiteNetLib</PackageProjectUrl>
Expand All @@ -39,6 +42,7 @@
<Description>Lite reliable UDP library for .NET, Mono, and .NET Core</Description>
<PackageIcon>LNL.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TargetFrameworks>net471;net6.0;net7.0;net8.0;net5.0;netstandard2.0;netstandard2.1;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>

<Target Name="UpdateUnityDLLS" AfterTargets="CopyFilesToOutputDirectory" Condition=" '$(TargetFramework)' == 'net471' and '$(Configuration)' == 'Release' ">
Expand Down
24 changes: 15 additions & 9 deletions source/UnityPackage/Assets/ThirdParty/LiteNetLib/NatPunchModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Sockets;
using LiteNetLib.Utils;

namespace LiteNetLib
Expand Down Expand Up @@ -58,21 +60,21 @@ struct SuccessEventData

class NatIntroduceRequestPacket
{
public IPEndPoint Internal { get; set; }
public string Token { get; set; }
public IPEndPoint Internal { [Preserve] get; [Preserve] set; }
public string Token { [Preserve] get; [Preserve] set; }
}

class NatIntroduceResponsePacket
{
public IPEndPoint Internal { get; set; }
public IPEndPoint External { get; set; }
public string Token { get; set; }
public IPEndPoint Internal { [Preserve] get; [Preserve] set; }
public IPEndPoint External { [Preserve] get; [Preserve] set; }
public string Token { [Preserve] get; [Preserve] set; }
}

class NatPunchPacket
{
public string Token { get; set; }
public bool IsExternal { get; set; }
public string Token { [Preserve] get; [Preserve] set; }
public bool IsExternal { [Preserve] get; [Preserve] set; }
}

private readonly NetManager _socket;
Expand Down Expand Up @@ -111,7 +113,11 @@ public void Init(INatPunchListener listener)
_natPunchListener = listener;
}

private void Send<T>(T packet, IPEndPoint target) where T : class, new()
private void Send<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.SerializerMemberTypes)]
#endif
T>(T packet, IPEndPoint target) where T : class, new()
{
_cacheWriter.Reset();
_cacheWriter.Put((byte)PacketProperty.NatMessage);
Expand Down Expand Up @@ -173,7 +179,7 @@ public void SendNatIntroduceRequest(IPEndPoint masterServerEndPoint, string addi
{
//prepare outgoing data
string networkIp = NetUtils.GetLocalIp(LocalAddrType.IPv4);
if (string.IsNullOrEmpty(networkIp))
if (string.IsNullOrEmpty(networkIp) || masterServerEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
{
networkIp = NetUtils.GetLocalIp(LocalAddrType.IPv6);
}
Expand Down
128 changes: 3 additions & 125 deletions source/UnityPackage/Assets/ThirdParty/LiteNetLib/NativeSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,111 +7,9 @@

namespace LiteNetLib
{
internal readonly struct NativeAddr : IEquatable<NativeAddr>
{
//common parts
private readonly long _part1; //family, port, etc
private readonly long _part2;
//ipv6 parts
private readonly long _part3;
private readonly int _part4;

private readonly int _hash;

public NativeAddr(byte[] address, int len)
{
_part1 = BitConverter.ToInt64(address, 0);
_part2 = BitConverter.ToInt64(address, 8);
if (len > 16)
{
_part3 = BitConverter.ToInt64(address, 16);
_part4 = BitConverter.ToInt32(address, 24);
}
else
{
_part3 = 0;
_part4 = 0;
}
_hash = (int)(_part1 >> 32) ^ (int)_part1 ^
(int)(_part2 >> 32) ^ (int)_part2 ^
(int)(_part3 >> 32) ^ (int)_part3 ^
_part4;
}

public override int GetHashCode()
{
return _hash;
}

public bool Equals(NativeAddr other)
{
return _part1 == other._part1 &&
_part2 == other._part2 &&
_part3 == other._part3 &&
_part4 == other._part4;
}

public override bool Equals(object obj)
{
return obj is NativeAddr other && Equals(other);
}

public static bool operator ==(NativeAddr left, NativeAddr right)
{
return left.Equals(right);
}

public static bool operator !=(NativeAddr left, NativeAddr right)
{
return !left.Equals(right);
}
}

internal class NativeEndPoint : IPEndPoint
{
public readonly byte[] NativeAddress;

public NativeEndPoint(byte[] address) : base(IPAddress.Any, 0)
{
NativeAddress = new byte[address.Length];
Buffer.BlockCopy(address, 0, NativeAddress, 0, address.Length);

short family = (short)((address[1] << 8) | address[0]);
Port =(ushort)((address[2] << 8) | address[3]);

if ((NativeSocket.UnixMode && family == NativeSocket.AF_INET6) || (!NativeSocket.UnixMode && (AddressFamily)family == AddressFamily.InterNetworkV6))
{
uint scope = unchecked((uint)(
(address[27] << 24) +
(address[26] << 16) +
(address[25] << 8) +
(address[24])));
#if NETCOREAPP || NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER
Address = new IPAddress(new ReadOnlySpan<byte>(address, 8, 16), scope);
#else
byte[] addrBuffer = new byte[16];
Buffer.BlockCopy(address, 8, addrBuffer, 0, 16);
Address = new IPAddress(addrBuffer, scope);
#endif
}
else //IPv4
{
long ipv4Addr = unchecked((uint)((address[4] & 0x000000FF) |
(address[5] << 8 & 0x0000FF00) |
(address[6] << 16 & 0x00FF0000) |
(address[7] << 24)));
Address = new IPAddress(ipv4Addr);
}
}
}

internal static class NativeSocket
{
static
#if LITENETLIB_UNSAFE
unsafe
#endif
class WinSock
static unsafe class WinSock
{
private const string LibName = "ws2_32.dll";

Expand All @@ -127,22 +25,14 @@ public static extern int recvfrom(
[DllImport(LibName, SetLastError = true)]
internal static extern int sendto(
IntPtr socketHandle,
#if LITENETLIB_UNSAFE
byte* pinnedBuffer,
#else
[In] byte[] pinnedBuffer,
#endif
[In] int len,
[In] SocketFlags socketFlags,
[In] byte[] socketAddress,
[In] int socketAddressSize);
}

static
#if LITENETLIB_UNSAFE
unsafe
#endif
class UnixSock
static unsafe class UnixSock
{
private const string LibName = "libc";

Expand All @@ -158,11 +48,7 @@ public static extern int recvfrom(
[DllImport(LibName, SetLastError = true)]
internal static extern int sendto(
IntPtr socketHandle,
#if LITENETLIB_UNSAFE
byte* pinnedBuffer,
#else
[In] byte[] pinnedBuffer,
#endif
[In] int len,
[In] SocketFlags socketFlags,
[In] byte[] socketAddress,
Expand Down Expand Up @@ -250,17 +136,9 @@ public static int RecvFrom(
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public
#if LITENETLIB_UNSAFE
unsafe
#endif
static int SendTo(
public static unsafe int SendTo(
IntPtr socketHandle,
#if LITENETLIB_UNSAFE
byte* pinnedBuffer,
#else
byte[] pinnedBuffer,
#endif
int len,
byte[] socketAddress,
int socketAddressSize)
Expand Down
Loading

0 comments on commit 84a76b1

Please sign in to comment.