From 8bf391a300add3fdfd90368dd8ced9a76c9e88b3 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Thu, 3 Nov 2016 14:33:03 +0000 Subject: [PATCH] Added Server.PublicIp --- Facepunch.Steamworks.Test/Server/Server.cs | 28 +++++++++++++++++++ .../Facepunch.Steamworks.csproj | 1 + Facepunch.Steamworks/Server.cs | 21 ++++++++++++++ Facepunch.Steamworks/Utility.cs | 18 ++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 Facepunch.Steamworks/Utility.cs diff --git a/Facepunch.Steamworks.Test/Server/Server.cs b/Facepunch.Steamworks.Test/Server/Server.cs index 66290ef..562988c 100644 --- a/Facepunch.Steamworks.Test/Server/Server.cs +++ b/Facepunch.Steamworks.Test/Server/Server.cs @@ -24,6 +24,34 @@ public void Init() } } + [TestMethod] + public void PublicIp() + { + using ( var server = new Facepunch.Steamworks.Server( 252490, 30002, 30003, false, "VersionString" ) ) + { + server.LogOnAnonymous(); + + Assert.IsTrue( server.IsValid ); + + while ( true ) + { + var ip = server.PublicIp; + + if ( ip == null ) + { + System.Threading.Thread.Sleep( 100 ); + server.Update(); + continue; + } + + Assert.IsNotNull( ip ); + Console.WriteLine( ip.ToString() ); + break; + } + + } + } + [TestMethod] public void AuthCallback() { diff --git a/Facepunch.Steamworks/Facepunch.Steamworks.csproj b/Facepunch.Steamworks/Facepunch.Steamworks.csproj index 4e68b3f..97bbc70 100644 --- a/Facepunch.Steamworks/Facepunch.Steamworks.csproj +++ b/Facepunch.Steamworks/Facepunch.Steamworks.csproj @@ -180,6 +180,7 @@ + diff --git a/Facepunch.Steamworks/Server.cs b/Facepunch.Steamworks/Server.cs index 1dc7069..7d2a5fe 100644 --- a/Facepunch.Steamworks/Server.cs +++ b/Facepunch.Steamworks/Server.cs @@ -250,6 +250,9 @@ public bool LoggedOn get { return native.gameServer.BLoggedOn(); } } + /// + /// Shutdown interface, disconnect from Steam + /// public override void Dispose() { if ( Query != null ) @@ -269,5 +272,23 @@ public override void Dispose() base.Dispose(); } + + /// + /// To the best of its ability this tries to get the server's + /// current public ip address. Be aware that this is likely to return + /// null for the first few seconds after initialization. + /// + public System.Net.IPAddress PublicIp + { + get + { + var ip = native.gameServer.GetPublicIP(); + if ( ip == 0 ) return null; + + return new System.Net.IPAddress( Utility.SwapBytes( ip ) ); + } + } + + } } diff --git a/Facepunch.Steamworks/Utility.cs b/Facepunch.Steamworks/Utility.cs new file mode 100644 index 0000000..6546416 --- /dev/null +++ b/Facepunch.Steamworks/Utility.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Facepunch.Steamworks +{ + internal static class Utility + { + static internal uint SwapBytes( uint x ) + { + return ( ( x & 0x000000ff ) << 24 ) + + ( ( x & 0x0000ff00 ) << 8 ) + + ( ( x & 0x00ff0000 ) >> 8 ) + + ( ( x & 0xff000000 ) >> 24 ); + } + } +}