mirror of
https://github.com/Kaveinator/NetickProForUnity.git
synced 2025-10-26 01:49:05 -07:00
Added settings to LiteNetLibTransportProvider. In addition, made general improvements and fixed a bug when sending user data with a byte size bigger than _bytes size.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -12,3 +12,7 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 5b519f64e02c1ed48a5a00039c4f6c4e, type: 3}
|
||||
m_Name: LiteNetLibTransportProvider
|
||||
m_EditorClassIdentifier:
|
||||
DisconnectTimeout: 5
|
||||
ReconnectInterval: 0.5
|
||||
MaxConnectAttempts: 10
|
||||
UpdateInterval: 0.015
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime;
|
||||
using System;
|
||||
using Netick.Unity;
|
||||
|
||||
@@ -14,7 +13,16 @@ namespace Netick.Transport
|
||||
[CreateAssetMenu(fileName = "LiteNetLibTransportProvider", menuName = "Netick/Transport/LiteNetLibTransportProvider", order = 1)]
|
||||
public class LiteNetLibTransportProvider : NetworkTransportProvider
|
||||
{
|
||||
public override NetworkTransport MakeTransportInstance() => new LiteNetLibTransport();
|
||||
[Tooltip("Time duration (in seconds) until a connection is dropped when no packets were received.")]
|
||||
public float DisconnectTimeout = 5;
|
||||
[Tooltip("Time interval (in seconds) between connection attempts.")]
|
||||
public float ReconnectInterval = 0.5f;
|
||||
[Tooltip("Max number of connect attempts.")]
|
||||
public int MaxConnectAttempts = 10;
|
||||
[Tooltip("LiteNetLib internal logic update interval (in seconds).")]
|
||||
public float UpdateInterval = 0.015f;
|
||||
|
||||
public override NetworkTransport MakeTransportInstance() => new LiteNetLibTransport(this);
|
||||
}
|
||||
|
||||
public class LiteNetLibTransport : NetworkTransport, INetEventListener
|
||||
@@ -44,36 +52,45 @@ namespace Netick.Transport
|
||||
|
||||
private unsafe void SendLNL(byte* ptr, int length, DeliveryMethod deliveryMethod)
|
||||
{
|
||||
if (Transport._bytes.Length < length)
|
||||
Transport._bytes = new byte[length];
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
Transport._bytes[i] = ptr[i];
|
||||
LNLPeer.Send(Transport._bytes, 0, length, deliveryMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private LiteNetLibTransportProvider _provider;
|
||||
private NetManager _netManager;
|
||||
private BitBuffer _buffer;
|
||||
|
||||
private readonly byte[] _bytes = new byte[2048];
|
||||
private 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
|
||||
// LAN Discovery
|
||||
private List<Session> _sessions = new List<Session>();
|
||||
private NetDataWriter _writer = new NetDataWriter();
|
||||
private string _machineName;
|
||||
|
||||
public LiteNetLibTransport(LiteNetLibTransportProvider provider)
|
||||
{
|
||||
this._provider = provider;
|
||||
}
|
||||
|
||||
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;
|
||||
_netManager.DisconnectTimeout = (int)( _provider.DisconnectTimeout * 1000);
|
||||
_netManager.ReconnectDelay = (int)(_provider.ReconnectInterval * 1000);
|
||||
_netManager.MaxConnectAttempts = _provider.MaxConnectAttempts;
|
||||
_netManager.UpdateTime = (int)(_provider.UpdateInterval * 1000);
|
||||
|
||||
for (int i = 0; i < Engine.Config.MaxPlayers; i++)
|
||||
_freeClients.Enqueue(new LNLConnection(this));
|
||||
@@ -95,14 +112,12 @@ namespace Netick.Transport
|
||||
{
|
||||
_netManager.UnconnectedMessagesEnabled = true;
|
||||
_netManager.Start();
|
||||
_isServer = false;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_netManager.BroadcastReceiveEnabled = true;
|
||||
_netManager.Start(port);
|
||||
_isServer = true;
|
||||
}
|
||||
|
||||
_port = port;
|
||||
@@ -124,12 +139,10 @@ namespace Netick.Transport
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.Reset();
|
||||
_writer.Put(connectionData, 0, connectionDataLen);
|
||||
_writer. Reset();
|
||||
_writer. Put(connectionData, 0, connectionDataLen);
|
||||
_netManager.Connect(address, port, _writer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Disconnect(TransportConnection connection)
|
||||
@@ -137,24 +150,6 @@ namespace Netick.Transport
|
||||
_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)
|
||||
@@ -178,23 +173,29 @@ namespace Netick.Transport
|
||||
var connection = _freeClients.Dequeue();
|
||||
connection.LNLPeer = peer;
|
||||
|
||||
_clients.Add(peer, connection);
|
||||
_clients. Add(peer, connection);
|
||||
NetworkPeer.OnConnected(connection);
|
||||
}
|
||||
|
||||
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
|
||||
{
|
||||
if (!_isServer)
|
||||
if (!Engine.IsServer)
|
||||
{
|
||||
if (disconnectInfo.Reason == DisconnectReason.ConnectionRejected || disconnectInfo.Reason == DisconnectReason.ConnectionFailed)
|
||||
if (disconnectInfo.Reason == DisconnectReason.ConnectionRejected)
|
||||
{
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
return;
|
||||
}
|
||||
|
||||
if (disconnectInfo.Reason == DisconnectReason.ConnectionFailed || disconnectInfo.Reason == DisconnectReason.Timeout)
|
||||
{
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Timeout);
|
||||
return;
|
||||
}
|
||||
|
||||
if (peer == null)
|
||||
{
|
||||
Debug.Log($"ERROR: {disconnectInfo.Reason}");
|
||||
Debug.Log($"LiteNetLib Network Error: {disconnectInfo.Reason}");
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
return;
|
||||
}
|
||||
@@ -210,9 +211,9 @@ namespace Netick.Transport
|
||||
{
|
||||
TransportDisconnectReason reason = disconnectInfo.Reason == DisconnectReason.Timeout ? TransportDisconnectReason.Timeout : TransportDisconnectReason.Shutdown;
|
||||
|
||||
NetworkPeer.OnDisconnected(_clients[peer], reason);
|
||||
NetworkPeer. OnDisconnected(_clients[peer], reason);
|
||||
_freeClients.Enqueue(_clients[peer]);
|
||||
_clients.Remove(peer);
|
||||
_clients. Remove(peer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,61 +222,27 @@ namespace Netick.Transport
|
||||
if (_clients.TryGetValue(peer, out var c))
|
||||
{
|
||||
var len = reader.AvailableBytes;
|
||||
reader.GetBytes(_bytes, 0, reader.AvailableBytes);
|
||||
reader. GetBytes(_bytes, 0, reader.AvailableBytes);
|
||||
|
||||
fixed(byte* ptr = _bytes)
|
||||
{
|
||||
_buffer.SetFrom(ptr, len, _bytes.Length);
|
||||
_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);
|
||||
Debug.Log("LiteNetLib Network Error: " + socketError);
|
||||
NetworkPeer.OnConnectFailed(ConnectionFailedReason.Refused);
|
||||
}
|
||||
|
||||
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) { }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "com.karrar.netick",
|
||||
"version": "0.12.22",
|
||||
"version": "0.12.23",
|
||||
"displayName": "Netick",
|
||||
"description": "A networking solution for Unity",
|
||||
"unity": "2021.3",
|
||||
|
||||
Reference in New Issue
Block a user