// Called when player connects to server private void ConnectionCallback() { this.debugWriter.WriteLine("Successfully connected to server"); isConnected = true; debugCallbackDelegate = (type, message) => { if (this.debugWriter != null) { this.debugWriter.WriteLine("Valve Debug - Type: {0}, Message: {1}", type, message); } }; NetworkingUtils utils = new NetworkingUtils(); utils.SetDebugCallback(DebugType.Important, debugCallbackDelegate); Main.utilityMenu.chat = ""; Main.utilityMenu.previousMessageCount = 0; unsafe { #if DEBUG // From data I saw a typical example of udp packets over the internet would be 0.3% loss, 25% reorder // For testing I'll use 25% reorder, 30ms delay on reorder, 150ms ping, and 0.2% loss // Re-order 25% of packets and add 30ms delay on reordered packets //float reorderPercent = 25f; //int reorderTime = 30; //utils.SetConfiguratioValue(ConfigurationValue.FakePacketReorderSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Float, new IntPtr(&reorderPercent)); //utils.SetConfiguratioValue(ConfigurationValue.FakePacketReorderTime, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&reorderTime)); //// Fake 150ms ping //int pingTime = 150; //utils.SetConfiguratioValue(ConfigurationValue.FakePacketLagSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&pingTime)); //// Simulate 0.2% packet loss //float lossPercent = 0.2f; //utils.SetConfiguratioValue(ConfigurationValue.FakePacketLossSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Float, new IntPtr(&lossPercent)); #endif int sendRateMin = 0; int sendRateMax = 209715200; int sendBufferSize = 10485760; utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMin)); utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMax)); utils.SetConfigurationValue(ConfigurationValue.SendBufferSize, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendBufferSize)); } }
public static void ServerLoop() { Library.Initialize(); server = new NetworkingSockets(); Address address = new Address(); NetworkingUtils utils = new NetworkingUtils(); utils.SetDebugCallback(DebugType.Important, (type, message) => { Console.WriteLine("Valve Debug - Type: {0}, Message: {1}", type, message); }); unsafe { int sendRateMin = 5 * 1024 * 1024; int sendRateMax = MAX_UPLOAD; int sendBufferSize = MAX_BUFFER; utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMin)); utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMax)); utils.SetConfigurationValue(ConfigurationValue.SendBufferSize, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendBufferSize)); } address.SetAddress("::0", port); uint listenSocket = server.CreateListenSocket(ref address); uint pollGroup = server.CreatePollGroup(); Console.WriteLine($"Server {SERVER_NAME} started Listening on port {port} for maximum of {MAX_PLAYERS} players\nEnforcing maps is {ENFORCE_MAPS}"); StartAnnouncing(); StatusCallback status = (ref StatusInfo info, IntPtr context) => { switch (info.connectionInfo.state) { case ConnectionState.None: break; case ConnectionState.Connecting: server.AcceptConnection(info.connection); server.SetConnectionPollGroup(pollGroup, info.connection); break; case ConnectionState.Connected: Console.WriteLine("Client connected - IP: " + info.connectionInfo.address.GetIP()); bool openSlot = false; for (byte i = 0; i < MAX_PLAYERS; i++) { if (players[i] == null) { players[i] = new Player(i, info.connection, info.connectionInfo.address); byte[] versionNumber = ASCIIEncoding.ASCII.GetBytes(VERSION_NUMBER); byte[] versionMessage = new byte[versionNumber.Length + 1]; versionMessage[0] = (byte)OpCode.VersionNumber; Array.Copy(versionNumber, 0, versionMessage, 1, versionNumber.Length); server.SendMessageToConnection(players[i].connection, versionMessage, SendFlags.Reliable | SendFlags.NoNagle); if (ENFORCE_MAPS) { server.SendMessageToConnection(players[i].connection, mapListBytes, SendFlags.Reliable); server.SendMessageToConnection(players[i].connection, GetCurrentMapHashMessage(), SendFlags.Reliable); } foreach (Player player in players) { if (player != null && player != players[i]) { server.SendMessageToConnection(players[i].connection, new byte[] { (byte)OpCode.Connect, player.playerID }, SendFlags.Reliable); server.SendMessageToConnection(player.connection, new byte[] { (byte)OpCode.Connect, i }, SendFlags.Reliable); if (player.usernameMessage != null) { server.SendMessageToConnection(players[i].connection, player.usernameMessage, SendFlags.Reliable); } if (player.allGearUploaded) { foreach (KeyValuePair <string, byte[]> value in player.gear) { server.SendMessageToConnection(players[i].connection, value.Value, SendFlags.Reliable); } } } } server.FlushMessagesOnConnection(players[i].connection); openSlot = true; break; } } if (!openSlot) { server.CloseConnection(info.connection); } break; case ConnectionState.ClosedByPeer: RemovePlayer(info.connection); break; } }; #if VALVESOCKETS_SPAN MessageCallback messageCallback = (in NetworkingMessage netMessage) => { byte[] messageData = new byte[netMessage.length]; netMessage.CopyTo(messageData); Player sendingPlayer = null; foreach (Player player in players) { if (player != null && player.connection == netMessage.connection) { sendingPlayer = player; break; } } if (sendingPlayer != null) { ProcessMessage(messageData, sendingPlayer.playerID, server); } }; #else const int maxMessages = 256; NetworkingMessage[] netMessages = new NetworkingMessage[maxMessages]; #endif while (RUNNING) { server.DispatchCallback(status); #if VALVESOCKETS_SPAN server.ReceiveMessagesOnPollGroup(pollGroup, messageCallback, 256); #else int netMessagesCount = server.ReceiveMessagesOnConnection(listenSocket, netMessages, maxMessages); if (netMessagesCount > 0) { for (int i = 0; i < netMessagesCount; i++) { ref NetworkingMessage netMessage = ref netMessages[i]; byte[] messageData = new byte[netMessage.length]; netMessage.CopyTo(messageData); Player sendingPlayer = null; foreach (Player player in players) { if (player != null && player.connection == netMessage.connection) { sendingPlayer = player; break; } } //Console.WriteLine("Recieved packet from connection {0}, sending player null: {1}", netMessage.connection, sendingPlayer == null); if (sendingPlayer != null) { ProcessMessage(messageData, sendingPlayer.playerID, server); } netMessage.Destroy(); } } #endif mapVotes.Clear(); total_players = 0; foreach (Player player in players) { if (player != null) { total_players++; if (player.timeoutWatch.ElapsedMilliseconds > 15000) { Console.WriteLine($"{player.playerID} has been timed out for not responding for 15 seconds"); RemovePlayer(player.connection, player.playerID, true); } if (!mapVotes.ContainsKey(player.currentVote)) { mapVotes.Add(player.currentVote, 1); } else { mapVotes[player.currentVote]++; } } } // Handle map voting and map enforcement if (ENFORCE_MAPS) { if (total_players == 0) { currentMapHash = "1"; } bool startNewTimer = false; if (mapVotes.ContainsKey("current")) { if (mapVotes["current"] < (int)Math.Ceiling((float)total_players / 2)) { if (!mapVoteTimer.IsRunning) { startNewTimer = true; } } } else if (!mapVoteTimer.IsRunning && total_players > 0) { startNewTimer = true; } if (startNewTimer) { mapVoteTimer.Restart(); byte[] mapVoteMsg = new byte[] { (byte)OpCode.MapVote, 0, 0 }; foreach (Player player in players) { if (player != null) { server.SendMessageToConnection(player.connection, mapVoteMsg, SendFlags.Reliable); } } } if (mapVoteTimer.IsRunning && mapVoteTimer.ElapsedMilliseconds > 30000 && total_players > 0) { mapVoteTimer.Stop(); Tuple <string, int> mostVoted = null; foreach (var item in mapVotes) { if (!item.Key.Equals("current")) { if (mostVoted == null || mostVoted.Item2 < item.Value) { mostVoted = Tuple.Create <string, int>(item.Key, item.Value); } } } currentMapHash = mostVoted.Item1; byte[] newMapMessage = GetCurrentMapHashMessage(); foreach (Player player in players) { if (player != null) { server.SendMessageToConnection(player.connection, newMapMessage, SendFlags.Reliable); player.currentVote = "current"; } } } else if (total_players == 0) { mapVoteTimer.Stop(); } } }
public void ServerLoop() { Library.Initialize(); server = new NetworkingSockets(); Address address = new Address(); //Console.WriteLine($"Gameplay port: {Server.port}, File Server Port: {(ushort)(Server.port + 1)}"); address.SetAddress("::0", (ushort)(Server.port + 1)); listenSocket = server.CreateListenSocket(ref address); pollGroup = server.CreatePollGroup(); NetworkingUtils utils = new NetworkingUtils(); unsafe { int sendRateMin = 512000; int sendRateMax = Server.FILE_MAX_UPLOAD; utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.ListenSocket, new IntPtr(listenSocket), ConfigurationDataType.Int32, new IntPtr(&sendRateMin)); utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.ListenSocket, new IntPtr(listenSocket), ConfigurationDataType.Int32, new IntPtr(&sendRateMax)); } StatusCallback status = StatusCallbackFunction; MessageCallback messageCallback = (in NetworkingMessage netMessage) => { byte[] message = new byte[netMessage.length]; netMessage.CopyTo(message); if ((OpCode)message[0] == OpCode.Connect) { uint originalConnection = BitConverter.ToUInt32(message, 1); Player newPlayer = null; foreach (Player player in mainServer.players) { if (player != null && player.connection == originalConnection) { player.fileConnection = netMessage.connection; newPlayer = player; break; } } if (newPlayer == null) { //Console.WriteLine("Connection on file server doesn't exist on gameplay server"); server.CloseConnection(netMessage.connection); } else { foreach (Player player in mainServer.players) { if (player != null && player.playerID != newPlayer.playerID && player.completedGearStream) { player.SendGear(newPlayer.fileConnection, server); } } foreach (Plugin plugin in mainServer.loadedPlugins) { if (plugin.hash != "" && plugin.dependencyFile != "") { byte[] hashBytes = ASCIIEncoding.ASCII.GetBytes(plugin.hash); byte[] hashMessage = new byte[hashBytes.Length + 2]; hashMessage[0] = (byte)OpCode.PluginHash; hashMessage[1] = plugin.pluginID; Array.Copy(hashBytes, 0, hashMessage, 2, hashBytes.Length); server.SendMessageToConnection(newPlayer.fileConnection, hashMessage, SendFlags.Reliable); } } } } else if ((OpCode)message[0] == OpCode.StillAlive) { server.SendMessageToConnection(netMessage.connection, message, SendFlags.Unreliable | SendFlags.NoNagle); } else { foreach (Player player in mainServer.players) { if (player != null && player.fileConnection == netMessage.connection) { mainServer.ProcessMessage(message, player.playerID, mainServer.server); } } } }; Stopwatch fileServerLoopTime = new Stopwatch(); while (mainServer.RUNNING) { fileServerLoopTime.Restart(); server.DispatchCallback(status); server.ReceiveMessagesOnPollGroup(pollGroup, messageCallback, 256); SpinWait.SpinUntil(() => fileServerLoopTime.Elapsed.TotalMilliseconds >= 1, 1); } }
public void ServerLoop() { Library.Initialize(); server = new NetworkingSockets(); Address address = new Address(); //Console.WriteLine($"Gameplay port: {Server.port}, File Server Port: {(ushort)(Server.port + 1)}"); address.SetAddress("::0", (ushort)(Server.port + 1)); listenSocket = server.CreateListenSocket(ref address); pollGroup = server.CreatePollGroup(); NetworkingUtils utils = new NetworkingUtils(); unsafe { int sendRateMin = 512000; int sendRateMax = Server.FILE_MAX_UPLOAD; utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.ListenSocket, new IntPtr(listenSocket), ConfigurationDataType.Int32, new IntPtr(&sendRateMin)); utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.ListenSocket, new IntPtr(listenSocket), ConfigurationDataType.Int32, new IntPtr(&sendRateMax)); } StatusCallback status = StatusCallbackFunction; MessageCallback messageCallback = (in NetworkingMessage netMessage) => { byte[] message = new byte[netMessage.length]; netMessage.CopyTo(message); if ((OpCode)message[0] == OpCode.Connect) { uint originalConnection = BitConverter.ToUInt32(message, 1); Player newPlayer = null; foreach (Player player in mainServer.players) { if (player != null && player.connection == originalConnection) { player.fileConnection = netMessage.connection; newPlayer = player; break; } } if (newPlayer == null) { //Console.WriteLine("Connection on file server doesn't exist on gameplay server"); server.CloseConnection(netMessage.connection); } else { foreach (Player player in mainServer.players) { if (player != null && player.playerID != newPlayer.playerID && player.completedGearStream) { player.SendGear(newPlayer.fileConnection, server); } } } } else if ((OpCode)message[0] == OpCode.StillAlive) { server.SendMessageToConnection(netMessage.connection, message, SendFlags.Unreliable | SendFlags.NoNagle); } else { foreach (Player player in mainServer.players) { if (player != null && player.fileConnection == netMessage.connection) { mainServer.ProcessMessage(message, player.playerID, mainServer.server); } } } }; while (mainServer.RUNNING) { server.DispatchCallback(status); server.ReceiveMessagesOnPollGroup(pollGroup, messageCallback, 256); } }