diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs index 6c887f3..1848e40 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs @@ -23,12 +23,10 @@ namespace LightReflectiveMirror /// Who to relay the data to void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1) { - Room playersRoom = GetRoomForPlayer(clientId); + Room room = _cachedClientRooms[clientId]; - if(playersRoom != null) + if(room != null) { - Room room = playersRoom; - if(room.hostId == clientId) { if (room.clients.Contains(sendTo)) diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerCallbacks.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerCallbacks.cs index 11892d4..99c6b2d 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerCallbacks.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerCallbacks.cs @@ -71,7 +71,7 @@ namespace LightReflectiveMirror ProcessData(clientId, data.ReadBytes(ref pos), channel, data.ReadInt(ref pos)); break; case OpCodes.UpdateRoomData: - var plyRoom = GetRoomForPlayer(clientId); + var plyRoom = _cachedClientRooms[clientId]; if (plyRoom == null) return; diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs index aa252cf..773871a 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerRoomMethods.cs @@ -7,24 +7,6 @@ namespace LightReflectiveMirror { public partial class RelayHandler { - /// - /// Returns the current room the client is in, null if client is not in a room. - /// - /// The client we are getting the room for - /// - Room GetRoomForPlayer(int clientId) - { - for (int i = 0; i < rooms.Count; i++) - { - if (rooms[i].hostId == clientId) - return rooms[i]; - - if (rooms[i].clients.Contains(clientId)) - return rooms[i]; - } - - return null; - } /// /// Attempts to join a room for a client. @@ -44,6 +26,7 @@ namespace LightReflectiveMirror if (rooms[i].clients.Count < rooms[i].maxPlayers) { rooms[i].clients.Add(clientId); + _cachedClientRooms.Add(clientId, rooms[i]); int sendJoinPos = 0; byte[] sendJoinBuffer = _sendBuffers.Rent(500); @@ -143,6 +126,7 @@ namespace LightReflectiveMirror }; rooms.Add(room); + _cachedClientRooms.Add(clientId, room); int pos = 0; byte[] sendBuffer = _sendBuffers.Rent(5); @@ -172,17 +156,21 @@ namespace LightReflectiveMirror sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft); for (int x = 0; x < rooms[i].clients.Count; x++) + { Program.transport.ServerSend(rooms[i].clients[x], 0, new ArraySegment(sendBuffer, 0, pos)); + _cachedClientRooms.Remove(rooms[i].clients[x]); + } _sendBuffers.Return(sendBuffer); rooms[i].clients.Clear(); rooms.RemoveAt(i); + _cachedClientRooms.Remove(clientId); Endpoint.RoomsModified(); return; } else { - if (requiredHostId >= 0 && rooms[i].hostId != requiredHostId) + if (requiredHostId != -1 && rooms[i].hostId != requiredHostId) continue; if (rooms[i].clients.RemoveAll(x => x == clientId) > 0) @@ -196,6 +184,7 @@ namespace LightReflectiveMirror Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment(sendBuffer, 0, pos)); _sendBuffers.Return(sendBuffer); Endpoint.RoomsModified(); + _cachedClientRooms.Remove(clientId); } } } diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs index abe5267..5f908aa 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandlerVariables.cs @@ -9,5 +9,6 @@ namespace LightReflectiveMirror private List _pendingAuthentication = new List(); private ArrayPool _sendBuffers; private int _maxPacketSize = 0; + private Dictionary _cachedClientRooms = new Dictionary(); } }