diff --git a/OpenRA.Game/Server/Connection.cs b/OpenRA.Game/Server/Connection.cs index b9d4b07b80..8ee7061b0e 100644 --- a/OpenRA.Game/Server/Connection.cs +++ b/OpenRA.Game/Server/Connection.cs @@ -12,6 +12,8 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; +using System.Net.NetworkInformation; +using System.Threading; namespace OpenRA.Server { @@ -22,8 +24,9 @@ namespace OpenRA.Server public ReceiveState State = ReceiveState.Header; public int ExpectLength = 8; public int Frame = 0; - public int MostRecentFrame = 0; + public string RemoteAddress; + public int Latency = -1; /* client data */ public int PlayerIndex; @@ -97,7 +100,36 @@ namespace OpenRA.Server } break; } } - }} + } + + bool hasBeenPinged; + public void Ping() + { + if (!hasBeenPinged) + { + hasBeenPinged = true; + var pingSender = new Ping(); + pingSender.PingCompleted += new PingCompletedEventHandler(pongRecieved); + AutoResetEvent waiter = new AutoResetEvent(false); + pingSender.SendAsync(RemoteAddress, waiter); + } + } + + void pongRecieved(object sender, PingCompletedEventArgs e) + { + if (e.Cancelled || e.Error != null) + Latency = -1; + else + { + PingReply pong = e.Reply; + if (pong != null && pong.Status == IPStatus.Success) + Latency = (int)pong.RoundtripTime; + else + Latency = -1; + } + } + + } public enum ReceiveState { Header, Data }; } diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 5ade506ef9..e9de70762a 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -251,6 +251,9 @@ namespace OpenRA.Server DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize()); } catch (Exception) { DropClient(newConn); } + + newConn.RemoteAddress = ((IPEndPoint)newConn.socket.RemoteEndPoint).Address.ToString(); + newConn.Ping(); } void ValidateClient(Connection newConn, string data) @@ -302,8 +305,7 @@ namespace OpenRA.Server // Check if IP is banned if (lobbyInfo.GlobalSettings.Ban != null) { - var remote_addr = ((IPEndPoint)newConn.socket.RemoteEndPoint).Address.ToString(); - if (lobbyInfo.GlobalSettings.Ban.Contains(remote_addr)) + if (lobbyInfo.GlobalSettings.Ban.Contains(newConn.RemoteAddress)) { Console.WriteLine("Rejected connection from "+client.Name+"("+newConn.socket.RemoteEndPoint+"); Banned."); Log.Write("server", "Rejected connection from {0}; Banned.", @@ -338,16 +340,16 @@ namespace OpenRA.Server foreach (var t in ServerTraits.WithInterface()) t.ClientJoined(this, newConn); + SendChat(newConn, "has joined the game. Ping: {0} ms".F(newConn.Latency)); SyncLobbyInfo(); - SendChat(newConn, "has joined the game."); - if ( File.Exists("{0}motd_{1}.txt".F(Platform.SupportDir, lobbyInfo.GlobalSettings.Mods[0])) ) + if (File.Exists("{0}motd_{1}.txt".F(Platform.SupportDir, lobbyInfo.GlobalSettings.Mods[0]))) { var motd = System.IO.File.ReadAllText("{0}motd_{1}.txt".F(Platform.SupportDir, lobbyInfo.GlobalSettings.Mods[0])); SendChatTo(newConn, motd); } - if ( lobbyInfo.GlobalSettings.Dedicated ) + if (lobbyInfo.GlobalSettings.Dedicated) { if (client.IsAdmin) SendChatTo(newConn, " You are admin now!"); @@ -578,7 +580,7 @@ namespace OpenRA.Server t.GameStarted(this); // Check TimeOut - if ( Settings.TimeOut > 10000 ) + if (Settings.TimeOut > 10000) { gameTimeout = new XTimer(Settings.TimeOut); gameTimeout.Elapsed += (_,e) =>