Cached what room each player is in, removing need for a loop
This commit is contained in:
parent
bf39a95962
commit
3c6b314c0c
4 changed files with 12 additions and 24 deletions
|
|
@ -23,12 +23,10 @@ namespace LightReflectiveMirror
|
||||||
/// <param name="sendTo">Who to relay the data to</param>
|
/// <param name="sendTo">Who to relay the data to</param>
|
||||||
void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1)
|
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.hostId == clientId)
|
||||||
{
|
{
|
||||||
if (room.clients.Contains(sendTo))
|
if (room.clients.Contains(sendTo))
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ namespace LightReflectiveMirror
|
||||||
ProcessData(clientId, data.ReadBytes(ref pos), channel, data.ReadInt(ref pos));
|
ProcessData(clientId, data.ReadBytes(ref pos), channel, data.ReadInt(ref pos));
|
||||||
break;
|
break;
|
||||||
case OpCodes.UpdateRoomData:
|
case OpCodes.UpdateRoomData:
|
||||||
var plyRoom = GetRoomForPlayer(clientId);
|
var plyRoom = _cachedClientRooms[clientId];
|
||||||
|
|
||||||
if (plyRoom == null)
|
if (plyRoom == null)
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -7,24 +7,6 @@ namespace LightReflectiveMirror
|
||||||
{
|
{
|
||||||
public partial class RelayHandler
|
public partial class RelayHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Returns the current room the client is in, null if client is not in a room.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="clientId">The client we are getting the room for</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to join a room for a client.
|
/// Attempts to join a room for a client.
|
||||||
|
|
@ -44,6 +26,7 @@ namespace LightReflectiveMirror
|
||||||
if (rooms[i].clients.Count < rooms[i].maxPlayers)
|
if (rooms[i].clients.Count < rooms[i].maxPlayers)
|
||||||
{
|
{
|
||||||
rooms[i].clients.Add(clientId);
|
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);
|
||||||
|
|
@ -143,6 +126,7 @@ namespace LightReflectiveMirror
|
||||||
};
|
};
|
||||||
|
|
||||||
rooms.Add(room);
|
rooms.Add(room);
|
||||||
|
_cachedClientRooms.Add(clientId, room);
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
byte[] sendBuffer = _sendBuffers.Rent(5);
|
byte[] sendBuffer = _sendBuffers.Rent(5);
|
||||||
|
|
@ -172,17 +156,21 @@ namespace LightReflectiveMirror
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft);
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft);
|
||||||
|
|
||||||
for (int x = 0; x < rooms[i].clients.Count; x++)
|
for (int x = 0; x < rooms[i].clients.Count; x++)
|
||||||
|
{
|
||||||
Program.transport.ServerSend(rooms[i].clients[x], 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(rooms[i].clients[x], 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
|
_cachedClientRooms.Remove(rooms[i].clients[x]);
|
||||||
|
}
|
||||||
|
|
||||||
_sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
rooms[i].clients.Clear();
|
rooms[i].clients.Clear();
|
||||||
rooms.RemoveAt(i);
|
rooms.RemoveAt(i);
|
||||||
|
_cachedClientRooms.Remove(clientId);
|
||||||
Endpoint.RoomsModified();
|
Endpoint.RoomsModified();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (requiredHostId >= 0 && rooms[i].hostId != requiredHostId)
|
if (requiredHostId != -1 && rooms[i].hostId != requiredHostId)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (rooms[i].clients.RemoveAll(x => x == clientId) > 0)
|
if (rooms[i].clients.RemoveAll(x => x == clientId) > 0)
|
||||||
|
|
@ -196,6 +184,7 @@ namespace LightReflectiveMirror
|
||||||
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
_sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
Endpoint.RoomsModified();
|
Endpoint.RoomsModified();
|
||||||
|
_cachedClientRooms.Remove(clientId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,6 @@ namespace LightReflectiveMirror
|
||||||
private List<int> _pendingAuthentication = new List<int>();
|
private List<int> _pendingAuthentication = new List<int>();
|
||||||
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>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue