Luke Stampfli
4 年前
当前提交
b6a4ffea
共有 77 个文件被更改,包括 8248 次插入 和 0 次删除
-
8Packages/packages-lock.json
-
5Packages/com.unity.multiplayer.transport.litenet/CHANGELOG.md
-
7Packages/com.unity.multiplayer.transport.litenet/CHANGELOG.md.meta
-
21Packages/com.unity.multiplayer.transport.litenet/LICENSE.md
-
7Packages/com.unity.multiplayer.transport.litenet/LICENSE.md.meta
-
1Packages/com.unity.multiplayer.transport.litenet/README.md
-
7Packages/com.unity.multiplayer.transport.litenet/README.md.meta
-
8Packages/com.unity.multiplayer.transport.litenet/Runtime.meta
-
8Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib.meta
-
31Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/BaseChannel.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/BaseChannel.cs.meta
-
137Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/ConnectionRequest.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/ConnectionRequest.cs.meta
-
224Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/INetEventListener.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/INetEventListener.cs.meta
-
8Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers.meta
-
36Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/Crc32cLayer.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/Crc32cLayer.cs.meta
-
15Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/PacketLayerBase.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/PacketLayerBase.cs.meta
-
59Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/XorEncryptLayer.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Layers/XorEncryptLayer.cs.meta
-
7Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/LiteNetLib.csproj.meta
-
245Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NatPunchModule.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NatPunchModule.cs.meta
-
72Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetConstants.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetConstants.cs.meta
-
92Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetDebug.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetDebug.cs.meta
-
1001Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetManager.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetManager.cs.meta
-
276Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPacket.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPacket.cs.meta
-
74Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPacketPool.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPacketPool.cs.meta
-
1001Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPeer.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPeer.cs.meta
-
483Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetSocket.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetSocket.cs.meta
-
39Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetStatistics.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetStatistics.cs.meta
-
197Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetUtils.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetUtils.cs.meta
-
323Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/ReliableChannel.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/ReliableChannel.cs.meta
-
99Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/SequencedChannel.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/SequencedChannel.cs.meta
-
8Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils.meta
-
111Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/CRC32C.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/CRC32C.cs.meta
-
118Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/FastBitConverter.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/FastBitConverter.cs.meta
-
8Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/INetSerializable.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/INetSerializable.cs.meta
-
694Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetDataReader.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetDataReader.cs.meta
-
382Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetDataWriter.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetDataWriter.cs.meta
-
314Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetPacketProcessor.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetPacketProcessor.cs.meta
-
752Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetSerializer.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NetSerializer.cs.meta
-
426Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NtpPacket.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NtpPacket.cs.meta
-
175Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NtpRequest.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/Utils/NtpRequest.cs.meta
-
415Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLibTransport.cs
-
11Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLibTransport.cs.meta
-
16Packages/com.unity.multiplayer.transport.litenet/Runtime/com.unity.multiplayer.transport.litenet.asmdef
-
7Packages/com.unity.multiplayer.transport.litenet/Runtime/com.unity.multiplayer.transport.litenet.asmdef.meta
-
16Packages/com.unity.multiplayer.transport.litenet/package.json
-
7Packages/com.unity.multiplayer.transport.litenet/package.json.meta
|
|||
Changelog |
|||
All notable changes to this package will be documented in this file. The format is based on Keep a Changelog |
|||
|
|||
[0.0.1-preview.1] - 2020-10-05 |
|||
This is the first release of Enet MLAPI Package |
|
|||
fileFormatVersion: 2 |
|||
guid: 28f71d7dc1ed3bc4ba12892aa139f9e6 |
|||
TextScriptImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
MIT License |
|||
|
|||
Copyright (c) 2018, 2019 Albin Corén |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
|
|||
fileFormatVersion: 2 |
|||
guid: 0112cf41b55769347836bdb4bfc2f66e |
|||
TextScriptImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
Enet transport for MLAPI |
|
|||
fileFormatVersion: 2 |
|||
guid: 859976d97fd97dc48adc4bc7ff818f45 |
|||
TextScriptImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 76d634f7c88c1da4cb3ea69d7bae43bb |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 22e683408a542064b9a7e0670cba52a2 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace LiteNetLib |
|||
{ |
|||
internal abstract class BaseChannel |
|||
{ |
|||
public BaseChannel Next; |
|||
protected readonly NetPeer Peer; |
|||
protected readonly Queue<NetPacket> OutgoingQueue; |
|||
|
|||
protected BaseChannel(NetPeer peer) |
|||
{ |
|||
Peer = peer; |
|||
OutgoingQueue = new Queue<NetPacket>(64); |
|||
} |
|||
|
|||
public int PacketsInQueue |
|||
{ |
|||
get { return OutgoingQueue.Count; } |
|||
} |
|||
|
|||
public void AddToQueue(NetPacket packet) |
|||
{ |
|||
lock (OutgoingQueue) |
|||
OutgoingQueue.Enqueue(packet); |
|||
} |
|||
|
|||
public abstract void SendNextPackets(); |
|||
public abstract bool ProcessPacket(NetPacket packet); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a74c865f121743d479838192cd3b2e06 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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 readonly NetDataReader Data; |
|||
|
|||
internal ConnectionRequestResult Result { get; private set; } |
|||
internal long ConnectionTime; |
|||
internal byte ConnectionNumber; |
|||
public readonly IPEndPoint RemoteEndPoint; |
|||
|
|||
private bool TryActivate() |
|||
{ |
|||
return Interlocked.CompareExchange(ref _used, 1, 0) == 0; |
|||
} |
|||
|
|||
internal void UpdateRequest(NetConnectRequestPacket connRequest) |
|||
{ |
|||
if (connRequest.ConnectionTime >= ConnectionTime) |
|||
{ |
|||
ConnectionTime = connRequest.ConnectionTime; |
|||
ConnectionNumber = connRequest.ConnectionNumber; |
|||
} |
|||
} |
|||
|
|||
internal ConnectionRequest( |
|||
long connectionId, |
|||
byte connectionNumber, |
|||
NetDataReader netDataReader, |
|||
IPEndPoint endPoint, |
|||
NetManager listener) |
|||
{ |
|||
ConnectionTime = connectionId; |
|||
ConnectionNumber = connectionNumber; |
|||
RemoteEndPoint = endPoint; |
|||
Data = netDataReader; |
|||
_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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ab9cb220487967e4ba764971b6eb9fac |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Net; |
|||
using System.Net.Sockets; |
|||
|
|||
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 |
|||
} |
|||
|
|||
/// <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="deliveryMethod">Type of received packet</param>
|
|||
void OnNetworkReceive(NetPeer peer, NetPacketReader reader, 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 class EventBasedNetListener : INetEventListener, IDeliveryEventListener |
|||
{ |
|||
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, 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 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 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; |
|||
} |
|||
|
|||
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, DeliveryMethod deliveryMethod) |
|||
{ |
|||
if (NetworkReceiveEvent != null) |
|||
NetworkReceiveEvent(peer, reader, 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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 28881074a6c781a439d38403a3182c2b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: aebc2d698872c56429dc96aa2ddcd270 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using LiteNetLib.Utils; |
|||
using System; |
|||
|
|||
namespace LiteNetLib.Layers |
|||
{ |
|||
public sealed class Crc32cLayer : PacketLayerBase |
|||
{ |
|||
public Crc32cLayer() : base(CRC32C.ChecksumSize) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public override void ProcessInboundPacket(ref byte[] data, ref int length) |
|||
{ |
|||
if (length < NetConstants.HeaderSize + CRC32C.ChecksumSize) |
|||
{ |
|||
NetDebug.WriteError("[NM] DataReceived size: bad!"); |
|||
return; |
|||
} |
|||
|
|||
int checksumPoint = length - CRC32C.ChecksumSize; |
|||
if (CRC32C.Compute(data, 0, checksumPoint) != BitConverter.ToUInt32(data, checksumPoint)) |
|||
{ |
|||
NetDebug.Write("[NM] DataReceived checksum: bad!"); |
|||
return; |
|||
} |
|||
length -= CRC32C.ChecksumSize; |
|||
} |
|||
|
|||
public override void ProcessOutBoundPacket(ref byte[] data, ref int offset, ref int length) |
|||
{ |
|||
FastBitConverter.GetBytes(data, length, CRC32C.Compute(data, offset, length)); |
|||
length += CRC32C.ChecksumSize; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d658f1c6b66f5684780b95233d5fe00b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace LiteNetLib.Layers |
|||
{ |
|||
public abstract class PacketLayerBase |
|||
{ |
|||
public readonly int ExtraPacketSizeForLayer; |
|||
|
|||
protected PacketLayerBase(int extraPacketSizeForLayer) |
|||
{ |
|||
ExtraPacketSizeForLayer = extraPacketSizeForLayer; |
|||
} |
|||
|
|||
public abstract void ProcessInboundPacket(ref byte[] data, ref int length); |
|||
public abstract void ProcessOutBoundPacket(ref byte[] data, ref int offset, ref int length); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: aba74192fa2edf74cac4992b18f7bc84 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Text; |
|||
|
|||
namespace LiteNetLib.Layers |
|||
{ |
|||
public class XorEncryptLayer : PacketLayerBase |
|||
{ |
|||
private byte[] _byteKey; |
|||
|
|||
public XorEncryptLayer() : base(0) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public XorEncryptLayer(byte[] key) : this() |
|||
{ |
|||
SetKey(key); |
|||
} |
|||
|
|||
public XorEncryptLayer(string key) : this() |
|||
{ |
|||
SetKey(key); |
|||
} |
|||
|
|||
public void SetKey(string key) |
|||
{ |
|||
_byteKey = Encoding.UTF8.GetBytes(key); |
|||
} |
|||
|
|||
public void SetKey(byte[] key) |
|||
{ |
|||
if (_byteKey == null || _byteKey.Length != key.Length) |
|||
_byteKey = new byte[key.Length]; |
|||
Buffer.BlockCopy(key, 0, _byteKey, 0, key.Length); |
|||
} |
|||
|
|||
public override void ProcessInboundPacket(ref byte[] data, ref int length) |
|||
{ |
|||
if (_byteKey == null) |
|||
return; |
|||
for (var i = 0; i < length; i++) |
|||
{ |
|||
var offset = i % _byteKey.Length; |
|||
data[i] = (byte)(data[i] ^ _byteKey[offset]); |
|||
} |
|||
} |
|||
|
|||
public override void ProcessOutBoundPacket(ref byte[] data, ref int offset, ref int length) |
|||
{ |
|||
if (_byteKey == null) |
|||
return; |
|||
var cur = offset; |
|||
for (var i = 0; i < length; i++, cur++) |
|||
{ |
|||
data[cur] = (byte)(data[cur] ^ _byteKey[i % _byteKey.Length]); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0aca7a11bf01a5146a4e435b448bf342 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 11c9a199da28e0144b82a7c9dbd440be |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using System.Net; |
|||
using System.Net.Sockets; |
|||
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 NetSocket _socket; |
|||
private readonly Queue<RequestEventData> _requestEvents = new Queue<RequestEventData>(); |
|||
private readonly Queue<SuccessEventData> _successEvents = new Queue<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; |
|||
|
|||
internal NatPunchModule(NetSocket 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() |
|||
{ |
|||
SocketError errorCode = 0; |
|||
_cacheWriter.Reset(); |
|||
_cacheWriter.Put((byte)PacketProperty.NatMessage); |
|||
_netPacketProcessor.Write(_cacheWriter, packet); |
|||
_socket.SendTo(_cacheWriter.Data, 0, _cacheWriter.Length, target, ref errorCode); |
|||
} |
|||
|
|||
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 (_natPunchListener == null || (_successEvents.Count == 0 && _requestEvents.Count == 0)) |
|||
return; |
|||
lock (_successEvents) |
|||
{ |
|||
while (_successEvents.Count > 0) |
|||
{ |
|||
var evt = _successEvents.Dequeue(); |
|||
_natPunchListener.OnNatIntroductionSuccess( |
|||
evt.TargetEndPoint, |
|||
evt.Type, |
|||
evt.Token); |
|||
} |
|||
} |
|||
lock (_requestEvents) |
|||
{ |
|||
while (_requestEvents.Count > 0) |
|||
{ |
|||
var evt = _requestEvents.Dequeue(); |
|||
_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) |
|||
{ |
|||
lock (_requestEvents) |
|||
{ |
|||
_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
|
|||
SocketError errorCode = 0; |
|||
_socket.Ttl = 2; |
|||
_socket.SendTo(new[] { (byte)PacketProperty.Empty }, 0, 1, req.External, ref errorCode); |
|||
|
|||
// 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 {0} - additional info: {1}", |
|||
senderEndPoint, req.Token); |
|||
|
|||
//Release punch success to client; enabling him to Connect() to Sender if token is ok
|
|||
lock (_successEvents) |
|||
{ |
|||
_successEvents.Enqueue(new SuccessEventData |
|||
{ |
|||
TargetEndPoint = senderEndPoint, |
|||
Type = req.IsExternal ? NatAddressType.External : NatAddressType.Internal, |
|||
Token = req.Token |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a7d2dee632c79624ea2b1909415f2755 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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.
|
|||
/// </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 = 11; |
|||
internal const int MaxUdpHeaderSize = 68; |
|||
|
|||
internal static readonly int[] PossibleMtu = |
|||
{ |
|||
576 - MaxUdpHeaderSize, //minimal
|
|||
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)
|
|||
}; |
|||
|
|||
internal static readonly int MaxPacketSize = PossibleMtu[PossibleMtu.Length - 1]; |
|||
|
|||
//peer specific
|
|||
public const byte MaxConnectionNumber = 4; |
|||
|
|||
public const int PacketPoolSize = 1000; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: fff3c8002deb1964b8adfb05e6d70f9f |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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_4 || UNITY_5 || 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, params object[] args) |
|||
{ |
|||
WriteLogic(NetLogLevel.Trace, str, args); |
|||
} |
|||
|
|||
[Conditional("DEBUG_MESSAGES")] |
|||
internal static void Write(NetLogLevel level, string str, params object[] args) |
|||
{ |
|||
WriteLogic(level, str, args); |
|||
} |
|||
|
|||
[Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")] |
|||
internal static void WriteForce(string str, params object[] args) |
|||
{ |
|||
WriteLogic(NetLogLevel.Trace, str, args); |
|||
} |
|||
|
|||
[Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")] |
|||
internal static void WriteForce(NetLogLevel level, string str, params object[] args) |
|||
{ |
|||
WriteLogic(level, str, args); |
|||
} |
|||
|
|||
internal static void WriteError(string str, params object[] args) |
|||
{ |
|||
WriteLogic(NetLogLevel.Error, str, args); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 164aa985149ec81468dbb9bed6d746f0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1001
Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetManager.cs
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 68a7d55eeb581134cb7700df070236f7 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Net; |
|||
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 LastProperty = Enum.GetValues(typeof(PacketProperty)).Length; |
|||
private static readonly int[] HeaderSizes; |
|||
|
|||
static NetPacket() |
|||
{ |
|||
HeaderSizes = new int[LastProperty+1]; |
|||
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 { return (PacketProperty)(RawData[0] & 0x1F); } |
|||
set { RawData[0] = (byte)((RawData[0] & 0xE0) | (byte)value); } |
|||
} |
|||
|
|||
public byte ConnectionNumber |
|||
{ |
|||
get { return (byte)((RawData[0] & 0x60) >> 5); } |
|||
set { RawData[0] = (byte) ((RawData[0] & 0x9F) | (value << 5)); } |
|||
} |
|||
|
|||
public ushort Sequence |
|||
{ |
|||
get { return BitConverter.ToUInt16(RawData, 1); } |
|||
set { FastBitConverter.GetBytes(RawData, 1, value); } |
|||
} |
|||
|
|||
public bool IsFragmented |
|||
{ |
|||
get { return (RawData[0] & 0x80) != 0; } |
|||
} |
|||
|
|||
public void MarkFragmented() |
|||
{ |
|||
RawData[0] |= 0x80; //set first bit
|
|||
} |
|||
|
|||
public byte ChannelId |
|||
{ |
|||
get { return RawData[3]; } |
|||
set { RawData[3] = value; } |
|||
} |
|||
|
|||
public ushort FragmentId |
|||
{ |
|||
get { return BitConverter.ToUInt16(RawData, 4); } |
|||
set { FastBitConverter.GetBytes(RawData, 4, value); } |
|||
} |
|||
|
|||
public ushort FragmentPart |
|||
{ |
|||
get { return BitConverter.ToUInt16(RawData, 6); } |
|||
set { FastBitConverter.GetBytes(RawData, 6, value); } |
|||
} |
|||
|
|||
public ushort FragmentsTotal |
|||
{ |
|||
get { return 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]; |
|||
} |
|||
|
|||
//Packet constructor from byte array
|
|||
public bool FromBytes(byte[] data, int start, int packetSize) |
|||
{ |
|||
//Reading property
|
|||
byte property = (byte)(data[start] & 0x1F); |
|||
bool fragmented = (data[start] & 0x80) != 0; |
|||
int headerSize = HeaderSizes[property]; |
|||
|
|||
if (property > LastProperty || packetSize < headerSize || |
|||
(fragmented && packetSize < headerSize + NetConstants.FragmentHeaderSize) || |
|||
data.Length < start + packetSize) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
Buffer.BlockCopy(data, start, RawData, 0, packetSize); |
|||
Size = (ushort)packetSize; |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
internal sealed class NetConnectRequestPacket |
|||
{ |
|||
public const int HeaderSize = 14; |
|||
public readonly long ConnectionTime; |
|||
public readonly byte ConnectionNumber; |
|||
public readonly byte[] TargetAddress; |
|||
public readonly NetDataReader Data; |
|||
|
|||
private NetConnectRequestPacket(long connectionTime, byte connectionNumber, byte[] targetAddress, NetDataReader data) |
|||
{ |
|||
ConnectionTime = connectionTime; |
|||
ConnectionNumber = connectionNumber; |
|||
TargetAddress = targetAddress; |
|||
Data = data; |
|||
} |
|||
|
|||
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 new id for peer
|
|||
long connectionId = BitConverter.ToInt64(packet.RawData, 5); |
|||
|
|||
//Get target address
|
|||
int addrSize = packet.RawData[13]; |
|||
if (addrSize != 16 && addrSize != 28) |
|||
return null; |
|||
byte[] addressBytes = new byte[addrSize]; |
|||
Buffer.BlockCopy(packet.RawData, 14, 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(connectionId, packet.ConnectionNumber, addressBytes, reader); |
|||
} |
|||
|
|||
public static NetPacket Make(NetDataWriter connectData, SocketAddress addressBytes, long connectId) |
|||
{ |
|||
//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, connectId); |
|||
packet.RawData[13] = (byte)addressBytes.Size; |
|||
for (int i = 0; i < addressBytes.Size; i++) |
|||
packet.RawData[14+i] = addressBytes[i]; |
|||
Buffer.BlockCopy(connectData.Data, 0, packet.RawData, 14+addressBytes.Size, connectData.Length); |
|||
return packet; |
|||
} |
|||
} |
|||
|
|||
internal sealed class NetConnectAcceptPacket |
|||
{ |
|||
public const int Size = 11; |
|||
public readonly long ConnectionId; |
|||
public readonly byte ConnectionNumber; |
|||
public readonly bool IsReusedPeer; |
|||
|
|||
private NetConnectAcceptPacket(long connectionId, byte connectionNumber, bool isReusedPeer) |
|||
{ |
|||
ConnectionId = connectionId; |
|||
ConnectionNumber = connectionNumber; |
|||
IsReusedPeer = isReusedPeer; |
|||
} |
|||
|
|||
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; |
|||
|
|||
return new NetConnectAcceptPacket(connectionId, connectionNumber, isReused == 1); |
|||
} |
|||
|
|||
public static NetPacket Make(long connectId, byte connectNum, bool reusedPeer) |
|||
{ |
|||
var packet = new NetPacket(PacketProperty.ConnectAccept, 0); |
|||
FastBitConverter.GetBytes(packet.RawData, 1, connectId); |
|||
packet.RawData[9] = connectNum; |
|||
packet.RawData[10] = (byte)(reusedPeer ? 1 : 0); |
|||
return packet; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9bbfda42bbf0c64468c913e03e91b6f5 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Threading; |
|||
|
|||
namespace LiteNetLib |
|||
{ |
|||
internal sealed class NetPacketPool |
|||
{ |
|||
private NetPacket _head; |
|||
private int _count; |
|||
|
|||
public NetPacket GetWithData(PacketProperty property, byte[] data, int start, int length) |
|||
{ |
|||
int headerSize = NetPacket.GetHeaderSize(property); |
|||
NetPacket packet = GetPacket(length + headerSize); |
|||
packet.Property = property; |
|||
Buffer.BlockCopy(data, start, packet.RawData, headerSize, length); |
|||
return packet; |
|||
} |
|||
|
|||
//Get packet with size
|
|||
public NetPacket GetWithProperty(PacketProperty property, int size) |
|||
{ |
|||
NetPacket packet = GetPacket(size + NetPacket.GetHeaderSize(property)); |
|||
packet.Property = property; |
|||
return packet; |
|||
} |
|||
|
|||
public NetPacket GetWithProperty(PacketProperty property) |
|||
{ |
|||
NetPacket packet = GetPacket(NetPacket.GetHeaderSize(property)); |
|||
packet.Property = property; |
|||
return packet; |
|||
} |
|||
|
|||
public NetPacket GetPacket(int size) |
|||
{ |
|||
if (size > NetConstants.MaxPacketSize) |
|||
return new NetPacket(size); |
|||
|
|||
NetPacket packet; |
|||
do |
|||
{ |
|||
packet = _head; |
|||
if (packet == null) |
|||
return new NetPacket(size); |
|||
} while (packet != Interlocked.CompareExchange(ref _head, packet.Next, packet)); |
|||
|
|||
_count--; |
|||
packet.Size = size; |
|||
if (packet.RawData.Length < size) |
|||
packet.RawData = new byte[size]; |
|||
return packet; |
|||
} |
|||
|
|||
public void Recycle(NetPacket packet) |
|||
{ |
|||
if (packet.RawData.Length > NetConstants.MaxPacketSize || _count >= NetConstants.PacketPoolSize) |
|||
{ |
|||
//Don't pool big packets. Save memory
|
|||
return; |
|||
} |
|||
|
|||
_count++; |
|||
|
|||
//Clean fragmented flag
|
|||
packet.RawData[0] = 0; |
|||
|
|||
do |
|||
{ |
|||
packet.Next = _head; |
|||
} while (packet.Next != Interlocked.CompareExchange(ref _head, packet, packet.Next)); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: c9de9278957067a47b6fb32e6edcf6a8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1001
Packages/com.unity.multiplayer.transport.litenet/Runtime/LiteNetLib/NetPeer.cs
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 7d8b1e2f1951cd64fa5f3ddaf623f006 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
#if UNITY_5_3_OR_NEWER
|
|||
#define UNITY
|
|||
#if UNITY_IOS && !UNITY_EDITOR
|
|||
using UnityEngine; |
|||
#endif
|
|||
#endif
|
|||
#if NETSTANDARD || NETCOREAPP
|
|||
using System.Runtime.InteropServices; |
|||
#endif
|
|||
|
|||
using System; |
|||
using System.Net; |
|||
using System.Net.Sockets; |
|||
using System.Threading; |
|||
|
|||
namespace LiteNetLib |
|||
{ |
|||
#if UNITY_IOS && !UNITY_EDITOR
|
|||
public class UnitySocketFix : MonoBehaviour |
|||
{ |
|||
internal IPAddress BindAddrIPv4; |
|||
internal IPAddress BindAddrIPv6; |
|||
internal bool Reuse; |
|||
internal IPv6Mode IPv6; |
|||
internal int Port; |
|||
internal bool Paused; |
|||
internal NetSocket Socket; |
|||
|
|||
private void Update() |
|||
{ |
|||
if (Socket == null) |
|||
Destroy(gameObject); |
|||
} |
|||
|
|||
private void OnApplicationPause(bool pause) |
|||
{ |
|||
if (Socket == null) |
|||