mirror of
https://github.com/Kaveinator/NetickProForUnity.git
synced 2025-10-26 09:49:06 -07:00
auto
This commit is contained in:
13
Transport/LiteNetLib Transport/LiteNetLib.asmdef
Normal file
13
Transport/LiteNetLib Transport/LiteNetLib.asmdef
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "LiteNetLib",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
7
Transport/LiteNetLib Transport/LiteNetLib.asmdef.meta
Normal file
7
Transport/LiteNetLib Transport/LiteNetLib.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166c61f9e22d9df41a00ea20161d3d57
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Transport/LiteNetLib Transport/LiteNetLib.meta
Normal file
8
Transport/LiteNetLib Transport/LiteNetLib.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93d0e374be380054baa67bb06948b893
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
48
Transport/LiteNetLib Transport/LiteNetLib/BaseChannel.cs
Normal file
48
Transport/LiteNetLib Transport/LiteNetLib/BaseChannel.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal abstract class BaseChannel
|
||||
{
|
||||
protected readonly NetPeer Peer;
|
||||
protected readonly Queue<NetPacket> OutgoingQueue = new Queue<NetPacket>(NetConstants.DefaultWindowSize);
|
||||
private int _isAddedToPeerChannelSendQueue;
|
||||
|
||||
public int PacketsInQueue => OutgoingQueue.Count;
|
||||
|
||||
protected BaseChannel(NetPeer peer)
|
||||
{
|
||||
Peer = peer;
|
||||
}
|
||||
|
||||
public void AddToQueue(NetPacket packet)
|
||||
{
|
||||
lock (OutgoingQueue)
|
||||
{
|
||||
OutgoingQueue.Enqueue(packet);
|
||||
}
|
||||
AddToPeerChannelSendQueue();
|
||||
}
|
||||
|
||||
protected void AddToPeerChannelSendQueue()
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _isAddedToPeerChannelSendQueue, 1, 0) == 0)
|
||||
{
|
||||
Peer.AddToReliableChannelSendQueue(this);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SendAndCheckQueue()
|
||||
{
|
||||
bool hasPacketsToSend = SendNextPackets();
|
||||
if (!hasPacketsToSend)
|
||||
Interlocked.Exchange(ref _isAddedToPeerChannelSendQueue, 0);
|
||||
|
||||
return hasPacketsToSend;
|
||||
}
|
||||
|
||||
protected abstract bool SendNextPackets();
|
||||
public abstract bool ProcessPacket(NetPacket packet);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0eff1fca12b255a4897973f25ddfe588
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
134
Transport/LiteNetLib Transport/LiteNetLib/ConnectionRequest.cs
Normal file
134
Transport/LiteNetLib Transport/LiteNetLib/ConnectionRequest.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal enum ConnectionRequestResult
|
||||
{
|
||||
None,
|
||||
Accept,
|
||||
Reject,
|
||||
RejectForce
|
||||
}
|
||||
|
||||
public class ConnectionRequest
|
||||
{
|
||||
private readonly NetManager _listener;
|
||||
private int _used;
|
||||
|
||||
public NetDataReader Data => InternalPacket.Data;
|
||||
|
||||
internal ConnectionRequestResult Result { get; private set; }
|
||||
internal NetConnectRequestPacket InternalPacket;
|
||||
|
||||
public readonly IPEndPoint RemoteEndPoint;
|
||||
|
||||
internal void UpdateRequest(NetConnectRequestPacket connectRequest)
|
||||
{
|
||||
//old request
|
||||
if (connectRequest.ConnectionTime < InternalPacket.ConnectionTime)
|
||||
return;
|
||||
|
||||
if (connectRequest.ConnectionTime == InternalPacket.ConnectionTime &&
|
||||
connectRequest.ConnectionNumber == InternalPacket.ConnectionNumber)
|
||||
return;
|
||||
|
||||
InternalPacket = connectRequest;
|
||||
}
|
||||
|
||||
private bool TryActivate()
|
||||
{
|
||||
return Interlocked.CompareExchange(ref _used, 1, 0) == 0;
|
||||
}
|
||||
|
||||
internal ConnectionRequest(IPEndPoint remoteEndPoint, NetConnectRequestPacket requestPacket, NetManager listener)
|
||||
{
|
||||
InternalPacket = requestPacket;
|
||||
RemoteEndPoint = remoteEndPoint;
|
||||
_listener = listener;
|
||||
}
|
||||
|
||||
public NetPeer AcceptIfKey(string key)
|
||||
{
|
||||
if (!TryActivate())
|
||||
return null;
|
||||
try
|
||||
{
|
||||
if (Data.GetString() == key)
|
||||
Result = ConnectionRequestResult.Accept;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Accept connection and get new NetPeer as result
|
||||
/// </summary>
|
||||
/// <returns>Connected NetPeer</returns>
|
||||
public NetPeer Accept()
|
||||
{
|
||||
if (!TryActivate())
|
||||
return null;
|
||||
Result = ConnectionRequestResult.Accept;
|
||||
return _listener.OnConnectionSolved(this, null, 0, 0);
|
||||
}
|
||||
|
||||
public void Reject(byte[] rejectData, int start, int length, bool force)
|
||||
{
|
||||
if (!TryActivate())
|
||||
return;
|
||||
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, false);
|
||||
}
|
||||
|
||||
public void Reject(byte[] rejectData)
|
||||
{
|
||||
Reject(rejectData, 0, rejectData.Length, false);
|
||||
}
|
||||
|
||||
public void Reject(NetDataWriter rejectData)
|
||||
{
|
||||
Reject(rejectData.Data, 0, rejectData.Length, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c8834fc3ea90184badbfaecd769db0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
272
Transport/LiteNetLib Transport/LiteNetLib/INetEventListener.cs
Normal file
272
Transport/LiteNetLib Transport/LiteNetLib/INetEventListener.cs
Normal file
@@ -0,0 +1,272 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Type of message that you receive in OnNetworkReceiveUnconnected event
|
||||
/// </summary>
|
||||
public enum UnconnectedMessageType
|
||||
{
|
||||
BasicMessage,
|
||||
Broadcast
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect reason that you receive in OnPeerDisconnected event
|
||||
/// </summary>
|
||||
public enum DisconnectReason
|
||||
{
|
||||
ConnectionFailed,
|
||||
Timeout,
|
||||
HostUnreachable,
|
||||
NetworkUnreachable,
|
||||
RemoteConnectionClose,
|
||||
DisconnectPeerCalled,
|
||||
ConnectionRejected,
|
||||
InvalidProtocol,
|
||||
UnknownHost,
|
||||
Reconnect,
|
||||
PeerToPeerConnection,
|
||||
PeerNotFound
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Additional information about disconnection
|
||||
/// </summary>
|
||||
public struct DisconnectInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Additional info why peer disconnected
|
||||
/// </summary>
|
||||
public DisconnectReason Reason;
|
||||
|
||||
/// <summary>
|
||||
/// Error code (if reason is SocketSendError or SocketReceiveError)
|
||||
/// </summary>
|
||||
public SocketError SocketErrorCode;
|
||||
|
||||
/// <summary>
|
||||
/// Additional data that can be accessed (only if reason is RemoteConnectionClose)
|
||||
/// </summary>
|
||||
public NetPacketReader AdditionalData;
|
||||
}
|
||||
|
||||
public interface INetEventListener
|
||||
{
|
||||
/// <summary>
|
||||
/// New remote peer connected to host, or client connected to remote host
|
||||
/// </summary>
|
||||
/// <param name="peer">Connected peer object</param>
|
||||
void OnPeerConnected(NetPeer peer);
|
||||
|
||||
/// <summary>
|
||||
/// Peer disconnected
|
||||
/// </summary>
|
||||
/// <param name="peer">disconnected peer</param>
|
||||
/// <param name="disconnectInfo">additional info about reason, errorCode or data received with disconnect message</param>
|
||||
void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo);
|
||||
|
||||
/// <summary>
|
||||
/// Network error (on send or receive)
|
||||
/// </summary>
|
||||
/// <param name="endPoint">From endPoint (can be null)</param>
|
||||
/// <param name="socketError">Socket error</param>
|
||||
void OnNetworkError(IPEndPoint endPoint, SocketError socketError);
|
||||
|
||||
/// <summary>
|
||||
/// Received some data
|
||||
/// </summary>
|
||||
/// <param name="peer">From peer</param>
|
||||
/// <param name="reader">DataReader containing all received data</param>
|
||||
/// <param name="channelNumber">Number of channel at which packet arrived</param>
|
||||
/// <param name="deliveryMethod">Type of received packet</param>
|
||||
void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Received unconnected message
|
||||
/// </summary>
|
||||
/// <param name="remoteEndPoint">From address (IP and Port)</param>
|
||||
/// <param name="reader">Message data</param>
|
||||
/// <param name="messageType">Message type (simple, discovery request or response)</param>
|
||||
void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType);
|
||||
|
||||
/// <summary>
|
||||
/// Latency information updated
|
||||
/// </summary>
|
||||
/// <param name="peer">Peer with updated latency</param>
|
||||
/// <param name="latency">latency value in milliseconds</param>
|
||||
void OnNetworkLatencyUpdate(NetPeer peer, int latency);
|
||||
|
||||
/// <summary>
|
||||
/// On peer connection requested
|
||||
/// </summary>
|
||||
/// <param name="request">Request information (EndPoint, internal id, additional data)</param>
|
||||
void OnConnectionRequest(ConnectionRequest request);
|
||||
}
|
||||
|
||||
public interface IDeliveryEventListener
|
||||
{
|
||||
/// <summary>
|
||||
/// On reliable message delivered
|
||||
/// </summary>
|
||||
/// <param name="peer"></param>
|
||||
/// <param name="userData"></param>
|
||||
void OnMessageDelivered(NetPeer peer, object userData);
|
||||
}
|
||||
|
||||
public interface INtpEventListener
|
||||
{
|
||||
/// <summary>
|
||||
/// Ntp response
|
||||
/// </summary>
|
||||
/// <param name="packet"></param>
|
||||
void OnNtpResponse(NtpPacket packet);
|
||||
}
|
||||
|
||||
public interface IPeerAddressChangedListener
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when peer address changed (when AllowPeerAddressChange is enabled)
|
||||
/// </summary>
|
||||
/// <param name="peer">Peer that changed address (with new address)</param>
|
||||
/// <param name="previousAddress">previous IP</param>
|
||||
void OnPeerAddressChanged(NetPeer peer, IPEndPoint previousAddress);
|
||||
}
|
||||
|
||||
public class EventBasedNetListener : INetEventListener, IDeliveryEventListener, INtpEventListener, IPeerAddressChangedListener
|
||||
{
|
||||
public delegate void OnPeerConnected(NetPeer peer);
|
||||
public delegate void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo);
|
||||
public delegate void OnNetworkError(IPEndPoint endPoint, SocketError socketError);
|
||||
public delegate void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod);
|
||||
public delegate void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType);
|
||||
public delegate void OnNetworkLatencyUpdate(NetPeer peer, int latency);
|
||||
public delegate void OnConnectionRequest(ConnectionRequest request);
|
||||
public delegate void OnDeliveryEvent(NetPeer peer, object userData);
|
||||
public delegate void OnNtpResponseEvent(NtpPacket packet);
|
||||
public delegate void OnPeerAddressChangedEvent(NetPeer peer, IPEndPoint previousAddress);
|
||||
|
||||
public event OnPeerConnected PeerConnectedEvent;
|
||||
public event OnPeerDisconnected PeerDisconnectedEvent;
|
||||
public event OnNetworkError NetworkErrorEvent;
|
||||
public event OnNetworkReceive NetworkReceiveEvent;
|
||||
public event OnNetworkReceiveUnconnected NetworkReceiveUnconnectedEvent;
|
||||
public event OnNetworkLatencyUpdate NetworkLatencyUpdateEvent;
|
||||
public event OnConnectionRequest ConnectionRequestEvent;
|
||||
public event OnDeliveryEvent DeliveryEvent;
|
||||
public event OnNtpResponseEvent NtpResponseEvent;
|
||||
public event OnPeerAddressChangedEvent PeerAddressChangedEvent;
|
||||
|
||||
public void ClearPeerConnectedEvent()
|
||||
{
|
||||
PeerConnectedEvent = null;
|
||||
}
|
||||
|
||||
public void ClearPeerDisconnectedEvent()
|
||||
{
|
||||
PeerDisconnectedEvent = null;
|
||||
}
|
||||
|
||||
public void ClearNetworkErrorEvent()
|
||||
{
|
||||
NetworkErrorEvent = null;
|
||||
}
|
||||
|
||||
public void ClearNetworkReceiveEvent()
|
||||
{
|
||||
NetworkReceiveEvent = null;
|
||||
}
|
||||
|
||||
public void ClearNetworkReceiveUnconnectedEvent()
|
||||
{
|
||||
NetworkReceiveUnconnectedEvent = null;
|
||||
}
|
||||
|
||||
public void ClearNetworkLatencyUpdateEvent()
|
||||
{
|
||||
NetworkLatencyUpdateEvent = null;
|
||||
}
|
||||
|
||||
public void ClearConnectionRequestEvent()
|
||||
{
|
||||
ConnectionRequestEvent = null;
|
||||
}
|
||||
|
||||
public void ClearDeliveryEvent()
|
||||
{
|
||||
DeliveryEvent = null;
|
||||
}
|
||||
|
||||
public void ClearNtpResponseEvent()
|
||||
{
|
||||
NtpResponseEvent = null;
|
||||
}
|
||||
|
||||
public void ClearPeerAddressChangedEvent()
|
||||
{
|
||||
PeerAddressChangedEvent = null;
|
||||
}
|
||||
|
||||
void INetEventListener.OnPeerConnected(NetPeer peer)
|
||||
{
|
||||
if (PeerConnectedEvent != null)
|
||||
PeerConnectedEvent(peer);
|
||||
}
|
||||
|
||||
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
|
||||
{
|
||||
if (PeerDisconnectedEvent != null)
|
||||
PeerDisconnectedEvent(peer, disconnectInfo);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode)
|
||||
{
|
||||
if (NetworkErrorEvent != null)
|
||||
NetworkErrorEvent(endPoint, socketErrorCode);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod)
|
||||
{
|
||||
if (NetworkReceiveEvent != null)
|
||||
NetworkReceiveEvent(peer, reader, channelNumber, deliveryMethod);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
|
||||
{
|
||||
if (NetworkReceiveUnconnectedEvent != null)
|
||||
NetworkReceiveUnconnectedEvent(remoteEndPoint, reader, messageType);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency)
|
||||
{
|
||||
if (NetworkLatencyUpdateEvent != null)
|
||||
NetworkLatencyUpdateEvent(peer, latency);
|
||||
}
|
||||
|
||||
void INetEventListener.OnConnectionRequest(ConnectionRequest request)
|
||||
{
|
||||
if (ConnectionRequestEvent != null)
|
||||
ConnectionRequestEvent(request);
|
||||
}
|
||||
|
||||
void IDeliveryEventListener.OnMessageDelivered(NetPeer peer, object userData)
|
||||
{
|
||||
if (DeliveryEvent != null)
|
||||
DeliveryEvent(peer, userData);
|
||||
}
|
||||
|
||||
void INtpEventListener.OnNtpResponse(NtpPacket packet)
|
||||
{
|
||||
if (NtpResponseEvent != null)
|
||||
NtpResponseEvent(packet);
|
||||
}
|
||||
|
||||
void IPeerAddressChangedListener.OnPeerAddressChanged(NetPeer peer, IPEndPoint previousAddress)
|
||||
{
|
||||
if (PeerAddressChangedEvent != null)
|
||||
PeerAddressChangedEvent(peer, previousAddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35d960e405f680e4b8289d6a6b836799
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
133
Transport/LiteNetLib Transport/LiteNetLib/InternalPackets.cs
Normal file
133
Transport/LiteNetLib Transport/LiteNetLib/InternalPackets.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal sealed class NetConnectRequestPacket
|
||||
{
|
||||
public const int HeaderSize = 18;
|
||||
public readonly long ConnectionTime;
|
||||
public byte ConnectionNumber;
|
||||
public readonly byte[] TargetAddress;
|
||||
public readonly NetDataReader Data;
|
||||
public readonly int PeerId;
|
||||
|
||||
private NetConnectRequestPacket(long connectionTime, byte connectionNumber, int localId, byte[] targetAddress, NetDataReader data)
|
||||
{
|
||||
ConnectionTime = connectionTime;
|
||||
ConnectionNumber = connectionNumber;
|
||||
TargetAddress = targetAddress;
|
||||
Data = data;
|
||||
PeerId = localId;
|
||||
}
|
||||
|
||||
public static int GetProtocolId(NetPacket packet)
|
||||
{
|
||||
return BitConverter.ToInt32(packet.RawData, 1);
|
||||
}
|
||||
|
||||
public static NetConnectRequestPacket FromData(NetPacket packet)
|
||||
{
|
||||
if (packet.ConnectionNumber >= NetConstants.MaxConnectionNumber)
|
||||
return null;
|
||||
|
||||
//Getting connection time for peer
|
||||
long connectionTime = BitConverter.ToInt64(packet.RawData, 5);
|
||||
|
||||
//Get peer id
|
||||
int peerId = BitConverter.ToInt32(packet.RawData, 13);
|
||||
|
||||
//Get target address
|
||||
int addrSize = packet.RawData[HeaderSize-1];
|
||||
if (addrSize != 16 && addrSize != 28)
|
||||
return null;
|
||||
byte[] addressBytes = new byte[addrSize];
|
||||
Buffer.BlockCopy(packet.RawData, HeaderSize, addressBytes, 0, addrSize);
|
||||
|
||||
// Read data and create request
|
||||
var reader = new NetDataReader(null, 0, 0);
|
||||
if (packet.Size > HeaderSize+addrSize)
|
||||
reader.SetSource(packet.RawData, HeaderSize + addrSize, packet.Size);
|
||||
|
||||
return new NetConnectRequestPacket(connectionTime, packet.ConnectionNumber, peerId, addressBytes, reader);
|
||||
}
|
||||
|
||||
public static NetPacket Make(NetDataWriter connectData, SocketAddress addressBytes, long connectTime, int localId)
|
||||
{
|
||||
//Make initial packet
|
||||
var packet = new NetPacket(PacketProperty.ConnectRequest, connectData.Length+addressBytes.Size);
|
||||
|
||||
//Add data
|
||||
FastBitConverter.GetBytes(packet.RawData, 1, NetConstants.ProtocolId);
|
||||
FastBitConverter.GetBytes(packet.RawData, 5, connectTime);
|
||||
FastBitConverter.GetBytes(packet.RawData, 13, localId);
|
||||
packet.RawData[HeaderSize-1] = (byte)addressBytes.Size;
|
||||
for (int i = 0; i < addressBytes.Size; i++)
|
||||
packet.RawData[HeaderSize + i] = addressBytes[i];
|
||||
Buffer.BlockCopy(connectData.Data, 0, packet.RawData, HeaderSize + addressBytes.Size, connectData.Length);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class NetConnectAcceptPacket
|
||||
{
|
||||
public const int Size = 15;
|
||||
public readonly long ConnectionTime;
|
||||
public readonly byte ConnectionNumber;
|
||||
public readonly int PeerId;
|
||||
public readonly bool PeerNetworkChanged;
|
||||
|
||||
private NetConnectAcceptPacket(long connectionTime, byte connectionNumber, int peerId, bool peerNetworkChanged)
|
||||
{
|
||||
ConnectionTime = connectionTime;
|
||||
ConnectionNumber = connectionNumber;
|
||||
PeerId = peerId;
|
||||
PeerNetworkChanged = peerNetworkChanged;
|
||||
}
|
||||
|
||||
public static NetConnectAcceptPacket FromData(NetPacket packet)
|
||||
{
|
||||
if (packet.Size != Size)
|
||||
return null;
|
||||
|
||||
long connectionId = BitConverter.ToInt64(packet.RawData, 1);
|
||||
|
||||
//check connect num
|
||||
byte connectionNumber = packet.RawData[9];
|
||||
if (connectionNumber >= NetConstants.MaxConnectionNumber)
|
||||
return null;
|
||||
|
||||
//check reused flag
|
||||
byte isReused = packet.RawData[10];
|
||||
if (isReused > 1)
|
||||
return null;
|
||||
|
||||
//get remote peer id
|
||||
int peerId = BitConverter.ToInt32(packet.RawData, 11);
|
||||
if (peerId < 0)
|
||||
return null;
|
||||
|
||||
return new NetConnectAcceptPacket(connectionId, connectionNumber, peerId, isReused == 1);
|
||||
}
|
||||
|
||||
public static NetPacket Make(long connectTime, byte connectNum, int localPeerId)
|
||||
{
|
||||
var packet = new NetPacket(PacketProperty.ConnectAccept, 0);
|
||||
FastBitConverter.GetBytes(packet.RawData, 1, connectTime);
|
||||
packet.RawData[9] = connectNum;
|
||||
FastBitConverter.GetBytes(packet.RawData, 11, localPeerId);
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static NetPacket MakeNetworkChanged(NetPeer peer)
|
||||
{
|
||||
var packet = new NetPacket(PacketProperty.PeerNotFound, Size-1);
|
||||
FastBitConverter.GetBytes(packet.RawData, 1, peer.ConnectTime);
|
||||
packet.RawData[9] = peer.ConnectionNum;
|
||||
packet.RawData[10] = 1;
|
||||
FastBitConverter.GetBytes(packet.RawData, 11, peer.RemoteId);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0e98393e9ec208478ca501f1ac9cce3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Transport/LiteNetLib Transport/LiteNetLib/Layers.meta
Normal file
8
Transport/LiteNetLib Transport/LiteNetLib/Layers.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09eca1ec4a1ba4b4bb571718bd1a1519
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using LiteNetLib.Utils;
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace LiteNetLib.Layers
|
||||
{
|
||||
public sealed class Crc32cLayer : PacketLayerBase
|
||||
{
|
||||
public Crc32cLayer() : base(CRC32C.ChecksumSize)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
|
||||
{
|
||||
if (length < NetConstants.HeaderSize + CRC32C.ChecksumSize)
|
||||
{
|
||||
NetDebug.WriteError("[NM] DataReceived size: bad!");
|
||||
//Set length to 0 to have netManager drop the packet.
|
||||
length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
int checksumPoint = length - CRC32C.ChecksumSize;
|
||||
if (CRC32C.Compute(data, offset, checksumPoint) != BitConverter.ToUInt32(data, checksumPoint))
|
||||
{
|
||||
NetDebug.Write("[NM] DataReceived checksum: bad!");
|
||||
//Set length to 0 to have netManager drop the packet.
|
||||
length = 0;
|
||||
return;
|
||||
}
|
||||
length -= CRC32C.ChecksumSize;
|
||||
}
|
||||
|
||||
public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
|
||||
{
|
||||
FastBitConverter.GetBytes(data, length, CRC32C.Compute(data, offset, length));
|
||||
length += CRC32C.ChecksumSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6e636fb040ae9f4298fce9d2a0e208f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Net;
|
||||
|
||||
namespace LiteNetLib.Layers
|
||||
{
|
||||
public abstract class PacketLayerBase
|
||||
{
|
||||
public readonly int ExtraPacketSizeForLayer;
|
||||
|
||||
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 ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2bb25f5fc9603f14497f9bdd87162125
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
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 == null || _byteKey.Length != key.Length)
|
||||
_byteKey = new byte[key.Length];
|
||||
Buffer.BlockCopy(key, 0, _byteKey, 0, key.Length);
|
||||
}
|
||||
|
||||
public override void ProcessInboundPacket(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++)
|
||||
{
|
||||
data[cur] = (byte)(data[cur] ^ _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++)
|
||||
{
|
||||
data[cur] = (byte)(data[cur] ^ _byteKey[i % _byteKey.Length]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25402be4bd2613142815528b859dc57c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
258
Transport/LiteNetLib Transport/LiteNetLib/NatPunchModule.cs
Normal file
258
Transport/LiteNetLib Transport/LiteNetLib/NatPunchModule.cs
Normal file
@@ -0,0 +1,258 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public enum NatAddressType
|
||||
{
|
||||
Internal,
|
||||
External
|
||||
}
|
||||
|
||||
public interface INatPunchListener
|
||||
{
|
||||
void OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token);
|
||||
void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token);
|
||||
}
|
||||
|
||||
public class EventBasedNatPunchListener : INatPunchListener
|
||||
{
|
||||
public delegate void OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token);
|
||||
public delegate void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token);
|
||||
|
||||
public event OnNatIntroductionRequest NatIntroductionRequest;
|
||||
public event OnNatIntroductionSuccess NatIntroductionSuccess;
|
||||
|
||||
void INatPunchListener.OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token)
|
||||
{
|
||||
if(NatIntroductionRequest != null)
|
||||
NatIntroductionRequest(localEndPoint, remoteEndPoint, token);
|
||||
}
|
||||
|
||||
void INatPunchListener.OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token)
|
||||
{
|
||||
if (NatIntroductionSuccess != null)
|
||||
NatIntroductionSuccess(targetEndPoint, type, token);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Module for UDP NAT Hole punching operations. Can be accessed from NetManager
|
||||
/// </summary>
|
||||
public sealed class NatPunchModule
|
||||
{
|
||||
struct RequestEventData
|
||||
{
|
||||
public IPEndPoint LocalEndPoint;
|
||||
public IPEndPoint RemoteEndPoint;
|
||||
public string Token;
|
||||
}
|
||||
|
||||
struct SuccessEventData
|
||||
{
|
||||
public IPEndPoint TargetEndPoint;
|
||||
public NatAddressType Type;
|
||||
public string Token;
|
||||
}
|
||||
|
||||
class NatIntroduceRequestPacket
|
||||
{
|
||||
public IPEndPoint Internal { get; set; }
|
||||
public string Token { get; set; }
|
||||
}
|
||||
|
||||
class NatIntroduceResponsePacket
|
||||
{
|
||||
public IPEndPoint Internal { get; set; }
|
||||
public IPEndPoint External { get; set; }
|
||||
public string Token { get; set; }
|
||||
}
|
||||
|
||||
class NatPunchPacket
|
||||
{
|
||||
public string Token { get; set; }
|
||||
public bool IsExternal { get; set; }
|
||||
}
|
||||
|
||||
private readonly NetManager _socket;
|
||||
private readonly ConcurrentQueue<RequestEventData> _requestEvents = new ConcurrentQueue<RequestEventData>();
|
||||
private readonly ConcurrentQueue<SuccessEventData> _successEvents = new ConcurrentQueue<SuccessEventData>();
|
||||
private readonly NetDataReader _cacheReader = new NetDataReader();
|
||||
private readonly NetDataWriter _cacheWriter = new NetDataWriter();
|
||||
private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor(MaxTokenLength);
|
||||
private INatPunchListener _natPunchListener;
|
||||
public const int MaxTokenLength = 256;
|
||||
|
||||
/// <summary>
|
||||
/// Events automatically will be called without PollEvents method from another thread
|
||||
/// </summary>
|
||||
public bool UnsyncedEvents = false;
|
||||
|
||||
internal NatPunchModule(NetManager socket)
|
||||
{
|
||||
_socket = socket;
|
||||
_netPacketProcessor.SubscribeReusable<NatIntroduceResponsePacket>(OnNatIntroductionResponse);
|
||||
_netPacketProcessor.SubscribeReusable<NatIntroduceRequestPacket, IPEndPoint>(OnNatIntroductionRequest);
|
||||
_netPacketProcessor.SubscribeReusable<NatPunchPacket, IPEndPoint>(OnNatPunch);
|
||||
}
|
||||
|
||||
internal void ProcessMessage(IPEndPoint senderEndPoint, NetPacket packet)
|
||||
{
|
||||
lock (_cacheReader)
|
||||
{
|
||||
_cacheReader.SetSource(packet.RawData, NetConstants.HeaderSize, packet.Size);
|
||||
_netPacketProcessor.ReadAllPackets(_cacheReader, senderEndPoint);
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(INatPunchListener listener)
|
||||
{
|
||||
_natPunchListener = listener;
|
||||
}
|
||||
|
||||
private void Send<T>(T packet, IPEndPoint target) where T : class, new()
|
||||
{
|
||||
_cacheWriter.Reset();
|
||||
_cacheWriter.Put((byte)PacketProperty.NatMessage);
|
||||
_netPacketProcessor.Write(_cacheWriter, packet);
|
||||
_socket.SendRaw(_cacheWriter.Data, 0, _cacheWriter.Length, target);
|
||||
}
|
||||
|
||||
public void NatIntroduce(
|
||||
IPEndPoint hostInternal,
|
||||
IPEndPoint hostExternal,
|
||||
IPEndPoint clientInternal,
|
||||
IPEndPoint clientExternal,
|
||||
string additionalInfo)
|
||||
{
|
||||
var req = new NatIntroduceResponsePacket
|
||||
{
|
||||
Token = additionalInfo
|
||||
};
|
||||
|
||||
//First packet (server) send to client
|
||||
req.Internal = hostInternal;
|
||||
req.External = hostExternal;
|
||||
Send(req, clientExternal);
|
||||
|
||||
//Second packet (client) send to server
|
||||
req.Internal = clientInternal;
|
||||
req.External = clientExternal;
|
||||
Send(req, hostExternal);
|
||||
}
|
||||
|
||||
public void PollEvents()
|
||||
{
|
||||
if (UnsyncedEvents)
|
||||
return;
|
||||
|
||||
if (_natPunchListener == null || (_successEvents.IsEmpty && _requestEvents.IsEmpty))
|
||||
return;
|
||||
|
||||
while (_successEvents.TryDequeue(out var evt))
|
||||
{
|
||||
_natPunchListener.OnNatIntroductionSuccess(
|
||||
evt.TargetEndPoint,
|
||||
evt.Type,
|
||||
evt.Token);
|
||||
}
|
||||
|
||||
while (_requestEvents.TryDequeue(out var evt))
|
||||
{
|
||||
_natPunchListener.OnNatIntroductionRequest(evt.LocalEndPoint, evt.RemoteEndPoint, evt.Token);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendNatIntroduceRequest(string host, int port, string additionalInfo)
|
||||
{
|
||||
SendNatIntroduceRequest(NetUtils.MakeEndPoint(host, port), additionalInfo);
|
||||
}
|
||||
|
||||
public void SendNatIntroduceRequest(IPEndPoint masterServerEndPoint, string additionalInfo)
|
||||
{
|
||||
//prepare outgoing data
|
||||
string networkIp = NetUtils.GetLocalIp(LocalAddrType.IPv4);
|
||||
if (string.IsNullOrEmpty(networkIp))
|
||||
{
|
||||
networkIp = NetUtils.GetLocalIp(LocalAddrType.IPv6);
|
||||
}
|
||||
|
||||
Send(
|
||||
new NatIntroduceRequestPacket
|
||||
{
|
||||
Internal = NetUtils.MakeEndPoint(networkIp, _socket.LocalPort),
|
||||
Token = additionalInfo
|
||||
},
|
||||
masterServerEndPoint);
|
||||
}
|
||||
|
||||
//We got request and must introduce
|
||||
private void OnNatIntroductionRequest(NatIntroduceRequestPacket req, IPEndPoint senderEndPoint)
|
||||
{
|
||||
if (UnsyncedEvents)
|
||||
{
|
||||
_natPunchListener.OnNatIntroductionRequest(
|
||||
req.Internal,
|
||||
senderEndPoint,
|
||||
req.Token);
|
||||
}
|
||||
else
|
||||
{
|
||||
_requestEvents.Enqueue(new RequestEventData
|
||||
{
|
||||
LocalEndPoint = req.Internal,
|
||||
RemoteEndPoint = senderEndPoint,
|
||||
Token = req.Token
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//We got introduce and must punch
|
||||
private void OnNatIntroductionResponse(NatIntroduceResponsePacket req)
|
||||
{
|
||||
NetDebug.Write(NetLogLevel.Trace, "[NAT] introduction received");
|
||||
|
||||
// send internal punch
|
||||
var punchPacket = new NatPunchPacket {Token = req.Token};
|
||||
Send(punchPacket, req.Internal);
|
||||
NetDebug.Write(NetLogLevel.Trace, $"[NAT] internal punch sent to {req.Internal}");
|
||||
|
||||
// hack for some routers
|
||||
_socket.Ttl = 2;
|
||||
_socket.SendRaw(new[] { (byte)PacketProperty.Empty }, 0, 1, req.External);
|
||||
|
||||
// send external punch
|
||||
_socket.Ttl = NetConstants.SocketTTL;
|
||||
punchPacket.IsExternal = true;
|
||||
Send(punchPacket, req.External);
|
||||
NetDebug.Write(NetLogLevel.Trace, $"[NAT] external punch sent to {req.External}");
|
||||
}
|
||||
|
||||
//We got punch and can connect
|
||||
private void OnNatPunch(NatPunchPacket req, IPEndPoint senderEndPoint)
|
||||
{
|
||||
//Read info
|
||||
NetDebug.Write(NetLogLevel.Trace, $"[NAT] punch received from {senderEndPoint} - additional info: {req.Token}");
|
||||
|
||||
//Release punch success to client; enabling him to Connect() to Sender if token is ok
|
||||
if(UnsyncedEvents)
|
||||
{
|
||||
_natPunchListener.OnNatIntroductionSuccess(
|
||||
senderEndPoint,
|
||||
req.IsExternal ? NatAddressType.External : NatAddressType.Internal,
|
||||
req.Token
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
_successEvents.Enqueue(new SuccessEventData
|
||||
{
|
||||
TargetEndPoint = senderEndPoint,
|
||||
Type = req.IsExternal ? NatAddressType.External : NatAddressType.Internal,
|
||||
Token = req.Token
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3922a7026e6584b4bb7ea037a153894c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
301
Transport/LiteNetLib Transport/LiteNetLib/NativeSocket.cs
Normal file
301
Transport/LiteNetLib Transport/LiteNetLib/NativeSocket.cs
Normal file
@@ -0,0 +1,301 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
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
|
||||
{
|
||||
private const string LibName = "ws2_32.dll";
|
||||
|
||||
[DllImport(LibName, SetLastError = true)]
|
||||
public static extern int recvfrom(
|
||||
IntPtr socketHandle,
|
||||
[In, Out] byte[] pinnedBuffer,
|
||||
[In] int len,
|
||||
[In] SocketFlags socketFlags,
|
||||
[Out] byte[] socketAddress,
|
||||
[In, Out] ref int socketAddressSize);
|
||||
|
||||
[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
|
||||
{
|
||||
private const string LibName = "libc";
|
||||
|
||||
[DllImport(LibName, SetLastError = true)]
|
||||
public static extern int recvfrom(
|
||||
IntPtr socketHandle,
|
||||
[In, Out] byte[] pinnedBuffer,
|
||||
[In] int len,
|
||||
[In] SocketFlags socketFlags,
|
||||
[Out] byte[] socketAddress,
|
||||
[In, Out] ref int socketAddressSize);
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
public static readonly bool IsSupported = false;
|
||||
public static readonly bool UnixMode = false;
|
||||
|
||||
public const int IPv4AddrSize = 16;
|
||||
public const int IPv6AddrSize = 28;
|
||||
public const int AF_INET = 2;
|
||||
public const int AF_INET6 = 10;
|
||||
|
||||
private static readonly Dictionary<int, SocketError> NativeErrorToSocketError = new Dictionary<int, SocketError>
|
||||
{
|
||||
{ 13, SocketError.AccessDenied }, //EACCES
|
||||
{ 98, SocketError.AddressAlreadyInUse }, //EADDRINUSE
|
||||
{ 99, SocketError.AddressNotAvailable }, //EADDRNOTAVAIL
|
||||
{ 97, SocketError.AddressFamilyNotSupported }, //EAFNOSUPPORT
|
||||
{ 11, SocketError.WouldBlock }, //EAGAIN
|
||||
{ 114, SocketError.AlreadyInProgress }, //EALREADY
|
||||
{ 9, SocketError.OperationAborted }, //EBADF
|
||||
{ 125, SocketError.OperationAborted }, //ECANCELED
|
||||
{ 103, SocketError.ConnectionAborted }, //ECONNABORTED
|
||||
{ 111, SocketError.ConnectionRefused }, //ECONNREFUSED
|
||||
{ 104, SocketError.ConnectionReset }, //ECONNRESET
|
||||
{ 89, SocketError.DestinationAddressRequired }, //EDESTADDRREQ
|
||||
{ 14, SocketError.Fault }, //EFAULT
|
||||
{ 112, SocketError.HostDown }, //EHOSTDOWN
|
||||
{ 6, SocketError.HostNotFound }, //ENXIO
|
||||
{ 113, SocketError.HostUnreachable }, //EHOSTUNREACH
|
||||
{ 115, SocketError.InProgress }, //EINPROGRESS
|
||||
{ 4, SocketError.Interrupted }, //EINTR
|
||||
{ 22, SocketError.InvalidArgument }, //EINVAL
|
||||
{ 106, SocketError.IsConnected }, //EISCONN
|
||||
{ 24, SocketError.TooManyOpenSockets }, //EMFILE
|
||||
{ 90, SocketError.MessageSize }, //EMSGSIZE
|
||||
{ 100, SocketError.NetworkDown }, //ENETDOWN
|
||||
{ 102, SocketError.NetworkReset }, //ENETRESET
|
||||
{ 101, SocketError.NetworkUnreachable }, //ENETUNREACH
|
||||
{ 23, SocketError.TooManyOpenSockets }, //ENFILE
|
||||
{ 105, SocketError.NoBufferSpaceAvailable }, //ENOBUFS
|
||||
{ 61, SocketError.NoData }, //ENODATA
|
||||
{ 2, SocketError.AddressNotAvailable }, //ENOENT
|
||||
{ 92, SocketError.ProtocolOption }, //ENOPROTOOPT
|
||||
{ 107, SocketError.NotConnected }, //ENOTCONN
|
||||
{ 88, SocketError.NotSocket }, //ENOTSOCK
|
||||
{ 3440, SocketError.OperationNotSupported }, //ENOTSUP
|
||||
{ 1, SocketError.AccessDenied }, //EPERM
|
||||
{ 32, SocketError.Shutdown }, //EPIPE
|
||||
{ 96, SocketError.ProtocolFamilyNotSupported }, //EPFNOSUPPORT
|
||||
{ 93, SocketError.ProtocolNotSupported }, //EPROTONOSUPPORT
|
||||
{ 91, SocketError.ProtocolType }, //EPROTOTYPE
|
||||
{ 94, SocketError.SocketNotSupported }, //ESOCKTNOSUPPORT
|
||||
{ 108, SocketError.Disconnecting }, //ESHUTDOWN
|
||||
{ 110, SocketError.TimedOut }, //ETIMEDOUT
|
||||
{ 0, SocketError.Success }
|
||||
};
|
||||
|
||||
static NativeSocket()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
IsSupported = true;
|
||||
UnixMode = true;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
IsSupported = true;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int RecvFrom(
|
||||
IntPtr socketHandle,
|
||||
byte[] pinnedBuffer,
|
||||
int len,
|
||||
byte[] socketAddress,
|
||||
ref int socketAddressSize)
|
||||
{
|
||||
return UnixMode
|
||||
? UnixSock.recvfrom(socketHandle, pinnedBuffer, len, 0, socketAddress, ref socketAddressSize)
|
||||
: WinSock.recvfrom(socketHandle, pinnedBuffer, len, 0, socketAddress, ref socketAddressSize);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public
|
||||
#if LITENETLIB_UNSAFE
|
||||
unsafe
|
||||
#endif
|
||||
static int SendTo(
|
||||
IntPtr socketHandle,
|
||||
#if LITENETLIB_UNSAFE
|
||||
byte* pinnedBuffer,
|
||||
#else
|
||||
byte[] pinnedBuffer,
|
||||
#endif
|
||||
int len,
|
||||
byte[] socketAddress,
|
||||
int socketAddressSize)
|
||||
{
|
||||
return UnixMode
|
||||
? UnixSock.sendto(socketHandle, pinnedBuffer, len, 0, socketAddress, socketAddressSize)
|
||||
: WinSock.sendto(socketHandle, pinnedBuffer, len, 0, socketAddress, socketAddressSize);
|
||||
}
|
||||
|
||||
public static SocketError GetSocketError()
|
||||
{
|
||||
int error = Marshal.GetLastWin32Error();
|
||||
if (UnixMode)
|
||||
return NativeErrorToSocketError.TryGetValue(error, out var err)
|
||||
? err
|
||||
: SocketError.SocketError;
|
||||
return (SocketError)error;
|
||||
}
|
||||
|
||||
public static SocketException GetSocketException()
|
||||
{
|
||||
int error = Marshal.GetLastWin32Error();
|
||||
if (UnixMode)
|
||||
return NativeErrorToSocketError.TryGetValue(error, out var err)
|
||||
? new SocketException((int)err)
|
||||
: new SocketException((int)SocketError.SocketError);
|
||||
return new SocketException(error);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static short GetNativeAddressFamily(IPEndPoint remoteEndPoint)
|
||||
{
|
||||
return UnixMode
|
||||
? (short)(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork ? AF_INET : AF_INET6)
|
||||
: (short)remoteEndPoint.AddressFamily;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9590e69ce1ea9f0448ff4ab0808fb55e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
75
Transport/LiteNetLib Transport/LiteNetLib/NetConstants.cs
Normal file
75
Transport/LiteNetLib Transport/LiteNetLib/NetConstants.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
namespace LiteNetLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Sending method type
|
||||
/// </summary>
|
||||
public enum DeliveryMethod : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// Unreliable. Packets can be dropped, can be duplicated, can arrive without order.
|
||||
/// </summary>
|
||||
Unreliable = 4,
|
||||
|
||||
/// <summary>
|
||||
/// Reliable. Packets won't be dropped, won't be duplicated, can arrive without order.
|
||||
/// </summary>
|
||||
ReliableUnordered = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Unreliable. Packets can be dropped, won't be duplicated, will arrive in order.
|
||||
/// </summary>
|
||||
Sequenced = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Reliable and ordered. Packets won't be dropped, won't be duplicated, will arrive in order.
|
||||
/// </summary>
|
||||
ReliableOrdered = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Reliable only last packet. Packets can be dropped (except the last one), won't be duplicated, will arrive in order.
|
||||
/// Cannot be fragmented
|
||||
/// </summary>
|
||||
ReliableSequenced = 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Network constants. Can be tuned from sources for your purposes.
|
||||
/// </summary>
|
||||
public static class NetConstants
|
||||
{
|
||||
//can be tuned
|
||||
public const int DefaultWindowSize = 64;
|
||||
public const int SocketBufferSize = 1024 * 1024; //1mb
|
||||
public const int SocketTTL = 255;
|
||||
|
||||
public const int HeaderSize = 1;
|
||||
public const int ChanneledHeaderSize = 4;
|
||||
public const int FragmentHeaderSize = 6;
|
||||
public const int FragmentedHeaderTotalSize = ChanneledHeaderSize + FragmentHeaderSize;
|
||||
public const ushort MaxSequence = 32768;
|
||||
public const ushort HalfMaxSequence = MaxSequence / 2;
|
||||
|
||||
//protocol
|
||||
internal const int ProtocolId = 13;
|
||||
internal const int MaxUdpHeaderSize = 68;
|
||||
internal const int ChannelTypeCount = 4;
|
||||
|
||||
internal static readonly int[] PossibleMtu =
|
||||
{
|
||||
576 - MaxUdpHeaderSize, //minimal (RFC 1191)
|
||||
1024, //most games standard
|
||||
1232 - MaxUdpHeaderSize,
|
||||
1460 - MaxUdpHeaderSize, //google cloud
|
||||
1472 - MaxUdpHeaderSize, //VPN
|
||||
1492 - MaxUdpHeaderSize, //Ethernet with LLC and SNAP, PPPoE (RFC 1042)
|
||||
1500 - MaxUdpHeaderSize //Ethernet II (RFC 1191)
|
||||
};
|
||||
|
||||
//Max possible single packet size
|
||||
public static readonly int MaxPacketSize = PossibleMtu[PossibleMtu.Length - 1];
|
||||
public static readonly int MaxUnreliableDataSize = MaxPacketSize - HeaderSize;
|
||||
|
||||
//peer specific
|
||||
public const byte MaxConnectionNumber = 4;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6dd87d091a1ad845bf2ff8b94e589cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
92
Transport/LiteNetLib Transport/LiteNetLib/NetDebug.cs
Normal file
92
Transport/LiteNetLib Transport/LiteNetLib/NetDebug.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public class InvalidPacketException : ArgumentException
|
||||
{
|
||||
public InvalidPacketException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class TooBigPacketException : InvalidPacketException
|
||||
{
|
||||
public TooBigPacketException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public enum NetLogLevel
|
||||
{
|
||||
Warning,
|
||||
Error,
|
||||
Trace,
|
||||
Info
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface to implement for your own logger
|
||||
/// </summary>
|
||||
public interface INetLogger
|
||||
{
|
||||
void WriteNet(NetLogLevel level, string str, params object[] args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static class for defining your own LiteNetLib logger instead of Console.WriteLine
|
||||
/// or Debug.Log if compiled with UNITY flag
|
||||
/// </summary>
|
||||
public static class NetDebug
|
||||
{
|
||||
public static INetLogger Logger = null;
|
||||
private static readonly object DebugLogLock = new object();
|
||||
private static void WriteLogic(NetLogLevel logLevel, string str, params object[] args)
|
||||
{
|
||||
lock (DebugLogLock)
|
||||
{
|
||||
if (Logger == null)
|
||||
{
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
UnityEngine.Debug.Log(string.Format(str, args));
|
||||
#else
|
||||
Console.WriteLine(str, args);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.WriteNet(logLevel, str, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Conditional("DEBUG_MESSAGES")]
|
||||
internal static void Write(string str)
|
||||
{
|
||||
WriteLogic(NetLogLevel.Trace, str);
|
||||
}
|
||||
|
||||
[Conditional("DEBUG_MESSAGES")]
|
||||
internal static void Write(NetLogLevel level, string str)
|
||||
{
|
||||
WriteLogic(level, str);
|
||||
}
|
||||
|
||||
[Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")]
|
||||
internal static void WriteForce(string str)
|
||||
{
|
||||
WriteLogic(NetLogLevel.Trace, str);
|
||||
}
|
||||
|
||||
[Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")]
|
||||
internal static void WriteForce(NetLogLevel level, string str)
|
||||
{
|
||||
WriteLogic(level, str);
|
||||
}
|
||||
|
||||
internal static void WriteError(string str)
|
||||
{
|
||||
WriteLogic(NetLogLevel.Error, str);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Transport/LiteNetLib Transport/LiteNetLib/NetDebug.cs.meta
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/NetDebug.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7248d233449a9334c84012cb0165dfcf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public partial class NetManager
|
||||
{
|
||||
private NetPacket _poolHead;
|
||||
private int _poolCount;
|
||||
private readonly object _poolLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Maximum packet pool size (increase if you have tons of packets sending)
|
||||
/// </summary>
|
||||
public int PacketPoolSize = 1000;
|
||||
|
||||
public int PoolCount => _poolCount;
|
||||
|
||||
private NetPacket PoolGetWithData(PacketProperty property, byte[] data, int start, int length)
|
||||
{
|
||||
int headerSize = NetPacket.GetHeaderSize(property);
|
||||
NetPacket packet = PoolGetPacket(length + headerSize);
|
||||
packet.Property = property;
|
||||
Buffer.BlockCopy(data, start, packet.RawData, headerSize, length);
|
||||
return packet;
|
||||
}
|
||||
|
||||
//Get packet with size
|
||||
private NetPacket PoolGetWithProperty(PacketProperty property, int size)
|
||||
{
|
||||
NetPacket packet = PoolGetPacket(size + NetPacket.GetHeaderSize(property));
|
||||
packet.Property = property;
|
||||
return packet;
|
||||
}
|
||||
|
||||
private NetPacket PoolGetWithProperty(PacketProperty property)
|
||||
{
|
||||
NetPacket packet = PoolGetPacket(NetPacket.GetHeaderSize(property));
|
||||
packet.Property = property;
|
||||
return packet;
|
||||
}
|
||||
|
||||
internal NetPacket PoolGetPacket(int size)
|
||||
{
|
||||
if (size > NetConstants.MaxPacketSize)
|
||||
return new NetPacket(size);
|
||||
|
||||
NetPacket packet;
|
||||
lock (_poolLock)
|
||||
{
|
||||
packet = _poolHead;
|
||||
if (packet == null)
|
||||
return new NetPacket(size);
|
||||
|
||||
_poolHead = _poolHead.Next;
|
||||
_poolCount--;
|
||||
}
|
||||
|
||||
packet.Size = size;
|
||||
if (packet.RawData.Length < size)
|
||||
packet.RawData = new byte[size];
|
||||
return packet;
|
||||
}
|
||||
|
||||
internal void PoolRecycle(NetPacket packet)
|
||||
{
|
||||
if (packet.RawData.Length > NetConstants.MaxPacketSize || _poolCount >= PacketPoolSize)
|
||||
{
|
||||
//Don't pool big packets. Save memory
|
||||
return;
|
||||
}
|
||||
|
||||
//Clean fragmented flag
|
||||
packet.RawData[0] = 0;
|
||||
lock (_poolLock)
|
||||
{
|
||||
packet.Next = _poolHead;
|
||||
_poolHead = packet;
|
||||
_poolCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 692101fdcfee9eb4d83ab7397d63a0d3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
728
Transport/LiteNetLib Transport/LiteNetLib/NetManager.Socket.cs
Normal file
728
Transport/LiteNetLib Transport/LiteNetLib/NetManager.Socket.cs
Normal file
@@ -0,0 +1,728 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public partial class NetManager
|
||||
{
|
||||
private const int ReceivePollingTime = 500000; //0.5 second
|
||||
|
||||
private Socket _udpSocketv4;
|
||||
private Socket _udpSocketv6;
|
||||
private Thread _receiveThread;
|
||||
private IPEndPoint _bufferEndPointv4;
|
||||
private IPEndPoint _bufferEndPointv6;
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
private PausedSocketFix _pausedSocketFix;
|
||||
#endif
|
||||
|
||||
#if !LITENETLIB_UNSAFE
|
||||
[ThreadStatic] private static byte[] _sendToBuffer;
|
||||
#endif
|
||||
[ThreadStatic] private static byte[] _endPointBuffer;
|
||||
|
||||
private readonly Dictionary<NativeAddr, IPEndPoint> _nativeAddrMap = new Dictionary<NativeAddr, IPEndPoint>();
|
||||
|
||||
private const int SioUdpConnreset = -1744830452; //SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12
|
||||
private static readonly IPAddress MulticastAddressV6 = IPAddress.Parse("ff02::1");
|
||||
public static readonly bool IPv6Support;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum packets count that will be processed in Manual PollEvents
|
||||
/// </summary>
|
||||
public int MaxPacketsReceivePerUpdate = 0;
|
||||
|
||||
// special case in iOS (and possibly android that should be resolved in unity)
|
||||
internal bool NotConnected;
|
||||
|
||||
public short Ttl
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_SWITCH
|
||||
return 0;
|
||||
#else
|
||||
return _udpSocketv4.Ttl;
|
||||
#endif
|
||||
}
|
||||
internal set
|
||||
{
|
||||
#if !UNITY_SWITCH
|
||||
_udpSocketv4.Ttl = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static NetManager()
|
||||
{
|
||||
#if DISABLE_IPV6
|
||||
IPv6Support = false;
|
||||
#elif !UNITY_2019_1_OR_NEWER && !UNITY_2018_4_OR_NEWER && (!UNITY_EDITOR && ENABLE_IL2CPP)
|
||||
string version = UnityEngine.Application.unityVersion;
|
||||
IPv6Support = Socket.OSSupportsIPv6 && int.Parse(version.Remove(version.IndexOf('f')).Split('.')[2]) >= 6;
|
||||
#else
|
||||
IPv6Support = Socket.OSSupportsIPv6;
|
||||
#endif
|
||||
}
|
||||
|
||||
private void RegisterEndPoint(IPEndPoint ep)
|
||||
{
|
||||
if (UseNativeSockets && ep is NativeEndPoint nep)
|
||||
{
|
||||
_nativeAddrMap.Add(new NativeAddr(nep.NativeAddress, nep.NativeAddress.Length), nep);
|
||||
}
|
||||
}
|
||||
|
||||
private void UnregisterEndPoint(IPEndPoint ep)
|
||||
{
|
||||
if (UseNativeSockets && ep is NativeEndPoint nep)
|
||||
{
|
||||
var nativeAddr = new NativeAddr(nep.NativeAddress, nep.NativeAddress.Length);
|
||||
_nativeAddrMap.Remove(nativeAddr);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ProcessError(SocketException ex)
|
||||
{
|
||||
switch (ex.SocketErrorCode)
|
||||
{
|
||||
case SocketError.NotConnected:
|
||||
NotConnected = true;
|
||||
return true;
|
||||
case SocketError.Interrupted:
|
||||
case SocketError.NotSocket:
|
||||
case SocketError.OperationAborted:
|
||||
return true;
|
||||
case SocketError.ConnectionReset:
|
||||
case SocketError.MessageSize:
|
||||
case SocketError.TimedOut:
|
||||
case SocketError.NetworkReset:
|
||||
//NetDebug.Write($"[R]Ignored error: {(int)ex.SocketErrorCode} - {ex}");
|
||||
break;
|
||||
default:
|
||||
NetDebug.WriteError($"[R]Error code: {(int)ex.SocketErrorCode} - {ex}");
|
||||
CreateEvent(NetEvent.EType.Error, errorCode: ex.SocketErrorCode);
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ManualReceive(Socket socket, EndPoint bufferEndPoint)
|
||||
{
|
||||
//Reading data
|
||||
try
|
||||
{
|
||||
int packetsReceived = 0;
|
||||
while (socket.Available > 0)
|
||||
{
|
||||
ReceiveFrom(socket, ref bufferEndPoint);
|
||||
packetsReceived++;
|
||||
if (packetsReceived == MaxPacketsReceivePerUpdate)
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
ProcessError(ex);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//protects socket receive thread
|
||||
NetDebug.WriteError("[NM] SocketReceiveThread error: " + e );
|
||||
}
|
||||
}
|
||||
|
||||
private bool NativeReceiveFrom(ref NetPacket packet, IntPtr s, byte[] addrBuffer, int addrSize)
|
||||
{
|
||||
//Reading data
|
||||
packet.Size = NativeSocket.RecvFrom(s, packet.RawData, NetConstants.MaxPacketSize, addrBuffer, ref addrSize);
|
||||
if (packet.Size == 0)
|
||||
return false; //socket closed
|
||||
if (packet.Size == -1)
|
||||
{
|
||||
var errorCode = NativeSocket.GetSocketError();
|
||||
//Linux timeout EAGAIN
|
||||
return errorCode == SocketError.WouldBlock || errorCode == SocketError.TimedOut || ProcessError(new SocketException((int)errorCode)) == false;
|
||||
}
|
||||
|
||||
var nativeAddr = new NativeAddr(addrBuffer, addrSize);
|
||||
if (!_nativeAddrMap.TryGetValue(nativeAddr, out var endPoint))
|
||||
endPoint = new NativeEndPoint(addrBuffer);
|
||||
|
||||
//All ok!
|
||||
//NetDebug.WriteForce($"[R]Received data from {endPoint}, result: {packet.Size}");
|
||||
OnMessageReceived(packet, endPoint);
|
||||
packet = PoolGetPacket(NetConstants.MaxPacketSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void NativeReceiveLogic()
|
||||
{
|
||||
IntPtr socketHandle4 = _udpSocketv4.Handle;
|
||||
IntPtr socketHandle6 = _udpSocketv6?.Handle ?? IntPtr.Zero;
|
||||
byte[] addrBuffer4 = new byte[NativeSocket.IPv4AddrSize];
|
||||
byte[] addrBuffer6 = new byte[NativeSocket.IPv6AddrSize];
|
||||
int addrSize4 = addrBuffer4.Length;
|
||||
int addrSize6 = addrBuffer6.Length;
|
||||
var selectReadList = new List<Socket>(2);
|
||||
var socketv4 = _udpSocketv4;
|
||||
var socketV6 = _udpSocketv6;
|
||||
var packet = PoolGetPacket(NetConstants.MaxPacketSize);
|
||||
|
||||
while (IsRunning)
|
||||
{
|
||||
if (socketV6 == null)
|
||||
{
|
||||
if (NativeReceiveFrom(ref packet, socketHandle4, addrBuffer4, addrSize4) == false)
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
bool messageReceived = false;
|
||||
if (socketv4.Available != 0)
|
||||
{
|
||||
if (NativeReceiveFrom(ref packet, socketHandle4, addrBuffer4, addrSize4) == false)
|
||||
return;
|
||||
messageReceived = true;
|
||||
}
|
||||
if (socketV6.Available != 0)
|
||||
{
|
||||
if (NativeReceiveFrom(ref packet, socketHandle6, addrBuffer6, addrSize6) == false)
|
||||
return;
|
||||
messageReceived = true;
|
||||
}
|
||||
if (messageReceived)
|
||||
continue;
|
||||
selectReadList.Clear();
|
||||
selectReadList.Add(socketv4);
|
||||
selectReadList.Add(socketV6);
|
||||
try
|
||||
{
|
||||
Socket.Select(selectReadList, null, null, ReceivePollingTime);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
if (ProcessError(ex))
|
||||
return;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
//socket closed
|
||||
return;
|
||||
}
|
||||
catch (ThreadAbortException)
|
||||
{
|
||||
//thread closed
|
||||
return;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//protects socket receive thread
|
||||
NetDebug.WriteError("[NM] SocketReceiveThread error: " + e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReceiveFrom(Socket s, ref EndPoint bufferEndPoint)
|
||||
{
|
||||
var packet = PoolGetPacket(NetConstants.MaxPacketSize);
|
||||
packet.Size = s.ReceiveFrom(packet.RawData, 0, NetConstants.MaxPacketSize, SocketFlags.None, ref bufferEndPoint);
|
||||
OnMessageReceived(packet, (IPEndPoint)bufferEndPoint);
|
||||
}
|
||||
|
||||
private void ReceiveLogic()
|
||||
{
|
||||
EndPoint bufferEndPoint4 = new IPEndPoint(IPAddress.Any, 0);
|
||||
EndPoint bufferEndPoint6 = new IPEndPoint(IPAddress.IPv6Any, 0);
|
||||
var selectReadList = new List<Socket>(2);
|
||||
var socketv4 = _udpSocketv4;
|
||||
var socketV6 = _udpSocketv6;
|
||||
|
||||
while (IsRunning)
|
||||
{
|
||||
//Reading data
|
||||
try
|
||||
{
|
||||
if (socketV6 == null)
|
||||
{
|
||||
if (socketv4.Available == 0 && !socketv4.Poll(ReceivePollingTime, SelectMode.SelectRead))
|
||||
continue;
|
||||
ReceiveFrom(socketv4, ref bufferEndPoint4);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool messageReceived = false;
|
||||
if (socketv4.Available != 0)
|
||||
{
|
||||
ReceiveFrom(socketv4, ref bufferEndPoint4);
|
||||
messageReceived = true;
|
||||
}
|
||||
if (socketV6.Available != 0)
|
||||
{
|
||||
ReceiveFrom(socketV6, ref bufferEndPoint6);
|
||||
messageReceived = true;
|
||||
}
|
||||
if (messageReceived)
|
||||
continue;
|
||||
|
||||
selectReadList.Clear();
|
||||
selectReadList.Add(socketv4);
|
||||
selectReadList.Add(socketV6);
|
||||
Socket.Select(selectReadList, null, null, ReceivePollingTime);
|
||||
}
|
||||
//NetDebug.Write(NetLogLevel.Trace, $"[R]Received data from {bufferEndPoint}, result: {packet.Size}");
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
if (ProcessError(ex))
|
||||
return;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
//socket closed
|
||||
return;
|
||||
}
|
||||
catch (ThreadAbortException)
|
||||
{
|
||||
//thread closed
|
||||
return;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//protects socket receive thread
|
||||
NetDebug.WriteError("[NM] SocketReceiveThread error: " + e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start logic thread and listening on selected port
|
||||
/// </summary>
|
||||
/// <param name="addressIPv4">bind to specific ipv4 address</param>
|
||||
/// <param name="addressIPv6">bind to specific ipv6 address</param>
|
||||
/// <param name="port">port to listen</param>
|
||||
/// <param name="manualMode">mode of library</param>
|
||||
public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool manualMode)
|
||||
{
|
||||
if (IsRunning && NotConnected == false)
|
||||
return false;
|
||||
|
||||
NotConnected = false;
|
||||
_manualMode = manualMode;
|
||||
UseNativeSockets = UseNativeSockets && NativeSocket.IsSupported;
|
||||
_udpSocketv4 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
if (!BindSocket(_udpSocketv4, new IPEndPoint(addressIPv4, port)))
|
||||
return false;
|
||||
|
||||
LocalPort = ((IPEndPoint) _udpSocketv4.LocalEndPoint).Port;
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
if (_pausedSocketFix == null)
|
||||
_pausedSocketFix = new PausedSocketFix(this, addressIPv4, addressIPv6, port, manualMode);
|
||||
#endif
|
||||
|
||||
IsRunning = true;
|
||||
if (_manualMode)
|
||||
{
|
||||
_bufferEndPointv4 = new IPEndPoint(IPAddress.Any, 0);
|
||||
}
|
||||
|
||||
//Check IPv6 support
|
||||
if (IPv6Support && IPv6Enabled)
|
||||
{
|
||||
_udpSocketv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||
//Use one port for two sockets
|
||||
if (BindSocket(_udpSocketv6, new IPEndPoint(addressIPv6, LocalPort)))
|
||||
{
|
||||
if (_manualMode)
|
||||
{
|
||||
_bufferEndPointv6 = new IPEndPoint(IPAddress.IPv6Any, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_udpSocketv6 = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!manualMode)
|
||||
{
|
||||
ThreadStart ts = ReceiveLogic;
|
||||
if (UseNativeSockets)
|
||||
ts = NativeReceiveLogic;
|
||||
_receiveThread = new Thread(ts)
|
||||
{
|
||||
Name = $"ReceiveThread({LocalPort})",
|
||||
IsBackground = true
|
||||
};
|
||||
_receiveThread.Start();
|
||||
if (_logicThread == null)
|
||||
{
|
||||
_logicThread = new Thread(UpdateLogic) { Name = "LogicThread", IsBackground = true };
|
||||
_logicThread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool BindSocket(Socket socket, IPEndPoint ep)
|
||||
{
|
||||
//Setup socket
|
||||
socket.ReceiveTimeout = 500;
|
||||
socket.SendTimeout = 500;
|
||||
socket.ReceiveBufferSize = NetConstants.SocketBufferSize;
|
||||
socket.SendBufferSize = NetConstants.SocketBufferSize;
|
||||
socket.Blocking = true;
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
try
|
||||
{
|
||||
socket.IOControl(SioUdpConnreset, new byte[] {0}, null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//ignored
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
socket.ExclusiveAddressUse = !ReuseAddress;
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, ReuseAddress);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Unity with IL2CPP throws an exception here, it doesn't matter in most cases so just ignore it
|
||||
}
|
||||
if (ep.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
Ttl = NetConstants.SocketTTL;
|
||||
|
||||
try { socket.EnableBroadcast = true; }
|
||||
catch (SocketException e)
|
||||
{
|
||||
NetDebug.WriteError($"[B]Broadcast error: {e.SocketErrorCode}");
|
||||
}
|
||||
|
||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
try { socket.DontFragment = true; }
|
||||
catch (SocketException e)
|
||||
{
|
||||
NetDebug.WriteError($"[B]DontFragment error: {e.SocketErrorCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
//Bind
|
||||
try
|
||||
{
|
||||
socket.Bind(ep);
|
||||
NetDebug.Write(NetLogLevel.Trace, $"[B]Successfully binded to port: {((IPEndPoint)socket.LocalEndPoint).Port}, AF: {socket.AddressFamily}");
|
||||
|
||||
//join multicast
|
||||
if (ep.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
try
|
||||
{
|
||||
#if !UNITY_2018_3_OR_NEWER
|
||||
socket.SetSocketOption(
|
||||
SocketOptionLevel.IPv6,
|
||||
SocketOptionName.AddMembership,
|
||||
new IPv6MulticastOption(MulticastAddressV6));
|
||||
#endif
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Unity3d throws exception - ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SocketException bindException)
|
||||
{
|
||||
switch (bindException.SocketErrorCode)
|
||||
{
|
||||
//IPv6 bind fix
|
||||
case SocketError.AddressAlreadyInUse:
|
||||
if (socket.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Set IPv6Only
|
||||
socket.DualMode = false;
|
||||
socket.Bind(ep);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
//because its fixed in 2018_3
|
||||
NetDebug.WriteError($"[B]Bind exception: {ex}, errorCode: {ex.SocketErrorCode}");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
//hack for iOS (Unity3D)
|
||||
case SocketError.AddressFamilyNotSupported:
|
||||
return true;
|
||||
}
|
||||
NetDebug.WriteError($"[B]Bind exception: {bindException}, errorCode: {bindException.SocketErrorCode}");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
internal int SendRawAndRecycle(NetPacket packet, IPEndPoint remoteEndPoint)
|
||||
{
|
||||
int result = SendRaw(packet.RawData, 0, packet.Size, remoteEndPoint);
|
||||
PoolRecycle(packet);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal int SendRaw(NetPacket packet, IPEndPoint remoteEndPoint)
|
||||
{
|
||||
return SendRaw(packet.RawData, 0, packet.Size, remoteEndPoint);
|
||||
}
|
||||
|
||||
internal int SendRaw(byte[] message, int start, int length, IPEndPoint remoteEndPoint)
|
||||
{
|
||||
if (!IsRunning)
|
||||
return 0;
|
||||
|
||||
NetPacket expandedPacket = null;
|
||||
if (_extraPacketLayer != null)
|
||||
{
|
||||
expandedPacket = PoolGetPacket(length + _extraPacketLayer.ExtraPacketSizeForLayer);
|
||||
Buffer.BlockCopy(message, start, expandedPacket.RawData, 0, length);
|
||||
start = 0;
|
||||
_extraPacketLayer.ProcessOutBoundPacket(ref remoteEndPoint, ref expandedPacket.RawData, ref start, ref length);
|
||||
message = expandedPacket.RawData;
|
||||
}
|
||||
|
||||
var socket = _udpSocketv4;
|
||||
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6 && IPv6Support)
|
||||
{
|
||||
socket = _udpSocketv6;
|
||||
if (socket == null)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int result;
|
||||
try
|
||||
{
|
||||
if (UseNativeSockets)
|
||||
{
|
||||
byte[] socketAddress;
|
||||
|
||||
if (remoteEndPoint is NativeEndPoint nep)
|
||||
{
|
||||
socketAddress = nep.NativeAddress;
|
||||
}
|
||||
else //Convert endpoint to raw
|
||||
{
|
||||
if (_endPointBuffer == null)
|
||||
_endPointBuffer = new byte[NativeSocket.IPv6AddrSize];
|
||||
socketAddress = _endPointBuffer;
|
||||
|
||||
bool ipv4 = remoteEndPoint.AddressFamily == AddressFamily.InterNetwork;
|
||||
short addressFamily = NativeSocket.GetNativeAddressFamily(remoteEndPoint);
|
||||
|
||||
socketAddress[0] = (byte) (addressFamily);
|
||||
socketAddress[1] = (byte) (addressFamily >> 8);
|
||||
socketAddress[2] = (byte) (remoteEndPoint.Port >> 8);
|
||||
socketAddress[3] = (byte) (remoteEndPoint.Port);
|
||||
|
||||
if (ipv4)
|
||||
{
|
||||
#pragma warning disable 618
|
||||
long addr = remoteEndPoint.Address.Address;
|
||||
#pragma warning restore 618
|
||||
socketAddress[4] = (byte) (addr);
|
||||
socketAddress[5] = (byte) (addr >> 8);
|
||||
socketAddress[6] = (byte) (addr >> 16);
|
||||
socketAddress[7] = (byte) (addr >> 24);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NETCOREAPP || NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER
|
||||
remoteEndPoint.Address.TryWriteBytes(new Span<byte>(socketAddress, 8, 16), out _);
|
||||
#else
|
||||
byte[] addrBytes = remoteEndPoint.Address.GetAddressBytes();
|
||||
Buffer.BlockCopy(addrBytes, 0, socketAddress, 8, 16);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if LITENETLIB_UNSAFE
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* dataWithOffset = &message[start])
|
||||
{
|
||||
result =
|
||||
NativeSocket.SendTo(socket.Handle, dataWithOffset, length, socketAddress, socketAddress.Length);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (start > 0)
|
||||
{
|
||||
if (_sendToBuffer == null)
|
||||
_sendToBuffer = new byte[NetConstants.MaxPacketSize];
|
||||
Buffer.BlockCopy(message, start, _sendToBuffer, 0, length);
|
||||
message = _sendToBuffer;
|
||||
}
|
||||
|
||||
result = NativeSocket.SendTo(socket.Handle, message, length, socketAddress, socketAddress.Length);
|
||||
#endif
|
||||
if (result == -1)
|
||||
throw NativeSocket.GetSocketException();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = socket.SendTo(message, start, length, SocketFlags.None, remoteEndPoint);
|
||||
}
|
||||
//NetDebug.WriteForce("[S]Send packet to {0}, result: {1}", remoteEndPoint, result);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
switch (ex.SocketErrorCode)
|
||||
{
|
||||
case SocketError.NoBufferSpaceAvailable:
|
||||
case SocketError.Interrupted:
|
||||
return 0;
|
||||
case SocketError.MessageSize:
|
||||
NetDebug.Write(NetLogLevel.Trace, $"[SRD] 10040, datalen: {length}");
|
||||
return 0;
|
||||
|
||||
case SocketError.HostUnreachable:
|
||||
case SocketError.NetworkUnreachable:
|
||||
if (DisconnectOnUnreachable && TryGetPeer(remoteEndPoint, out var fromPeer))
|
||||
{
|
||||
DisconnectPeerForce(
|
||||
fromPeer,
|
||||
ex.SocketErrorCode == SocketError.HostUnreachable
|
||||
? DisconnectReason.HostUnreachable
|
||||
: DisconnectReason.NetworkUnreachable,
|
||||
ex.SocketErrorCode,
|
||||
null);
|
||||
}
|
||||
|
||||
CreateEvent(NetEvent.EType.Error, remoteEndPoint: remoteEndPoint, errorCode: ex.SocketErrorCode);
|
||||
return -1;
|
||||
|
||||
default:
|
||||
NetDebug.WriteError($"[S] {ex}");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NetDebug.WriteError($"[S] {ex}");
|
||||
return 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (expandedPacket != null)
|
||||
{
|
||||
PoolRecycle(expandedPacket);
|
||||
}
|
||||
}
|
||||
|
||||
if (result <= 0)
|
||||
return 0;
|
||||
|
||||
if (EnableStatistics)
|
||||
{
|
||||
Statistics.IncrementPacketsSent();
|
||||
Statistics.AddBytesSent(length);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool SendBroadcast(NetDataWriter writer, int port)
|
||||
{
|
||||
return SendBroadcast(writer.Data, 0, writer.Length, port);
|
||||
}
|
||||
|
||||
public bool SendBroadcast(byte[] data, int port)
|
||||
{
|
||||
return SendBroadcast(data, 0, data.Length, port);
|
||||
}
|
||||
|
||||
public bool SendBroadcast(byte[] data, int start, int length, int port)
|
||||
{
|
||||
if (!IsRunning)
|
||||
return false;
|
||||
|
||||
NetPacket packet;
|
||||
if (_extraPacketLayer != null)
|
||||
{
|
||||
var headerSize = NetPacket.GetHeaderSize(PacketProperty.Broadcast);
|
||||
packet = PoolGetPacket(headerSize + length + _extraPacketLayer.ExtraPacketSizeForLayer);
|
||||
packet.Property = PacketProperty.Broadcast;
|
||||
Buffer.BlockCopy(data, start, packet.RawData, headerSize, length);
|
||||
var checksumComputeStart = 0;
|
||||
int preCrcLength = length + headerSize;
|
||||
IPEndPoint emptyEp = null;
|
||||
_extraPacketLayer.ProcessOutBoundPacket(ref emptyEp, ref packet.RawData, ref checksumComputeStart, ref preCrcLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
packet = PoolGetWithData(PacketProperty.Broadcast, data, start, length);
|
||||
}
|
||||
|
||||
bool broadcastSuccess = false;
|
||||
bool multicastSuccess = false;
|
||||
try
|
||||
{
|
||||
broadcastSuccess = _udpSocketv4.SendTo(
|
||||
packet.RawData,
|
||||
0,
|
||||
packet.Size,
|
||||
SocketFlags.None,
|
||||
new IPEndPoint(IPAddress.Broadcast, port)) > 0;
|
||||
|
||||
if (_udpSocketv6 != null)
|
||||
{
|
||||
multicastSuccess = _udpSocketv6.SendTo(
|
||||
packet.RawData,
|
||||
0,
|
||||
packet.Size,
|
||||
SocketFlags.None,
|
||||
new IPEndPoint(MulticastAddressV6, port)) > 0;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NetDebug.WriteError($"[S][MCAST] {ex}");
|
||||
return broadcastSuccess;
|
||||
}
|
||||
finally
|
||||
{
|
||||
PoolRecycle(packet);
|
||||
}
|
||||
|
||||
return broadcastSuccess || multicastSuccess;
|
||||
}
|
||||
|
||||
private void CloseSocket()
|
||||
{
|
||||
IsRunning = false;
|
||||
_udpSocketv4?.Close();
|
||||
_udpSocketv6?.Close();
|
||||
_udpSocketv4 = null;
|
||||
_udpSocketv6 = null;
|
||||
if (_receiveThread != null && _receiveThread != Thread.CurrentThread)
|
||||
_receiveThread.Join();
|
||||
_receiveThread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df17cb0f44f9c5a4faa41f85cc643dd1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1821
Transport/LiteNetLib Transport/LiteNetLib/NetManager.cs
Normal file
1821
Transport/LiteNetLib Transport/LiteNetLib/NetManager.cs
Normal file
File diff suppressed because it is too large
Load Diff
11
Transport/LiteNetLib Transport/LiteNetLib/NetManager.cs.meta
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/NetManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d529e6190dc14f4e881a06af959b7d2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
160
Transport/LiteNetLib Transport/LiteNetLib/NetPacket.cs
Normal file
160
Transport/LiteNetLib Transport/LiteNetLib/NetPacket.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal enum PacketProperty : byte
|
||||
{
|
||||
Unreliable,
|
||||
Channeled,
|
||||
Ack,
|
||||
Ping,
|
||||
Pong,
|
||||
ConnectRequest,
|
||||
ConnectAccept,
|
||||
Disconnect,
|
||||
UnconnectedMessage,
|
||||
MtuCheck,
|
||||
MtuOk,
|
||||
Broadcast,
|
||||
Merged,
|
||||
ShutdownOk,
|
||||
PeerNotFound,
|
||||
InvalidProtocol,
|
||||
NatMessage,
|
||||
Empty
|
||||
}
|
||||
|
||||
internal sealed class NetPacket
|
||||
{
|
||||
private static readonly int PropertiesCount = Enum.GetValues(typeof(PacketProperty)).Length;
|
||||
private static readonly int[] HeaderSizes;
|
||||
|
||||
static NetPacket()
|
||||
{
|
||||
HeaderSizes = NetUtils.AllocatePinnedUninitializedArray<int>(PropertiesCount);
|
||||
for (int i = 0; i < HeaderSizes.Length; i++)
|
||||
{
|
||||
switch ((PacketProperty)i)
|
||||
{
|
||||
case PacketProperty.Channeled:
|
||||
case PacketProperty.Ack:
|
||||
HeaderSizes[i] = NetConstants.ChanneledHeaderSize;
|
||||
break;
|
||||
case PacketProperty.Ping:
|
||||
HeaderSizes[i] = NetConstants.HeaderSize + 2;
|
||||
break;
|
||||
case PacketProperty.ConnectRequest:
|
||||
HeaderSizes[i] = NetConnectRequestPacket.HeaderSize;
|
||||
break;
|
||||
case PacketProperty.ConnectAccept:
|
||||
HeaderSizes[i] = NetConnectAcceptPacket.Size;
|
||||
break;
|
||||
case PacketProperty.Disconnect:
|
||||
HeaderSizes[i] = NetConstants.HeaderSize + 8;
|
||||
break;
|
||||
case PacketProperty.Pong:
|
||||
HeaderSizes[i] = NetConstants.HeaderSize + 10;
|
||||
break;
|
||||
default:
|
||||
HeaderSizes[i] = NetConstants.HeaderSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Header
|
||||
public PacketProperty Property
|
||||
{
|
||||
get => (PacketProperty)(RawData[0] & 0x1F);
|
||||
set => RawData[0] = (byte)((RawData[0] & 0xE0) | (byte)value);
|
||||
}
|
||||
|
||||
public byte ConnectionNumber
|
||||
{
|
||||
get => (byte)((RawData[0] & 0x60) >> 5);
|
||||
set => RawData[0] = (byte) ((RawData[0] & 0x9F) | (value << 5));
|
||||
}
|
||||
|
||||
public ushort Sequence
|
||||
{
|
||||
get => BitConverter.ToUInt16(RawData, 1);
|
||||
set => FastBitConverter.GetBytes(RawData, 1, value);
|
||||
}
|
||||
|
||||
public bool IsFragmented => (RawData[0] & 0x80) != 0;
|
||||
|
||||
public void MarkFragmented()
|
||||
{
|
||||
RawData[0] |= 0x80; //set first bit
|
||||
}
|
||||
|
||||
public byte ChannelId
|
||||
{
|
||||
get => RawData[3];
|
||||
set => RawData[3] = value;
|
||||
}
|
||||
|
||||
public ushort FragmentId
|
||||
{
|
||||
get => BitConverter.ToUInt16(RawData, 4);
|
||||
set => FastBitConverter.GetBytes(RawData, 4, value);
|
||||
}
|
||||
|
||||
public ushort FragmentPart
|
||||
{
|
||||
get => BitConverter.ToUInt16(RawData, 6);
|
||||
set => FastBitConverter.GetBytes(RawData, 6, value);
|
||||
}
|
||||
|
||||
public ushort FragmentsTotal
|
||||
{
|
||||
get => BitConverter.ToUInt16(RawData, 8);
|
||||
set => FastBitConverter.GetBytes(RawData, 8, value);
|
||||
}
|
||||
|
||||
//Data
|
||||
public byte[] RawData;
|
||||
public int Size;
|
||||
|
||||
//Delivery
|
||||
public object UserData;
|
||||
|
||||
//Pool node
|
||||
public NetPacket Next;
|
||||
|
||||
public NetPacket(int size)
|
||||
{
|
||||
RawData = new byte[size];
|
||||
Size = size;
|
||||
}
|
||||
|
||||
public NetPacket(PacketProperty property, int size)
|
||||
{
|
||||
size += GetHeaderSize(property);
|
||||
RawData = new byte[size];
|
||||
Property = property;
|
||||
Size = size;
|
||||
}
|
||||
|
||||
public static int GetHeaderSize(PacketProperty property)
|
||||
{
|
||||
return HeaderSizes[(int)property];
|
||||
}
|
||||
|
||||
public int GetHeaderSize()
|
||||
{
|
||||
return HeaderSizes[RawData[0] & 0x1F];
|
||||
}
|
||||
|
||||
public bool Verify()
|
||||
{
|
||||
byte property = (byte)(RawData[0] & 0x1F);
|
||||
if (property >= PropertiesCount)
|
||||
return false;
|
||||
int headerSize = HeaderSizes[property];
|
||||
bool fragmented = (RawData[0] & 0x80) != 0;
|
||||
return Size >= headerSize && (!fragmented || Size >= headerSize + NetConstants.FragmentHeaderSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Transport/LiteNetLib Transport/LiteNetLib/NetPacket.cs.meta
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/NetPacket.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f943003c119d42349b59de2a831064b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1398
Transport/LiteNetLib Transport/LiteNetLib/NetPeer.cs
Normal file
1398
Transport/LiteNetLib Transport/LiteNetLib/NetPeer.cs
Normal file
File diff suppressed because it is too large
Load Diff
11
Transport/LiteNetLib Transport/LiteNetLib/NetPeer.cs.meta
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/NetPeer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9595868d4426b0447af6d21086c54949
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
81
Transport/LiteNetLib Transport/LiteNetLib/NetStatistics.cs
Normal file
81
Transport/LiteNetLib Transport/LiteNetLib/NetStatistics.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public sealed class NetStatistics
|
||||
{
|
||||
private long _packetsSent;
|
||||
private long _packetsReceived;
|
||||
private long _bytesSent;
|
||||
private long _bytesReceived;
|
||||
private long _packetLoss;
|
||||
|
||||
public long PacketsSent => Interlocked.Read(ref _packetsSent);
|
||||
public long PacketsReceived => Interlocked.Read(ref _packetsReceived);
|
||||
public long BytesSent => Interlocked.Read(ref _bytesSent);
|
||||
public long BytesReceived => Interlocked.Read(ref _bytesReceived);
|
||||
public long PacketLoss => Interlocked.Read(ref _packetLoss);
|
||||
|
||||
public long PacketLossPercent
|
||||
{
|
||||
get
|
||||
{
|
||||
long sent = PacketsSent, loss = PacketLoss;
|
||||
|
||||
return sent == 0 ? 0 : loss * 100 / sent;
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Interlocked.Exchange(ref _packetsSent, 0);
|
||||
Interlocked.Exchange(ref _packetsReceived, 0);
|
||||
Interlocked.Exchange(ref _bytesSent, 0);
|
||||
Interlocked.Exchange(ref _bytesReceived, 0);
|
||||
Interlocked.Exchange(ref _packetLoss, 0);
|
||||
}
|
||||
|
||||
public void IncrementPacketsSent()
|
||||
{
|
||||
Interlocked.Increment(ref _packetsSent);
|
||||
}
|
||||
|
||||
public void IncrementPacketsReceived()
|
||||
{
|
||||
Interlocked.Increment(ref _packetsReceived);
|
||||
}
|
||||
|
||||
public void AddBytesSent(long bytesSent)
|
||||
{
|
||||
Interlocked.Add(ref _bytesSent, bytesSent);
|
||||
}
|
||||
|
||||
public void AddBytesReceived(long bytesReceived)
|
||||
{
|
||||
Interlocked.Add(ref _bytesReceived, bytesReceived);
|
||||
}
|
||||
|
||||
public void IncrementPacketLoss()
|
||||
{
|
||||
Interlocked.Increment(ref _packetLoss);
|
||||
}
|
||||
|
||||
public void AddPacketLoss(long packetLoss)
|
||||
{
|
||||
Interlocked.Add(ref _packetLoss, packetLoss);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return
|
||||
string.Format(
|
||||
"BytesReceived: {0}\nPacketsReceived: {1}\nBytesSent: {2}\nPacketsSent: {3}\nPacketLoss: {4}\nPacketLossPercent: {5}\n",
|
||||
BytesReceived,
|
||||
PacketsReceived,
|
||||
BytesSent,
|
||||
PacketsSent,
|
||||
PacketLoss,
|
||||
PacketLossPercent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 319dbb0d3626df34ba9ae71a996b7273
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
190
Transport/LiteNetLib Transport/LiteNetLib/NetUtils.cs
Normal file
190
Transport/LiteNetLib Transport/LiteNetLib/NetUtils.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Address type that you want to receive from NetUtils.GetLocalIp method
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum LocalAddrType
|
||||
{
|
||||
IPv4 = 1,
|
||||
IPv6 = 2,
|
||||
All = IPv4 | IPv6
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Some specific network utilities
|
||||
/// </summary>
|
||||
public static class NetUtils
|
||||
{
|
||||
public static IPEndPoint MakeEndPoint(string hostStr, int port)
|
||||
{
|
||||
return new IPEndPoint(ResolveAddress(hostStr), port);
|
||||
}
|
||||
|
||||
public static IPAddress ResolveAddress(string hostStr)
|
||||
{
|
||||
if(hostStr == "localhost")
|
||||
return IPAddress.Loopback;
|
||||
|
||||
if (!IPAddress.TryParse(hostStr, out var ipAddress))
|
||||
{
|
||||
if (NetManager.IPv6Support)
|
||||
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetworkV6);
|
||||
if (ipAddress == null)
|
||||
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetwork);
|
||||
}
|
||||
if (ipAddress == null)
|
||||
throw new ArgumentException("Invalid address: " + hostStr);
|
||||
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public static IPAddress ResolveAddress(string hostStr, AddressFamily addressFamily)
|
||||
{
|
||||
IPAddress[] addresses = Dns.GetHostEntry(hostStr).AddressList;
|
||||
foreach (IPAddress ip in addresses)
|
||||
{
|
||||
if (ip.AddressFamily == addressFamily)
|
||||
{
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all local ip addresses
|
||||
/// </summary>
|
||||
/// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
|
||||
/// <returns>List with all local ip addresses</returns>
|
||||
public static List<string> GetLocalIpList(LocalAddrType addrType)
|
||||
{
|
||||
List<string> targetList = new List<string>();
|
||||
GetLocalIpList(targetList, addrType);
|
||||
return targetList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all local ip addresses (non alloc version)
|
||||
/// </summary>
|
||||
/// <param name="targetList">result list</param>
|
||||
/// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
|
||||
public static void GetLocalIpList(IList<string> targetList, LocalAddrType addrType)
|
||||
{
|
||||
bool ipv4 = (addrType & LocalAddrType.IPv4) == LocalAddrType.IPv4;
|
||||
bool ipv6 = (addrType & LocalAddrType.IPv6) == LocalAddrType.IPv6;
|
||||
try
|
||||
{
|
||||
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
//Skip loopback and disabled network interfaces
|
||||
if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
|
||||
ni.OperationalStatus != OperationalStatus.Up)
|
||||
continue;
|
||||
|
||||
var ipProps = ni.GetIPProperties();
|
||||
|
||||
//Skip address without gateway
|
||||
if (ipProps.GatewayAddresses.Count == 0)
|
||||
continue;
|
||||
|
||||
foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
|
||||
{
|
||||
var address = ip.Address;
|
||||
if ((ipv4 && address.AddressFamily == AddressFamily.InterNetwork) ||
|
||||
(ipv6 && address.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
targetList.Add(address.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
//Fallback mode (unity android)
|
||||
if (targetList.Count == 0)
|
||||
{
|
||||
IPAddress[] addresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
|
||||
foreach (IPAddress ip in addresses)
|
||||
{
|
||||
if((ipv4 && ip.AddressFamily == AddressFamily.InterNetwork) ||
|
||||
(ipv6 && ip.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
targetList.Add(ip.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
//ignored
|
||||
}
|
||||
|
||||
if (targetList.Count == 0)
|
||||
{
|
||||
if(ipv4)
|
||||
targetList.Add("127.0.0.1");
|
||||
if(ipv6)
|
||||
targetList.Add("::1");
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly List<string> IpList = new List<string>();
|
||||
/// <summary>
|
||||
/// Get first detected local ip address
|
||||
/// </summary>
|
||||
/// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
|
||||
/// <returns>IP address if available. Else - string.Empty</returns>
|
||||
public static string GetLocalIp(LocalAddrType addrType)
|
||||
{
|
||||
lock (IpList)
|
||||
{
|
||||
IpList.Clear();
|
||||
GetLocalIpList(IpList, addrType);
|
||||
return IpList.Count == 0 ? string.Empty : IpList[0];
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================================
|
||||
// Internal and debug log related stuff
|
||||
// ===========================================
|
||||
internal static void PrintInterfaceInfos()
|
||||
{
|
||||
NetDebug.WriteForce(NetLogLevel.Info, $"IPv6Support: { NetManager.IPv6Support}");
|
||||
try
|
||||
{
|
||||
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
|
||||
{
|
||||
if (ip.Address.AddressFamily == AddressFamily.InterNetwork ||
|
||||
ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
NetDebug.WriteForce(
|
||||
NetLogLevel.Info,
|
||||
$"Interface: {ni.Name}, Type: {ni.NetworkInterfaceType}, Ip: {ip.Address}, OpStatus: {ni.OperationalStatus}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
NetDebug.WriteForce(NetLogLevel.Info, $"Error while getting interface infos: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
internal static int RelativeSequenceNumber(int number, int expected)
|
||||
{
|
||||
return (number - expected + NetConstants.MaxSequence + NetConstants.HalfMaxSequence) % NetConstants.MaxSequence - NetConstants.HalfMaxSequence;
|
||||
}
|
||||
|
||||
internal static T[] AllocatePinnedUninitializedArray<T>(int count) where T : unmanaged
|
||||
{
|
||||
#if NET5_0_OR_GREATER || NET5_0
|
||||
return GC.AllocateUninitializedArray<T>(count, true);
|
||||
#else
|
||||
return new T[count];
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Transport/LiteNetLib Transport/LiteNetLib/NetUtils.cs.meta
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/NetUtils.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 06dc3a0405d23f34f83c3f19cd3733d2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
57
Transport/LiteNetLib Transport/LiteNetLib/PausedSocketFix.cs
Normal file
57
Transport/LiteNetLib Transport/LiteNetLib/PausedSocketFix.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
using System.Net;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public class PausedSocketFix
|
||||
{
|
||||
private readonly NetManager _netManager;
|
||||
private readonly IPAddress _ipv4;
|
||||
private readonly IPAddress _ipv6;
|
||||
private readonly int _port;
|
||||
private readonly bool _manualMode;
|
||||
private bool _initialized;
|
||||
|
||||
public PausedSocketFix(NetManager netManager, IPAddress ipv4, IPAddress ipv6, int port, bool manualMode)
|
||||
{
|
||||
_netManager = netManager;
|
||||
_ipv4 = ipv4;
|
||||
_ipv6 = ipv6;
|
||||
_port = port;
|
||||
_manualMode = manualMode;
|
||||
UnityEngine.Application.focusChanged += Application_focusChanged;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
public void Deinitialize()
|
||||
{
|
||||
if (_initialized)
|
||||
UnityEngine.Application.focusChanged -= Application_focusChanged;
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
private void Application_focusChanged(bool focused)
|
||||
{
|
||||
//If coming back into focus see if a reconnect is needed.
|
||||
if (focused)
|
||||
{
|
||||
//try reconnect
|
||||
if (!_initialized)
|
||||
return;
|
||||
//Was intentionally disconnected at some point.
|
||||
if (!_netManager.IsRunning)
|
||||
return;
|
||||
//Socket is in working state.
|
||||
if (_netManager.NotConnected == false)
|
||||
return;
|
||||
|
||||
//Socket isn't running but should be. Try to start again.
|
||||
if (!_netManager.Start(_ipv4, _ipv6, _port, _manualMode))
|
||||
{
|
||||
NetDebug.WriteError($"[S] Cannot restore connection. Ipv4 {_ipv4}, Ipv6 {_ipv6}, Port {_port}, ManualMode {_manualMode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab312f4da204c864cb3b3a1a010aba4b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
32
Transport/LiteNetLib Transport/LiteNetLib/PooledPacket.cs
Normal file
32
Transport/LiteNetLib Transport/LiteNetLib/PooledPacket.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace LiteNetLib
|
||||
{
|
||||
public readonly ref struct PooledPacket
|
||||
{
|
||||
internal readonly NetPacket _packet;
|
||||
internal readonly byte _channelNumber;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum data size that you can put into such packet
|
||||
/// </summary>
|
||||
public readonly int MaxUserDataSize;
|
||||
|
||||
/// <summary>
|
||||
/// Offset for user data when writing to Data array
|
||||
/// </summary>
|
||||
public readonly int UserDataOffset;
|
||||
|
||||
/// <summary>
|
||||
/// Raw packet data. Do not modify header! Use UserDataOffset as start point for your data
|
||||
/// </summary>
|
||||
public byte[] Data => _packet.RawData;
|
||||
|
||||
internal PooledPacket(NetPacket packet, int maxDataSize, byte channelNumber)
|
||||
{
|
||||
_packet = packet;
|
||||
UserDataOffset = _packet.GetHeaderSize();
|
||||
_packet.Size = UserDataOffset;
|
||||
MaxUserDataSize = maxDataSize - UserDataOffset;
|
||||
_channelNumber = channelNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb2cf9f0ef17bc84ba55d4d130e3ee98
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
337
Transport/LiteNetLib Transport/LiteNetLib/ReliableChannel.cs
Normal file
337
Transport/LiteNetLib Transport/LiteNetLib/ReliableChannel.cs
Normal file
@@ -0,0 +1,337 @@
|
||||
using System;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal sealed class ReliableChannel : BaseChannel
|
||||
{
|
||||
private struct PendingPacket
|
||||
{
|
||||
private NetPacket _packet;
|
||||
private long _timeStamp;
|
||||
private bool _isSent;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _packet == null ? "Empty" : _packet.Sequence.ToString();
|
||||
}
|
||||
|
||||
public void Init(NetPacket packet)
|
||||
{
|
||||
_packet = packet;
|
||||
_isSent = false;
|
||||
}
|
||||
|
||||
//Returns true if there is a pending packet inside
|
||||
public bool TrySend(long currentTime, NetPeer peer)
|
||||
{
|
||||
if (_packet == null)
|
||||
return false;
|
||||
|
||||
if (_isSent) //check send time
|
||||
{
|
||||
double resendDelay = peer.ResendDelay * TimeSpan.TicksPerMillisecond;
|
||||
double packetHoldTime = currentTime - _timeStamp;
|
||||
if (packetHoldTime < resendDelay)
|
||||
return true;
|
||||
NetDebug.Write($"[RC]Resend: {packetHoldTime} > {resendDelay}");
|
||||
}
|
||||
_timeStamp = currentTime;
|
||||
_isSent = true;
|
||||
peer.SendUserData(_packet);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Clear(NetPeer peer)
|
||||
{
|
||||
if (_packet != null)
|
||||
{
|
||||
peer.RecycleAndDeliver(_packet);
|
||||
_packet = null;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly NetPacket _outgoingAcks; //for send acks
|
||||
private readonly PendingPacket[] _pendingPackets; //for unacked packets and duplicates
|
||||
private readonly NetPacket[] _receivedPackets; //for order
|
||||
private readonly bool[] _earlyReceived; //for unordered
|
||||
|
||||
private int _localSeqence;
|
||||
private int _remoteSequence;
|
||||
private int _localWindowStart;
|
||||
private int _remoteWindowStart;
|
||||
|
||||
private bool _mustSendAcks;
|
||||
|
||||
private readonly DeliveryMethod _deliveryMethod;
|
||||
private readonly bool _ordered;
|
||||
private readonly int _windowSize;
|
||||
private const int BitsInByte = 8;
|
||||
private readonly byte _id;
|
||||
|
||||
public ReliableChannel(NetPeer peer, bool ordered, byte id) : base(peer)
|
||||
{
|
||||
_id = id;
|
||||
_windowSize = NetConstants.DefaultWindowSize;
|
||||
_ordered = ordered;
|
||||
_pendingPackets = new PendingPacket[_windowSize];
|
||||
for (int i = 0; i < _pendingPackets.Length; i++)
|
||||
_pendingPackets[i] = new PendingPacket();
|
||||
|
||||
if (_ordered)
|
||||
{
|
||||
_deliveryMethod = DeliveryMethod.ReliableOrdered;
|
||||
_receivedPackets = new NetPacket[_windowSize];
|
||||
}
|
||||
else
|
||||
{
|
||||
_deliveryMethod = DeliveryMethod.ReliableUnordered;
|
||||
_earlyReceived = new bool[_windowSize];
|
||||
}
|
||||
|
||||
_localWindowStart = 0;
|
||||
_localSeqence = 0;
|
||||
_remoteSequence = 0;
|
||||
_remoteWindowStart = 0;
|
||||
_outgoingAcks = new NetPacket(PacketProperty.Ack, (_windowSize - 1) / BitsInByte + 2) {ChannelId = id};
|
||||
}
|
||||
|
||||
//ProcessAck in packet
|
||||
private void ProcessAck(NetPacket packet)
|
||||
{
|
||||
if (packet.Size != _outgoingAcks.Size)
|
||||
{
|
||||
NetDebug.Write("[PA]Invalid acks packet size");
|
||||
return;
|
||||
}
|
||||
|
||||
ushort ackWindowStart = packet.Sequence;
|
||||
int windowRel = NetUtils.RelativeSequenceNumber(_localWindowStart, ackWindowStart);
|
||||
if (ackWindowStart >= NetConstants.MaxSequence || windowRel < 0)
|
||||
{
|
||||
NetDebug.Write("[PA]Bad window start");
|
||||
return;
|
||||
}
|
||||
|
||||
//check relevance
|
||||
if (windowRel >= _windowSize)
|
||||
{
|
||||
NetDebug.Write("[PA]Old acks");
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] acksData = packet.RawData;
|
||||
lock (_pendingPackets)
|
||||
{
|
||||
for (int pendingSeq = _localWindowStart;
|
||||
pendingSeq != _localSeqence;
|
||||
pendingSeq = (pendingSeq + 1) % NetConstants.MaxSequence)
|
||||
{
|
||||
int rel = NetUtils.RelativeSequenceNumber(pendingSeq, ackWindowStart);
|
||||
if (rel >= _windowSize)
|
||||
{
|
||||
NetDebug.Write("[PA]REL: " + rel);
|
||||
break;
|
||||
}
|
||||
|
||||
int pendingIdx = pendingSeq % _windowSize;
|
||||
int currentByte = NetConstants.ChanneledHeaderSize + pendingIdx / BitsInByte;
|
||||
int currentBit = pendingIdx % BitsInByte;
|
||||
if ((acksData[currentByte] & (1 << currentBit)) == 0)
|
||||
{
|
||||
if (Peer.NetManager.EnableStatistics)
|
||||
{
|
||||
Peer.Statistics.IncrementPacketLoss();
|
||||
Peer.NetManager.Statistics.IncrementPacketLoss();
|
||||
}
|
||||
|
||||
//Skip false ack
|
||||
NetDebug.Write($"[PA]False ack: {pendingSeq}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pendingSeq == _localWindowStart)
|
||||
{
|
||||
//Move window
|
||||
_localWindowStart = (_localWindowStart + 1) % NetConstants.MaxSequence;
|
||||
}
|
||||
|
||||
//clear packet
|
||||
if (_pendingPackets[pendingIdx].Clear(Peer))
|
||||
NetDebug.Write($"[PA]Removing reliableInOrder ack: {pendingSeq} - true");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool SendNextPackets()
|
||||
{
|
||||
if (_mustSendAcks)
|
||||
{
|
||||
_mustSendAcks = false;
|
||||
NetDebug.Write("[RR]SendAcks");
|
||||
lock(_outgoingAcks)
|
||||
Peer.SendUserData(_outgoingAcks);
|
||||
}
|
||||
|
||||
long currentTime = DateTime.UtcNow.Ticks;
|
||||
bool hasPendingPackets = false;
|
||||
|
||||
lock (_pendingPackets)
|
||||
{
|
||||
//get packets from queue
|
||||
lock (OutgoingQueue)
|
||||
{
|
||||
while (OutgoingQueue.Count > 0)
|
||||
{
|
||||
int relate = NetUtils.RelativeSequenceNumber(_localSeqence, _localWindowStart);
|
||||
if (relate >= _windowSize)
|
||||
break;
|
||||
|
||||
var netPacket = OutgoingQueue.Dequeue();
|
||||
netPacket.Sequence = (ushort) _localSeqence;
|
||||
netPacket.ChannelId = _id;
|
||||
_pendingPackets[_localSeqence % _windowSize].Init(netPacket);
|
||||
_localSeqence = (_localSeqence + 1) % NetConstants.MaxSequence;
|
||||
}
|
||||
}
|
||||
|
||||
//send
|
||||
for (int pendingSeq = _localWindowStart; pendingSeq != _localSeqence; pendingSeq = (pendingSeq + 1) % NetConstants.MaxSequence)
|
||||
{
|
||||
// Please note: TrySend is invoked on a mutable struct, it's important to not extract it into a variable here
|
||||
if (_pendingPackets[pendingSeq % _windowSize].TrySend(currentTime, Peer))
|
||||
hasPendingPackets = true;
|
||||
}
|
||||
}
|
||||
|
||||
return hasPendingPackets || _mustSendAcks || OutgoingQueue.Count > 0;
|
||||
}
|
||||
|
||||
//Process incoming packet
|
||||
public override bool ProcessPacket(NetPacket packet)
|
||||
{
|
||||
if (packet.Property == PacketProperty.Ack)
|
||||
{
|
||||
ProcessAck(packet);
|
||||
return false;
|
||||
}
|
||||
int seq = packet.Sequence;
|
||||
if (seq >= NetConstants.MaxSequence)
|
||||
{
|
||||
NetDebug.Write("[RR]Bad sequence");
|
||||
return false;
|
||||
}
|
||||
|
||||
int relate = NetUtils.RelativeSequenceNumber(seq, _remoteWindowStart);
|
||||
int relateSeq = NetUtils.RelativeSequenceNumber(seq, _remoteSequence);
|
||||
|
||||
if (relateSeq > _windowSize)
|
||||
{
|
||||
NetDebug.Write("[RR]Bad sequence");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Drop bad packets
|
||||
if (relate < 0)
|
||||
{
|
||||
//Too old packet doesn't ack
|
||||
NetDebug.Write("[RR]ReliableInOrder too old");
|
||||
return false;
|
||||
}
|
||||
if (relate >= _windowSize * 2)
|
||||
{
|
||||
//Some very new packet
|
||||
NetDebug.Write("[RR]ReliableInOrder too new");
|
||||
return false;
|
||||
}
|
||||
|
||||
//If very new - move window
|
||||
int ackIdx;
|
||||
int ackByte;
|
||||
int ackBit;
|
||||
lock (_outgoingAcks)
|
||||
{
|
||||
if (relate >= _windowSize)
|
||||
{
|
||||
//New window position
|
||||
int newWindowStart = (_remoteWindowStart + relate - _windowSize + 1) % NetConstants.MaxSequence;
|
||||
_outgoingAcks.Sequence = (ushort) newWindowStart;
|
||||
|
||||
//Clean old data
|
||||
while (_remoteWindowStart != newWindowStart)
|
||||
{
|
||||
ackIdx = _remoteWindowStart % _windowSize;
|
||||
ackByte = NetConstants.ChanneledHeaderSize + ackIdx / BitsInByte;
|
||||
ackBit = ackIdx % BitsInByte;
|
||||
_outgoingAcks.RawData[ackByte] &= (byte) ~(1 << ackBit);
|
||||
_remoteWindowStart = (_remoteWindowStart + 1) % NetConstants.MaxSequence;
|
||||
}
|
||||
}
|
||||
|
||||
//Final stage - process valid packet
|
||||
//trigger acks send
|
||||
_mustSendAcks = true;
|
||||
|
||||
ackIdx = seq % _windowSize;
|
||||
ackByte = NetConstants.ChanneledHeaderSize + ackIdx / BitsInByte;
|
||||
ackBit = ackIdx % BitsInByte;
|
||||
if ((_outgoingAcks.RawData[ackByte] & (1 << ackBit)) != 0)
|
||||
{
|
||||
NetDebug.Write("[RR]ReliableInOrder duplicate");
|
||||
//because _mustSendAcks == true
|
||||
AddToPeerChannelSendQueue();
|
||||
return false;
|
||||
}
|
||||
|
||||
//save ack
|
||||
_outgoingAcks.RawData[ackByte] |= (byte) (1 << ackBit);
|
||||
}
|
||||
|
||||
AddToPeerChannelSendQueue();
|
||||
|
||||
//detailed check
|
||||
if (seq == _remoteSequence)
|
||||
{
|
||||
NetDebug.Write("[RR]ReliableInOrder packet succes");
|
||||
Peer.AddReliablePacket(_deliveryMethod, packet);
|
||||
_remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
|
||||
|
||||
if (_ordered)
|
||||
{
|
||||
NetPacket p;
|
||||
while ((p = _receivedPackets[_remoteSequence % _windowSize]) != null)
|
||||
{
|
||||
//process holden packet
|
||||
_receivedPackets[_remoteSequence % _windowSize] = null;
|
||||
Peer.AddReliablePacket(_deliveryMethod, p);
|
||||
_remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (_earlyReceived[_remoteSequence % _windowSize])
|
||||
{
|
||||
//process early packet
|
||||
_earlyReceived[_remoteSequence % _windowSize] = false;
|
||||
_remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//holden packet
|
||||
if (_ordered)
|
||||
{
|
||||
_receivedPackets[ackIdx] = packet;
|
||||
}
|
||||
else
|
||||
{
|
||||
_earlyReceived[ackIdx] = true;
|
||||
Peer.AddReliablePacket(_deliveryMethod, packet);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ff71f2b64d81a64ea6232007c316adf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
114
Transport/LiteNetLib Transport/LiteNetLib/SequencedChannel.cs
Normal file
114
Transport/LiteNetLib Transport/LiteNetLib/SequencedChannel.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal sealed class SequencedChannel : BaseChannel
|
||||
{
|
||||
private int _localSequence;
|
||||
private ushort _remoteSequence;
|
||||
private readonly bool _reliable;
|
||||
private NetPacket _lastPacket;
|
||||
private readonly NetPacket _ackPacket;
|
||||
private bool _mustSendAck;
|
||||
private readonly byte _id;
|
||||
private long _lastPacketSendTime;
|
||||
|
||||
public SequencedChannel(NetPeer peer, bool reliable, byte id) : base(peer)
|
||||
{
|
||||
_id = id;
|
||||
_reliable = reliable;
|
||||
if (_reliable)
|
||||
_ackPacket = new NetPacket(PacketProperty.Ack, 0) {ChannelId = id};
|
||||
}
|
||||
|
||||
protected override bool SendNextPackets()
|
||||
{
|
||||
if (_reliable && OutgoingQueue.Count == 0)
|
||||
{
|
||||
long currentTime = DateTime.UtcNow.Ticks;
|
||||
long packetHoldTime = currentTime - _lastPacketSendTime;
|
||||
if (packetHoldTime >= Peer.ResendDelay * TimeSpan.TicksPerMillisecond)
|
||||
{
|
||||
var packet = _lastPacket;
|
||||
if (packet != null)
|
||||
{
|
||||
_lastPacketSendTime = currentTime;
|
||||
Peer.SendUserData(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lock (OutgoingQueue)
|
||||
{
|
||||
while (OutgoingQueue.Count > 0)
|
||||
{
|
||||
NetPacket packet = OutgoingQueue.Dequeue();
|
||||
_localSequence = (_localSequence + 1) % NetConstants.MaxSequence;
|
||||
packet.Sequence = (ushort)_localSequence;
|
||||
packet.ChannelId = _id;
|
||||
Peer.SendUserData(packet);
|
||||
|
||||
if (_reliable && OutgoingQueue.Count == 0)
|
||||
{
|
||||
_lastPacketSendTime = DateTime.UtcNow.Ticks;
|
||||
_lastPacket = packet;
|
||||
}
|
||||
else
|
||||
{
|
||||
Peer.NetManager.PoolRecycle(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_reliable && _mustSendAck)
|
||||
{
|
||||
_mustSendAck = false;
|
||||
_ackPacket.Sequence = _remoteSequence;
|
||||
Peer.SendUserData(_ackPacket);
|
||||
}
|
||||
|
||||
return _lastPacket != null;
|
||||
}
|
||||
|
||||
public override bool ProcessPacket(NetPacket packet)
|
||||
{
|
||||
if (packet.IsFragmented)
|
||||
return false;
|
||||
if (packet.Property == PacketProperty.Ack)
|
||||
{
|
||||
if (_reliable && _lastPacket != null && packet.Sequence == _lastPacket.Sequence)
|
||||
_lastPacket = null;
|
||||
return false;
|
||||
}
|
||||
int relative = NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence);
|
||||
bool packetProcessed = false;
|
||||
if (packet.Sequence < NetConstants.MaxSequence && relative > 0)
|
||||
{
|
||||
if (Peer.NetManager.EnableStatistics)
|
||||
{
|
||||
Peer.Statistics.AddPacketLoss(relative - 1);
|
||||
Peer.NetManager.Statistics.AddPacketLoss(relative - 1);
|
||||
}
|
||||
|
||||
_remoteSequence = packet.Sequence;
|
||||
Peer.NetManager.CreateReceiveEvent(
|
||||
packet,
|
||||
_reliable ? DeliveryMethod.ReliableSequenced : DeliveryMethod.Sequenced,
|
||||
(byte)(packet.ChannelId / NetConstants.ChannelTypeCount),
|
||||
NetConstants.ChanneledHeaderSize,
|
||||
Peer);
|
||||
packetProcessed = true;
|
||||
}
|
||||
|
||||
if (_reliable)
|
||||
{
|
||||
_mustSendAck = true;
|
||||
AddToPeerChannelSendQueue();
|
||||
}
|
||||
|
||||
return packetProcessed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 424b628addd10374abcf8b934ae19768
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Transport/LiteNetLib Transport/LiteNetLib/Utils.meta
Normal file
8
Transport/LiteNetLib Transport/LiteNetLib/Utils.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9e74c8c18ebed5d4a9ba20c8623fafaa
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
150
Transport/LiteNetLib Transport/LiteNetLib/Utils/CRC32C.cs
Normal file
150
Transport/LiteNetLib Transport/LiteNetLib/Utils/CRC32C.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
#if NETCOREAPP3_0_OR_GREATER || NETCOREAPP3_1 || NET5_0
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
#endif
|
||||
#if NET5_0_OR_GREATER || NET5_0
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
#endif
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
//Implementation from Crc32.NET
|
||||
public static class CRC32C
|
||||
{
|
||||
public const int ChecksumSize = 4;
|
||||
private const uint Poly = 0x82F63B78u;
|
||||
private static readonly uint[] Table;
|
||||
|
||||
static CRC32C()
|
||||
{
|
||||
#if NETCOREAPP3_0_OR_GREATER || NETCOREAPP3_1 || NET5_0
|
||||
if (Sse42.IsSupported)
|
||||
return;
|
||||
#endif
|
||||
#if NET5_0_OR_GREATER || NET5_0
|
||||
if (Crc32.IsSupported)
|
||||
return;
|
||||
#endif
|
||||
Table = NetUtils.AllocatePinnedUninitializedArray<uint>(16 * 256);
|
||||
for (uint i = 0; i < 256; i++)
|
||||
{
|
||||
uint res = i;
|
||||
for (int t = 0; t < 16; t++)
|
||||
{
|
||||
for (int k = 0; k < 8; k++)
|
||||
res = (res & 1) == 1 ? Poly ^ (res >> 1) : (res >> 1);
|
||||
Table[t * 256 + i] = res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute CRC32C for data
|
||||
/// </summary>
|
||||
/// <param name="input">input data</param>
|
||||
/// <param name="offset">offset</param>
|
||||
/// <param name="length">length</param>
|
||||
/// <returns>CRC32C checksum</returns>
|
||||
public static uint Compute(byte[] input, int offset, int length)
|
||||
{
|
||||
uint crcLocal = uint.MaxValue;
|
||||
#if NETCOREAPP3_0_OR_GREATER || NETCOREAPP3_1 || NET5_0
|
||||
if (Sse42.IsSupported)
|
||||
{
|
||||
var data = new ReadOnlySpan<byte>(input, offset, length);
|
||||
int processed = 0;
|
||||
if (Sse42.X64.IsSupported && data.Length > sizeof(ulong))
|
||||
{
|
||||
processed = data.Length / sizeof(ulong) * sizeof(ulong);
|
||||
var ulongs = MemoryMarshal.Cast<byte, ulong>(data.Slice(0, processed));
|
||||
ulong crclong = crcLocal;
|
||||
for (int i = 0; i < ulongs.Length; i++)
|
||||
{
|
||||
crclong = Sse42.X64.Crc32(crclong, ulongs[i]);
|
||||
}
|
||||
|
||||
crcLocal = (uint)crclong;
|
||||
}
|
||||
else if (data.Length > sizeof(uint))
|
||||
{
|
||||
processed = data.Length / sizeof(uint) * sizeof(uint);
|
||||
var uints = MemoryMarshal.Cast<byte, uint>(data.Slice(0, processed));
|
||||
for (int i = 0; i < uints.Length; i++)
|
||||
{
|
||||
crcLocal = Sse42.Crc32(crcLocal, uints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = processed; i < data.Length; i++)
|
||||
{
|
||||
crcLocal = Sse42.Crc32(crcLocal, data[i]);
|
||||
}
|
||||
|
||||
return crcLocal ^ uint.MaxValue;
|
||||
}
|
||||
#endif
|
||||
#if NET5_0_OR_GREATER || NET5_0
|
||||
if (Crc32.IsSupported)
|
||||
{
|
||||
var data = new ReadOnlySpan<byte>(input, offset, length);
|
||||
int processed = 0;
|
||||
if (Crc32.Arm64.IsSupported && data.Length > sizeof(ulong))
|
||||
{
|
||||
processed = data.Length / sizeof(ulong) * sizeof(ulong);
|
||||
var ulongs = MemoryMarshal.Cast<byte, ulong>(data.Slice(0, processed));
|
||||
for (int i = 0; i < ulongs.Length; i++)
|
||||
{
|
||||
crcLocal = Crc32.Arm64.ComputeCrc32C(crcLocal, ulongs[i]);
|
||||
}
|
||||
}
|
||||
else if (data.Length > sizeof(uint))
|
||||
{
|
||||
processed = data.Length / sizeof(uint) * sizeof(uint);
|
||||
var uints = MemoryMarshal.Cast<byte, uint>(data.Slice(0, processed));
|
||||
for (int i = 0; i < uints.Length; i++)
|
||||
{
|
||||
crcLocal = Crc32.ComputeCrc32C(crcLocal, uints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = processed; i < data.Length; i++)
|
||||
{
|
||||
crcLocal = Crc32.ComputeCrc32C(crcLocal, data[i]);
|
||||
}
|
||||
|
||||
return crcLocal ^ uint.MaxValue;
|
||||
}
|
||||
#endif
|
||||
while (length >= 16)
|
||||
{
|
||||
var a = Table[(3 * 256) + input[offset + 12]]
|
||||
^ Table[(2 * 256) + input[offset + 13]]
|
||||
^ Table[(1 * 256) + input[offset + 14]]
|
||||
^ Table[(0 * 256) + input[offset + 15]];
|
||||
|
||||
var b = Table[(7 * 256) + input[offset + 8]]
|
||||
^ Table[(6 * 256) + input[offset + 9]]
|
||||
^ Table[(5 * 256) + input[offset + 10]]
|
||||
^ Table[(4 * 256) + input[offset + 11]];
|
||||
|
||||
var c = Table[(11 * 256) + input[offset + 4]]
|
||||
^ Table[(10 * 256) + input[offset + 5]]
|
||||
^ Table[(9 * 256) + input[offset + 6]]
|
||||
^ Table[(8 * 256) + input[offset + 7]];
|
||||
|
||||
var d = Table[(15 * 256) + ((byte)crcLocal ^ input[offset])]
|
||||
^ Table[(14 * 256) + ((byte)(crcLocal >> 8) ^ input[offset + 1])]
|
||||
^ Table[(13 * 256) + ((byte)(crcLocal >> 16) ^ input[offset + 2])]
|
||||
^ Table[(12 * 256) + ((crcLocal >> 24) ^ input[offset + 3])];
|
||||
|
||||
crcLocal = d ^ c ^ b ^ a;
|
||||
offset += 16;
|
||||
length -= 16;
|
||||
}
|
||||
while (--length >= 0)
|
||||
crcLocal = Table[(byte)(crcLocal ^ input[offset++])] ^ crcLocal >> 8;
|
||||
return crcLocal ^ uint.MaxValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76883c4cc6dbb0a43b3a7755fd827824
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public static class FastBitConverter
|
||||
{
|
||||
#if (LITENETLIB_UNSAFE || LITENETLIB_UNSAFELIB || NETCOREAPP3_1 || NET5_0 || NETCOREAPP3_0_OR_GREATER) && !BIGENDIAN
|
||||
#if LITENETLIB_UNSAFE
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void GetBytes<T>(byte[] bytes, int startIndex, T value) where T : unmanaged
|
||||
{
|
||||
int size = sizeof(T);
|
||||
if (bytes.Length < startIndex + size)
|
||||
ThrowIndexOutOfRangeException();
|
||||
#if LITENETLIB_UNSAFELIB || NETCOREAPP3_1 || NET5_0 || NETCOREAPP3_0_OR_GREATER
|
||||
Unsafe.As<byte, T>(ref bytes[startIndex]) = value;
|
||||
#else
|
||||
fixed (byte* ptr = &bytes[startIndex])
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
// On some android systems, assigning *(T*)ptr throws a NRE if
|
||||
// the ptr isn't aligned (i.e. if Position is 1,2,3,5, etc.).
|
||||
// Here we have to use memcpy.
|
||||
//
|
||||
// => we can't get a pointer of a struct in C# without
|
||||
// marshalling allocations
|
||||
// => instead, we stack allocate an array of type T and use that
|
||||
// => stackalloc avoids GC and is very fast. it only works for
|
||||
// value types, but all blittable types are anyway.
|
||||
T* valueBuffer = stackalloc T[1] { value };
|
||||
UnsafeUtility.MemCpy(ptr, valueBuffer, size);
|
||||
#else
|
||||
*(T*)ptr = value;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes<T>(byte[] bytes, int startIndex, T value) where T : unmanaged
|
||||
{
|
||||
if (bytes.Length < startIndex + Unsafe.SizeOf<T>())
|
||||
ThrowIndexOutOfRangeException();
|
||||
Unsafe.As<byte, T>(ref bytes[startIndex]) = value;
|
||||
}
|
||||
#endif
|
||||
|
||||
private static void ThrowIndexOutOfRangeException() => throw new IndexOutOfRangeException();
|
||||
#else
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct ConverterHelperDouble
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public ulong Along;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public double Adouble;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct ConverterHelperFloat
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public int Aint;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public float Afloat;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void WriteLittleEndian(byte[] buffer, int offset, ulong data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 7] = (byte)(data);
|
||||
buffer[offset + 6] = (byte)(data >> 8);
|
||||
buffer[offset + 5] = (byte)(data >> 16);
|
||||
buffer[offset + 4] = (byte)(data >> 24);
|
||||
buffer[offset + 3] = (byte)(data >> 32);
|
||||
buffer[offset + 2] = (byte)(data >> 40);
|
||||
buffer[offset + 1] = (byte)(data >> 48);
|
||||
buffer[offset ] = (byte)(data >> 56);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
buffer[offset + 2] = (byte)(data >> 16);
|
||||
buffer[offset + 3] = (byte)(data >> 24);
|
||||
buffer[offset + 4] = (byte)(data >> 32);
|
||||
buffer[offset + 5] = (byte)(data >> 40);
|
||||
buffer[offset + 6] = (byte)(data >> 48);
|
||||
buffer[offset + 7] = (byte)(data >> 56);
|
||||
#endif
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void WriteLittleEndian(byte[] buffer, int offset, int data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 3] = (byte)(data);
|
||||
buffer[offset + 2] = (byte)(data >> 8);
|
||||
buffer[offset + 1] = (byte)(data >> 16);
|
||||
buffer[offset ] = (byte)(data >> 24);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
buffer[offset + 2] = (byte)(data >> 16);
|
||||
buffer[offset + 3] = (byte)(data >> 24);
|
||||
#endif
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteLittleEndian(byte[] buffer, int offset, short data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 1] = (byte)(data);
|
||||
buffer[offset ] = (byte)(data >> 8);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
#endif
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, double value)
|
||||
{
|
||||
ConverterHelperDouble ch = new ConverterHelperDouble { Adouble = value };
|
||||
WriteLittleEndian(bytes, startIndex, ch.Along);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, float value)
|
||||
{
|
||||
ConverterHelperFloat ch = new ConverterHelperFloat { Afloat = value };
|
||||
WriteLittleEndian(bytes, startIndex, ch.Aint);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, short value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, ushort value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (short)value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, int value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, uint value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (int)value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, long value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (ulong)value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void GetBytes(byte[] bytes, int startIndex, ulong value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62e022071d6c4284986276c32b92b32c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public interface INetSerializable
|
||||
{
|
||||
void Serialize(NetDataWriter writer);
|
||||
void Deserialize(NetDataReader reader);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 819c3f8d629972f49bdf77b1fc3d1c2e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
673
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetDataReader.cs
Normal file
673
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetDataReader.cs
Normal file
@@ -0,0 +1,673 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public class NetDataReader
|
||||
{
|
||||
protected byte[] _data;
|
||||
protected int _position;
|
||||
protected int _dataSize;
|
||||
private int _offset;
|
||||
|
||||
public byte[] RawData
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _data;
|
||||
}
|
||||
public int RawDataSize
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _dataSize;
|
||||
}
|
||||
public int UserDataOffset
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _offset;
|
||||
}
|
||||
public int UserDataSize
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _dataSize - _offset;
|
||||
}
|
||||
public bool IsNull
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _data == null;
|
||||
}
|
||||
public int Position
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _position;
|
||||
}
|
||||
public bool EndOfData
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _position == _dataSize;
|
||||
}
|
||||
public int AvailableBytes
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _dataSize - _position;
|
||||
}
|
||||
|
||||
public void SkipBytes(int count)
|
||||
{
|
||||
_position += count;
|
||||
}
|
||||
|
||||
public void SetPosition(int position)
|
||||
{
|
||||
_position = position;
|
||||
}
|
||||
|
||||
public void SetSource(NetDataWriter dataWriter)
|
||||
{
|
||||
_data = dataWriter.Data;
|
||||
_position = 0;
|
||||
_offset = 0;
|
||||
_dataSize = dataWriter.Length;
|
||||
}
|
||||
|
||||
public void SetSource(byte[] source)
|
||||
{
|
||||
_data = source;
|
||||
_position = 0;
|
||||
_offset = 0;
|
||||
_dataSize = source.Length;
|
||||
}
|
||||
|
||||
public void SetSource(byte[] source, int offset, int maxSize)
|
||||
{
|
||||
_data = source;
|
||||
_position = offset;
|
||||
_offset = offset;
|
||||
_dataSize = maxSize;
|
||||
}
|
||||
|
||||
public NetDataReader()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public NetDataReader(NetDataWriter writer)
|
||||
{
|
||||
SetSource(writer);
|
||||
}
|
||||
|
||||
public NetDataReader(byte[] source)
|
||||
{
|
||||
SetSource(source);
|
||||
}
|
||||
|
||||
public NetDataReader(byte[] source, int offset, int maxSize)
|
||||
{
|
||||
SetSource(source, offset, maxSize);
|
||||
}
|
||||
|
||||
#region GetMethods
|
||||
public IPEndPoint GetNetEndPoint()
|
||||
{
|
||||
string host = GetString(1000);
|
||||
int port = GetInt();
|
||||
return NetUtils.MakeEndPoint(host, port);
|
||||
}
|
||||
|
||||
public byte GetByte()
|
||||
{
|
||||
byte res = _data[_position];
|
||||
_position++;
|
||||
return res;
|
||||
}
|
||||
|
||||
public sbyte GetSByte()
|
||||
{
|
||||
return (sbyte)GetByte();
|
||||
}
|
||||
|
||||
public T[] GetArray<T>(ushort size)
|
||||
{
|
||||
ushort length = BitConverter.ToUInt16(_data, _position);
|
||||
_position += 2;
|
||||
T[] result = new T[length];
|
||||
length *= size;
|
||||
Buffer.BlockCopy(_data, _position, result, 0, length);
|
||||
_position += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool[] GetBoolArray()
|
||||
{
|
||||
return GetArray<bool>(1);
|
||||
}
|
||||
|
||||
public ushort[] GetUShortArray()
|
||||
{
|
||||
return GetArray<ushort>(2);
|
||||
}
|
||||
|
||||
public short[] GetShortArray()
|
||||
{
|
||||
return GetArray<short>(2);
|
||||
}
|
||||
|
||||
public int[] GetIntArray()
|
||||
{
|
||||
return GetArray<int>(4);
|
||||
}
|
||||
|
||||
public uint[] GetUIntArray()
|
||||
{
|
||||
return GetArray<uint>(4);
|
||||
}
|
||||
|
||||
public float[] GetFloatArray()
|
||||
{
|
||||
return GetArray<float>(4);
|
||||
}
|
||||
|
||||
public double[] GetDoubleArray()
|
||||
{
|
||||
return GetArray<double>(8);
|
||||
}
|
||||
|
||||
public long[] GetLongArray()
|
||||
{
|
||||
return GetArray<long>(8);
|
||||
}
|
||||
|
||||
public ulong[] GetULongArray()
|
||||
{
|
||||
return GetArray<ulong>(8);
|
||||
}
|
||||
|
||||
public string[] GetStringArray()
|
||||
{
|
||||
ushort length = GetUShort();
|
||||
string[] arr = new string[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
arr[i] = GetString();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Note that "maxStringLength" only limits the number of characters in a string, not its size in bytes.
|
||||
/// Strings that exceed this parameter are returned as empty
|
||||
/// </summary>
|
||||
public string[] GetStringArray(int maxStringLength)
|
||||
{
|
||||
ushort length = GetUShort();
|
||||
string[] arr = new string[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
arr[i] = GetString(maxStringLength);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public bool GetBool()
|
||||
{
|
||||
return GetByte() == 1;
|
||||
}
|
||||
|
||||
public char GetChar()
|
||||
{
|
||||
return (char)GetUShort();
|
||||
}
|
||||
|
||||
public ushort GetUShort()
|
||||
{
|
||||
ushort result = BitConverter.ToUInt16(_data, _position);
|
||||
_position += 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
public short GetShort()
|
||||
{
|
||||
short result = BitConverter.ToInt16(_data, _position);
|
||||
_position += 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
public long GetLong()
|
||||
{
|
||||
long result = BitConverter.ToInt64(_data, _position);
|
||||
_position += 8;
|
||||
return result;
|
||||
}
|
||||
|
||||
public ulong GetULong()
|
||||
{
|
||||
ulong result = BitConverter.ToUInt64(_data, _position);
|
||||
_position += 8;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int GetInt()
|
||||
{
|
||||
int result = BitConverter.ToInt32(_data, _position);
|
||||
_position += 4;
|
||||
return result;
|
||||
}
|
||||
|
||||
public uint GetUInt()
|
||||
{
|
||||
uint result = BitConverter.ToUInt32(_data, _position);
|
||||
_position += 4;
|
||||
return result;
|
||||
}
|
||||
|
||||
public float GetFloat()
|
||||
{
|
||||
float result = BitConverter.ToSingle(_data, _position);
|
||||
_position += 4;
|
||||
return result;
|
||||
}
|
||||
|
||||
public double GetDouble()
|
||||
{
|
||||
double result = BitConverter.ToDouble(_data, _position);
|
||||
_position += 8;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Note that "maxLength" only limits the number of characters in a string, not its size in bytes.
|
||||
/// </summary>
|
||||
/// <returns>"string.Empty" if value > "maxLength"</returns>
|
||||
public string GetString(int maxLength)
|
||||
{
|
||||
ushort size = GetUShort();
|
||||
if (size == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int actualSize = size - 1;
|
||||
if (actualSize >= NetDataWriter.StringBufferMaxLength)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ArraySegment<byte> data = GetBytesSegment(actualSize);
|
||||
|
||||
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ?
|
||||
string.Empty :
|
||||
NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
|
||||
}
|
||||
|
||||
public string GetString()
|
||||
{
|
||||
ushort size = GetUShort();
|
||||
if (size == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int actualSize = size - 1;
|
||||
if (actualSize >= NetDataWriter.StringBufferMaxLength)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ArraySegment<byte> data = GetBytesSegment(actualSize);
|
||||
|
||||
return NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
|
||||
}
|
||||
|
||||
public ArraySegment<byte> GetBytesSegment(int count)
|
||||
{
|
||||
ArraySegment<byte> segment = new ArraySegment<byte>(_data, _position, count);
|
||||
_position += count;
|
||||
return segment;
|
||||
}
|
||||
|
||||
public ArraySegment<byte> GetRemainingBytesSegment()
|
||||
{
|
||||
ArraySegment<byte> segment = new ArraySegment<byte>(_data, _position, AvailableBytes);
|
||||
_position = _data.Length;
|
||||
return segment;
|
||||
}
|
||||
|
||||
public T Get<T>() where T : struct, INetSerializable
|
||||
{
|
||||
var obj = default(T);
|
||||
obj.Deserialize(this);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public T Get<T>(Func<T> constructor) where T : class, INetSerializable
|
||||
{
|
||||
var obj = constructor();
|
||||
obj.Deserialize(this);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public byte[] GetRemainingBytes()
|
||||
{
|
||||
byte[] outgoingData = new byte[AvailableBytes];
|
||||
Buffer.BlockCopy(_data, _position, outgoingData, 0, AvailableBytes);
|
||||
_position = _data.Length;
|
||||
return outgoingData;
|
||||
}
|
||||
|
||||
public void GetBytes(byte[] destination, int start, int count)
|
||||
{
|
||||
Buffer.BlockCopy(_data, _position, destination, start, count);
|
||||
_position += count;
|
||||
}
|
||||
|
||||
public void GetBytes(byte[] destination, int count)
|
||||
{
|
||||
Buffer.BlockCopy(_data, _position, destination, 0, count);
|
||||
_position += count;
|
||||
}
|
||||
|
||||
public sbyte[] GetSBytesWithLength()
|
||||
{
|
||||
return GetArray<sbyte>(1);
|
||||
}
|
||||
|
||||
public byte[] GetBytesWithLength()
|
||||
{
|
||||
return GetArray<byte>(1);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region PeekMethods
|
||||
|
||||
public byte PeekByte()
|
||||
{
|
||||
return _data[_position];
|
||||
}
|
||||
|
||||
public sbyte PeekSByte()
|
||||
{
|
||||
return (sbyte)_data[_position];
|
||||
}
|
||||
|
||||
public bool PeekBool()
|
||||
{
|
||||
return _data[_position] == 1;
|
||||
}
|
||||
|
||||
public char PeekChar()
|
||||
{
|
||||
return (char)PeekUShort();
|
||||
}
|
||||
|
||||
public ushort PeekUShort()
|
||||
{
|
||||
return BitConverter.ToUInt16(_data, _position);
|
||||
}
|
||||
|
||||
public short PeekShort()
|
||||
{
|
||||
return BitConverter.ToInt16(_data, _position);
|
||||
}
|
||||
|
||||
public long PeekLong()
|
||||
{
|
||||
return BitConverter.ToInt64(_data, _position);
|
||||
}
|
||||
|
||||
public ulong PeekULong()
|
||||
{
|
||||
return BitConverter.ToUInt64(_data, _position);
|
||||
}
|
||||
|
||||
public int PeekInt()
|
||||
{
|
||||
return BitConverter.ToInt32(_data, _position);
|
||||
}
|
||||
|
||||
public uint PeekUInt()
|
||||
{
|
||||
return BitConverter.ToUInt32(_data, _position);
|
||||
}
|
||||
|
||||
public float PeekFloat()
|
||||
{
|
||||
return BitConverter.ToSingle(_data, _position);
|
||||
}
|
||||
|
||||
public double PeekDouble()
|
||||
{
|
||||
return BitConverter.ToDouble(_data, _position);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Note that "maxLength" only limits the number of characters in a string, not its size in bytes.
|
||||
/// </summary>
|
||||
public string PeekString(int maxLength)
|
||||
{
|
||||
ushort size = PeekUShort();
|
||||
if (size == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int actualSize = size - 1;
|
||||
if (actualSize >= NetDataWriter.StringBufferMaxLength)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(_data, _position + 2, actualSize) > maxLength) ?
|
||||
string.Empty :
|
||||
NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
|
||||
}
|
||||
|
||||
public string PeekString()
|
||||
{
|
||||
ushort size = PeekUShort();
|
||||
if (size == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int actualSize = size - 1;
|
||||
if (actualSize >= NetDataWriter.StringBufferMaxLength)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region TryGetMethods
|
||||
public bool TryGetByte(out byte result)
|
||||
{
|
||||
if (AvailableBytes >= 1)
|
||||
{
|
||||
result = GetByte();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetSByte(out sbyte result)
|
||||
{
|
||||
if (AvailableBytes >= 1)
|
||||
{
|
||||
result = GetSByte();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetBool(out bool result)
|
||||
{
|
||||
if (AvailableBytes >= 1)
|
||||
{
|
||||
result = GetBool();
|
||||
return true;
|
||||
}
|
||||
result = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetChar(out char result)
|
||||
{
|
||||
if (!TryGetUShort(out ushort uShortValue))
|
||||
{
|
||||
result = '\0';
|
||||
return false;
|
||||
}
|
||||
result = (char)uShortValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetShort(out short result)
|
||||
{
|
||||
if (AvailableBytes >= 2)
|
||||
{
|
||||
result = GetShort();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetUShort(out ushort result)
|
||||
{
|
||||
if (AvailableBytes >= 2)
|
||||
{
|
||||
result = GetUShort();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetInt(out int result)
|
||||
{
|
||||
if (AvailableBytes >= 4)
|
||||
{
|
||||
result = GetInt();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetUInt(out uint result)
|
||||
{
|
||||
if (AvailableBytes >= 4)
|
||||
{
|
||||
result = GetUInt();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetLong(out long result)
|
||||
{
|
||||
if (AvailableBytes >= 8)
|
||||
{
|
||||
result = GetLong();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetULong(out ulong result)
|
||||
{
|
||||
if (AvailableBytes >= 8)
|
||||
{
|
||||
result = GetULong();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetFloat(out float result)
|
||||
{
|
||||
if (AvailableBytes >= 4)
|
||||
{
|
||||
result = GetFloat();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetDouble(out double result)
|
||||
{
|
||||
if (AvailableBytes >= 8)
|
||||
{
|
||||
result = GetDouble();
|
||||
return true;
|
||||
}
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetString(out string result)
|
||||
{
|
||||
if (AvailableBytes >= 2)
|
||||
{
|
||||
ushort strSize = PeekUShort();
|
||||
if (AvailableBytes >= strSize + 1)
|
||||
{
|
||||
result = GetString();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetStringArray(out string[] result)
|
||||
{
|
||||
if (!TryGetUShort(out ushort strArrayLength)) {
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new string[strArrayLength];
|
||||
for (int i = 0; i < strArrayLength; i++)
|
||||
{
|
||||
if (!TryGetString(out result[i]))
|
||||
{
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetBytesWithLength(out byte[] result)
|
||||
{
|
||||
if (AvailableBytes >= 2)
|
||||
{
|
||||
ushort length = PeekUShort();
|
||||
if (length >= 0 && AvailableBytes >= 2 + length)
|
||||
{
|
||||
result = GetBytesWithLength();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_position = 0;
|
||||
_dataSize = 0;
|
||||
_data = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40f1663f22634e9499bb37bbe6a10992
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
381
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetDataWriter.cs
Normal file
381
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetDataWriter.cs
Normal file
@@ -0,0 +1,381 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public class NetDataWriter
|
||||
{
|
||||
protected byte[] _data;
|
||||
protected int _position;
|
||||
private const int InitialSize = 64;
|
||||
private readonly bool _autoResize;
|
||||
|
||||
public int Capacity
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _data.Length;
|
||||
}
|
||||
public byte[] Data
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _data;
|
||||
}
|
||||
public int Length
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _position;
|
||||
}
|
||||
|
||||
public static readonly ThreadLocal<UTF8Encoding> uTF8Encoding = new ThreadLocal<UTF8Encoding>(() => new UTF8Encoding(false, true));
|
||||
public const int StringBufferMaxLength = 65535;
|
||||
private readonly byte[] _stringBuffer = new byte[StringBufferMaxLength];
|
||||
|
||||
public NetDataWriter() : this(true, InitialSize)
|
||||
{
|
||||
}
|
||||
|
||||
public NetDataWriter(bool autoResize) : this(autoResize, InitialSize)
|
||||
{
|
||||
}
|
||||
|
||||
public NetDataWriter(bool autoResize, int initialSize)
|
||||
{
|
||||
_data = new byte[initialSize];
|
||||
_autoResize = autoResize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates NetDataWriter from existing ByteArray
|
||||
/// </summary>
|
||||
/// <param name="bytes">Source byte array</param>
|
||||
/// <param name="copy">Copy array to new location or use existing</param>
|
||||
public static NetDataWriter FromBytes(byte[] bytes, bool copy)
|
||||
{
|
||||
if (copy)
|
||||
{
|
||||
var netDataWriter = new NetDataWriter(true, bytes.Length);
|
||||
netDataWriter.Put(bytes);
|
||||
return netDataWriter;
|
||||
}
|
||||
return new NetDataWriter(true, 0) {_data = bytes, _position = bytes.Length};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates NetDataWriter from existing ByteArray (always copied data)
|
||||
/// </summary>
|
||||
/// <param name="bytes">Source byte array</param>
|
||||
/// <param name="offset">Offset of array</param>
|
||||
/// <param name="length">Length of array</param>
|
||||
public static NetDataWriter FromBytes(byte[] bytes, int offset, int length)
|
||||
{
|
||||
var netDataWriter = new NetDataWriter(true, bytes.Length);
|
||||
netDataWriter.Put(bytes, offset, length);
|
||||
return netDataWriter;
|
||||
}
|
||||
|
||||
public static NetDataWriter FromString(string value)
|
||||
{
|
||||
var netDataWriter = new NetDataWriter();
|
||||
netDataWriter.Put(value);
|
||||
return netDataWriter;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ResizeIfNeed(int newSize)
|
||||
{
|
||||
if (_data.Length < newSize)
|
||||
{
|
||||
Array.Resize(ref _data, Math.Max(newSize, _data.Length * 2));
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void EnsureFit(int additionalSize)
|
||||
{
|
||||
if (_data.Length < _position + additionalSize)
|
||||
{
|
||||
Array.Resize(ref _data, Math.Max(_position + additionalSize, _data.Length * 2));
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset(int size)
|
||||
{
|
||||
ResizeIfNeed(size);
|
||||
_position = 0;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_position = 0;
|
||||
}
|
||||
|
||||
public byte[] CopyData()
|
||||
{
|
||||
byte[] resultData = new byte[_position];
|
||||
Buffer.BlockCopy(_data, 0, resultData, 0, _position);
|
||||
return resultData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets position of NetDataWriter to rewrite previous values
|
||||
/// </summary>
|
||||
/// <param name="position">new byte position</param>
|
||||
/// <returns>previous position of data writer</returns>
|
||||
public int SetPosition(int position)
|
||||
{
|
||||
int prevPosition = _position;
|
||||
_position = position;
|
||||
return prevPosition;
|
||||
}
|
||||
|
||||
public void Put(float value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(double value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(long value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(ulong value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(int value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(uint value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(char value)
|
||||
{
|
||||
Put((ushort)value);
|
||||
}
|
||||
|
||||
public void Put(ushort value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 2;
|
||||
}
|
||||
|
||||
public void Put(short value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 2;
|
||||
}
|
||||
|
||||
public void Put(sbyte value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 1);
|
||||
_data[_position] = (byte)value;
|
||||
_position++;
|
||||
}
|
||||
|
||||
public void Put(byte value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 1);
|
||||
_data[_position] = value;
|
||||
_position++;
|
||||
}
|
||||
|
||||
public void Put(byte[] data, int offset, int length)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + length);
|
||||
Buffer.BlockCopy(data, offset, _data, _position, length);
|
||||
_position += length;
|
||||
}
|
||||
|
||||
public void Put(byte[] data)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, _position, data.Length);
|
||||
_position += data.Length;
|
||||
}
|
||||
|
||||
public void PutSBytesWithLength(sbyte[] data, int offset, ushort length)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2 + length);
|
||||
FastBitConverter.GetBytes(_data, _position, length);
|
||||
Buffer.BlockCopy(data, offset, _data, _position + 2, length);
|
||||
_position += 2 + length;
|
||||
}
|
||||
|
||||
public void PutSBytesWithLength(sbyte[] data)
|
||||
{
|
||||
PutArray(data, 1);
|
||||
}
|
||||
|
||||
public void PutBytesWithLength(byte[] data, int offset, ushort length)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2 + length);
|
||||
FastBitConverter.GetBytes(_data, _position, length);
|
||||
Buffer.BlockCopy(data, offset, _data, _position + 2, length);
|
||||
_position += 2 + length;
|
||||
}
|
||||
|
||||
public void PutBytesWithLength(byte[] data)
|
||||
{
|
||||
PutArray(data, 1);
|
||||
}
|
||||
|
||||
public void Put(bool value)
|
||||
{
|
||||
Put((byte)(value ? 1 : 0));
|
||||
}
|
||||
|
||||
public void PutArray(Array arr, int sz)
|
||||
{
|
||||
ushort length = arr == null ? (ushort) 0 : (ushort)arr.Length;
|
||||
sz *= length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + sz + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, length);
|
||||
if (arr != null)
|
||||
Buffer.BlockCopy(arr, 0, _data, _position + 2, sz);
|
||||
_position += sz + 2;
|
||||
}
|
||||
|
||||
public void PutArray(float[] value)
|
||||
{
|
||||
PutArray(value, 4);
|
||||
}
|
||||
|
||||
public void PutArray(double[] value)
|
||||
{
|
||||
PutArray(value, 8);
|
||||
}
|
||||
|
||||
public void PutArray(long[] value)
|
||||
{
|
||||
PutArray(value, 8);
|
||||
}
|
||||
|
||||
public void PutArray(ulong[] value)
|
||||
{
|
||||
PutArray(value, 8);
|
||||
}
|
||||
|
||||
public void PutArray(int[] value)
|
||||
{
|
||||
PutArray(value, 4);
|
||||
}
|
||||
|
||||
public void PutArray(uint[] value)
|
||||
{
|
||||
PutArray(value, 4);
|
||||
}
|
||||
|
||||
public void PutArray(ushort[] value)
|
||||
{
|
||||
PutArray(value, 2);
|
||||
}
|
||||
|
||||
public void PutArray(short[] value)
|
||||
{
|
||||
PutArray(value, 2);
|
||||
}
|
||||
|
||||
public void PutArray(bool[] value)
|
||||
{
|
||||
PutArray(value, 1);
|
||||
}
|
||||
|
||||
public void PutArray(string[] value)
|
||||
{
|
||||
ushort strArrayLength = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
Put(strArrayLength);
|
||||
for (int i = 0; i < strArrayLength; i++)
|
||||
Put(value[i]);
|
||||
}
|
||||
|
||||
public void PutArray(string[] value, int strMaxLength)
|
||||
{
|
||||
ushort strArrayLength = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
Put(strArrayLength);
|
||||
for (int i = 0; i < strArrayLength; i++)
|
||||
Put(value[i], strMaxLength);
|
||||
}
|
||||
|
||||
public void Put(IPEndPoint endPoint)
|
||||
{
|
||||
Put(endPoint.Address.ToString());
|
||||
Put(endPoint.Port);
|
||||
}
|
||||
|
||||
public void Put(string value)
|
||||
{
|
||||
Put(value, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Note that "maxLength" only limits the number of characters in a string, not its size in bytes.
|
||||
/// </summary>
|
||||
public void Put(string value, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
Put((ushort)0);
|
||||
return;
|
||||
}
|
||||
|
||||
int length = maxLength > 0 && value.Length > maxLength ? maxLength : value.Length;
|
||||
int size = uTF8Encoding.Value.GetBytes(value, 0, length, _stringBuffer, 0);
|
||||
|
||||
if (size == 0 || size >= StringBufferMaxLength)
|
||||
{
|
||||
Put((ushort)0);
|
||||
return;
|
||||
}
|
||||
|
||||
Put(checked((ushort)(size + 1)));
|
||||
Put(_stringBuffer, 0, size);
|
||||
}
|
||||
|
||||
public void Put<T>(T obj) where T : INetSerializable
|
||||
{
|
||||
obj.Serialize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a7c2666c073f0241ba672f218278a4d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,267 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public class NetPacketProcessor
|
||||
{
|
||||
private static class HashCache<T>
|
||||
{
|
||||
public static readonly ulong Id;
|
||||
|
||||
//FNV-1 64 bit hash
|
||||
static HashCache()
|
||||
{
|
||||
ulong hash = 14695981039346656037UL; //offset
|
||||
string typeName = typeof(T).ToString();
|
||||
for (var i = 0; i < typeName.Length; i++)
|
||||
{
|
||||
hash ^= typeName[i];
|
||||
hash *= 1099511628211UL; //prime
|
||||
}
|
||||
Id = hash;
|
||||
}
|
||||
}
|
||||
|
||||
protected delegate void SubscribeDelegate(NetDataReader reader, object userData);
|
||||
private readonly NetSerializer _netSerializer;
|
||||
private readonly Dictionary<ulong, SubscribeDelegate> _callbacks = new Dictionary<ulong, SubscribeDelegate>();
|
||||
|
||||
public NetPacketProcessor()
|
||||
{
|
||||
_netSerializer = new NetSerializer();
|
||||
}
|
||||
|
||||
public NetPacketProcessor(int maxStringLength)
|
||||
{
|
||||
_netSerializer = new NetSerializer(maxStringLength);
|
||||
}
|
||||
|
||||
protected virtual ulong GetHash<T>()
|
||||
{
|
||||
return HashCache<T>.Id;
|
||||
}
|
||||
|
||||
protected virtual SubscribeDelegate GetCallbackFromData(NetDataReader reader)
|
||||
{
|
||||
ulong hash = reader.GetULong();
|
||||
if (!_callbacks.TryGetValue(hash, out var action))
|
||||
{
|
||||
throw new ParseException("Undefined packet in NetDataReader");
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
protected virtual void WriteHash<T>(NetDataWriter writer)
|
||||
{
|
||||
writer.Put(GetHash<T>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register nested property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable structure</typeparam>
|
||||
public void RegisterNestedType<T>() where T : struct, INetSerializable
|
||||
{
|
||||
_netSerializer.RegisterNestedType<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register nested property type
|
||||
/// </summary>
|
||||
/// <param name="writeDelegate"></param>
|
||||
/// <param name="readDelegate"></param>
|
||||
public void RegisterNestedType<T>(Action<NetDataWriter, T> writeDelegate, Func<NetDataReader, T> readDelegate)
|
||||
{
|
||||
_netSerializer.RegisterNestedType<T>(writeDelegate, readDelegate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register nested property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable class</typeparam>
|
||||
public void RegisterNestedType<T>(Func<T> constructor) where T : class, INetSerializable
|
||||
{
|
||||
_netSerializer.RegisterNestedType(constructor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all available data from NetDataReader and calls OnReceive delegates
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packets data</param>
|
||||
public void ReadAllPackets(NetDataReader reader)
|
||||
{
|
||||
while (reader.AvailableBytes > 0)
|
||||
ReadPacket(reader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all available data from NetDataReader and calls OnReceive delegates
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packets data</param>
|
||||
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
|
||||
/// <exception cref="ParseException">Malformed packet</exception>
|
||||
public void ReadAllPackets(NetDataReader reader, object userData)
|
||||
{
|
||||
while (reader.AvailableBytes > 0)
|
||||
ReadPacket(reader, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one packet from NetDataReader and calls OnReceive delegate
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <exception cref="ParseException">Malformed packet</exception>
|
||||
public void ReadPacket(NetDataReader reader)
|
||||
{
|
||||
ReadPacket(reader, null);
|
||||
}
|
||||
|
||||
public void Write<T>(NetDataWriter writer, T packet) where T : class, new()
|
||||
{
|
||||
WriteHash<T>(writer);
|
||||
_netSerializer.Serialize(writer, packet);
|
||||
}
|
||||
|
||||
public void WriteNetSerializable<T>(NetDataWriter writer, ref T packet) where T : INetSerializable
|
||||
{
|
||||
WriteHash<T>(writer);
|
||||
packet.Serialize(writer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one packet from NetDataReader and calls OnReceive delegate
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
|
||||
/// <exception cref="ParseException">Malformed packet</exception>
|
||||
public void ReadPacket(NetDataReader reader, object userData)
|
||||
{
|
||||
GetCallbackFromData(reader)(reader, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <param name="packetConstructor">Method that constructs packet instead of slow Activator.CreateInstance</param>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void Subscribe<T>(Action<T> onReceive, Func<T> packetConstructor) where T : class, new()
|
||||
{
|
||||
_netSerializer.Register<T>();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
var reference = packetConstructor();
|
||||
_netSerializer.Deserialize(reader, reference);
|
||||
onReceive(reference);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event (with userData)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <param name="packetConstructor">Method that constructs packet instead of slow Activator.CreateInstance</param>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void Subscribe<T, TUserData>(Action<T, TUserData> onReceive, Func<T> packetConstructor) where T : class, new()
|
||||
{
|
||||
_netSerializer.Register<T>();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
var reference = packetConstructor();
|
||||
_netSerializer.Deserialize(reader, reference);
|
||||
onReceive(reference, (TUserData)userData);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// This method will overwrite last received packet class on receive (less garbage)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void SubscribeReusable<T>(Action<T> onReceive) where T : class, new()
|
||||
{
|
||||
_netSerializer.Register<T>();
|
||||
var reference = new T();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
_netSerializer.Deserialize(reader, reference);
|
||||
onReceive(reference);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// This method will overwrite last received packet class on receive (less garbage)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void SubscribeReusable<T, TUserData>(Action<T, TUserData> onReceive) where T : class, new()
|
||||
{
|
||||
_netSerializer.Register<T>();
|
||||
var reference = new T();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
_netSerializer.Deserialize(reader, reference);
|
||||
onReceive(reference, (TUserData)userData);
|
||||
};
|
||||
}
|
||||
|
||||
public void SubscribeNetSerializable<T, TUserData>(
|
||||
Action<T, TUserData> onReceive,
|
||||
Func<T> packetConstructor) where T : INetSerializable
|
||||
{
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
var pkt = packetConstructor();
|
||||
pkt.Deserialize(reader);
|
||||
onReceive(pkt, (TUserData)userData);
|
||||
};
|
||||
}
|
||||
|
||||
public void SubscribeNetSerializable<T>(
|
||||
Action<T> onReceive,
|
||||
Func<T> packetConstructor) where T : INetSerializable
|
||||
{
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
var pkt = packetConstructor();
|
||||
pkt.Deserialize(reader);
|
||||
onReceive(pkt);
|
||||
};
|
||||
}
|
||||
|
||||
public void SubscribeNetSerializable<T, TUserData>(
|
||||
Action<T, TUserData> onReceive) where T : INetSerializable, new()
|
||||
{
|
||||
var reference = new T();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
reference.Deserialize(reader);
|
||||
onReceive(reference, (TUserData)userData);
|
||||
};
|
||||
}
|
||||
|
||||
public void SubscribeNetSerializable<T>(
|
||||
Action<T> onReceive) where T : INetSerializable, new()
|
||||
{
|
||||
var reference = new T();
|
||||
_callbacks[GetHash<T>()] = (reader, userData) =>
|
||||
{
|
||||
reference.Deserialize(reader);
|
||||
onReceive(reference);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove any subscriptions by type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Packet type</typeparam>
|
||||
/// <returns>true if remove is success</returns>
|
||||
public bool RemoveSubscription<T>()
|
||||
{
|
||||
return _callbacks.Remove(GetHash<T>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31df8ab0b770a5149a8ba538a6d16989
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
738
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetSerializer.cs
Normal file
738
Transport/LiteNetLib Transport/LiteNetLib/Utils/NetSerializer.cs
Normal file
@@ -0,0 +1,738 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public class InvalidTypeException : ArgumentException
|
||||
{
|
||||
public InvalidTypeException(string message) : base(message) { }
|
||||
}
|
||||
|
||||
public class ParseException : Exception
|
||||
{
|
||||
public ParseException(string message) : base(message) { }
|
||||
}
|
||||
|
||||
public class NetSerializer
|
||||
{
|
||||
private enum CallType
|
||||
{
|
||||
Basic,
|
||||
Array,
|
||||
List
|
||||
}
|
||||
|
||||
private abstract class FastCall<T>
|
||||
{
|
||||
public CallType Type;
|
||||
public virtual void Init(MethodInfo getMethod, MethodInfo setMethod, CallType type) { Type = type; }
|
||||
public abstract void Read(T inf, NetDataReader r);
|
||||
public abstract void Write(T inf, NetDataWriter w);
|
||||
public abstract void ReadArray(T inf, NetDataReader r);
|
||||
public abstract void WriteArray(T inf, NetDataWriter w);
|
||||
public abstract void ReadList(T inf, NetDataReader r);
|
||||
public abstract void WriteList(T inf, NetDataWriter w);
|
||||
}
|
||||
|
||||
private abstract class FastCallSpecific<TClass, TProperty> : FastCall<TClass>
|
||||
{
|
||||
protected Func<TClass, TProperty> Getter;
|
||||
protected Action<TClass, TProperty> Setter;
|
||||
protected Func<TClass, TProperty[]> GetterArr;
|
||||
protected Action<TClass, TProperty[]> SetterArr;
|
||||
protected Func<TClass, List<TProperty>> GetterList;
|
||||
protected Action<TClass, List<TProperty>> SetterList;
|
||||
|
||||
public override void ReadArray(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
|
||||
public override void WriteArray(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
|
||||
public override void ReadList(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
|
||||
public override void WriteList(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
|
||||
|
||||
protected TProperty[] ReadArrayHelper(TClass inf, NetDataReader r)
|
||||
{
|
||||
ushort count = r.GetUShort();
|
||||
var arr = GetterArr(inf);
|
||||
arr = arr == null || arr.Length != count ? new TProperty[count] : arr;
|
||||
SetterArr(inf, arr);
|
||||
return arr;
|
||||
}
|
||||
|
||||
protected TProperty[] WriteArrayHelper(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var arr = GetterArr(inf);
|
||||
w.Put((ushort)arr.Length);
|
||||
return arr;
|
||||
}
|
||||
|
||||
protected List<TProperty> ReadListHelper(TClass inf, NetDataReader r, out int len)
|
||||
{
|
||||
len = r.GetUShort();
|
||||
var list = GetterList(inf);
|
||||
if (list == null)
|
||||
{
|
||||
list = new List<TProperty>(len);
|
||||
SetterList(inf, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
protected List<TProperty> WriteListHelper(TClass inf, NetDataWriter w, out int len)
|
||||
{
|
||||
var list = GetterList(inf);
|
||||
if (list == null)
|
||||
{
|
||||
len = 0;
|
||||
w.Put(0);
|
||||
return null;
|
||||
}
|
||||
len = list.Count;
|
||||
w.Put((ushort)len);
|
||||
return list;
|
||||
}
|
||||
|
||||
public override void Init(MethodInfo getMethod, MethodInfo setMethod, CallType type)
|
||||
{
|
||||
base.Init(getMethod, setMethod, type);
|
||||
switch (type)
|
||||
{
|
||||
case CallType.Array:
|
||||
GetterArr = (Func<TClass, TProperty[]>)Delegate.CreateDelegate(typeof(Func<TClass, TProperty[]>), getMethod);
|
||||
SetterArr = (Action<TClass, TProperty[]>)Delegate.CreateDelegate(typeof(Action<TClass, TProperty[]>), setMethod);
|
||||
break;
|
||||
case CallType.List:
|
||||
GetterList = (Func<TClass, List<TProperty>>)Delegate.CreateDelegate(typeof(Func<TClass, List<TProperty>>), getMethod);
|
||||
SetterList = (Action<TClass, List<TProperty>>)Delegate.CreateDelegate(typeof(Action<TClass, List<TProperty>>), setMethod);
|
||||
break;
|
||||
default:
|
||||
Getter = (Func<TClass, TProperty>)Delegate.CreateDelegate(typeof(Func<TClass, TProperty>), getMethod);
|
||||
Setter = (Action<TClass, TProperty>)Delegate.CreateDelegate(typeof(Action<TClass, TProperty>), setMethod);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class FastCallSpecificAuto<TClass, TProperty> : FastCallSpecific<TClass, TProperty>
|
||||
{
|
||||
protected abstract void ElementRead(NetDataReader r, out TProperty prop);
|
||||
protected abstract void ElementWrite(NetDataWriter w, ref TProperty prop);
|
||||
|
||||
public override void Read(TClass inf, NetDataReader r)
|
||||
{
|
||||
ElementRead(r, out var elem);
|
||||
Setter(inf, elem);
|
||||
}
|
||||
|
||||
public override void Write(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var elem = Getter(inf);
|
||||
ElementWrite(w, ref elem);
|
||||
}
|
||||
|
||||
public override void ReadArray(TClass inf, NetDataReader r)
|
||||
{
|
||||
var arr = ReadArrayHelper(inf, r);
|
||||
for (int i = 0; i < arr.Length; i++)
|
||||
ElementRead(r, out arr[i]);
|
||||
}
|
||||
|
||||
public override void WriteArray(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var arr = WriteArrayHelper(inf, w);
|
||||
for (int i = 0; i < arr.Length; i++)
|
||||
ElementWrite(w, ref arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FastCallStatic<TClass, TProperty> : FastCallSpecific<TClass, TProperty>
|
||||
{
|
||||
private readonly Action<NetDataWriter, TProperty> _writer;
|
||||
private readonly Func<NetDataReader, TProperty> _reader;
|
||||
|
||||
public FastCallStatic(Action<NetDataWriter, TProperty> write, Func<NetDataReader, TProperty> read)
|
||||
{
|
||||
_writer = write;
|
||||
_reader = read;
|
||||
}
|
||||
|
||||
public override void Read(TClass inf, NetDataReader r) { Setter(inf, _reader(r)); }
|
||||
public override void Write(TClass inf, NetDataWriter w) { _writer(w, Getter(inf)); }
|
||||
|
||||
public override void ReadList(TClass inf, NetDataReader r)
|
||||
{
|
||||
var list = ReadListHelper(inf, r, out int len);
|
||||
int listCount = list.Count;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
if (i < listCount)
|
||||
list[i] = _reader(r);
|
||||
else
|
||||
list.Add(_reader(r));
|
||||
}
|
||||
if (len < listCount)
|
||||
list.RemoveRange(len, listCount - len);
|
||||
}
|
||||
|
||||
public override void WriteList(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var list = WriteListHelper(inf, w, out int len);
|
||||
for (int i = 0; i < len; i++)
|
||||
_writer(w, list[i]);
|
||||
}
|
||||
|
||||
public override void ReadArray(TClass inf, NetDataReader r)
|
||||
{
|
||||
var arr = ReadArrayHelper(inf, r);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
arr[i] = _reader(r);
|
||||
}
|
||||
|
||||
public override void WriteArray(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var arr = WriteArrayHelper(inf, w);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
_writer(w, arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FastCallStruct<TClass, TProperty> : FastCallSpecific<TClass, TProperty> where TProperty : struct, INetSerializable
|
||||
{
|
||||
private TProperty _p;
|
||||
|
||||
public override void Read(TClass inf, NetDataReader r)
|
||||
{
|
||||
_p.Deserialize(r);
|
||||
Setter(inf, _p);
|
||||
}
|
||||
|
||||
public override void Write(TClass inf, NetDataWriter w)
|
||||
{
|
||||
_p = Getter(inf);
|
||||
_p.Serialize(w);
|
||||
}
|
||||
|
||||
public override void ReadList(TClass inf, NetDataReader r)
|
||||
{
|
||||
var list = ReadListHelper(inf, r, out int len);
|
||||
int listCount = list.Count;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var itm = default(TProperty);
|
||||
itm.Deserialize(r);
|
||||
if(i < listCount)
|
||||
list[i] = itm;
|
||||
else
|
||||
list.Add(itm);
|
||||
}
|
||||
if (len < listCount)
|
||||
list.RemoveRange(len, listCount - len);
|
||||
}
|
||||
|
||||
public override void WriteList(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var list = WriteListHelper(inf, w, out int len);
|
||||
for (int i = 0; i < len; i++)
|
||||
list[i].Serialize(w);
|
||||
}
|
||||
|
||||
public override void ReadArray(TClass inf, NetDataReader r)
|
||||
{
|
||||
var arr = ReadArrayHelper(inf, r);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
arr[i].Deserialize(r);
|
||||
}
|
||||
|
||||
public override void WriteArray(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var arr = WriteArrayHelper(inf, w);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
arr[i].Serialize(w);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FastCallClass<TClass, TProperty> : FastCallSpecific<TClass, TProperty> where TProperty : class, INetSerializable
|
||||
{
|
||||
private readonly Func<TProperty> _constructor;
|
||||
public FastCallClass(Func<TProperty> constructor) { _constructor = constructor; }
|
||||
|
||||
public override void Read(TClass inf, NetDataReader r)
|
||||
{
|
||||
var p = _constructor();
|
||||
p.Deserialize(r);
|
||||
Setter(inf, p);
|
||||
}
|
||||
|
||||
public override void Write(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var p = Getter(inf);
|
||||
p?.Serialize(w);
|
||||
}
|
||||
|
||||
public override void ReadList(TClass inf, NetDataReader r)
|
||||
{
|
||||
var list = ReadListHelper(inf, r, out int len);
|
||||
int listCount = list.Count;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
if (i < listCount)
|
||||
{
|
||||
list[i].Deserialize(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
var itm = _constructor();
|
||||
itm.Deserialize(r);
|
||||
list.Add(itm);
|
||||
}
|
||||
}
|
||||
if (len < listCount)
|
||||
list.RemoveRange(len, listCount - len);
|
||||
}
|
||||
|
||||
public override void WriteList(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var list = WriteListHelper(inf, w, out int len);
|
||||
for (int i = 0; i < len; i++)
|
||||
list[i].Serialize(w);
|
||||
}
|
||||
|
||||
public override void ReadArray(TClass inf, NetDataReader r)
|
||||
{
|
||||
var arr = ReadArrayHelper(inf, r);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
arr[i] = _constructor();
|
||||
arr[i].Deserialize(r);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteArray(TClass inf, NetDataWriter w)
|
||||
{
|
||||
var arr = WriteArrayHelper(inf, w);
|
||||
int len = arr.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
arr[i].Serialize(w);
|
||||
}
|
||||
}
|
||||
|
||||
private class IntSerializer<T> : FastCallSpecific<T, int>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetInt()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetIntArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class UIntSerializer<T> : FastCallSpecific<T, uint>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetUInt()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetUIntArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class ShortSerializer<T> : FastCallSpecific<T, short>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetShort()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetShortArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class UShortSerializer<T> : FastCallSpecific<T, ushort>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetUShort()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetUShortArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class LongSerializer<T> : FastCallSpecific<T, long>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetLong()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetLongArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class ULongSerializer<T> : FastCallSpecific<T, ulong>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetULong()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetULongArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class ByteSerializer<T> : FastCallSpecific<T, byte>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetByte()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetBytesWithLength()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutBytesWithLength(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class SByteSerializer<T> : FastCallSpecific<T, sbyte>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetSByte()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetSBytesWithLength()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutSBytesWithLength(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class FloatSerializer<T> : FastCallSpecific<T, float>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetFloat()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetFloatArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class DoubleSerializer<T> : FastCallSpecific<T, double>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetDouble()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetDoubleArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class BoolSerializer<T> : FastCallSpecific<T, bool>
|
||||
{
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetBool()); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetBoolArray()); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf)); }
|
||||
}
|
||||
|
||||
private class CharSerializer<T> : FastCallSpecificAuto<T, char>
|
||||
{
|
||||
protected override void ElementWrite(NetDataWriter w, ref char prop) { w.Put(prop); }
|
||||
protected override void ElementRead(NetDataReader r, out char prop) { prop = r.GetChar(); }
|
||||
}
|
||||
|
||||
private class IPEndPointSerializer<T> : FastCallSpecificAuto<T, IPEndPoint>
|
||||
{
|
||||
protected override void ElementWrite(NetDataWriter w, ref IPEndPoint prop) { w.Put(prop); }
|
||||
protected override void ElementRead(NetDataReader r, out IPEndPoint prop) { prop = r.GetNetEndPoint(); }
|
||||
}
|
||||
|
||||
private class StringSerializer<T> : FastCallSpecific<T, string>
|
||||
{
|
||||
private readonly int _maxLength;
|
||||
public StringSerializer(int maxLength) { _maxLength = maxLength > 0 ? maxLength : short.MaxValue; }
|
||||
public override void Read(T inf, NetDataReader r) { Setter(inf, r.GetString(_maxLength)); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf), _maxLength); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { SetterArr(inf, r.GetStringArray(_maxLength)); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf), _maxLength); }
|
||||
}
|
||||
|
||||
private class EnumByteSerializer<T> : FastCall<T>
|
||||
{
|
||||
protected readonly PropertyInfo Property;
|
||||
protected readonly Type PropertyType;
|
||||
public EnumByteSerializer(PropertyInfo property, Type propertyType)
|
||||
{
|
||||
Property = property;
|
||||
PropertyType = propertyType;
|
||||
}
|
||||
public override void Read(T inf, NetDataReader r) { Property.SetValue(inf, Enum.ToObject(PropertyType, r.GetByte()), null); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put((byte)Property.GetValue(inf, null)); }
|
||||
public override void ReadArray(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
|
||||
public override void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
|
||||
public override void ReadList(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
|
||||
public override void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
|
||||
}
|
||||
|
||||
private class EnumIntSerializer<T> : EnumByteSerializer<T>
|
||||
{
|
||||
public EnumIntSerializer(PropertyInfo property, Type propertyType) : base(property, propertyType) { }
|
||||
public override void Read(T inf, NetDataReader r) { Property.SetValue(inf, Enum.ToObject(PropertyType, r.GetInt()), null); }
|
||||
public override void Write(T inf, NetDataWriter w) { w.Put((int)Property.GetValue(inf, null)); }
|
||||
}
|
||||
|
||||
private sealed class ClassInfo<T>
|
||||
{
|
||||
public static ClassInfo<T> Instance;
|
||||
private readonly FastCall<T>[] _serializers;
|
||||
private readonly int _membersCount;
|
||||
|
||||
public ClassInfo(List<FastCall<T>> serializers)
|
||||
{
|
||||
_membersCount = serializers.Count;
|
||||
_serializers = serializers.ToArray();
|
||||
}
|
||||
|
||||
public void Write(T obj, NetDataWriter writer)
|
||||
{
|
||||
for (int i = 0; i < _membersCount; i++)
|
||||
{
|
||||
var s = _serializers[i];
|
||||
if (s.Type == CallType.Basic)
|
||||
s.Write(obj, writer);
|
||||
else if (s.Type == CallType.Array)
|
||||
s.WriteArray(obj, writer);
|
||||
else
|
||||
s.WriteList(obj, writer);
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(T obj, NetDataReader reader)
|
||||
{
|
||||
for (int i = 0; i < _membersCount; i++)
|
||||
{
|
||||
var s = _serializers[i];
|
||||
if (s.Type == CallType.Basic)
|
||||
s.Read(obj, reader);
|
||||
else if(s.Type == CallType.Array)
|
||||
s.ReadArray(obj, reader);
|
||||
else
|
||||
s.ReadList(obj, reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class CustomType
|
||||
{
|
||||
public abstract FastCall<T> Get<T>();
|
||||
}
|
||||
|
||||
private sealed class CustomTypeStruct<TProperty> : CustomType where TProperty : struct, INetSerializable
|
||||
{
|
||||
public override FastCall<T> Get<T>() { return new FastCallStruct<T, TProperty>(); }
|
||||
}
|
||||
|
||||
private sealed class CustomTypeClass<TProperty> : CustomType where TProperty : class, INetSerializable
|
||||
{
|
||||
private readonly Func<TProperty> _constructor;
|
||||
public CustomTypeClass(Func<TProperty> constructor) { _constructor = constructor; }
|
||||
public override FastCall<T> Get<T>() { return new FastCallClass<T, TProperty>(_constructor); }
|
||||
}
|
||||
|
||||
private sealed class CustomTypeStatic<TProperty> : CustomType
|
||||
{
|
||||
private readonly Action<NetDataWriter, TProperty> _writer;
|
||||
private readonly Func<NetDataReader, TProperty> _reader;
|
||||
public CustomTypeStatic(Action<NetDataWriter, TProperty> writer, Func<NetDataReader, TProperty> reader)
|
||||
{
|
||||
_writer = writer;
|
||||
_reader = reader;
|
||||
}
|
||||
public override FastCall<T> Get<T>() { return new FastCallStatic<T, TProperty>(_writer, _reader); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable structure</typeparam>
|
||||
public void RegisterNestedType<T>() where T : struct, INetSerializable
|
||||
{
|
||||
_registeredTypes.Add(typeof(T), new CustomTypeStruct<T>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable class</typeparam>
|
||||
public void RegisterNestedType<T>(Func<T> constructor) where T : class, INetSerializable
|
||||
{
|
||||
_registeredTypes.Add(typeof(T), new CustomTypeClass<T>(constructor));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Any packet</typeparam>
|
||||
/// <param name="writer">custom type writer</param>
|
||||
/// <param name="reader">custom type reader</param>
|
||||
public void RegisterNestedType<T>(Action<NetDataWriter, T> writer, Func<NetDataReader, T> reader)
|
||||
{
|
||||
_registeredTypes.Add(typeof(T), new CustomTypeStatic<T>(writer, reader));
|
||||
}
|
||||
|
||||
private NetDataWriter _writer;
|
||||
private readonly int _maxStringLength;
|
||||
private readonly Dictionary<Type, CustomType> _registeredTypes = new Dictionary<Type, CustomType>();
|
||||
|
||||
public NetSerializer() : this(0)
|
||||
{
|
||||
}
|
||||
|
||||
public NetSerializer(int maxStringLength)
|
||||
{
|
||||
_maxStringLength = maxStringLength;
|
||||
}
|
||||
|
||||
private ClassInfo<T> RegisterInternal<T>()
|
||||
{
|
||||
if (ClassInfo<T>.Instance != null)
|
||||
return ClassInfo<T>.Instance;
|
||||
|
||||
Type t = typeof(T);
|
||||
var props = t.GetProperties(
|
||||
BindingFlags.Instance |
|
||||
BindingFlags.Public |
|
||||
BindingFlags.GetProperty |
|
||||
BindingFlags.SetProperty);
|
||||
var serializers = new List<FastCall<T>>();
|
||||
for (int i = 0; i < props.Length; i++)
|
||||
{
|
||||
var property = props[i];
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
var elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
|
||||
var callType = propertyType.IsArray ? CallType.Array : CallType.Basic;
|
||||
|
||||
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
elementType = propertyType.GetGenericArguments()[0];
|
||||
callType = CallType.List;
|
||||
}
|
||||
|
||||
if (Attribute.IsDefined(property, typeof(IgnoreDataMemberAttribute)))
|
||||
continue;
|
||||
|
||||
var getMethod = property.GetGetMethod();
|
||||
var setMethod = property.GetSetMethod();
|
||||
if (getMethod == null || setMethod == null)
|
||||
continue;
|
||||
|
||||
FastCall<T> serialzer = null;
|
||||
if (propertyType.IsEnum)
|
||||
{
|
||||
var underlyingType = Enum.GetUnderlyingType(propertyType);
|
||||
if (underlyingType == typeof(byte))
|
||||
serialzer = new EnumByteSerializer<T>(property, propertyType);
|
||||
else if (underlyingType == typeof(int))
|
||||
serialzer = new EnumIntSerializer<T>(property, propertyType);
|
||||
else
|
||||
throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
|
||||
}
|
||||
else if (elementType == typeof(string))
|
||||
serialzer = new StringSerializer<T>(_maxStringLength);
|
||||
else if (elementType == typeof(bool))
|
||||
serialzer = new BoolSerializer<T>();
|
||||
else if (elementType == typeof(byte))
|
||||
serialzer = new ByteSerializer<T>();
|
||||
else if (elementType == typeof(sbyte))
|
||||
serialzer = new SByteSerializer<T>();
|
||||
else if (elementType == typeof(short))
|
||||
serialzer = new ShortSerializer<T>();
|
||||
else if (elementType == typeof(ushort))
|
||||
serialzer = new UShortSerializer<T>();
|
||||
else if (elementType == typeof(int))
|
||||
serialzer = new IntSerializer<T>();
|
||||
else if (elementType == typeof(uint))
|
||||
serialzer = new UIntSerializer<T>();
|
||||
else if (elementType == typeof(long))
|
||||
serialzer = new LongSerializer<T>();
|
||||
else if (elementType == typeof(ulong))
|
||||
serialzer = new ULongSerializer<T>();
|
||||
else if (elementType == typeof(float))
|
||||
serialzer = new FloatSerializer<T>();
|
||||
else if (elementType == typeof(double))
|
||||
serialzer = new DoubleSerializer<T>();
|
||||
else if (elementType == typeof(char))
|
||||
serialzer = new CharSerializer<T>();
|
||||
else if (elementType == typeof(IPEndPoint))
|
||||
serialzer = new IPEndPointSerializer<T>();
|
||||
else
|
||||
{
|
||||
_registeredTypes.TryGetValue(elementType, out var customType);
|
||||
if (customType != null)
|
||||
serialzer = customType.Get<T>();
|
||||
}
|
||||
|
||||
if (serialzer != null)
|
||||
{
|
||||
serialzer.Init(getMethod, setMethod, callType);
|
||||
serializers.Add(serialzer);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
|
||||
}
|
||||
}
|
||||
ClassInfo<T>.Instance = new ClassInfo<T>(serializers);
|
||||
return ClassInfo<T>.Instance;
|
||||
}
|
||||
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void Register<T>()
|
||||
{
|
||||
RegisterInternal<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads packet with known type
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <returns>Returns packet if packet in reader is matched type</returns>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public T Deserialize<T>(NetDataReader reader) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
var result = new T();
|
||||
try
|
||||
{
|
||||
info.Read(result, reader);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads packet with known type (non alloc variant)
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <param name="target">Deserialization target</param>
|
||||
/// <returns>Returns true if packet in reader is matched type</returns>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public bool Deserialize<T>(NetDataReader reader, T target) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
try
|
||||
{
|
||||
info.Read(target, reader);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize object to NetDataWriter (fast)
|
||||
/// </summary>
|
||||
/// <param name="writer">Serialization target NetDataWriter</param>
|
||||
/// <param name="obj">Object to serialize</param>
|
||||
/// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception>
|
||||
public void Serialize<T>(NetDataWriter writer, T obj) where T : class, new()
|
||||
{
|
||||
RegisterInternal<T>().Write(obj, writer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize object to byte array
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to serialize</param>
|
||||
/// <returns>byte array with serialized data</returns>
|
||||
public byte[] Serialize<T>(T obj) where T : class, new()
|
||||
{
|
||||
if (_writer == null)
|
||||
_writer = new NetDataWriter();
|
||||
_writer.Reset();
|
||||
Serialize(_writer, obj);
|
||||
return _writer.CopyData();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e77693e1445e6a748b5044f29adc4d4e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
423
Transport/LiteNetLib Transport/LiteNetLib/Utils/NtpPacket.cs
Normal file
423
Transport/LiteNetLib Transport/LiteNetLib/Utils/NtpPacket.cs
Normal file
@@ -0,0 +1,423 @@
|
||||
using System;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents RFC4330 SNTP packet used for communication to and from a network time server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Most applications should just use the <see cref="NtpPacket.CorrectionOffset" /> property.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The same data structure represents both request and reply packets.
|
||||
/// Request and reply differ in which properties are set and to what values.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The only real property is <see cref="NtpPacket.Bytes" />.
|
||||
/// All other properties read from and write to the underlying byte array
|
||||
/// with the exception of <see cref="NtpPacket.DestinationTimestamp" />,
|
||||
/// which is not part of the packet on network and it is instead set locally after receiving the packet.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Copied from <a href="https://guerrillantp.machinezoo.com/">GuerrillaNtp project</a>
|
||||
/// with permission from Robert Vazan (@robertvazan) under MIT license, see https://github.com/RevenantX/LiteNetLib/pull/236
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class NtpPacket
|
||||
{
|
||||
private static readonly DateTime Epoch = new DateTime(1900, 1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Gets RFC4330-encoded SNTP packet.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Byte array containing RFC4330-encoded SNTP packet. It is at least 48 bytes long.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This is the only real property. All other properties except
|
||||
/// <see cref="NtpPacket.DestinationTimestamp" /> read from or write to this byte array.
|
||||
/// </remarks>
|
||||
public byte[] Bytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the leap second indicator.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Leap second warning, if any. Special value
|
||||
/// <see cref="NtpLeapIndicator.AlarmCondition" /> indicates unsynchronized server clock.
|
||||
/// Default is <see cref="NtpLeapIndicator.NoWarning" />.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// Only servers fill in this property. Clients can consult this property for possible leap second warning.
|
||||
/// </remarks>
|
||||
public NtpLeapIndicator LeapIndicator => (NtpLeapIndicator)((Bytes[0] & 0xC0) >> 6);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets protocol version number.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// SNTP protocol version. Default is 4, which is the latest version at the time of this writing.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// In request packets, clients should leave this property at default value 4.
|
||||
/// Servers usually reply with the same protocol version.
|
||||
/// </remarks>
|
||||
public int VersionNumber
|
||||
{
|
||||
get => (Bytes[0] & 0x38) >> 3;
|
||||
private set => Bytes[0] = (byte)((Bytes[0] & ~0x38) | value << 3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets SNTP packet mode, i.e. whether this is client or server packet.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// SNTP packet mode. Default is <see cref="NtpMode.Client" /> in newly created packets.
|
||||
/// Server reply should have this property set to <see cref="NtpMode.Server" />.
|
||||
/// </value>
|
||||
public NtpMode Mode
|
||||
{
|
||||
get => (NtpMode)(Bytes[0] & 0x07);
|
||||
private set => Bytes[0] = (byte)((Bytes[0] & ~0x07) | (int)value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets server's distance from the reference clock.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>
|
||||
/// Distance from the reference clock. This property is set only in server reply packets.
|
||||
/// Servers connected directly to reference clock hardware set this property to 1.
|
||||
/// Statum number is incremented by 1 on every hop down the NTP server hierarchy.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Special value 0 indicates that this packet is a Kiss-o'-Death message
|
||||
/// with kiss code stored in <see cref="NtpPacket.ReferenceId" />.
|
||||
/// </para>
|
||||
/// </value>
|
||||
public int Stratum => Bytes[1];
|
||||
|
||||
/// <summary>
|
||||
/// Gets server's preferred polling interval.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Polling interval in log2 seconds, e.g. 4 stands for 16s and 17 means 131,072s.
|
||||
/// </value>
|
||||
public int Poll => Bytes[2];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the precision of server clock.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Clock precision in log2 seconds, e.g. -20 for microsecond precision.
|
||||
/// </value>
|
||||
public int Precision => (sbyte)Bytes[3];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total round-trip delay from the server to the reference clock.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Round-trip delay to the reference clock. Normally a positive value smaller than one second.
|
||||
/// </value>
|
||||
public TimeSpan RootDelay => GetTimeSpan32(4);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the estimated error in time reported by the server.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Estimated error in time reported by the server. Normally a positive value smaller than one second.
|
||||
/// </value>
|
||||
public TimeSpan RootDispersion => GetTimeSpan32(8);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ID of the time source used by the server or Kiss-o'-Death code sent by the server.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>
|
||||
/// ID of server's time source or Kiss-o'-Death code.
|
||||
/// Purpose of this property depends on value of <see cref="NtpPacket.Stratum" /> property.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Stratum 1 servers write here one of several special values that describe the kind of hardware clock they use.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Stratum 2 and lower servers set this property to IPv4 address of their upstream server.
|
||||
/// If upstream server has IPv6 address, the address is hashed, because it doesn't fit in this property.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// When server sets <see cref="NtpPacket.Stratum" /> to special value 0,
|
||||
/// this property contains so called kiss code that instructs the client to stop querying the server.
|
||||
/// </para>
|
||||
/// </value>
|
||||
public uint ReferenceId => GetUInt32BE(12);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time when the server clock was last set or corrected.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Time when the server clock was last set or corrected or <c>null</c> when not specified.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This Property is usually set only by servers. It usually lags server's current time by several minutes,
|
||||
/// so don't use this property for time synchronization.
|
||||
/// </remarks>
|
||||
public DateTime? ReferenceTimestamp => GetDateTime64(16);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time when the client sent its request.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// This property is <c>null</c> in request packets.
|
||||
/// In reply packets, it is the time when the client sent its request.
|
||||
/// Servers copy this value from <see cref="NtpPacket.TransmitTimestamp" />
|
||||
/// that they find in received request packet.
|
||||
/// </value>
|
||||
/// <seealso cref="NtpPacket.CorrectionOffset" />
|
||||
/// <seealso cref="NtpPacket.RoundTripTime" />
|
||||
public DateTime? OriginTimestamp => GetDateTime64(24);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time when the request was received by the server.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// This property is <c>null</c> in request packets.
|
||||
/// In reply packets, it is the time when the server received client request.
|
||||
/// </value>
|
||||
/// <seealso cref="NtpPacket.CorrectionOffset" />
|
||||
/// <seealso cref="NtpPacket.RoundTripTime" />
|
||||
public DateTime? ReceiveTimestamp => GetDateTime64(32);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time when the packet was sent.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Time when the packet was sent. It should never be <c>null</c>.
|
||||
/// Default value is <see cref="System.DateTime.UtcNow" />.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This property must be set by both clients and servers.
|
||||
/// </remarks>
|
||||
/// <seealso cref="NtpPacket.CorrectionOffset" />
|
||||
/// <seealso cref="NtpPacket.RoundTripTime" />
|
||||
public DateTime? TransmitTimestamp { get { return GetDateTime64(40); } private set { SetDateTime64(40, value); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time of reception of response SNTP packet on the client.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Time of reception of response SNTP packet on the client. It is <c>null</c> in request packets.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This property is not part of the protocol and has to be set when reply packet is received.
|
||||
/// </remarks>
|
||||
/// <seealso cref="NtpPacket.CorrectionOffset" />
|
||||
/// <seealso cref="NtpPacket.RoundTripTime" />
|
||||
public DateTime? DestinationTimestamp { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the round-trip time to the server.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Time the request spent traveling to the server plus the time the reply spent traveling back.
|
||||
/// This is calculated from timestamps in the packet as <c>(t1 - t0) + (t3 - t2)</c>
|
||||
/// where t0 is <see cref="NtpPacket.OriginTimestamp" />,
|
||||
/// t1 is <see cref="NtpPacket.ReceiveTimestamp" />,
|
||||
/// t2 is <see cref="NtpPacket.TransmitTimestamp" />,
|
||||
/// and t3 is <see cref="NtpPacket.DestinationTimestamp" />.
|
||||
/// This property throws an exception in request packets.
|
||||
/// </value>
|
||||
public TimeSpan RoundTripTime
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckTimestamps();
|
||||
return (ReceiveTimestamp.Value - OriginTimestamp.Value) + (DestinationTimestamp.Value - TransmitTimestamp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the offset that should be added to local time to synchronize it with server time.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// Time difference between server and client. It should be added to local time to get server time.
|
||||
/// It is calculated from timestamps in the packet as <c>0.5 * ((t1 - t0) - (t3 - t2))</c>
|
||||
/// where t0 is <see cref="NtpPacket.OriginTimestamp" />,
|
||||
/// t1 is <see cref="NtpPacket.ReceiveTimestamp" />,
|
||||
/// t2 is <see cref="NtpPacket.TransmitTimestamp" />,
|
||||
/// and t3 is <see cref="NtpPacket.DestinationTimestamp" />.
|
||||
/// This property throws an exception in request packets.
|
||||
/// </value>
|
||||
public TimeSpan CorrectionOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckTimestamps();
|
||||
return TimeSpan.FromTicks(((ReceiveTimestamp.Value - OriginTimestamp.Value) - (DestinationTimestamp.Value - TransmitTimestamp.Value)).Ticks / 2);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes default request packet.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Properties <see cref="NtpPacket.Mode" /> and <see cref="NtpPacket.VersionNumber" />
|
||||
/// are set appropriately for request packet. Property <see cref="NtpPacket.TransmitTimestamp" />
|
||||
/// is set to <see cref="System.DateTime.UtcNow" />.
|
||||
/// </remarks>
|
||||
public NtpPacket() : this(new byte[48])
|
||||
{
|
||||
Mode = NtpMode.Client;
|
||||
VersionNumber = 4;
|
||||
TransmitTimestamp = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes packet from received data.
|
||||
/// </summary>
|
||||
internal NtpPacket(byte[] bytes)
|
||||
{
|
||||
if (bytes.Length < 48)
|
||||
throw new ArgumentException("SNTP reply packet must be at least 48 bytes long.", "bytes");
|
||||
Bytes = bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes packet from data received from a server.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Data received from the server.</param>
|
||||
/// <param name="destinationTimestamp">Utc time of reception of response SNTP packet on the client.</param>
|
||||
/// <returns></returns>
|
||||
public static NtpPacket FromServerResponse(byte[] bytes, DateTime destinationTimestamp)
|
||||
{
|
||||
return new NtpPacket(bytes) { DestinationTimestamp = destinationTimestamp };
|
||||
}
|
||||
|
||||
internal void ValidateRequest()
|
||||
{
|
||||
if (Mode != NtpMode.Client)
|
||||
throw new InvalidOperationException("This is not a request SNTP packet.");
|
||||
if (VersionNumber == 0)
|
||||
throw new InvalidOperationException("Protocol version of the request is not specified.");
|
||||
if (TransmitTimestamp == null)
|
||||
throw new InvalidOperationException("TransmitTimestamp must be set in request packet.");
|
||||
}
|
||||
|
||||
internal void ValidateReply()
|
||||
{
|
||||
if (Mode != NtpMode.Server)
|
||||
throw new InvalidOperationException("This is not a reply SNTP packet.");
|
||||
if (VersionNumber == 0)
|
||||
throw new InvalidOperationException("Protocol version of the reply is not specified.");
|
||||
if (Stratum == 0)
|
||||
throw new InvalidOperationException(string.Format("Received Kiss-o'-Death SNTP packet with code 0x{0:x}.", ReferenceId));
|
||||
if (LeapIndicator == NtpLeapIndicator.AlarmCondition)
|
||||
throw new InvalidOperationException("SNTP server has unsynchronized clock.");
|
||||
CheckTimestamps();
|
||||
}
|
||||
|
||||
private void CheckTimestamps()
|
||||
{
|
||||
if (OriginTimestamp == null)
|
||||
throw new InvalidOperationException("Origin timestamp is missing.");
|
||||
if (ReceiveTimestamp == null)
|
||||
throw new InvalidOperationException("Receive timestamp is missing.");
|
||||
if (TransmitTimestamp == null)
|
||||
throw new InvalidOperationException("Transmit timestamp is missing.");
|
||||
if (DestinationTimestamp == null)
|
||||
throw new InvalidOperationException("Destination timestamp is missing.");
|
||||
}
|
||||
|
||||
private DateTime? GetDateTime64(int offset)
|
||||
{
|
||||
var field = GetUInt64BE(offset);
|
||||
if (field == 0)
|
||||
return null;
|
||||
return new DateTime(Epoch.Ticks + Convert.ToInt64(field * (1.0 / (1L << 32) * 10000000.0)));
|
||||
}
|
||||
|
||||
private void SetDateTime64(int offset, DateTime? value)
|
||||
{
|
||||
SetUInt64BE(offset, value == null ? 0 : Convert.ToUInt64((value.Value.Ticks - Epoch.Ticks) * (0.0000001 * (1L << 32))));
|
||||
}
|
||||
|
||||
private TimeSpan GetTimeSpan32(int offset)
|
||||
{
|
||||
return TimeSpan.FromSeconds(GetInt32BE(offset) / (double)(1 << 16));
|
||||
}
|
||||
|
||||
private ulong GetUInt64BE(int offset)
|
||||
{
|
||||
return SwapEndianness(BitConverter.ToUInt64(Bytes, offset));
|
||||
}
|
||||
|
||||
private void SetUInt64BE(int offset, ulong value)
|
||||
{
|
||||
FastBitConverter.GetBytes(Bytes, offset, SwapEndianness(value));
|
||||
}
|
||||
|
||||
private int GetInt32BE(int offset)
|
||||
{
|
||||
return (int)GetUInt32BE(offset);
|
||||
}
|
||||
|
||||
private uint GetUInt32BE(int offset)
|
||||
{
|
||||
return SwapEndianness(BitConverter.ToUInt32(Bytes, offset));
|
||||
}
|
||||
|
||||
private static uint SwapEndianness(uint x)
|
||||
{
|
||||
return ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
private static ulong SwapEndianness(ulong x)
|
||||
{
|
||||
return ((ulong)SwapEndianness((uint)x) << 32) | SwapEndianness((uint)(x >> 32));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents leap second warning from the server that instructs the client to add or remove leap second.
|
||||
/// </summary>
|
||||
/// <seealso cref="NtpPacket.LeapIndicator" />
|
||||
public enum NtpLeapIndicator
|
||||
{
|
||||
/// <summary>
|
||||
/// No leap second warning. No action required.
|
||||
/// </summary>
|
||||
NoWarning,
|
||||
|
||||
/// <summary>
|
||||
/// Warns the client that the last minute of the current day has 61 seconds.
|
||||
/// </summary>
|
||||
LastMinuteHas61Seconds,
|
||||
|
||||
/// <summary>
|
||||
/// Warns the client that the last minute of the current day has 59 seconds.
|
||||
/// </summary>
|
||||
LastMinuteHas59Seconds,
|
||||
|
||||
/// <summary>
|
||||
/// Special value indicating that the server clock is unsynchronized and the returned time is unreliable.
|
||||
/// </summary>
|
||||
AlarmCondition
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes SNTP packet mode, i.e. client or server.
|
||||
/// </summary>
|
||||
/// <seealso cref="NtpPacket.Mode" />
|
||||
public enum NtpMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies client-to-server SNTP packet.
|
||||
/// </summary>
|
||||
Client = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Identifies server-to-client SNTP packet.
|
||||
/// </summary>
|
||||
Server = 4,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1ab93956916692409ea5f00f581db6f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
internal sealed class NtpRequest
|
||||
{
|
||||
private const int ResendTimer = 1000;
|
||||
private const int KillTimer = 10000;
|
||||
public const int DefaultPort = 123;
|
||||
private readonly IPEndPoint _ntpEndPoint;
|
||||
private int _resendTime = ResendTimer;
|
||||
private int _killTime = 0;
|
||||
|
||||
public NtpRequest(IPEndPoint endPoint)
|
||||
{
|
||||
_ntpEndPoint = endPoint;
|
||||
}
|
||||
|
||||
public bool NeedToKill => _killTime >= KillTimer;
|
||||
|
||||
public bool Send(Socket socket, int time)
|
||||
{
|
||||
_resendTime += time;
|
||||
_killTime += time;
|
||||
if (_resendTime < ResendTimer)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var packet = new NtpPacket();
|
||||
try
|
||||
{
|
||||
int sendCount = socket.SendTo(packet.Bytes, 0, packet.Bytes.Length, SocketFlags.None, _ntpEndPoint);
|
||||
return sendCount == packet.Bytes.Length;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2feec8d850902ea4daaad8d9b1dec189
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Transport/LiteNetLib Transport/LiteNetLib/package.json
Normal file
11
Transport/LiteNetLib Transport/LiteNetLib/package.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "com.revenantx.litenetlib",
|
||||
"version": "1.0.1-1",
|
||||
"displayName": "LiteNetLib",
|
||||
"description": "Lite reliable UDP library for .NET Standard 2.0 (Mono, .NET Core, .NET Framework)",
|
||||
"unity": "2018.3",
|
||||
"author": {
|
||||
"name": "RevenantX",
|
||||
"url": "https://github.com/RevenantX"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64eaff286dd146747bc5b21fda9c8213
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5b519f64e02c1ed48a5a00039c4f6c4e, type: 3}
|
||||
m_Name: LiteNetLibTransportProvider
|
||||
m_EditorClassIdentifier:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45d8249dfdf511d429f92c6a4a4e26f4
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
279
Transport/LiteNetLib Transport/LiteNetLibTransportProvider.cs
Normal file
279
Transport/LiteNetLib Transport/LiteNetLibTransportProvider.cs
Normal file
@@ -0,0 +1,279 @@
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime;
|
||||
using System;
|
||||
using Netick.Unity;
|
||||
|
||||
namespace Netick.Transport
|
||||
{
|
||||
[CreateAssetMenu(fileName = "LiteNetLibTransportProvider", menuName = "Netick/Transport/LiteNetLibTransportProvider", order = 1)]
|
||||
public class LiteNetLibTransportProvider : NetworkTransportProvider
|
||||
{
|
||||
public override NetworkTransport MakeTransportInstance() => new LiteNetLibTransport();
|
||||
}
|
||||
|
||||
public class LiteNetLibTransport : NetworkTransport, INetEventListener
|
||||
{
|
||||
public class LNLConnection : TransportConnection
|
||||
{
|
||||
public LiteNetLibTransport Transport;
|
||||
public NetPeer LNLPeer;
|
||||
public override IEndPoint EndPoint => LNLPeer.EndPoint.ToNetickEndPoint();
|
||||
|
||||
public override int Mtu => LNLPeer.Mtu;
|
||||
|
||||
public LNLConnection(LiteNetLibTransport transport)
|
||||
{
|
||||
Transport = transport;
|
||||
}
|
||||
|
||||
public unsafe override void Send(IntPtr ptr, int length)
|
||||
{
|
||||
byte* p = (byte*)ptr.ToPointer();
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
Transport._bytes[i] = p[i];
|
||||
|
||||
LNLPeer.Send(Transport._bytes, 0, length, DeliveryMethod.Unreliable);
|
||||
}
|
||||
}
|
||||
|
||||
private NetManager _netManager;
|
||||
|
||||
|
||||
private BitBuffer _buffer;
|
||||
// private int _bufferSize;
|
||||
|
||||
private readonly byte[] _bytes = new byte[2048];
|
||||
private readonly byte[] _connectionBytes = new byte[200];
|
||||
|
||||
private int _port;
|
||||
private bool _isServer = false;
|
||||
private Dictionary<NetPeer, LNLConnection> _clients = new Dictionary<NetPeer, LNLConnection>();
|
||||
private Queue<LNLConnection> _freeClients = new Queue<LNLConnection>();
|
||||
|
||||
// LAN Matchmaking
|
||||
private List<Session> _sessions = new List<Session>();
|
||||
private NetDataWriter _writer = new NetDataWriter();
|
||||
private string _machineName;
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
_buffer = new BitBuffer(createChunks: false);
|
||||
// _bufferSize = 875 * 4;
|
||||
|
||||
_netManager = new NetManager((INetEventListener)this) { AutoRecycle = true };
|
||||
_machineName = Environment.MachineName;
|
||||
//_netManager.DisconnectTimeout = 1000;
|
||||
|
||||
for (int i = 0; i < Engine.Config.MaxPlayers; i++)
|
||||
_freeClients.Enqueue(new LNLConnection(this));
|
||||
}
|
||||
|
||||
public override void PollEvents()
|
||||
{
|
||||
_netManager.PollEvents();
|
||||
}
|
||||
|
||||
public override void ForceUpdate()
|
||||
{
|
||||
_netManager.TriggerUpdate();
|
||||
}
|
||||
|
||||
public override void Run(RunMode mode, int port)
|
||||
{
|
||||
if (mode == RunMode.Client)
|
||||
{
|
||||
_netManager.UnconnectedMessagesEnabled = true;
|
||||
_netManager.Start();
|
||||
_isServer = false;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_netManager.BroadcastReceiveEnabled = true;
|
||||
_netManager.Start(port);
|
||||
_isServer = true;
|
||||
}
|
||||
|
||||
_port = port;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
_netManager.Stop();
|
||||
}
|
||||
|
||||
public override void Connect(string address, int port, byte[] connectionData, int connectionDataLen)
|
||||
{
|
||||
if (!_netManager.IsRunning)
|
||||
_netManager.Start();
|
||||
|
||||
if (connectionData == null)
|
||||
{
|
||||
_netManager.Connect(address, port, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.Reset();
|
||||
_writer.Put(connectionData, 0, connectionDataLen);
|
||||
_netManager.Connect(address, port, _writer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Disconnect(TransportConnection connection)
|
||||
{
|
||||
_netManager.DisconnectPeer(((LNLConnection)connection).LNLPeer);
|
||||
}
|
||||
|
||||
//public override void HostMatch(string name)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
//public override void UpdateMatchList()
|
||||
//{
|
||||
// if (!_netManager.IsRunning)
|
||||
// _netManager.Start();
|
||||
|
||||
// _sessions.Clear();
|
||||
// _writer.Reset();
|
||||
// _writer.Put(NetickConfig.LAN_DISCOVERY);
|
||||
// _netManager.SendBroadcast(_writer, _port);
|
||||
//}
|
||||
|
||||
/// ////////////////////////////////////////////
|
||||
|
||||
void INetEventListener.OnConnectionRequest(ConnectionRequest request)
|
||||
{
|
||||
if (_clients.Count >= Engine.Config.MaxPlayers)
|
||||
{
|
||||
request.Reject();
|
||||
return;
|
||||
}
|
||||
|
||||
int len = request.Data.AvailableBytes;
|
||||
request.Data.GetBytes(_connectionBytes, 0, len);
|
||||
bool accepted = NetworkPeer.OnConnectRequest(_connectionBytes, len, request.RemoteEndPoint.ToNetickEndPoint());
|
||||
|
||||
if (accepted)
|
||||
request.Accept();
|
||||
else
|
||||
request.Reject();
|
||||
}
|
||||
|
||||
void INetEventListener.OnPeerConnected(NetPeer peer)
|
||||
{
|
||||
var connection = _freeClients.Dequeue();
|
||||
connection.LNLPeer = peer;
|
||||
|
||||
_clients.Add(peer, connection);
|
||||
NetworkPeer.OnConnected(connection);
|
||||
}
|
||||
|
||||
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
|
||||
{
|
||||
if (!_isServer)
|
||||
{
|
||||
if (disconnectInfo.Reason == DisconnectReason.ConnectionRejected || disconnectInfo.Reason == DisconnectReason.ConnectionFailed)
|
||||
{
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
return;
|
||||
}
|
||||
|
||||
if (peer == null)
|
||||
{
|
||||
Debug.Log($"ERROR: {disconnectInfo.Reason}");
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (peer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_clients.ContainsKey(peer))
|
||||
{
|
||||
TransportDisconnectReason reason = disconnectInfo.Reason == DisconnectReason.Timeout ? TransportDisconnectReason.Timeout : TransportDisconnectReason.Shutdown;
|
||||
|
||||
NetworkPeer.OnDisconnected(_clients[peer], reason);
|
||||
_freeClients.Enqueue(_clients[peer]);
|
||||
_clients.Remove(peer);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
|
||||
{
|
||||
if (_clients.TryGetValue(peer, out var c))
|
||||
{
|
||||
var len = reader.AvailableBytes;
|
||||
reader.GetBytes(_bytes, 0, reader.AvailableBytes);
|
||||
|
||||
fixed(byte* ptr = _bytes)
|
||||
{
|
||||
_buffer.SetFrom(ptr, len, _bytes.Length);
|
||||
NetworkPeer.Receive(c, _buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
|
||||
{
|
||||
//ulong msgType = reader.GetULong();
|
||||
|
||||
//if (msgType == NetickConfig.LAN_DISCOVERY_RESPONSE)
|
||||
//{
|
||||
// string name = reader.GetString();
|
||||
// int port = reader.GetInt();
|
||||
|
||||
// var newSession = new Session()
|
||||
// {
|
||||
// Name = name,
|
||||
// IP = remoteEndPoint.Address.ToString(),
|
||||
// Port = port
|
||||
// };
|
||||
|
||||
// if (!_sessions.Contains(newSession))
|
||||
// _sessions.Add(newSession);
|
||||
|
||||
// OnMatchListUpdate(_sessions);
|
||||
//}
|
||||
|
||||
//else if (_isServer && msgType == NetickConfig.LAN_DISCOVERY)
|
||||
//{
|
||||
// _writer.Reset();
|
||||
// _writer.Put(NetickConfig.LAN_DISCOVERY_RESPONSE);
|
||||
// _writer.Put(_machineName);
|
||||
// _writer.Put(_port);
|
||||
|
||||
// _netManager.SendUnconnectedMessage(_writer, remoteEndPoint);
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketError)
|
||||
{
|
||||
Debug.Log("[S] NetworkError: " + socketError);
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) { }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b519f64e02c1ed48a5a00039c4f6c4e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user