mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-04-27 15:29:33 +03:00
SocketInterface/ConnectionInterface
This commit is contained in:
parent
00bdc717c6
commit
ab23781a5d
68
Facepunch.Steamworks/Classes/ConnectionInterface.cs
Normal file
68
Facepunch.Steamworks/Classes/ConnectionInterface.cs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Steamworks.Data;
|
||||||
|
|
||||||
|
namespace Steamworks
|
||||||
|
{
|
||||||
|
public class ConnectionInterface
|
||||||
|
{
|
||||||
|
public NetConnection Connection;
|
||||||
|
public bool Connected = false;
|
||||||
|
|
||||||
|
public string ConnectionName
|
||||||
|
{
|
||||||
|
get => Connection.ConnectionName;
|
||||||
|
set => Connection.ConnectionName = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long UserData
|
||||||
|
{
|
||||||
|
get => Connection.UserData;
|
||||||
|
set => Connection.UserData = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close() => Connection.Close();
|
||||||
|
|
||||||
|
public override string ToString() => Connection.ToString();
|
||||||
|
|
||||||
|
public virtual void OnConnectionChanged( ConnectionInfo data )
|
||||||
|
{
|
||||||
|
switch ( data.State )
|
||||||
|
{
|
||||||
|
case ConnectionState.Connecting:
|
||||||
|
OnConnecting( data );
|
||||||
|
break;
|
||||||
|
case ConnectionState.Connected:
|
||||||
|
OnConnected( data );
|
||||||
|
break;
|
||||||
|
case ConnectionState.ClosedByPeer:
|
||||||
|
case ConnectionState.ProblemDetectedLocally:
|
||||||
|
OnDisconnected( data );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// We're trying to connect!
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnConnecting( ConnectionInfo data )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Client is connected. They move from connecting to Connections
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnConnected( ConnectionInfo data )
|
||||||
|
{
|
||||||
|
Connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The connection has been closed remotely or disconnected locally. Check data.State for details.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnDisconnected( ConnectionInfo data )
|
||||||
|
{
|
||||||
|
Connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
Facepunch.Steamworks/Classes/SocketInterface.cs
Normal file
62
Facepunch.Steamworks/Classes/SocketInterface.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Steamworks.Data;
|
||||||
|
|
||||||
|
namespace Steamworks
|
||||||
|
{
|
||||||
|
public class SocketInterface
|
||||||
|
{
|
||||||
|
public List<NetConnection> Connecting = new List<NetConnection>();
|
||||||
|
public List<NetConnection> Connected = new List<NetConnection>();
|
||||||
|
public Socket Socket { get; internal set; }
|
||||||
|
|
||||||
|
public bool Close() => Socket.Close();
|
||||||
|
|
||||||
|
public override string ToString() => Socket.ToString();
|
||||||
|
|
||||||
|
public virtual void OnConnectionChanged( NetConnection connection, ConnectionInfo data )
|
||||||
|
{
|
||||||
|
switch ( data.State )
|
||||||
|
{
|
||||||
|
case ConnectionState.Connecting:
|
||||||
|
OnConnecting( connection, data );
|
||||||
|
break;
|
||||||
|
case ConnectionState.Connected:
|
||||||
|
OnConnected( connection, data );
|
||||||
|
break;
|
||||||
|
case ConnectionState.ClosedByPeer:
|
||||||
|
case ConnectionState.ProblemDetectedLocally:
|
||||||
|
OnDisconnected( connection, data );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default behaviour is to accept every connection
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnConnecting( NetConnection connection, ConnectionInfo data )
|
||||||
|
{
|
||||||
|
connection.Accept();
|
||||||
|
Connecting.Add( connection );
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Client is connected. They move from connecting to Connections
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnConnected( NetConnection connection, ConnectionInfo data )
|
||||||
|
{
|
||||||
|
Connecting.Remove( connection );
|
||||||
|
Connected.Add( connection );
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The connection has been closed remotely or disconnected locally. Check data.State for details.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnDisconnected( NetConnection connection, ConnectionInfo data )
|
||||||
|
{
|
||||||
|
connection.Close();
|
||||||
|
|
||||||
|
Connecting.Remove( connection );
|
||||||
|
Connected.Remove( connection );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user