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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
@ -12,6 +13,23 @@ namespace Facepunch.Steamworks.Test
[DeploymentItem( "steam_api64.dll" )] [DeploymentItem( "steam_api64.dll" )]
public partial class ServerList 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] [TestMethod]
public void InternetList() public void InternetList()
{ {

View File

@ -70,7 +70,6 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj ) private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj )
{ {
Console.WriteLine( "onDefinitionsUpdated" );
LoadDefinitions(); LoadDefinitions();
UpdatePrices(); UpdatePrices();

View File

@ -33,13 +33,15 @@ namespace Facepunch.Steamworks
Instance = this; Instance = this;
native = new Interop.NativeInterface(); native = new Interop.NativeInterface();
uint ipaddress = 0; // Any Port
if ( init.SteamPort == 0 ) init.RandomSteamPort(); if ( init.SteamPort == 0 ) init.RandomSteamPort();
if ( init.IpAddress != null ) ipaddress = Utility.IpToInt32( init.IpAddress );
// //
// Get other interfaces // 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.Dispose();
native = null; native = null;
@ -300,7 +302,7 @@ namespace Facepunch.Steamworks
var ip = native.gameServer.GetPublicIP(); var ip = native.gameServer.GetPublicIP();
if ( ip == 0 ) return null; 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -12,7 +13,7 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public class ServerInit public class ServerInit
{ {
public uint IpAddress = 0; public IPAddress IpAddress;
public ushort SteamPort; public ushort SteamPort;
public ushort GamePort = 27015; public ushort GamePort = 27015;
public ushort QueryPort = 27016; public ushort QueryPort = 27016;

View File

@ -6,25 +6,24 @@ using System.Text;
namespace Facepunch.Steamworks 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 ) + return ((x & 0x000000ff) << 24) +
( ( x & 0x0000ff00 ) << 8 ) + ((x & 0x0000ff00) << 8) +
( ( x & 0x00ff0000 ) >> 8 ) + ((x & 0x00ff0000) >> 8) +
( ( x & 0xff000000 ) >> 24 ); ((x & 0xff000000) >> 24);
} }
static public uint IpToInt32( this IPAddress ipAddress )
static internal 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 static internal class Epoch