SocketInterface/ConnectionInterface

This commit is contained in:
Garry Newman 2019-05-03 15:07:01 +01:00
parent 00bdc717c6
commit ab23781a5d
2 changed files with 130 additions and 0 deletions

View 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;
}
}
}

View 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 );
}
}
}