Fixed LAN direct connect and joining server via ID without refreshing

This commit is contained in:
Derek S 2021-04-09 09:20:55 -05:00
parent 55a80f62cc
commit c5c2750334
8 changed files with 145 additions and 27 deletions

View file

@ -195,6 +195,59 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 45, y: 180, z: 0} m_LocalEulerAnglesHint: {x: 45, y: 180, z: 0}
--- !u!1 &126344731
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 126344733}
- component: {fileID: 126344732}
m_Layer: 0
m_Name: LRM - Direct Connect
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &126344732
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 126344731}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6b0fecffa3f624585964b0d0eb21b18e, type: 3}
m_Name:
m_EditorClassIdentifier:
Port: 7777
NoDelay: 1
Interval: 10
FastResend: 2
CongestionWindow: 0
SendWindowSize: 4096
ReceiveWindowSize: 4096
debugLog: 0
statisticsGUI: 0
statisticsLog: 0
--- !u!4 &126344733
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 126344731}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1282001518}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &171810013 --- !u!1 &171810013
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1086,6 +1139,7 @@ GameObject:
- component: {fileID: 1282001520} - component: {fileID: 1282001520}
- component: {fileID: 1282001519} - component: {fileID: 1282001519}
- component: {fileID: 1282001521} - component: {fileID: 1282001521}
- component: {fileID: 1282001522}
m_Layer: 0 m_Layer: 0
m_Name: NetworkManager m_Name: NetworkManager
m_TagString: Untagged m_TagString: Untagged
@ -1105,6 +1159,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: m_Children:
- {fileID: 1884364513} - {fileID: 1884364513}
- {fileID: 126344733}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -1177,9 +1232,9 @@ MonoBehaviour:
diconnectedFromRelay: diconnectedFromRelay:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
useNATPunch: 0 useNATPunch: 1
NATPunchtroughPort: 1 NATPunchtroughPort: 1
useLoadBalancer: 0 useLoadBalancer: 1
loadBalancerPort: 7070 loadBalancerPort: 7070
loadBalancerAddress: 172.105.109.117 loadBalancerAddress: 172.105.109.117
serverName: My awesome server! serverName: My awesome server!
@ -1191,6 +1246,21 @@ MonoBehaviour:
m_Calls: [] m_Calls: []
serverStatus: Not Started. serverStatus: Not Started.
serverId: serverId:
region: 1
--- !u!114 &1282001522
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1282001517}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9c4cbff877abc42448dd829920c6c233, type: 3}
m_Name:
m_EditorClassIdentifier:
directConnectTransport: {fileID: 126344732}
showDebugLogs: 0
--- !u!1 &1458789072 --- !u!1 &1458789072
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -57,8 +57,11 @@ public class LRMDirectConnectModule : MonoBehaviour
public void JoinServer(string ip, int port) public void JoinServer(string ip, int port)
{ {
if(SupportsNATPunch()) if (SupportsNATPunch())
SetTransportPort(port); SetTransportPort(port);
directConnectTransport.ClientConnect(ip); directConnectTransport.ClientConnect(ip);
} }

View file

@ -72,26 +72,34 @@ namespace LightReflectiveMirror
var room = GetServerForID(address); var room = GetServerForID(address);
if (!useLoadBalancer || room.relayInfo.Address == serverIP) if (!useLoadBalancer)
{ {
int pos = 0; if (room.HasValue && room.Value.relayInfo.Address == serverIP)
_directConnected = false; {
_clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); int pos = 0;
_clientSendBuffer.WriteString(ref pos, address); _directConnected = false;
_clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer);
_clientSendBuffer.WriteString(ref pos, address);
_clientSendBuffer.WriteBool(ref pos, _directConnectModule != null);
if (GetLocalIp() == null) if (GetLocalIp() == null)
_clientSendBuffer.WriteString(ref pos, "0.0.0.0"); _clientSendBuffer.WriteString(ref pos, "0.0.0.0");
else
_clientSendBuffer.WriteString(ref pos, GetLocalIp());
_isClient = true;
clientToServerTransport.ClientSend(0, new System.ArraySegment<byte>(_clientSendBuffer, 0, pos));
}
else else
_clientSendBuffer.WriteString(ref pos, GetLocalIp()); {
OnClientDisconnected?.Invoke();
_isClient = true; Debug.LogWarning("LRM | Client tried to join server that doesnt exist!");
}
clientToServerTransport.ClientSend(0, new System.ArraySegment<byte>(_clientSendBuffer, 0, pos));
} }
else else
{ {
StartCoroutine(JoinOtherRelayAndMatch(room)); StartCoroutine(JoinOtherRelayAndMatch(room, address));
} }
} }

View file

@ -58,8 +58,34 @@ namespace LightReflectiveMirror
} }
} }
IEnumerator JoinOtherRelayAndMatch(Room room) IEnumerator JoinOtherRelayAndMatch(Room? roomValue, string ID)
{ {
var room = new Room();
// using load balancer, we NEED the server's relay address
if (roomValue.HasValue)
room = roomValue.Value;
else
{
_serverListUpdated = false;
RequestServerList();
yield return new WaitUntil(() => _serverListUpdated);
var foundRoom = GetServerForID(ID);
if (foundRoom.HasValue)
{
room = foundRoom.Value;
}
else
{
Debug.LogWarning("LRM | Client tried to join a server that does not exist!");
OnClientDisconnected?.Invoke();
yield break;
}
}
// Wait for disconnection // Wait for disconnection
DisconnectFromRelay(); DisconnectFromRelay();
@ -166,6 +192,7 @@ namespace LightReflectiveMirror
relayServerList?.Clear(); relayServerList?.Clear();
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result); relayServerList = JsonConvert.DeserializeObject<List<Room>>(result);
serverListUpdated?.Invoke(); serverListUpdated?.Invoke();
_serverListUpdated = true;
break; break;
} }
#else #else
@ -178,6 +205,7 @@ namespace LightReflectiveMirror
relayServerList?.Clear(); relayServerList?.Clear();
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result); relayServerList = JsonConvert.DeserializeObject<List<Room>>(result);
serverListUpdated?.Invoke(); serverListUpdated?.Invoke();
_serverListUpdated = true;
} }
#endif #endif
} }

View file

@ -61,6 +61,7 @@ namespace LightReflectiveMirror
private BiDictionary<IPEndPoint, SocketProxy> _serverProxies = new BiDictionary<IPEndPoint, SocketProxy>(); private BiDictionary<IPEndPoint, SocketProxy> _serverProxies = new BiDictionary<IPEndPoint, SocketProxy>();
private BiDictionary<int, int> _connectedRelayClients = new BiDictionary<int, int>(); private BiDictionary<int, int> _connectedRelayClients = new BiDictionary<int, int>();
private BiDictionary<int, int> _connectedDirectClients = new BiDictionary<int, int>(); private BiDictionary<int, int> _connectedDirectClients = new BiDictionary<int, int>();
private bool _serverListUpdated = false;
} }
public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania } public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania }

View file

@ -63,7 +63,6 @@ namespace LightReflectiveMirror
private void OnConnectedToRelay() private void OnConnectedToRelay()
{ {
_connectedToRelay = true; _connectedToRelay = true;
RequestServerList();
} }
public void ConnectToRelay() public void ConnectToRelay()
@ -157,6 +156,7 @@ namespace LightReflectiveMirror
case OpCodes.Authenticated: case OpCodes.Authenticated:
serverStatus = "Authenticated! Good to go!"; serverStatus = "Authenticated! Good to go!";
_isAuthenticated = true; _isAuthenticated = true;
RequestServerList();
break; break;
case OpCodes.AuthenticationRequest: case OpCodes.AuthenticationRequest:
serverStatus = "Sent authentication to relay..."; serverStatus = "Sent authentication to relay...";
@ -229,7 +229,12 @@ namespace LightReflectiveMirror
} }
if (useNATPunch && attemptNatPunch) if (useNATPunch && attemptNatPunch)
_directConnectModule.JoinServer("127.0.0.1", _NATIP.Port - 1); {
if (ip == "127.0.0.1")
_directConnectModule.JoinServer("127.0.0.1", port + 1);
else
_directConnectModule.JoinServer("127.0.0.1", _NATIP.Port - 1);
}
else else
_directConnectModule.JoinServer(ip, port); _directConnectModule.JoinServer(ip, port);
} }
@ -336,7 +341,7 @@ namespace LightReflectiveMirror
} }
} }
Room GetServerForID(string serverID) Room? GetServerForID(string serverID)
{ {
for(int i = 0; i < relayServerList.Count; i++) for(int i = 0; i < relayServerList.Count; i++)
{ {
@ -344,8 +349,7 @@ namespace LightReflectiveMirror
return relayServerList[i]; return relayServerList[i];
} }
OnClientDisconnected?.Invoke(); return null;
throw new Exception("LRM | An attempt was made to connect to a server which does not exist!");
} }
void SendAuthKey() void SendAuthKey()

View file

@ -4,5 +4,8 @@
EditorBuildSettings: EditorBuildSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 2 serializedVersion: 2
m_Scenes: [] m_Scenes:
- enabled: 1
path: Assets/LRMTestScene.unity
guid: e860d5862486efc438d11e20bc7660aa
m_configObjects: {} m_configObjects: {}

View file

@ -80,7 +80,7 @@ PlayerSettings:
bakeCollisionMeshes: 0 bakeCollisionMeshes: 0
forceSingleInstance: 0 forceSingleInstance: 0
useFlipModelSwapchain: 1 useFlipModelSwapchain: 1
resizableWindow: 0 resizableWindow: 1
useMacAppStoreValidation: 0 useMacAppStoreValidation: 0
macAppStoreCategory: public.app-category.games macAppStoreCategory: public.app-category.games
gpuSkinning: 0 gpuSkinning: 0
@ -91,7 +91,7 @@ PlayerSettings:
xboxEnableFitness: 0 xboxEnableFitness: 0
visibleInBackground: 1 visibleInBackground: 1
allowFullscreenSwitch: 1 allowFullscreenSwitch: 1
fullscreenMode: 1 fullscreenMode: 3
xboxSpeechDB: 0 xboxSpeechDB: 0
xboxEnableHeadOrientation: 0 xboxEnableHeadOrientation: 0
xboxEnableGuest: 0 xboxEnableGuest: 0
@ -524,7 +524,8 @@ PlayerSettings:
scriptingRuntimeVersion: 1 scriptingRuntimeVersion: 1
gcIncremental: 0 gcIncremental: 0
gcWBarrierValidation: 0 gcWBarrierValidation: 0
apiCompatibilityLevelPerPlatform: {} apiCompatibilityLevelPerPlatform:
Standalone: 3
m_RenderingPath: 1 m_RenderingPath: 1
m_MobileRenderingPath: 1 m_MobileRenderingPath: 1
metroPackageName: UnityProject metroPackageName: UnityProject