server side refactorings
This commit is contained in:
parent
f9fb3d38af
commit
8fcb0ef117
3 changed files with 127 additions and 139 deletions
Binary file not shown.
|
|
@ -12,15 +12,17 @@ namespace LightReflectiveMirror
|
||||||
{
|
{
|
||||||
public static Transport transport;
|
public static Transport transport;
|
||||||
public static Config conf;
|
public static Config conf;
|
||||||
RelayHandler relay;
|
|
||||||
|
|
||||||
MethodInfo awakeMethod;
|
private RelayHandler relay;
|
||||||
MethodInfo startMethod;
|
private MethodInfo awakeMethod;
|
||||||
MethodInfo updateMethod;
|
private MethodInfo startMethod;
|
||||||
MethodInfo lateUpdateMethod;
|
private MethodInfo updateMethod;
|
||||||
|
private MethodInfo lateUpdateMethod;
|
||||||
|
|
||||||
List<int> _currentConnections = new List<int>();
|
private List<int> _currentConnections = new List<int>();
|
||||||
int _currentHeartbeatTimer = 0;
|
private int _currentHeartbeatTimer = 0;
|
||||||
|
|
||||||
|
private const string CONFIG_PATH = "config.json";
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
=> new Program().MainAsync().GetAwaiter().GetResult();
|
=> new Program().MainAsync().GetAwaiter().GetResult();
|
||||||
|
|
@ -29,19 +31,18 @@ namespace LightReflectiveMirror
|
||||||
{
|
{
|
||||||
WriteTitle();
|
WriteTitle();
|
||||||
|
|
||||||
if (!File.Exists("config.json"))
|
if (!File.Exists(CONFIG_PATH))
|
||||||
{
|
{
|
||||||
File.WriteAllText("config.json", JsonConvert.SerializeObject(new Config(), Formatting.Indented));
|
File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(new Config(), Formatting.Indented));
|
||||||
WriteLogMessage("A config.json file was generated. Please configure it to the proper settings and re-run!", ConsoleColor.Yellow);
|
WriteLogMessage("A config.json file was generated. Please configure it to the proper settings and re-run!", ConsoleColor.Yellow);
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
|
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText(CONFIG_PATH));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine(Directory.GetCurrentDirectory());
|
|
||||||
var asm = Assembly.LoadFile(Directory.GetCurrentDirectory() + @"\" + conf.TransportDLL);
|
var asm = Assembly.LoadFile(Directory.GetCurrentDirectory() + @"\" + conf.TransportDLL);
|
||||||
WriteLogMessage($"Loaded Assembly: {asm.FullName}", ConsoleColor.Green);
|
WriteLogMessage($"Loaded Assembly: {asm.FullName}", ConsoleColor.Green);
|
||||||
|
|
||||||
|
|
@ -68,7 +69,8 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
WriteLogMessage("Starting Transport...", ConsoleColor.Green);
|
WriteLogMessage("Starting Transport...", ConsoleColor.Green);
|
||||||
|
|
||||||
transport.OnServerError = (clientID, error) => {
|
transport.OnServerError = (clientID, error) =>
|
||||||
|
{
|
||||||
WriteLogMessage($"Transport Error, Client: {clientID}, Error: {error}", ConsoleColor.Red);
|
WriteLogMessage($"Transport Error, Client: {clientID}, Error: {error}", ConsoleColor.Red);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -109,11 +111,8 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (updateMethod != null)
|
if (updateMethod != null) { updateMethod.Invoke(transport, null); }
|
||||||
updateMethod.Invoke(transport, null);
|
if (lateUpdateMethod != null) { lateUpdateMethod.Invoke(transport, null); }
|
||||||
|
|
||||||
if (lateUpdateMethod != null)
|
|
||||||
lateUpdateMethod.Invoke(transport, null);
|
|
||||||
|
|
||||||
_currentHeartbeatTimer++;
|
_currentHeartbeatTimer++;
|
||||||
|
|
||||||
|
|
@ -122,9 +121,7 @@ namespace LightReflectiveMirror
|
||||||
_currentHeartbeatTimer = 0;
|
_currentHeartbeatTimer = 0;
|
||||||
|
|
||||||
for(int i = 0; i < _currentConnections.Count; i++)
|
for(int i = 0; i < _currentConnections.Count; i++)
|
||||||
{
|
|
||||||
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 }));
|
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 }));
|
||||||
}
|
|
||||||
|
|
||||||
GC.Collect();
|
GC.Collect();
|
||||||
}
|
}
|
||||||
|
|
@ -146,17 +143,10 @@ namespace LightReflectiveMirror
|
||||||
updateMethod = type.GetMethod("Update", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
updateMethod = type.GetMethod("Update", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||||
lateUpdateMethod = type.GetMethod("LateUpdate", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
lateUpdateMethod = type.GetMethod("LateUpdate", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||||
|
|
||||||
if (awakeMethod != null)
|
if (awakeMethod != null) { WriteLogMessage("'Awake' Loaded!", ConsoleColor.Yellow); }
|
||||||
WriteLogMessage("'Awake' Loaded!", ConsoleColor.Yellow);
|
if (startMethod != null) { WriteLogMessage("'Start' Loaded!", ConsoleColor.Yellow); }
|
||||||
|
if (updateMethod != null) { WriteLogMessage("'Update' Loaded!", ConsoleColor.Yellow); }
|
||||||
if (startMethod != null)
|
if (lateUpdateMethod != null) { WriteLogMessage("'LateUpdate' Loaded!", ConsoleColor.Yellow); }
|
||||||
WriteLogMessage("'Start' Loaded!", ConsoleColor.Yellow);
|
|
||||||
|
|
||||||
if (updateMethod != null)
|
|
||||||
WriteLogMessage("'Update' Loaded!", ConsoleColor.Yellow);
|
|
||||||
|
|
||||||
if (lateUpdateMethod != null)
|
|
||||||
WriteLogMessage("'LateUpdate' Loaded!", ConsoleColor.Yellow);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteTitle()
|
void WriteTitle()
|
||||||
|
|
|
||||||
|
|
@ -7,25 +7,25 @@ namespace LightReflectiveMirror
|
||||||
{
|
{
|
||||||
public class RelayHandler
|
public class RelayHandler
|
||||||
{
|
{
|
||||||
List<Room> rooms = new List<Room>();
|
private List<Room> _rooms = new List<Room>();
|
||||||
List<int> pendingAuthentication = new List<int>();
|
private List<int> _pendingAuthentication = new List<int>();
|
||||||
ArrayPool<byte> sendBuffers;
|
private ArrayPool<byte> _sendBuffers;
|
||||||
int maxPacketSize = 0;
|
private int _maxPacketSize = 0;
|
||||||
|
|
||||||
public RelayHandler(int maxPacketSize)
|
public RelayHandler(int maxPacketSize)
|
||||||
{
|
{
|
||||||
this.maxPacketSize = maxPacketSize;
|
this._maxPacketSize = maxPacketSize;
|
||||||
sendBuffers = ArrayPool<byte>.Create(maxPacketSize, 50);
|
_sendBuffers = ArrayPool<byte>.Create(maxPacketSize, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClientConnected(int clientId)
|
public void ClientConnected(int clientId)
|
||||||
{
|
{
|
||||||
pendingAuthentication.Add(clientId);
|
_pendingAuthentication.Add(clientId);
|
||||||
var buffer = sendBuffers.Rent(1);
|
var buffer = _sendBuffers.Rent(1);
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
buffer.WriteByte(ref pos, (byte)OpCodes.AuthenticationRequest);
|
buffer.WriteByte(ref pos, (byte)OpCodes.AuthenticationRequest);
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(buffer, 0, pos));
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(buffer, 0, pos));
|
||||||
sendBuffers.Return(buffer);
|
_sendBuffers.Return(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandleMessage(int clientId, ArraySegment<byte> segmentData, int channel)
|
public void HandleMessage(int clientId, ArraySegment<byte> segmentData, int channel)
|
||||||
|
|
@ -37,16 +37,16 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
OpCodes opcode = (OpCodes)data.ReadByte(ref pos);
|
OpCodes opcode = (OpCodes)data.ReadByte(ref pos);
|
||||||
|
|
||||||
if (pendingAuthentication.Contains(clientId))
|
if (_pendingAuthentication.Contains(clientId))
|
||||||
{
|
{
|
||||||
if (opcode == OpCodes.AuthenticationResponse)
|
if (opcode == OpCodes.AuthenticationResponse)
|
||||||
{
|
{
|
||||||
string authResponse = data.ReadString(ref pos);
|
string authResponse = data.ReadString(ref pos);
|
||||||
if (authResponse == Program.conf.AuthenticationKey)
|
if (authResponse == Program.conf.AuthenticationKey)
|
||||||
{
|
{
|
||||||
pendingAuthentication.Remove(clientId);
|
_pendingAuthentication.Remove(clientId);
|
||||||
int writePos = 0;
|
int writePos = 0;
|
||||||
var sendBuffer = sendBuffers.Rent(1);
|
var sendBuffer = _sendBuffers.Rent(1);
|
||||||
sendBuffer.WriteByte(ref writePos, (byte)OpCodes.Authenticated);
|
sendBuffer.WriteByte(ref writePos, (byte)OpCodes.Authenticated);
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, writePos));
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, writePos));
|
||||||
}
|
}
|
||||||
|
|
@ -108,31 +108,28 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandleDisconnect(int clientId)
|
public void HandleDisconnect(int clientId) => LeaveRoom(clientId);
|
||||||
{
|
|
||||||
LeaveRoom(clientId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendServerList(int clientId)
|
void SendServerList(int clientId)
|
||||||
{
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
var buffer = sendBuffers.Rent(500);
|
var buffer = _sendBuffers.Rent(500);
|
||||||
buffer.WriteByte(ref pos, (byte)OpCodes.ServerListReponse);
|
buffer.WriteByte(ref pos, (byte)OpCodes.ServerListReponse);
|
||||||
for(int i = 0; i < rooms.Count; i++)
|
for(int i = 0; i < _rooms.Count; i++)
|
||||||
{
|
{
|
||||||
if (rooms[i].isPublic)
|
if (_rooms[i].isPublic)
|
||||||
{
|
{
|
||||||
buffer.WriteBool(ref pos, true);
|
buffer.WriteBool(ref pos, true);
|
||||||
buffer.WriteString(ref pos, rooms[i].serverName);
|
buffer.WriteString(ref pos, _rooms[i].serverName);
|
||||||
buffer.WriteString(ref pos, rooms[i].serverData);
|
buffer.WriteString(ref pos, _rooms[i].serverData);
|
||||||
buffer.WriteInt(ref pos, rooms[i].serverId);
|
buffer.WriteInt(ref pos, _rooms[i].serverId);
|
||||||
buffer.WriteInt(ref pos, rooms[i].maxPlayers);
|
buffer.WriteInt(ref pos, _rooms[i].maxPlayers);
|
||||||
buffer.WriteInt(ref pos, rooms[i].clients.Count + 1);
|
buffer.WriteInt(ref pos, _rooms[i].clients.Count + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer.WriteBool(ref pos, false);
|
buffer.WriteBool(ref pos, false);
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(buffer, 0, pos));
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(buffer, 0, pos));
|
||||||
sendBuffers.Return(buffer);
|
_sendBuffers.Return(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1)
|
void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1)
|
||||||
|
|
@ -148,40 +145,40 @@ namespace LightReflectiveMirror
|
||||||
if (room.clients.Contains(sendTo))
|
if (room.clients.Contains(sendTo))
|
||||||
{
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
byte[] sendBuffer = sendBuffers.Rent(maxPacketSize);
|
byte[] sendBuffer = _sendBuffers.Rent(_maxPacketSize);
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData);
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData);
|
||||||
sendBuffer.WriteBytes(ref pos, clientData);
|
sendBuffer.WriteBytes(ref pos, clientData);
|
||||||
|
|
||||||
Program.transport.ServerSend(sendTo, channel, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(sendTo, channel, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// We are not the host, so send the data to the host.
|
// We are not the host, so send the data to the host.
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
byte[] sendBuffer = sendBuffers.Rent(maxPacketSize);
|
byte[] sendBuffer = _sendBuffers.Rent(_maxPacketSize);
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData);
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData);
|
||||||
sendBuffer.WriteBytes(ref pos, clientData);
|
sendBuffer.WriteBytes(ref pos, clientData);
|
||||||
sendBuffer.WriteInt(ref pos, clientId);
|
sendBuffer.WriteInt(ref pos, clientId);
|
||||||
|
|
||||||
Program.transport.ServerSend(room.hostId, channel, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(room.hostId, channel, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Room GetRoomForPlayer(int clientId)
|
Room GetRoomForPlayer(int clientId)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < rooms.Count; i++)
|
for(int i = 0; i < _rooms.Count; i++)
|
||||||
{
|
{
|
||||||
if (rooms[i].hostId == clientId)
|
if (_rooms[i].hostId == clientId)
|
||||||
return rooms[i];
|
return _rooms[i];
|
||||||
|
|
||||||
if (rooms[i].clients.Contains(clientId))
|
if (_rooms[i].clients.Contains(clientId))
|
||||||
return rooms[i];
|
return _rooms[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -191,23 +188,23 @@ namespace LightReflectiveMirror
|
||||||
{
|
{
|
||||||
LeaveRoom(clientId);
|
LeaveRoom(clientId);
|
||||||
|
|
||||||
for(int i = 0; i < rooms.Count; i++)
|
for(int i = 0; i < _rooms.Count; i++)
|
||||||
{
|
{
|
||||||
if(rooms[i].serverId == serverId)
|
if(_rooms[i].serverId == serverId)
|
||||||
{
|
{
|
||||||
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);
|
||||||
|
|
||||||
int sendJoinPos = 0;
|
int sendJoinPos = 0;
|
||||||
byte[] sendJoinBuffer = sendBuffers.Rent(5);
|
byte[] sendJoinBuffer = _sendBuffers.Rent(5);
|
||||||
|
|
||||||
sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.ServerJoined);
|
sendJoinBuffer.WriteByte(ref sendJoinPos, (byte)OpCodes.ServerJoined);
|
||||||
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(_rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
|
||||||
sendBuffers.Return(sendJoinBuffer);
|
_sendBuffers.Return(sendJoinBuffer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -215,37 +212,90 @@ 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;
|
||||||
byte[] sendBuffer = sendBuffers.Rent(1);
|
byte[] sendBuffer = _sendBuffers.Rent(1);
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft);
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft);
|
||||||
|
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateRoom(int clientId, int maxPlayers, string serverName, bool isPublic, string serverData)
|
void CreateRoom(int clientId, int maxPlayers, string serverName, bool isPublic, string serverData)
|
||||||
{
|
{
|
||||||
LeaveRoom(clientId);
|
LeaveRoom(clientId);
|
||||||
|
|
||||||
Room room = new Room();
|
Room room = new Room
|
||||||
room.hostId = clientId;
|
{
|
||||||
room.maxPlayers = maxPlayers;
|
hostId = clientId,
|
||||||
room.serverName = serverName;
|
maxPlayers = maxPlayers,
|
||||||
room.isPublic = isPublic;
|
serverName = serverName,
|
||||||
room.serverData = serverData;
|
isPublic = isPublic,
|
||||||
room.clients = new List<int>();
|
serverData = serverData,
|
||||||
|
clients = new List<int>(),
|
||||||
|
|
||||||
room.serverId = GetRandomServerID();
|
serverId = GetRandomServerID()
|
||||||
rooms.Add(room);
|
};
|
||||||
|
|
||||||
|
_rooms.Add(room);
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
byte[] sendBuffer = sendBuffers.Rent(5);
|
byte[] sendBuffer = _sendBuffers.Rent(5);
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.RoomCreated);
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.RoomCreated);
|
||||||
sendBuffer.WriteInt(ref pos, clientId);
|
sendBuffer.WriteInt(ref pos, clientId);
|
||||||
|
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
sendBuffers.Return(sendBuffer);
|
_sendBuffers.Return(sendBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LeaveRoom(int clientId, int requiredHostId = -1)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < _rooms.Count; i++)
|
||||||
|
{
|
||||||
|
if(_rooms[i].hostId == clientId)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
byte[] sendBuffer = _sendBuffers.Rent(1);
|
||||||
|
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<byte>(sendBuffer, 0, pos));
|
||||||
|
|
||||||
|
_sendBuffers.Return(sendBuffer);
|
||||||
|
_rooms[i].clients.Clear();
|
||||||
|
_rooms.RemoveAt(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (requiredHostId >= 0 && _rooms[i].hostId != requiredHostId)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(_rooms[i].clients.RemoveAll(x => x == clientId) > 0)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
byte[] sendBuffer = _sendBuffers.Rent(5);
|
||||||
|
|
||||||
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.PlayerDisconnected);
|
||||||
|
sendBuffer.WriteInt(ref pos, clientId);
|
||||||
|
|
||||||
|
Program.transport.ServerSend(_rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
|
_sendBuffers.Return(sendBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendClientID(int clientId)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
byte[] sendBuffer = _sendBuffers.Rent(5);
|
||||||
|
|
||||||
|
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetID);
|
||||||
|
sendBuffer.WriteInt(ref pos, clientId);
|
||||||
|
|
||||||
|
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||||
|
_sendBuffers.Return(sendBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetRandomServerID()
|
int GetRandomServerID()
|
||||||
|
|
@ -261,64 +311,12 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
bool DoesServerIdExist(int id)
|
bool DoesServerIdExist(int id)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < rooms.Count; i++)
|
for (int i = 0; i < _rooms.Count; i++)
|
||||||
{
|
if (_rooms[i].serverId == id)
|
||||||
if (rooms[i].serverId == id)
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LeaveRoom(int clientId, int requiredHostId = -1)
|
|
||||||
{
|
|
||||||
for(int i = 0; i < rooms.Count; i++)
|
|
||||||
{
|
|
||||||
if(rooms[i].hostId == clientId)
|
|
||||||
{
|
|
||||||
int pos = 0;
|
|
||||||
byte[] sendBuffer = sendBuffers.Rent(1);
|
|
||||||
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<byte>(sendBuffer, 0, pos));
|
|
||||||
|
|
||||||
sendBuffers.Return(sendBuffer);
|
|
||||||
rooms[i].clients.Clear();
|
|
||||||
rooms.RemoveAt(i);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (requiredHostId >= 0 && rooms[i].hostId != requiredHostId)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(rooms[i].clients.RemoveAll(x => x == clientId) > 0)
|
|
||||||
{
|
|
||||||
int pos = 0;
|
|
||||||
byte[] sendBuffer = sendBuffers.Rent(5);
|
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.PlayerDisconnected);
|
|
||||||
sendBuffer.WriteInt(ref pos, clientId);
|
|
||||||
|
|
||||||
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
|
||||||
sendBuffers.Return(sendBuffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendClientID(int clientId)
|
|
||||||
{
|
|
||||||
int pos = 0;
|
|
||||||
byte[] sendBuffer = sendBuffers.Rent(5);
|
|
||||||
|
|
||||||
sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetID);
|
|
||||||
sendBuffer.WriteInt(ref pos, clientId);
|
|
||||||
|
|
||||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
|
||||||
sendBuffers.Return(sendBuffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum OpCodes
|
public enum OpCodes
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue