(965c31410a) Unstable v0.10.4.0
This commit is contained in:
@@ -8,31 +8,21 @@ using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
public static class SteamNetworkingSockets
|
||||
public class SteamNetworkingSockets : SteamSharedClass<SteamNetworkingSockets>
|
||||
{
|
||||
static ISteamNetworkingSockets _internal;
|
||||
internal static ISteamNetworkingSockets Internal
|
||||
internal static ISteamNetworkingSockets Internal => Interface as ISteamNetworkingSockets;
|
||||
|
||||
internal override void InitializeInterface( bool server )
|
||||
{
|
||||
get
|
||||
{
|
||||
if ( _internal == null )
|
||||
{
|
||||
_internal = new ISteamNetworkingSockets();
|
||||
_internal.Init();
|
||||
|
||||
SocketInterfaces = new Dictionary<uint, SocketInterface>();
|
||||
ConnectionInterfaces = new Dictionary<uint, ConnectionInterface>();
|
||||
}
|
||||
|
||||
return _internal;
|
||||
}
|
||||
SetInterface( server, new ISteamNetworkingSockets( server ) );
|
||||
InstallEvents( server );
|
||||
}
|
||||
|
||||
#region SocketInterface
|
||||
|
||||
#region SocketInterface
|
||||
static readonly Dictionary<uint, SocketManager> SocketInterfaces = new Dictionary<uint, SocketManager>();
|
||||
|
||||
static Dictionary<uint, SocketInterface> SocketInterfaces;
|
||||
|
||||
internal static SocketInterface GetSocketInterface( uint id )
|
||||
internal static SocketManager GetSocketManager( uint id )
|
||||
{
|
||||
if ( SocketInterfaces == null ) return null;
|
||||
if ( id == 0 ) throw new System.ArgumentException( "Invalid Socket" );
|
||||
@@ -43,20 +33,17 @@ namespace Steamworks
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static void SetSocketInterface( uint id, SocketInterface iface )
|
||||
internal static void SetSocketManager( uint id, SocketManager manager )
|
||||
{
|
||||
if ( id == 0 ) throw new System.ArgumentException( "Invalid Socket" );
|
||||
|
||||
Console.WriteLine( $"Installing Socket For {id}" );
|
||||
SocketInterfaces[id] = iface;
|
||||
SocketInterfaces[id] = manager;
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region ConnectionInterface
|
||||
static Dictionary<uint, ConnectionInterface> ConnectionInterfaces;
|
||||
#region ConnectionInterface
|
||||
static readonly Dictionary<uint, ConnectionManager> ConnectionInterfaces = new Dictionary<uint, ConnectionManager>();
|
||||
|
||||
|
||||
internal static ConnectionInterface GetConnectionInterface( uint id )
|
||||
internal static ConnectionManager GetConnectionManager( uint id )
|
||||
{
|
||||
if ( ConnectionInterfaces == null ) return null;
|
||||
if ( id == 0 ) return null;
|
||||
@@ -67,24 +54,20 @@ namespace Steamworks
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static void SetConnectionInterface( uint id, ConnectionInterface iface )
|
||||
internal static void SetConnectionManager( uint id, ConnectionManager manager )
|
||||
{
|
||||
if ( id == 0 ) throw new System.ArgumentException( "Invalid Connection" );
|
||||
ConnectionInterfaces[id] = iface;
|
||||
ConnectionInterfaces[id] = manager;
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
internal static void Shutdown()
|
||||
|
||||
|
||||
internal void InstallEvents( bool server )
|
||||
{
|
||||
_internal = null;
|
||||
SocketInterfaces = null;
|
||||
ConnectionInterfaces = null;
|
||||
Dispatch.Install<SteamNetConnectionStatusChangedCallback_t>( ConnectionStatusChanged, server );
|
||||
}
|
||||
|
||||
internal static void InstallEvents( bool server = false )
|
||||
{
|
||||
SteamNetConnectionStatusChangedCallback_t.Install( x => ConnectionStatusChanged( x ), server );
|
||||
}
|
||||
|
||||
private static void ConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t data )
|
||||
{
|
||||
@@ -93,12 +76,12 @@ namespace Steamworks
|
||||
//
|
||||
if ( data.Nfo.listenSocket.Id > 0 )
|
||||
{
|
||||
var iface = GetSocketInterface( data.Nfo.listenSocket.Id );
|
||||
var iface = GetSocketManager( data.Nfo.listenSocket.Id );
|
||||
iface?.OnConnectionChanged( data.Conn, data.Nfo );
|
||||
}
|
||||
else
|
||||
{
|
||||
var iface = GetConnectionInterface( data.Conn.Id );
|
||||
var iface = GetConnectionManager( data.Conn.Id );
|
||||
iface?.OnConnectionChanged( data.Nfo );
|
||||
}
|
||||
|
||||
@@ -111,46 +94,128 @@ namespace Steamworks
|
||||
/// <summary>
|
||||
/// Creates a "server" socket that listens for clients to connect to by calling
|
||||
/// Connect, over ordinary UDP (IPv4 or IPv6)
|
||||
///
|
||||
/// To use this derive a class from SocketManager and override as much as you want.
|
||||
///
|
||||
/// </summary>
|
||||
public static T CreateNormalSocket<T>( NetAddress address ) where T : SocketInterface, new()
|
||||
public static T CreateNormalSocket<T>( NetAddress address ) where T : SocketManager, new()
|
||||
{
|
||||
var t = new T();
|
||||
t.Socket = Internal.CreateListenSocketIP( ref address );
|
||||
SetSocketInterface( t.Socket.Id, t );
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
t.Socket = Internal.CreateListenSocketIP( ref address, options.Length, options );
|
||||
t.Initialize();
|
||||
|
||||
SetSocketManager( t.Socket.Id, t );
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a "server" socket that listens for clients to connect to by calling
|
||||
/// Connect, over ordinary UDP (IPv4 or IPv6).
|
||||
///
|
||||
/// To use this you should pass a class that inherits ISocketManager. You can use
|
||||
/// SocketManager to get connections and send messages, but the ISocketManager class
|
||||
/// will received all the appropriate callbacks.
|
||||
///
|
||||
/// </summary>
|
||||
public static SocketManager CreateNormalSocket( NetAddress address, ISocketManager intrface )
|
||||
{
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
var socket = Internal.CreateListenSocketIP( ref address, options.Length, options );
|
||||
|
||||
var t = new SocketManager
|
||||
{
|
||||
Socket = socket,
|
||||
Interface = intrface
|
||||
};
|
||||
|
||||
t.Initialize();
|
||||
|
||||
SetSocketManager( t.Socket.Id, t );
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connect to a socket created via <method>CreateListenSocketIP</method>
|
||||
/// </summary>
|
||||
public static T ConnectNormal<T>( NetAddress address ) where T : ConnectionInterface, new()
|
||||
public static T ConnectNormal<T>( NetAddress address ) where T : ConnectionManager, new()
|
||||
{
|
||||
var t = new T();
|
||||
t.Connection = Internal.ConnectByIPAddress( ref address );
|
||||
SetConnectionInterface( t.Connection.Id, t );
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
t.Connection = Internal.ConnectByIPAddress( ref address, options.Length, options );
|
||||
SetConnectionManager( t.Connection.Id, t );
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connect to a socket created via <method>CreateListenSocketIP</method>
|
||||
/// </summary>
|
||||
public static ConnectionManager ConnectNormal( NetAddress address, IConnectionManager iface )
|
||||
{
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
var connection = Internal.ConnectByIPAddress( ref address, options.Length, options );
|
||||
|
||||
var t = new ConnectionManager
|
||||
{
|
||||
Connection = connection,
|
||||
Interface = iface
|
||||
};
|
||||
|
||||
SetConnectionManager( t.Connection.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 derive a class from SocketManager and override as much as you want.
|
||||
///
|
||||
/// </summary>
|
||||
public static T CreateRelaySocket<T>( int virtualport = 0 ) where T : SocketInterface, new()
|
||||
public static T CreateRelaySocket<T>( int virtualport = 0 ) where T : SocketManager, new()
|
||||
{
|
||||
var t = new T();
|
||||
t.Socket = Internal.CreateListenSocketP2P( virtualport );
|
||||
SetSocketInterface( t.Socket.Id, t );
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
t.Socket = Internal.CreateListenSocketP2P( virtualport, 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 ISocketManager. You can use
|
||||
/// SocketManager to get connections and send messages, but the ISocketManager class
|
||||
/// will received all the appropriate callbacks.
|
||||
///
|
||||
/// </summary>
|
||||
public static SocketManager CreateRelaySocket( int virtualport, ISocketManager intrface )
|
||||
{
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
var socket = Internal.CreateListenSocketP2P( virtualport, options.Length, options );
|
||||
|
||||
var t = new SocketManager
|
||||
{
|
||||
Socket = socket,
|
||||
Interface = intrface
|
||||
};
|
||||
|
||||
t.Initialize();
|
||||
|
||||
SetSocketManager( t.Socket.Id, t );
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connect to a relay server
|
||||
/// </summary>
|
||||
public static T ConnectRelay<T>( SteamId serverId, int virtualport = 0 ) where T : ConnectionInterface, new()
|
||||
public static T ConnectRelay<T>( SteamId serverId, int virtualport = 0 ) where T : ConnectionManager, new()
|
||||
{
|
||||
var t = new T();
|
||||
NetIdentity identity = serverId;
|
||||
t.Connection = Internal.ConnectP2P( ref identity, virtualport );
|
||||
SetConnectionInterface( t.Connection.Id, t );
|
||||
var options = Array.Empty<NetKeyValue>();
|
||||
t.Connection = Internal.ConnectP2P( ref identity, virtualport, options.Length, options );
|
||||
SetConnectionManager( t.Connection.Id, t );
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user