IPAddress Cleanup

This commit is contained in:
Garry Newman 2018-03-21 13:11:25 +00:00
parent e94866828a
commit 835770772f
5 changed files with 34 additions and 15 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@ -12,6 +13,23 @@ namespace Facepunch.Steamworks.Test
[DeploymentItem( "steam_api64.dll" )]
public partial class ServerList
{
[TestMethod]
public void IpAddressConversions()
{
var ipstr = "185.38.150.40";
var ip = IPAddress.Parse( ipstr );
var ip_int = Facepunch.Steamworks.Utility.IpToInt32( ip );
var ip_back = Facepunch.Steamworks.Utility.Int32ToIp( ip_int );
Console.WriteLine( "ipstr: " + ipstr );
Console.WriteLine( "ip: " + ip );
Console.WriteLine( "ip int: " + ip_int );
Console.WriteLine( "ip_back: " + ip_back );
}
[TestMethod]
public void InternetList()
{

View File

@ -70,7 +70,6 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo
/// </summary>
private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj )
{
Console.WriteLine( "onDefinitionsUpdated" );
LoadDefinitions();
UpdatePrices();

View File

@ -33,13 +33,15 @@ public Server( uint appId, ServerInit init) : base( appId )
Instance = this;
native = new Interop.NativeInterface();
uint ipaddress = 0; // Any Port
if ( init.SteamPort == 0 ) init.RandomSteamPort();
if ( init.IpAddress != null ) ipaddress = Utility.IpToInt32( init.IpAddress );
//
// Get other interfaces
//
if ( !native.InitServer( this, init.IpAddress, init.SteamPort, init.GamePort, init.QueryPort, init.Secure ? 3 : 2, init.VersionString ) )
if ( !native.InitServer( this, ipaddress, init.SteamPort, init.GamePort, init.QueryPort, init.Secure ? 3 : 2, init.VersionString ) )
{
native.Dispose();
native = null;
@ -300,7 +302,7 @@ public System.Net.IPAddress PublicIp
var ip = native.gameServer.GetPublicIP();
if ( ip == 0 ) return null;
return new System.Net.IPAddress( Utility.SwapBytes( ip ) );
return Utility.Int32ToIp( ip );
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
@ -12,7 +13,7 @@ namespace Facepunch.Steamworks
/// </summary>
public class ServerInit
{
public uint IpAddress = 0;
public IPAddress IpAddress;
public ushort SteamPort;
public ushort GamePort = 27015;
public ushort QueryPort = 27016;

View File

@ -6,25 +6,24 @@
namespace Facepunch.Steamworks
{
internal static class Utility
public static class Utility
{
static internal uint SwapBytes( uint x )
static internal uint Swap( uint x )
{
return ( ( x & 0x000000ff ) << 24 ) +
( ( x & 0x0000ff00 ) << 8 ) +
( ( x & 0x00ff0000 ) >> 8 ) +
( ( x & 0xff000000 ) >> 24 );
return ((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24);
}
static internal uint IpToInt32( this IPAddress ipAddress )
static public uint IpToInt32( this IPAddress ipAddress )
{
return BitConverter.ToUInt32( ipAddress.GetAddressBytes().Reverse().ToArray(), 0 );
return Swap( (uint) ipAddress.Address );
}
static internal IPAddress Int32ToIp( uint ipAddress )
static public IPAddress Int32ToIp( uint ipAddress )
{
return new IPAddress( BitConverter.GetBytes( ipAddress ).Reverse().ToArray() );
return new IPAddress( Swap( ipAddress ) );
}
static internal class Epoch