private void HandleServerConnect(NetworkClient <RelayClient> client, byte[] data) { lock (remoteRelays) { using (MessageReader mr = new MessageReader(data)) { string[] parents = mr.Read <string[]>(); string serverHash = mr.Read <string>(); string serverIP = mr.Read <string>(); int serverPort = mr.Read <int>(); RelayClient parentClient = GetRelayClient(parents); RelayClient childClient = new RelayClient(); childClient.relayHash = serverHash; childClient.remoteIP = serverIP; childClient.remotePort = serverPort; parentClient.remoteRelays.Add(childClient.relayHash, childClient); Console.WriteLine("RELAY: Remote receiver connect from " + parentClient.relayHash + " to " + childClient.relayHash); //Relay string[] newParents = new string[parents.Length + 1]; newParents[0] = settings.reporterHash; parents.CopyTo(newParents, 1); networkServer.QueueToOthers(client, new NetworkMessage((int)MessageType.SERVER_CONNECT, GetRemoteServerConnectBytes(newParents, client.stateObject))); } } }
private void RemoteServerDisconnect(RelayClient parentClient, RelayClient disconnectClient) { lock (remoteRelays) { List <RelayClient> relaysToDisconnect = new List <RelayClient>(disconnectClient.remoteRelays.Values); foreach (RelayClient childServer in relaysToDisconnect) { RemoteServerDisconnect(disconnectClient, childServer); } List <ReporterClient> serversToDisconnect = new List <ReporterClient>(disconnectClient.remoteClients.Values); foreach (ReporterClient childServer in serversToDisconnect) { RemoteDisconnect(disconnectClient, childServer); } if (parentClient == null) { Console.WriteLine("RELAY: Remote relay disconnect " + disconnectClient.relayHash + " from us"); remoteRelays.Remove(disconnectClient.relayHash); } else { Console.WriteLine("RELAY: Remote relay disconnect " + disconnectClient.relayHash + " from " + parentClient.relayHash); parentClient.remoteRelays.Remove(disconnectClient.relayHash); } } }
private void HandleRemoteConnect(NetworkClient <RelayClient> client, byte[] data) { lock (remoteRelays) { using (MessageReader mr = new MessageReader(data)) { string[] parents = mr.Read <string[]>(); int serverID = mr.Read <int>(); string serverIP = mr.Read <string>(); int serverPort = mr.Read <int>(); RelayClient relayClient = GetRelayClient(parents); ReporterClient reporterClient = new ReporterClient(); reporterClient.clientID = serverID; reporterClient.remoteIP = serverIP; reporterClient.remotePort = serverPort; //Add to DB Console.WriteLine("RELAY: Remote receiver connect from " + relayClient.relayHash + ", client: " + reporterClient.clientID); relayClient.remoteClients.Add(reporterClient.clientID, reporterClient); dbServer.Connect(relayClient.relayHash, reporterClient); //Relay string[] newParents = new string[parents.Length + 1]; newParents[0] = settings.reporterHash; parents.CopyTo(newParents, 1); networkServer.QueueToOthers(client, new NetworkMessage((int)MessageType.REMOTE_CONNECT, GetRemoteConnectBytes(newParents, reporterClient))); } } }
private RelayClient GetRelayClientRecursive(RelayClient parentClient, string[] treePath, int index) { if (treePath.Length == index) { return(parentClient); } return(GetRelayClientRecursive(parentClient.remoteRelays[treePath[index]], treePath, index + 1)); }
private byte[] GetRemoteServerDisconnectBytes(string[] parents, RelayClient relayClient) { byte[] retVal; using (MessageWriter mw = new MessageWriter()) { mw.Write <string[]>(parents); mw.Write <string>(relayClient.relayHash); retVal = mw.GetMessageBytes(); } return(retVal); }
private void SendCurrentChildren(NetworkClient <DBClient> client, RelayClient relayClient) { foreach (ReporterClient remoteClient in relayClient.remoteClients.Values) { client.QueueNetworkMessage(new NetworkMessage((int)MessageType.CONNECT, GetConnectMessageBytes(relayClient.relayHash, remoteClient))); if (remoteClient.lastMessage != null) { client.QueueNetworkMessage(new NetworkMessage((int)MessageType.REPORT, GetReportMessageBytes(relayClient.relayHash, remoteClient, remoteClient.lastMessage))); } } foreach (RelayClient childClient in relayClient.remoteRelays.Values) { SendCurrentChildren(client, childClient); } }
private void HandleServerDisconnect(NetworkClient <RelayClient> client, byte[] data) { lock (remoteRelays) { using (MessageReader mr = new MessageReader(data)) { string[] parents = mr.Read <string[]>(); string serverHash = mr.Read <string>(); RelayClient parentClient = GetRelayClient(parents); RemoteServerDisconnect(parentClient, parentClient.remoteRelays[serverHash]); Console.WriteLine("RELAY: Remote receiver disconnect " + serverHash + " from " + parentClient.relayHash); //Relay string[] newParents = new string[parents.Length + 1]; newParents[0] = settings.reporterHash; parents.CopyTo(newParents, 1); networkServer.QueueToOthers(client, new NetworkMessage((int)MessageType.SERVER_DISCONNECT, GetRemoteServerDisconnectBytes(parents, client.stateObject))); } } }
private void HandleRemoteDisconnect(NetworkClient <RelayClient> client, byte[] data) { lock (remoteRelays) { using (MessageReader mr = new MessageReader(data)) { string[] parents = mr.Read <string[]>(); int serverID = mr.Read <int>(); RelayClient relayClient = GetRelayClient(parents); ReporterClient reporterClient = relayClient.remoteClients[serverID]; RemoteDisconnect(relayClient, reporterClient); //Relay string[] newParents = new string[parents.Length + 1]; newParents[0] = settings.reporterHash; parents.CopyTo(newParents, 1); networkServer.QueueToOthers(client, new NetworkMessage((int)MessageType.REMOTE_DISCONNECT, GetRemoteDisconnectBytes(newParents, reporterClient))); } } }
private void PrintRecursive(RelayClient currentClient, int depth) { string prefix = "".PadLeft(depth * 2); Console.WriteLine(prefix + "@" + currentClient.relayHash + ", " + currentClient.remoteClients.Count + " clients, " + currentClient.remoteRelays.Count + " relays."); foreach (ReporterClient client in currentClient.remoteClients.Values) { if (client.lastMessage == null) { Console.WriteLine(prefix + " +" + client.clientID + " CONNECTING"); } else { Console.WriteLine(prefix + " +" + client.clientID + " " + client.lastMessage.gameAddress + ":" + client.lastMessage.gamePort + ", " + client.lastMessage.players.Length + " players."); } } foreach (RelayClient client in currentClient.remoteRelays.Values) { PrintRecursive(client, depth + 1); } }
private void HandleRemoteReport(NetworkClient <RelayClient> client, byte[] data) { lock (remoteRelays) { using (MessageReader mr = new MessageReader(data)) { string[] parents = mr.Read <string[]>(); int serverID = mr.Read <int>(); byte[] reportBytes = mr.Read <byte[]>(); RelayClient relayClient = GetRelayClient(parents); ReporterClient reporterClient = relayClient.remoteClients[serverID]; ReportingMessage reportMessage = ReportingMessage.FromBytesBE(reportBytes); reporterClient.lastMessage = reportMessage; //Add to DB Console.WriteLine("RELAY: Remote receiver report from " + relayClient.relayHash + ", client: " + reporterClient.clientID + ", players: " + reportMessage.players.Length); dbServer.Report(relayClient.relayHash, reporterClient, reportMessage); //Relay string[] newParents = new string[parents.Length + 1]; newParents[0] = settings.reporterHash; parents.CopyTo(newParents, 1); networkServer.QueueToOthers(client, new NetworkMessage((int)MessageType.REMOTE_REPORT, GetRemoteReportBytes(newParents, reporterClient, reportMessage))); } } }
private bool ContainsNode(RelayClient searchNode, string relayHash) { bool found = false; lock (remoteRelays) { if (searchNode.relayHash == relayHash) { found = true; } else { foreach (RelayClient relayClient in searchNode.remoteRelays.Values) { if (ContainsNode(relayClient, relayHash)) { found = true; break; } } } } return(found); }
private void SendTreeRecursive(NetworkClient <RelayClient> client, string[] parents, RelayClient relayClient) { string[] ourPosition = new string[parents.Length + 1]; parents.CopyTo(ourPosition, 0); ourPosition[parents.Length] = relayClient.relayHash; client.QueueNetworkMessage(new NetworkMessage((int)MessageType.SERVER_CONNECT, GetRemoteServerConnectBytes(parents, relayClient))); foreach (ReporterClient reporterClient in relayClient.remoteClients.Values) { client.QueueNetworkMessage(new NetworkMessage((int)MessageType.REMOTE_CONNECT, GetRemoteConnectBytes(ourPosition, reporterClient))); if (reporterClient.lastMessage != null) { client.QueueNetworkMessage(new NetworkMessage((int)MessageType.REMOTE_REPORT, GetRemoteReportBytes(ourPosition, reporterClient, reporterClient.lastMessage))); } } foreach (RelayClient childClient in relayClient.remoteRelays.Values) { SendTreeRecursive(client, ourPosition, childClient); } }
private void RemoteDisconnect(RelayClient parentClient, ReporterClient client) { Console.WriteLine("RELAY: Remote receiver disconnect from " + parentClient.relayHash + ", client: " + client.clientID); parentClient.remoteClients.Remove(client.clientID); dbServer.Disconnect(parentClient.relayHash, client); }