using System; using System.Buffers; namespace LightReflectiveMirror { public partial class RelayHandler { public RelayHandler(int maxPacketSize) { this._maxPacketSize = maxPacketSize; _sendBuffers = ArrayPool.Create(maxPacketSize, 50); } /// /// This is called when a client wants to send data to another player. /// /// The ID of the client who is sending the data /// The binary data the client is sending /// The channel the client is sending this data on /// Who to relay the data to void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1) { Room playersRoom = GetRoomForPlayer(clientId); if(playersRoom != null) { Room room = playersRoom; if(room.hostId == clientId) { if (room.clients.Contains(sendTo)) { int pos = 0; 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); } } else { // We are not the host, so send the data to the host. int pos = 0; 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); } } } /// /// Called when a client wants to request their own ID. /// /// The client requesting their ID 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); } /// /// Generates a random server ID. /// /// int GetRandomServerID() { Random rand = new Random(); int temp = rand.Next(int.MinValue, int.MaxValue); while (DoesServerIdExist(temp)) temp = rand.Next(int.MinValue, int.MaxValue); return temp; } /// /// Checks if a server id already is in use. /// /// The ID to check for /// bool DoesServerIdExist(int id) { for (int i = 0; i < rooms.Count; i++) if (rooms[i].serverId == id) return true; return false; } } public enum OpCodes { Default = 0, RequestID = 1, JoinServer = 2, SendData = 3, GetID = 4, ServerJoined = 5, GetData = 6, CreateRoom = 7, ServerLeft = 8, PlayerDisconnected = 9, RoomCreated = 10, LeaveRoom = 11, KickPlayer = 12, AuthenticationRequest = 13, AuthenticationResponse = 14, Authenticated = 17, UpdateRoomData = 18, ServerConnectionData = 19, RequestNATConnection = 20, DirectConnectIP = 21 } }