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

Commit

Permalink
Fix for the latest mirror, rework connect code handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalPetryka committed Nov 9, 2019
1 parent 8b0a74e commit 2535c7e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Net.Sockets;
using LiteNetLib;
using LiteNetLib.Utils;
using UnityEngine;

namespace Mirror.LiteNetLib4Mirror
Expand All @@ -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
{
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using LiteNetLib;
using LiteNetLib.Utils;
using LiteNetLib4Mirror.Open.Nat;
using UnityEngine;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
Expand All @@ -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()
{
Expand All @@ -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();
Expand All @@ -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<byte> data)
Expand All @@ -200,7 +219,7 @@ public override bool ServerActive()

public override void ServerStart()
{
LiteNetLib4MirrorServer.StartServer(GenerateCode());
LiteNetLib4MirrorServer.StartServer(GetConnectKey());
}

public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> data)
Expand Down

0 comments on commit 2535c7e

Please sign in to comment.