From 8fcb0ef1177c8b771e212928b9f90a5eefbaf7b1 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Mon, 29 Mar 2021 15:18:29 -0400 Subject: [PATCH] server side refactorings --- .../.vs/LRM-Server/v16/.suo | Bin 60416 -> 60416 bytes .../Program.cs | 52 ++--- .../RelayHandler.cs | 214 +++++++++--------- 3 files changed, 127 insertions(+), 139 deletions(-) diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/.vs/LRM-Server/v16/.suo b/ServerProject-DONT-IMPORT-INTO-UNITY/.vs/LRM-Server/v16/.suo index c550625dbcae2b8e74a518c9db1a883b85913429..9305656caa78f313a288969b3bcbd97de32996a9 100644 GIT binary patch delta 1490 zcmb_bZ)jUp6wkTId+VQwzjWJ$mZy5s++B)Xp3x5!;q`2mR^OFXGg8E@b7}lZS27+HiKXgtDE}omdK&Hbldf~^p z_jm5S=l8qsyws{WwQ4@CG%=GgY$j7JP0fUx#bS}O=9Rr}CQdzjp)0=?XW0_mrC(>; zAj$K082hRz(y}x*(fm$=o!}?92#tgmLJfg$>Y!*Pv>WR>ia29w+%6jg=_qZiqPUIV zCHM@kjbamFOM3KCvaBgx0^UjF= z`(zbl&NHwe1P8&1&j;Uw*YT>`foFrm=v4<$7+T0EO!LL8z`Lg0dR(MF=No3)Lo)tw zD5&+GGY`$o;BxQ7*{&E^fy)TrQnLG0CMu%Xr`mej>YnpezZE$8<;1nizE@t;lhF&H zC30H-VU+K!#zJBWKT~&DqKf|>JklT1VP_HaVhot}bO=v|dK!Wee<*k&F+Fqaz?)R0 zegClov&mUKwOa*zQdMCUhnfdbZT`B&Q>YbwTD~NGGIqT9Qrlu|cJIZPe;z;hxi=8` z_T6zurzt(^z=XRO)o2PIczgf{{7Z(Xf9OQp_Fpr@3`ci)@a)hOPVKVm>G30mbW14c zkKh-HLzUVOa=CT4le;0dFsR~+T923ecj8#M6LaAPk^iBJf^UZ$_(907MZRk}*LiWJ z@Zs`DCl^0H@!P96eo3!gslW8;BV+0Fxc*?@;Tt6rUquw0-tETk`}=Sr-0>f;%8WD3 LpZnLBm^}R#{J0BD delta 1403 zcmchW-%FEW6vyA^+4kz@rd!*ZndatxxS8x%&V?+SP3(s@oTOMm%nL)&EKI=?YD7U7 zUAP@2L?T%cU6${4Fp#<^=obWDc%%Ibc41WOobf8ttmvi#AI^E6=RD_p&%0--WkqUP zG2@44*VQJyAXo%JP#{0@`8+y-JwIK`#f8YL&q_Ot^INQ3`oim3Hy-D8avok2Xl;_$ z6z|2j0#PGc#D+ML{YWcfKpY6=lEJ95(zB;%!-cU1sYl#M17b!zNKHD?$x71+3+Poj zRtX_? z)u={ai_{_1vq}(Lf*lw85Gqt6dy2_u6!P0gIgn2H7OQVFCDnwxHB(tFZ)Z#pI6`}b z3S$u|K}zL)4f--fi|FJ%%H1?**SO)S#$>SJVI5*e9F!=f^nI93CBXLi+7lXzy`Dcb^*CA|4)Ugy@kfxZrg`?#v3T z^g6N!c=oXPr)K8gH+#(Hw=~QqnhS02_@;|KQc_~a@=VId7|%p=QYzlhU@|f!r8=Sv zYWrHm`#8C?7xWP~9QU>UyEL6~&LX-$7#K`U*XFPZrXc$?_u diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs index b12a7bf..4cc8ca3 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs @@ -12,15 +12,17 @@ namespace LightReflectiveMirror { public static Transport transport; public static Config conf; - RelayHandler relay; - MethodInfo awakeMethod; - MethodInfo startMethod; - MethodInfo updateMethod; - MethodInfo lateUpdateMethod; + private RelayHandler relay; + private MethodInfo awakeMethod; + private MethodInfo startMethod; + private MethodInfo updateMethod; + private MethodInfo lateUpdateMethod; - List _currentConnections = new List(); - int _currentHeartbeatTimer = 0; + private List _currentConnections = new List(); + private int _currentHeartbeatTimer = 0; + + private const string CONFIG_PATH = "config.json"; public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); @@ -29,19 +31,18 @@ namespace LightReflectiveMirror { 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); Console.ReadKey(); Environment.Exit(0); } else { - conf = JsonConvert.DeserializeObject(File.ReadAllText("config.json")); + conf = JsonConvert.DeserializeObject(File.ReadAllText(CONFIG_PATH)); try - { - Console.WriteLine(Directory.GetCurrentDirectory()); + { var asm = Assembly.LoadFile(Directory.GetCurrentDirectory() + @"\" + conf.TransportDLL); WriteLogMessage($"Loaded Assembly: {asm.FullName}", ConsoleColor.Green); @@ -68,7 +69,8 @@ namespace LightReflectiveMirror WriteLogMessage("Starting Transport...", ConsoleColor.Green); - transport.OnServerError = (clientID, error) => { + transport.OnServerError = (clientID, error) => + { WriteLogMessage($"Transport Error, Client: {clientID}, Error: {error}", ConsoleColor.Red); }; @@ -109,11 +111,8 @@ namespace LightReflectiveMirror while (true) { - if (updateMethod != null) - updateMethod.Invoke(transport, null); - - if (lateUpdateMethod != null) - lateUpdateMethod.Invoke(transport, null); + if (updateMethod != null) { updateMethod.Invoke(transport, null); } + if (lateUpdateMethod != null) { lateUpdateMethod.Invoke(transport, null); } _currentHeartbeatTimer++; @@ -122,9 +121,7 @@ namespace LightReflectiveMirror _currentHeartbeatTimer = 0; for(int i = 0; i < _currentConnections.Count; i++) - { transport.ServerSend(_currentConnections[i], 0, new ArraySegment(new byte[] { 200 })); - } GC.Collect(); } @@ -146,17 +143,10 @@ namespace LightReflectiveMirror updateMethod = type.GetMethod("Update", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); lateUpdateMethod = type.GetMethod("LateUpdate", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - if (awakeMethod != null) - WriteLogMessage("'Awake' Loaded!", ConsoleColor.Yellow); - - if (startMethod != null) - WriteLogMessage("'Start' Loaded!", ConsoleColor.Yellow); - - if (updateMethod != null) - WriteLogMessage("'Update' Loaded!", ConsoleColor.Yellow); - - if (lateUpdateMethod != null) - WriteLogMessage("'LateUpdate' Loaded!", ConsoleColor.Yellow); + if (awakeMethod != null) { WriteLogMessage("'Awake' Loaded!", ConsoleColor.Yellow); } + if (startMethod != null) { WriteLogMessage("'Start' Loaded!", ConsoleColor.Yellow); } + if (updateMethod != null) { WriteLogMessage("'Update' Loaded!", ConsoleColor.Yellow); } + if (lateUpdateMethod != null) { WriteLogMessage("'LateUpdate' Loaded!", ConsoleColor.Yellow); } } void WriteTitle() diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/RelayHandler.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/RelayHandler.cs index 57aca00..9becbaf 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/RelayHandler.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/RelayHandler.cs @@ -7,25 +7,25 @@ namespace LightReflectiveMirror { public class RelayHandler { - List rooms = new List(); - List pendingAuthentication = new List(); - ArrayPool sendBuffers; - int maxPacketSize = 0; + private List _rooms = new List(); + private List _pendingAuthentication = new List(); + private ArrayPool _sendBuffers; + private int _maxPacketSize = 0; public RelayHandler(int maxPacketSize) { - this.maxPacketSize = maxPacketSize; - sendBuffers = ArrayPool.Create(maxPacketSize, 50); + this._maxPacketSize = maxPacketSize; + _sendBuffers = ArrayPool.Create(maxPacketSize, 50); } public void ClientConnected(int clientId) { - pendingAuthentication.Add(clientId); - var buffer = sendBuffers.Rent(1); + _pendingAuthentication.Add(clientId); + var buffer = _sendBuffers.Rent(1); int pos = 0; buffer.WriteByte(ref pos, (byte)OpCodes.AuthenticationRequest); Program.transport.ServerSend(clientId, 0, new ArraySegment(buffer, 0, pos)); - sendBuffers.Return(buffer); + _sendBuffers.Return(buffer); } public void HandleMessage(int clientId, ArraySegment segmentData, int channel) @@ -37,16 +37,16 @@ namespace LightReflectiveMirror OpCodes opcode = (OpCodes)data.ReadByte(ref pos); - if (pendingAuthentication.Contains(clientId)) + if (_pendingAuthentication.Contains(clientId)) { if (opcode == OpCodes.AuthenticationResponse) { string authResponse = data.ReadString(ref pos); if (authResponse == Program.conf.AuthenticationKey) { - pendingAuthentication.Remove(clientId); + _pendingAuthentication.Remove(clientId); int writePos = 0; - var sendBuffer = sendBuffers.Rent(1); + var sendBuffer = _sendBuffers.Rent(1); sendBuffer.WriteByte(ref writePos, (byte)OpCodes.Authenticated); Program.transport.ServerSend(clientId, 0, new ArraySegment(sendBuffer, 0, writePos)); } @@ -108,31 +108,28 @@ namespace LightReflectiveMirror } } - public void HandleDisconnect(int clientId) - { - LeaveRoom(clientId); - } + public void HandleDisconnect(int clientId) => LeaveRoom(clientId); void SendServerList(int clientId) { int pos = 0; - var buffer = sendBuffers.Rent(500); + var buffer = _sendBuffers.Rent(500); 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.WriteString(ref pos, rooms[i].serverName); - buffer.WriteString(ref pos, rooms[i].serverData); - buffer.WriteInt(ref pos, rooms[i].serverId); - buffer.WriteInt(ref pos, rooms[i].maxPlayers); - buffer.WriteInt(ref pos, rooms[i].clients.Count + 1); + buffer.WriteString(ref pos, _rooms[i].serverName); + buffer.WriteString(ref pos, _rooms[i].serverData); + buffer.WriteInt(ref pos, _rooms[i].serverId); + buffer.WriteInt(ref pos, _rooms[i].maxPlayers); + buffer.WriteInt(ref pos, _rooms[i].clients.Count + 1); } } buffer.WriteBool(ref pos, false); Program.transport.ServerSend(clientId, 0, new ArraySegment(buffer, 0, pos)); - sendBuffers.Return(buffer); + _sendBuffers.Return(buffer); } void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1) @@ -148,40 +145,40 @@ namespace LightReflectiveMirror if (room.clients.Contains(sendTo)) { int pos = 0; - byte[] sendBuffer = sendBuffers.Rent(maxPacketSize); + byte[] sendBuffer = _sendBuffers.Rent(_maxPacketSize); sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData); sendBuffer.WriteBytes(ref pos, clientData); Program.transport.ServerSend(sendTo, channel, new ArraySegment(sendBuffer, 0, pos)); - sendBuffers.Return(sendBuffer); + _sendBuffers.Return(sendBuffer); } } else { // We are not the host, so send the data to the host. int pos = 0; - byte[] sendBuffer = sendBuffers.Rent(maxPacketSize); + byte[] sendBuffer = _sendBuffers.Rent(_maxPacketSize); sendBuffer.WriteByte(ref pos, (byte)OpCodes.GetData); sendBuffer.WriteBytes(ref pos, clientData); sendBuffer.WriteInt(ref pos, clientId); Program.transport.ServerSend(room.hostId, channel, new ArraySegment(sendBuffer, 0, pos)); - sendBuffers.Return(sendBuffer); + _sendBuffers.Return(sendBuffer); } } } 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) - return rooms[i]; + if (_rooms[i].hostId == clientId) + return _rooms[i]; - if (rooms[i].clients.Contains(clientId)) - return rooms[i]; + if (_rooms[i].clients.Contains(clientId)) + return _rooms[i]; } return null; @@ -191,23 +188,23 @@ namespace LightReflectiveMirror { 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; - byte[] sendJoinBuffer = sendBuffers.Rent(5); + byte[] sendJoinBuffer = _sendBuffers.Rent(5); 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); + Program.transport.ServerSend(_rooms[i].hostId, 0, new ArraySegment(sendJoinBuffer, 0, sendJoinPos)); + _sendBuffers.Return(sendJoinBuffer); return; } } @@ -215,37 +212,90 @@ namespace LightReflectiveMirror // If it got to here, then the server was not found, or full. Tell the client. int pos = 0; - byte[] sendBuffer = sendBuffers.Rent(1); + byte[] sendBuffer = _sendBuffers.Rent(1); sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft); Program.transport.ServerSend(clientId, 0, new ArraySegment(sendBuffer, 0, pos)); - sendBuffers.Return(sendBuffer); + _sendBuffers.Return(sendBuffer); } void CreateRoom(int clientId, int maxPlayers, string serverName, bool isPublic, string serverData) { LeaveRoom(clientId); - Room room = new Room(); - room.hostId = clientId; - room.maxPlayers = maxPlayers; - room.serverName = serverName; - room.isPublic = isPublic; - room.serverData = serverData; - room.clients = new List(); + Room room = new Room + { + hostId = clientId, + maxPlayers = maxPlayers, + serverName = serverName, + isPublic = isPublic, + serverData = serverData, + clients = new List(), - room.serverId = GetRandomServerID(); - rooms.Add(room); + serverId = GetRandomServerID() + }; + + _rooms.Add(room); int pos = 0; - byte[] sendBuffer = sendBuffers.Rent(5); + byte[] sendBuffer = _sendBuffers.Rent(5); sendBuffer.WriteByte(ref pos, (byte)OpCodes.RoomCreated); sendBuffer.WriteInt(ref pos, clientId); Program.transport.ServerSend(clientId, 0, new ArraySegment(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(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(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(sendBuffer, 0, pos)); + _sendBuffers.Return(sendBuffer); } int GetRandomServerID() @@ -261,64 +311,12 @@ namespace LightReflectiveMirror bool DoesServerIdExist(int id) { - for (int i = 0; i < rooms.Count; i++) - { - if (rooms[i].serverId == id) + for (int i = 0; i < _rooms.Count; i++) + if (_rooms[i].serverId == id) return true; - } 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(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(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(sendBuffer, 0, pos)); - sendBuffers.Return(sendBuffer); - } } public enum OpCodes