Added Server.PublicIp

This commit is contained in:
Garry Newman 2016-11-03 14:33:03 +00:00
parent 84e38601d6
commit 8bf391a300
4 changed files with 68 additions and 0 deletions

View File

@ -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()
{

View File

@ -180,6 +180,7 @@
<Compile Include="SteamNative\SteamNative.Callback.cs" />
<Compile Include="SteamNative\SteamNative.Structs.cs" />
<Compile Include="SteamNative\SteamNative.Types.cs" />
<Compile Include="Utility.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@ -250,6 +250,9 @@ public bool LoggedOn
get { return native.gameServer.BLoggedOn(); }
}
/// <summary>
/// Shutdown interface, disconnect from Steam
/// </summary>
public override void Dispose()
{
if ( Query != null )
@ -269,5 +272,23 @@ public override void Dispose()
base.Dispose();
}
/// <summary>
/// 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.
/// </summary>
public System.Net.IPAddress PublicIp
{
get
{
var ip = native.gameServer.GetPublicIP();
if ( ip == 0 ) return null;
return new System.Net.IPAddress( Utility.SwapBytes( ip ) );
}
}
}
}

View File

@ -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 );
}
}
}