diff --git a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorClient.cs b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorClient.cs index 7d32801..7aa7a57 100644 --- a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorClient.cs +++ b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorClient.cs @@ -2,6 +2,7 @@ using System.Net; using System.Net.Sockets; using LiteNetLib; +using LiteNetLib.Utils; using UnityEngine; namespace Mirror.LiteNetLib4Mirror @@ -23,7 +24,7 @@ internal static bool IsConnected() return LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.ClientConnected || LiteNetLib4MirrorCore.State == LiteNetLib4MirrorCore.States.ClientConnecting; } - internal static void ConnectClient(string code) + internal static void ConnectClient(NetDataWriter data) { try { @@ -41,7 +42,7 @@ internal static void ConnectClient(string code) LiteNetLib4MirrorCore.SetOptions(false); LiteNetLib4MirrorCore.Host.Start(); - LiteNetLib4MirrorCore.Host.Connect(LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.clientAddress, LiteNetLib4MirrorTransport.Singleton.port), code); + LiteNetLib4MirrorCore.Host.Connect(LiteNetLib4MirrorUtils.Parse(LiteNetLib4MirrorTransport.Singleton.clientAddress, LiteNetLib4MirrorTransport.Singleton.port), data); LiteNetLib4MirrorTransport.Polling = true; LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.ClientConnecting; @@ -62,13 +63,18 @@ private static void OnPeerConnected(NetPeer peer) private static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo) { - if (disconnectinfo.AdditionalData.TryGetString(out string reason) && !string.IsNullOrWhiteSpace(reason)) + switch (disconnectinfo.Reason) { - LastDisconnectReason = LiteNetLib4MirrorUtils.FromBase64(reason); - } - else - { - LastDisconnectReason = null; + case DisconnectReason.ConnectionRejected: + LiteNetLib4MirrorTransport.Singleton.OnConncetionRefused(disconnectinfo); + LastDisconnectReason = null; + break; + case DisconnectReason.DisconnectPeerCalled when disconnectinfo.AdditionalData.TryGetString(out string reason) && !string.IsNullOrWhiteSpace(reason): + LastDisconnectReason = LiteNetLib4MirrorUtils.FromBase64(reason); + break; + default: + LastDisconnectReason = null; + break; } LiteNetLib4MirrorCore.State = LiteNetLib4MirrorCore.States.Idle; LiteNetLib4MirrorCore.LastDisconnectError = disconnectinfo.SocketErrorCode; diff --git a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorCore.cs b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorCore.cs index 78c309d..7abf312 100644 --- a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorCore.cs +++ b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorCore.cs @@ -5,7 +5,7 @@ namespace Mirror.LiteNetLib4Mirror { public static class LiteNetLib4MirrorCore { - public const string TransportVersion = "1.2.7"; + public const string TransportVersion = "1.2.8"; public static SocketError LastError { get; internal set; } public static SocketError LastDisconnectError { get; internal set; } public static DisconnectReason LastDisconnectReason { get; internal set; } diff --git a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorServer.cs b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorServer.cs index 05d4398..dce4bc6 100644 --- a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorServer.cs +++ b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorServer.cs @@ -109,7 +109,7 @@ private static void OnConnectionRequest(ConnectionRequest request) { try { - LiteNetLib4MirrorTransport.Singleton.ProcessConnectionRequest(request, request.Data.PeekString()); + LiteNetLib4MirrorTransport.Singleton.ProcessConnectionRequest(request); } catch (Exception ex) { diff --git a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorTransport.cs b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorTransport.cs index 88cdae7..393b131 100644 --- a/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorTransport.cs +++ b/LiteNetLib4Mirror/Assets/Mirror/Runtime/Transport/LiteNetLib4Mirror/LiteNetLib4MirrorTransport.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using LiteNetLib; +using LiteNetLib.Utils; using LiteNetLib4Mirror.Open.Nat; using UnityEngine; @@ -114,13 +115,14 @@ public class LiteNetLib4MirrorTransport : Transport, ISegmentTransport public UnityEventIntError onServerSocketError; internal static bool Polling; + private static readonly NetDataWriter ConnectWriter = new NetDataWriter(); #region Overridable methods - protected internal virtual string GenerateCode() + protected internal virtual void GetConnectData(NetDataWriter writer) { - return LiteNetLib4MirrorUtils.ToBase64(Application.productName + Application.companyName + Application.unityVersion + LiteNetLib4MirrorCore.TransportVersion + Singleton.authCode); + writer.Put(GetConnectKey()); } - protected internal virtual void ProcessConnectionRequest(ConnectionRequest request, string code) + protected internal virtual void ProcessConnectionRequest(ConnectionRequest request) { if (LiteNetLib4MirrorCore.Host.PeersCount >= maxConnections) { @@ -131,6 +133,11 @@ protected internal virtual void ProcessConnectionRequest(ConnectionRequest reque Debug.LogWarning("Client tried to join with an invalid auth code! Current code:" + LiteNetLib4MirrorServer.Code); } } + + protected internal virtual void OnConncetionRefused(DisconnectInfo disconnectinfo) + { + + } #endregion internal void InitializeTransport() @@ -142,6 +149,11 @@ internal void InitializeTransport() } } + private static string GetConnectKey() + { + return LiteNetLib4MirrorUtils.ToBase64(Application.productName + Application.companyName + Application.unityVersion + LiteNetLib4MirrorCore.TransportVersion + Singleton.authCode); + } + #region Unity Functions private void Awake() { @@ -168,6 +180,11 @@ private void OnDestroy() #endregion #region Transport Overrides + public override bool Available() + { + return Application.platform != RuntimePlatform.WebGLPlayer; + } + public override bool ClientConnected() { return LiteNetLib4MirrorClient.IsConnected(); @@ -176,7 +193,9 @@ public override bool ClientConnected() public override void ClientConnect(string address) { clientAddress = address; - LiteNetLib4MirrorClient.ConnectClient(GenerateCode()); + ConnectWriter.Reset(); + GetConnectData(ConnectWriter); + LiteNetLib4MirrorClient.ConnectClient(ConnectWriter); } public override bool ClientSend(int channelId, ArraySegment data) @@ -200,7 +219,7 @@ public override bool ServerActive() public override void ServerStart() { - LiteNetLib4MirrorServer.StartServer(GenerateCode()); + LiteNetLib4MirrorServer.StartServer(GetConnectKey()); } public override bool ServerSend(List connectionIds, int channelId, ArraySegment data)