From 9d363de6b8e4e2bb7dd2e66e0f6a2c747b0645a3 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 3 May 2019 15:07:48 +0100 Subject: [PATCH] Creating a Socket/Connection means you need to create an interface --- .../SteamNetworkingSockets.cs | 123 ++++++++++++------ 1 file changed, 82 insertions(+), 41 deletions(-) diff --git a/Facepunch.Steamworks/SteamNetworkingSockets.cs b/Facepunch.Steamworks/SteamNetworkingSockets.cs index 84c6a54..8ef2841 100644 --- a/Facepunch.Steamworks/SteamNetworkingSockets.cs +++ b/Facepunch.Steamworks/SteamNetworkingSockets.cs @@ -19,94 +19,135 @@ namespace Steamworks { _internal = new ISteamNetworkingSockets(); _internal.InitClient(); + + SocketInterfaces = new Dictionary(); + ConnectionInterfaces = new Dictionary(); } return _internal; } } + static Dictionary SocketInterfaces; + + internal static SocketInterface GetSocketInterface( uint id ) + { + if ( SocketInterfaces == null ) return null; + if ( id == 0 ) throw new System.ArgumentException( "Invalid Socket" ); + + if ( SocketInterfaces.TryGetValue( id, out var isocket ) ) + return isocket; + + return null; + } + + internal static void SetSocketInterface( uint id, SocketInterface iface ) + { + if ( id == 0 ) throw new System.ArgumentException( "Invalid Socket" ); + + Console.WriteLine( $"Installing Socket For {id}" ); + SocketInterfaces[id] = iface; + } + + + static Dictionary ConnectionInterfaces; + + + internal static ConnectionInterface GetConnectionInterface( uint id ) + { + if ( ConnectionInterfaces == null ) return null; + if ( id == 0 ) return null; + + if ( ConnectionInterfaces.TryGetValue( id, out var iconnection ) ) + return iconnection; + + return null; + } + + internal static void SetConnectionInterface( uint id, ConnectionInterface iface ) + { + if ( id == 0 ) throw new System.ArgumentException( "Invalid Connection" ); + ConnectionInterfaces[id] = iface; + } + internal static void Shutdown() { _internal = null; + SocketInterfaces = null; + ConnectionInterfaces = null; } internal static void InstallEvents() { - SteamNetConnectionStatusChangedCallback_t.Install( x => OnConnectionStatusChanged( x ) ); + SteamNetConnectionStatusChangedCallback_t.Install( x => ConnectionStatusChanged( x ) ); } - private static void OnConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t data ) + private static void ConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t data ) { - if ( data.Nfo.state != data.OldState ) + // + // This is a message from/to a listen socket + // + if ( data.Nfo.listenSocket.Id > 0 ) { - OnConnectionStateChanged( data ); + var iface = GetSocketInterface( data.Nfo.listenSocket.Id ); + iface?.OnConnectionChanged( data.Conn, data.Nfo ); + } + else + { + var iface = GetConnectionInterface( data.Conn.Id ); + iface?.OnConnectionChanged( data.Nfo ); } - Console.WriteLine( $"data.Conn: {data.Conn.ToString()}" ); - Console.WriteLine( $"data.Conn.UserData: {data.Conn.UserData}" ); - Console.WriteLine( $"data.Conn.ConnectionName: {data.Conn.ConnectionName}" ); - - Console.WriteLine( $"identity: {data.Nfo.identity}" ); - Console.WriteLine( $"identity.type: {data.Nfo.identity.type}" ); - Console.WriteLine( $"identity.m_cbSize: {data.Nfo.identity.m_cbSize}" ); - Console.WriteLine( $"identity.steamID: {data.Nfo.identity.steamID}" ); - Console.WriteLine( $"userData: {data.Nfo.userData}" ); - Console.WriteLine( $"listenSocket: {data.Nfo.listenSocket}" ); - Console.WriteLine( $"address: {data.Nfo.address}" ); - Console.WriteLine( $"popRemote: {data.Nfo.popRemote}" ); - Console.WriteLine( $"popRelay: {data.Nfo.popRelay}" ); - Console.WriteLine( $"state: {data.Nfo.state}" ); - Console.WriteLine( $"endReason: {data.Nfo.endReason}" ); - Console.WriteLine( $"endDebug: {data.Nfo.endDebug}" ); - Console.WriteLine( $"connectionDescription: {data.Nfo.connectionDescription}" ); - Console.WriteLine( $"---" ); + OnConnectionStatusChanged?.Invoke( data.Conn, data.Nfo ); } - private static void OnConnectionStateChanged( SteamNetConnectionStatusChangedCallback_t data ) - { - switch ( data.Nfo.state ) - { - case SteamNetworkingConnectionState.Connecting: - OnConnecting?.Invoke( data.Conn, data.Nfo ); - return; - } - } - - public static event Action OnConnecting; + public static event Action OnConnectionStatusChanged; /// /// Creates a "server" socket that listens for clients to connect to by calling /// Connect, over ordinary UDP (IPv4 or IPv6) /// - public static Socket CreateNormalSocket( NetworkAddress address ) + public static T CreateNormalSocket( NetworkAddress address ) where T : SocketInterface, new() { - return Internal.CreateListenSocketIP( ref address ); + var t = new T(); + t.Socket = Internal.CreateListenSocketIP( ref address ); + SetSocketInterface( t.Socket.Id, t ); + return t; } /// /// Connect to a socket created via CreateListenSocketIP /// - public static NetConnection ConnectNormal( NetworkAddress address ) + public static T ConnectNormal( NetworkAddress address ) where T : ConnectionInterface, new() { - return Internal.ConnectByIPAddress( ref address ); + var t = new T(); + t.Connection = Internal.ConnectByIPAddress( ref address ); + SetConnectionInterface( t.Connection.Id, t ); + return t; } /// /// Creates a server that will be relayed via Valve's network (hiding the IP and improving ping) /// - public static Socket CreateRelaySocket( int virtualport = 0 ) + public static T CreateRelaySocket( int virtualport = 0 ) where T : SocketInterface, new() { - return Internal.CreateListenSocketP2P( virtualport ); + var t = new T(); + t.Socket = Internal.CreateListenSocketP2P( virtualport ); + SetSocketInterface( t.Socket.Id, t ); + return t; } /// /// Connect to a relay server /// - public static NetConnection ConnectRelay( SteamId serverId, int virtualport = 0 ) + public static T ConnectRelay( SteamId serverId, int virtualport = 0 ) where T : ConnectionInterface, new() { + var t = new T(); NetworkIdentity identity = serverId; - return Internal.ConnectP2P( ref identity, virtualport ); + t.Connection = Internal.ConnectP2P( ref identity, virtualport ); + SetConnectionInterface( t.Connection.Id, t ); + return t; } } } \ No newline at end of file