From 0455e7fc7237c2b7ec02e75392624b05588348a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Straubmeier?= Date: Tue, 23 Nov 2021 15:44:22 +0100 Subject: [PATCH] Add fake IP support to SteamNetworkingSockets --- .../SteamNetworkingSockets.cs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/Facepunch.Steamworks/SteamNetworkingSockets.cs b/Facepunch.Steamworks/SteamNetworkingSockets.cs index fcd1755..f59afee 100644 --- a/Facepunch.Steamworks/SteamNetworkingSockets.cs +++ b/Facepunch.Steamworks/SteamNetworkingSockets.cs @@ -85,6 +85,7 @@ namespace Steamworks internal void InstallEvents( bool server ) { Dispatch.Install( ConnectionStatusChanged, server ); + Dispatch.Install( FakeIPResult, server ); } @@ -109,6 +110,19 @@ namespace Steamworks public static event Action 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 OnFakeIPResult; /// /// 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; } + + /// + /// Return info about the FakeIP and port that we have been assigned, if any. + /// + public static bool RequestFakeIP( int numFakePorts = 1 ) + { + return Internal.BeginAsyncRequestFakeIP( numFakePorts ); + } + + /// + /// 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. + /// + 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; + } + + /// + /// Creates a server that will be relayed via Valve's network (hiding the IP and improving ping). + /// + /// To use this derive a class from and override as much as you want. + /// + /// + public static T CreateRelaySocketFakeIP( int fakePortIndex = 0 ) where T : SocketManager, new() + { + var t = new T(); + var options = Array.Empty(); + t.Socket = Internal.CreateListenSocketP2PFakeIP( 0, options.Length, options ); + t.Initialize(); + SetSocketManager( t.Socket.Id, t ); + return t; + } + + /// + /// 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 . You can use + /// to get connections and send messages, but the class + /// will received all the appropriate callbacks. + /// + /// + public static SocketManager CreateRelaySocketFakeIP( int fakePortIndex, ISocketManager intrface ) + { + var options = Array.Empty(); + 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; + } } }