Use IPAddress

This commit is contained in:
Garry Newman 2017-08-17 14:45:58 +01:00
parent c6bd69092a
commit cc4bdcea8d
5 changed files with 28 additions and 26 deletions

View File

@ -47,7 +47,7 @@ public void InternetList()
foreach ( var server in query.Responded.Take( 20 ) )
{
Console.WriteLine( "{0} {1}", server.AddressString, server.Name );
Console.WriteLine( "{0} {1}", server.Address, server.Name );
}
query.Dispose();
@ -370,7 +370,9 @@ public void CustomList()
foreach ( var s in query.Responded )
{
Console.WriteLine( "{0} - {1}", s.AddressString, s.Name );
Console.WriteLine( "{0} - {1}", s.Address, s.Name );
Assert.IsTrue( servers.Contains( $"{s.Address}:{s.QueryPort}" ) );
}
query.Dispose();

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;
@ -25,7 +26,7 @@ public class Server
public int Version { get; set; }
public string[] Tags { get; set; }
public ulong SteamId { get; set; }
public uint Address { get; set; }
public IPAddress Address { get; set; }
public int ConnectionPort { get; set; }
public int QueryPort { get; set; }
@ -42,27 +43,13 @@ public bool Favourite
internal Client Client;
public string AddressString
{
get
{
return string.Format( "{0}.{1}.{2}.{3}", ( Address >> 24 ) & 0xFFul, ( Address >> 16 ) & 0xFFul, ( Address >> 8 ) & 0xFFul, Address & 0xFFul );
}
}
public string ConnectionAddress
{
get
{
return string.Format( "{0}.{1}.{2}.{3}:{4}", ( Address >> 24 ) & 0xFFul, ( Address >> 16 ) & 0xFFul, ( Address >> 8 ) & 0xFFul, Address & 0xFFul, ConnectionPort );
}
}
internal static Server FromSteam( Client client, SteamNative.gameserveritem_t item )
{
return new Server()
{
Client = client,
Address = item.NetAdr.IP,
Address = Utility.Int32ToIp( item.NetAdr.IP ),
ConnectionPort = item.NetAdr.ConnectionPort,
QueryPort = item.NetAdr.QueryPort,
Name = item.ServerName,
@ -133,7 +120,7 @@ internal void OnServerRulesReceiveFinished( bool Success )
/// </summary>
public void AddToHistory()
{
Client.native.matchmaking.AddFavoriteGame( AppId, Address, (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagHistory, (uint)Utility.Epoch.Current );
Client.native.matchmaking.AddFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagHistory, (uint)Utility.Epoch.Current );
Client.ServerList.UpdateFavouriteList();
}
@ -142,7 +129,7 @@ public void AddToHistory()
/// </summary>
public void RemoveFromHistory()
{
Client.native.matchmaking.RemoveFavoriteGame( AppId, Address, (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagHistory );
Client.native.matchmaking.RemoveFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagHistory );
Client.ServerList.UpdateFavouriteList();
}
@ -151,7 +138,7 @@ public void RemoveFromHistory()
/// </summary>
public void AddToFavourites()
{
Client.native.matchmaking.AddFavoriteGame( AppId, Address, (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite, (uint)Utility.Epoch.Current );
Client.native.matchmaking.AddFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite, (uint)Utility.Epoch.Current );
Client.ServerList.UpdateFavouriteList();
}
@ -160,7 +147,7 @@ public void AddToFavourites()
/// </summary>
public void RemoveFromFavourites()
{
Client.native.matchmaking.RemoveFavoriteGame( AppId, Address, (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite );
Client.native.matchmaking.RemoveFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite );
Client.ServerList.UpdateFavouriteList();
}
}

View File

@ -248,7 +248,7 @@ public Request Local( Filter filter = null )
internal bool IsFavourite( Server server )
{
ulong encoded = server.Address;
ulong encoded = Utility.IpToInt32( server.Address );
encoded = encoded << 32;
encoded = encoded | (uint)server.ConnectionPort;
@ -257,7 +257,7 @@ internal bool IsFavourite( Server server )
internal bool IsHistory( Server server )
{
ulong encoded = server.Address;
ulong encoded = Utility.IpToInt32( server.Address );
encoded = encoded << 32;
encoded = encoded | (uint)server.ConnectionPort;

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;
@ -20,7 +21,7 @@ class ServerRules : IDisposable
// The server that called us
private ServerList.Server Server;
public ServerRules( ServerList.Server server, uint address, int queryPort )
public ServerRules( ServerList.Server server, IPAddress address, int queryPort )
{
Server = server;
@ -32,7 +33,7 @@ public ServerRules( ServerList.Server server, uint address, int queryPort )
//
// Ask Steam to get the server rules, respond to our fake vtable
//
Server.Client.native.servers.ServerRules( address, (ushort)queryPort, GetPtr() );
Server.Client.native.servers.ServerRules( Utility.IpToInt32( address ), (ushort)queryPort, GetPtr() );
}
public void Dispose()

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace Facepunch.Steamworks
@ -15,6 +16,17 @@ static internal uint SwapBytes( uint x )
( ( x & 0xff000000 ) >> 24 );
}
static internal uint IpToInt32( this IPAddress ipAddress )
{
return BitConverter.ToUInt32( ipAddress.GetAddressBytes().Reverse().ToArray(), 0 );
}
static internal IPAddress Int32ToIp( uint ipAddress )
{
return new IPAddress( BitConverter.GetBytes( ipAddress ).Reverse().ToArray() );
}
static internal class Epoch
{
private static readonly DateTime epoch = new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc );