mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2024-12-25 06:05:46 +03:00
IPAddress Cleanup
This commit is contained in:
parent
e94866828a
commit
835770772f
@ -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()
|
||||
{
|
||||
|
@ -70,7 +70,6 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo
|
||||
/// </summary>
|
||||
private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj )
|
||||
{
|
||||
Console.WriteLine( "onDefinitionsUpdated" );
|
||||
LoadDefinitions();
|
||||
UpdatePrices();
|
||||
|
||||
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
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) +
|
||||
@ -16,15 +16,14 @@ static internal uint SwapBytes( uint x )
|
||||
((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
|
||||
|
Loading…
Reference in New Issue
Block a user