Cached what rooms corresponds to what server IDs

This commit is contained in:
Derek S 2021-04-09 02:24:23 -05:00
parent 3c6b314c0c
commit a9b48465fe
3 changed files with 51 additions and 53 deletions

View file

@ -116,11 +116,7 @@ namespace LightReflectiveMirror
/// <returns></returns> /// <returns></returns>
bool DoesServerIdExist(string id) bool DoesServerIdExist(string id)
{ {
for (int i = 0; i < rooms.Count; i++) return _cachedRooms.ContainsKey(id);
if (rooms[i].serverId == id)
return true;
return false;
} }
} }

View file

@ -19,33 +19,33 @@ namespace LightReflectiveMirror
{ {
LeaveRoom(clientId); 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);
rooms[i].clients.Add(clientId);
_cachedClientRooms.Add(clientId, rooms[i]);
int sendJoinPos = 0; int sendJoinPos = 0;
byte[] sendJoinBuffer = _sendBuffers.Rent(500); byte[] sendJoinBuffer = _sendBuffers.Rent(500);
if (canDirectConnect && Program.instance.NATConnections.ContainsKey(clientId) && rooms[i].supportsDirectConnect) if (canDirectConnect && Program.instance.NATConnections.ContainsKey(clientId) && room.supportsDirectConnect)
{ {
sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP);
if (Program.instance.NATConnections[clientId].Address.Equals(rooms[i].hostIP.Address)) if (Program.instance.NATConnections[clientId].Address.Equals(room.hostIP.Address))
sendJoinBuffer.WriteString(ref sendJoinPos, rooms[i].hostLocalIP == localIP ? "127.0.0.1" : rooms[i].hostLocalIP); sendJoinBuffer.WriteString(ref sendJoinPos, room.hostLocalIP == localIP ? "127.0.0.1" : room.hostLocalIP);
else else
sendJoinBuffer.WriteString(ref sendJoinPos, rooms[i].hostIP.Address.ToString()); sendJoinBuffer.WriteString(ref sendJoinPos, room.hostIP.Address.ToString());
sendJoinBuffer.WriteInt(ref sendJoinPos, rooms[i].useNATPunch ? rooms[i].hostIP.Port : rooms[i].port); sendJoinBuffer.WriteInt(ref sendJoinPos, room.useNATPunch ? room.hostIP.Port : room.port);
sendJoinBuffer.WriteBool(ref sendJoinPos, rooms[i].useNATPunch); sendJoinBuffer.WriteBool(ref sendJoinPos, room.useNATPunch);
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
if (rooms[i].useNATPunch) if (room.useNATPunch)
{ {
sendJoinPos = 0; sendJoinPos = 0;
sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP); sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.DirectConnectIP);
@ -54,7 +54,7 @@ namespace LightReflectiveMirror
sendJoinBuffer.WriteInt(ref sendJoinPos, Program.instance.NATConnections[clientId].Port); sendJoinBuffer.WriteInt(ref sendJoinPos, Program.instance.NATConnections[clientId].Port);
sendJoinBuffer.WriteBool(ref sendJoinPos, true); sendJoinBuffer.WriteBool(ref sendJoinPos, true);
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(room.hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
} }
_sendBuffers.Return(sendJoinBuffer); _sendBuffers.Return(sendJoinBuffer);
@ -69,7 +69,7 @@ namespace LightReflectiveMirror
sendJoinBuffer.WriteInt(ref sendJoinPos, clientId); sendJoinBuffer.WriteInt(ref sendJoinPos, clientId);
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(room.hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
_sendBuffers.Return(sendJoinBuffer); _sendBuffers.Return(sendJoinBuffer);
Endpoint.RoomsModified(); Endpoint.RoomsModified();
@ -77,7 +77,6 @@ namespace LightReflectiveMirror
} }
} }
} }
}
// If it got to here, then the server was not found, or full. Tell the client. // If it got to here, then the server was not found, or full. Tell the client.
int pos = 0; int pos = 0;
@ -127,6 +126,7 @@ namespace LightReflectiveMirror
rooms.Add(room); rooms.Add(room);
_cachedClientRooms.Add(clientId, room); _cachedClientRooms.Add(clientId, room);
_cachedRooms.Add(room.serverId, room);
int pos = 0; int pos = 0;
byte[] sendBuffer = _sendBuffers.Rent(5); byte[] sendBuffer = _sendBuffers.Rent(5);
@ -163,6 +163,7 @@ namespace LightReflectiveMirror
_sendBuffers.Return(sendBuffer); _sendBuffers.Return(sendBuffer);
rooms[i].clients.Clear(); rooms[i].clients.Clear();
_cachedRooms.Remove(rooms[i].serverId);
rooms.RemoveAt(i); rooms.RemoveAt(i);
_cachedClientRooms.Remove(clientId); _cachedClientRooms.Remove(clientId);
Endpoint.RoomsModified(); Endpoint.RoomsModified();

View file

@ -10,5 +10,6 @@ namespace LightReflectiveMirror
private ArrayPool<byte> _sendBuffers; private ArrayPool<byte> _sendBuffers;
private int _maxPacketSize = 0; private int _maxPacketSize = 0;
private Dictionary<int, Room> _cachedClientRooms = new Dictionary<int, Room>(); private Dictionary<int, Room> _cachedClientRooms = new Dictionary<int, Room>();
private Dictionary<string, Room> _cachedRooms = new Dictionary<string, Room>();
} }
} }