Skip to content
This repository has been archived by the owner on Jan 29, 2021. It is now read-only.

Updated to LiteNetLib 0.9.2.2 and Mirror 16.9.0 #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 MichalPetryka
Copyright (c) 2019 - 2020 MichalPetryka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,49 @@ internal enum ConnectionRequestResult
{
None,
Accept,
Reject
}

internal interface IConnectionRequestListener
{
void OnConnectionSolved(ConnectionRequest request, byte[] rejectData, int start, int length);
Reject,
RejectForce
}

public class ConnectionRequest
{
private readonly IConnectionRequestListener _listener;
private readonly NetManager _listener;
private int _used;

public IPEndPoint RemoteEndPoint { get { return Peer.EndPoint; } }
public readonly NetDataReader Data;
public ConnectionRequestType Type { get; private set; }

internal ConnectionRequestResult Result { get; private set; }
internal readonly long ConnectionId;
internal readonly byte ConnectionNumber;
internal readonly NetPeer Peer;
internal long ConnectionTime;
internal byte ConnectionNumber;
public readonly IPEndPoint RemoteEndPoint;

private bool TryActivate()
{
return Interlocked.CompareExchange(ref _used, 1, 0) == 0;
}

internal void UpdateRequest(NetConnectRequestPacket connRequest)
{
if (connRequest.ConnectionTime >= ConnectionTime)
{
ConnectionTime = connRequest.ConnectionTime;
ConnectionNumber = connRequest.ConnectionNumber;
}
}

internal ConnectionRequest(
long connectionId,
byte connectionNumber,
ConnectionRequestType type,
NetDataReader netDataReader,
NetPeer peer,
IConnectionRequestListener listener)
IPEndPoint endPoint,
NetManager listener)
{
ConnectionId = connectionId;
ConnectionTime = connectionId;
ConnectionNumber = connectionNumber;
Type = type;
Peer = peer;
RemoteEndPoint = endPoint;
Data = netDataReader;
_listener = listener;
}
Expand All @@ -64,16 +68,15 @@ public NetPeer AcceptIfKey(string key)
try
{
if (Data.GetString() == key)
{
Result = ConnectionRequestResult.Accept;
_listener.OnConnectionSolved(this, null, 0, 0);
return Peer;
}
}
catch
{
NetDebug.WriteError("[AC] Invalid incoming data");
}
if (Result == ConnectionRequestResult.Accept)
return _listener.OnConnectionSolved(this, null, 0, 0);

Result = ConnectionRequestResult.Reject;
_listener.OnConnectionSolved(this, null, 0, 0);
return null;
Expand All @@ -88,31 +91,56 @@ public NetPeer Accept()
if (!TryActivate())
return null;
Result = ConnectionRequestResult.Accept;
_listener.OnConnectionSolved(this, null, 0, 0);
return Peer;
return _listener.OnConnectionSolved(this, null, 0, 0);
}

public void Reject(byte[] rejectData, int start, int length)
public void Reject(byte[] rejectData, int start, int length, bool force)
{
if (!TryActivate())
return;
Result = ConnectionRequestResult.Reject;
Result = force ? ConnectionRequestResult.RejectForce : ConnectionRequestResult.Reject;
_listener.OnConnectionSolved(this, rejectData, start, length);
}

public void Reject(byte[] rejectData, int start, int length)
{
Reject(rejectData, start, length, false);
}


public void RejectForce(byte[] rejectData, int start, int length)
{
Reject(rejectData, start, length, true);
}

public void RejectForce()
{
Reject(null, 0, 0, true);
}

public void RejectForce(byte[] rejectData)
{
Reject(rejectData, 0, rejectData.Length, true);
}

public void RejectForce(NetDataWriter rejectData)
{
Reject(rejectData.Data, 0, rejectData.Length, true);
}

public void Reject()
{
Reject(null, 0, 0);
Reject(null, 0, 0, false);
}

public void Reject(byte[] rejectData)
{
Reject(rejectData, 0, rejectData.Length);
Reject(rejectData, 0, rejectData.Length, false);
}

public void Reject(NetDataWriter rejectData)
{
Reject(rejectData.Data, 0, rejectData.Length);
Reject(rejectData.Data, 0, rejectData.Length, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum DisconnectReason
DisconnectPeerCalled,
ConnectionRejected,
InvalidProtocol,
UnknownHost
UnknownHost,
Reconnect
}

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using LiteNetLib.Utils;
using System;

namespace LiteNetLib.Layers
{
public sealed class Crc32cLayer : PacketLayerBase
{
public Crc32cLayer() : base(CRC32C.ChecksumSize)
{

}

public override void ProcessInboundPacket(ref byte[] data, ref int length)
{
if (length < NetConstants.HeaderSize + CRC32C.ChecksumSize)
{
NetDebug.WriteError("[NM] DataReceived size: bad!");
return;
}

int checksumPoint = length - CRC32C.ChecksumSize;
if (CRC32C.Compute(data, 0, checksumPoint) != BitConverter.ToUInt32(data, checksumPoint))
{
NetDebug.Write("[NM] DataReceived checksum: bad!");
return;
}
length -= CRC32C.ChecksumSize;
}

public override void ProcessOutBoundPacket(ref byte[] data, ref int offset, ref int length)
{
FastBitConverter.GetBytes(data, length, CRC32C.Compute(data, offset, length));
length += CRC32C.ChecksumSize;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace LiteNetLib.Layers
{
public abstract class PacketLayerBase
{
public readonly int ExtraPacketSizeForLayer;

protected PacketLayerBase(int extraPacketSizeForLayer)
{
ExtraPacketSizeForLayer = extraPacketSizeForLayer;
}

public abstract void ProcessInboundPacket(ref byte[] data, ref int length);
public abstract void ProcessOutBoundPacket(ref byte[] data, ref int offset, ref int length);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Text;

namespace LiteNetLib.Layers
{
public class XorEncryptLayer : PacketLayerBase
{
private byte[] _byteKey;

public XorEncryptLayer() : base(0)
{

}

public XorEncryptLayer(byte[] key) : this()
{
SetKey(key);
}

public XorEncryptLayer(string key) : this()
{
SetKey(key);
}

public void SetKey(string key)
{
_byteKey = Encoding.UTF8.GetBytes(key);
}

public void SetKey(byte[] key)
{
if(_byteKey.Length != key.Length)
_byteKey = new byte[key.Length];
Buffer.BlockCopy(key, 0, _byteKey, 0, key.Length);
}

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

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static class NetConstants
public const int HeaderSize = 1;
public const int ChanneledHeaderSize = 4;
public const int FragmentHeaderSize = 6;
public const int FragmentTotalSize = ChanneledHeaderSize + FragmentHeaderSize;
public const int FragmentedHeaderTotalSize = ChanneledHeaderSize + FragmentHeaderSize;
public const ushort MaxSequence = 32768;
public const ushort HalfMaxSequence = MaxSequence / 2;

Expand Down
Loading