Add fake IP support to SteamNetworkingSockets

This commit is contained in:
André Straubmeier 2021-11-23 15:44:22 +01:00
parent 045a3963b0
commit 0455e7fc72

View File

@ -85,6 +85,7 @@ namespace Steamworks
internal void InstallEvents( bool server )
{
Dispatch.Install<SteamNetConnectionStatusChangedCallback_t>( ConnectionStatusChanged, server );
Dispatch.Install<SteamNetworkingFakeIPResult_t>( FakeIPResult, server );
}
@ -109,6 +110,19 @@ namespace Steamworks
public static event Action<Connection, ConnectionInfo> OnConnectionStatusChanged;
private static void FakeIPResult( SteamNetworkingFakeIPResult_t data )
{
foreach ( var port in data.Ports )
{
if ( port == 0 ) continue;
var address = NetAddress.From( Utility.Int32ToIp( data.IP ), port );
OnFakeIPResult?.Invoke( address );
}
}
public static event Action<NetAddress> OnFakeIPResult;
/// <summary>
/// Creates a "server" socket that listens for clients to connect to by calling
@ -256,5 +270,69 @@ namespace Steamworks
SetConnectionManager( t.Connection.Id, t );
return t;
}
/// <summary>
/// Return info about the FakeIP and port that we have been assigned, if any.
/// </summary>
public static bool RequestFakeIP( int numFakePorts = 1 )
{
return Internal.BeginAsyncRequestFakeIP( numFakePorts );
}
/// <summary>
/// Begin asynchronous process of allocating a fake IPv4 address that other
/// peers can use to contact us via P2P. IP addresses returned by this
/// function are globally unique for a given appid.
/// </summary>
public static Result GetFakeIP( int fakePortIndex, out NetAddress address )
{
var pInfo = default( SteamNetworkingFakeIPResult_t );
Internal.GetFakeIP( 0, ref pInfo );
address = NetAddress.From( Utility.Int32ToIp( pInfo.IP ), pInfo.Ports[fakePortIndex] );
return pInfo.Result;
}
/// <summary>
/// Creates a server that will be relayed via Valve's network (hiding the IP and improving ping).
///
/// To use this derive a class from <see cref="SocketManager"/> and override as much as you want.
///
/// </summary>
public static T CreateRelaySocketFakeIP<T>( int fakePortIndex = 0 ) where T : SocketManager, new()
{
var t = new T();
var options = Array.Empty<NetKeyValue>();
t.Socket = Internal.CreateListenSocketP2PFakeIP( 0, options.Length, options );
t.Initialize();
SetSocketManager( t.Socket.Id, t );
return t;
}
/// <summary>
/// Creates a server that will be relayed via Valve's network (hiding the IP and improving ping).
///
/// To use this you should pass a class that inherits <see cref="ISocketManager"/>. You can use
/// <see cref="SocketManager"/> to get connections and send messages, but the <see cref="ISocketManager"/> class
/// will received all the appropriate callbacks.
///
/// </summary>
public static SocketManager CreateRelaySocketFakeIP( int fakePortIndex, ISocketManager intrface )
{
var options = Array.Empty<NetKeyValue>();
var socket = Internal.CreateListenSocketP2PFakeIP( 0, options.Length, options );
var t = new SocketManager
{
Socket = socket,
Interface = intrface
};
t.Initialize();
SetSocketManager( t.Socket.Id, t );
return t;
}
}
}