diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs index 1848e40..dd015a2 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs @@ -116,11 +116,7 @@ namespace LightReflectiveMirror /// bool DoesServerIdExist(string id) { - for (int i = 0; i < rooms.Count; i++) - if (rooms[i].serverId == id) - return true; - - return false; + return _cachedRooms.ContainsKey(id); } } diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs index 773871a..f37f277 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs @@ -19,62 +19,61 @@ namespace LightReflectiveMirror { LeaveRoom(clientId); - for (int i = 0; i < rooms.Count; i++) + if (_cachedRooms.ContainsKey(serverId)) { - if (rooms[i].serverId == serverId) + var room = _cachedRooms[serverId]; + + if (room.clients.Count < room.maxPlayers) { - if (rooms[i].clients.Count < rooms[i].maxPlayers) + room.clients.Add(clientId); + _cachedClientRooms.Add(clientId, room); + + int sendJoinPos = 0; + byte[] sendJoinBuffer = _sendBuffers.Rent(500); + + if (canDirectConnect && Program.instance.NATConnections.ContainsKey(clientId) && room.supportsDirectConnect) { - rooms[i].clients.Add(clientId); - _cachedClientRooms.Add(clientId, rooms[i]); + sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); - int sendJoinPos = 0; - byte[] sendJoinBuffer = _sendBuffers.Rent(500); - - if (canDirectConnect && Program.instance.NATConnections.ContainsKey(clientId) && rooms[i].supportsDirectConnect) - { - sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); - - if (Program.instance.NATConnections[clientId].Address.Equals(rooms[i].hostIP.Address)) - sendJoinBuffer.WriteString(ref sendJoinPos, rooms[i].hostLocalIP == localIP ? "127.0.0.1" : rooms[i].hostLocalIP); - else - sendJoinBuffer.WriteString(ref sendJoinPos, rooms[i].hostIP.Address.ToString()); - - sendJoinBuffer.WriteInt(ref sendJoinPos, rooms[i].useNATPunch ? rooms[i].hostIP.Port : rooms[i].port); - sendJoinBuffer.WriteBool(ref sendJoinPos, rooms[i].useNATPunch); - - Program.transport.ServerSend(clientId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); - - if (rooms[i].useNATPunch) - { - sendJoinPos = 0; - sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); - Console.WriteLine(Program.instance.NATConnections[clientId].Address.ToString()); - sendJoinBuffer.WriteString(ref sendJoinPos, Program.instance.NATConnections[clientId].Address.ToString()); - sendJoinBuffer.WriteInt(ref sendJoinPos, Program.instance.NATConnections[clientId].Port); - sendJoinBuffer.WriteBool(ref sendJoinPos, true); - - Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); - } - - _sendBuffers.Return(sendJoinBuffer); - - Endpoint.RoomsModified(); - return; - } + if (Program.instance.NATConnections[clientId].Address.Equals(room.hostIP.Address)) + sendJoinBuffer.WriteString(ref sendJoinPos, room.hostLocalIP == localIP ? "127.0.0.1" : room.hostLocalIP); else + sendJoinBuffer.WriteString(ref sendJoinPos, room.hostIP.Address.ToString()); + + sendJoinBuffer.WriteInt(ref sendJoinPos, room.useNATPunch ? room.hostIP.Port : room.port); + sendJoinBuffer.WriteBool(ref sendJoinPos, room.useNATPunch); + + Program.transport.ServerSend(clientId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); + + if (room.useNATPunch) { + sendJoinPos = 0; + sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); + Console.WriteLine(Program.instance.NATConnections[clientId].Address.ToString()); + sendJoinBuffer.WriteString(ref sendJoinPos, Program.instance.NATConnections[clientId].Address.ToString()); + sendJoinBuffer.WriteInt(ref sendJoinPos, Program.instance.NATConnections[clientId].Port); + sendJoinBuffer.WriteBool(ref sendJoinPos, true); - sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.ServerJoined); - sendJoinBuffer.WriteInt(ref sendJoinPos, clientId); - - Program.transport.ServerSend(clientId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); - Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); - _sendBuffers.Return(sendJoinBuffer); - - Endpoint.RoomsModified(); - return; + Program.transport.ServerSend(room.hostId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); } + + _sendBuffers.Return(sendJoinBuffer); + + Endpoint.RoomsModified(); + return; + } + else + { + + sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.ServerJoined); + sendJoinBuffer.WriteInt(ref sendJoinPos, clientId); + + Program.transport.ServerSend(clientId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); + Program.transport.ServerSend(room.hostId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); + _sendBuffers.Return(sendJoinBuffer); + + Endpoint.RoomsModified(); + return; } } } @@ -127,6 +126,7 @@ namespace LightReflectiveMirror rooms.Add(room); _cachedClientRooms.Add(clientId, room); + _cachedRooms.Add(room.serverId, room); int pos = 0; byte[] sendBuffer = _sendBuffers.Rent(5); @@ -163,6 +163,7 @@ namespace LightReflectiveMirror _sendBuffers.Return(sendBuffer); rooms[i].clients.Clear(); + _cachedRooms.Remove(rooms[i].serverId); rooms.RemoveAt(i); _cachedClientRooms.Remove(clientId); Endpoint.RoomsModified(); diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs index 5f908aa..c5fee9e 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs @@ -10,5 +10,6 @@ namespace LightReflectiveMirror private ArrayPool _sendBuffers; private int _maxPacketSize = 0; private Dictionary _cachedClientRooms = new Dictionary(); + private Dictionary _cachedRooms = new Dictionary(); } }