From ab23781a5de23e222bfefedd961bf8125e8a03e6 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Fri, 3 May 2019 15:07:01 +0100 Subject: [PATCH] SocketInterface/ConnectionInterface --- .../Classes/ConnectionInterface.cs | 68 +++++++++++++++++++ .../Classes/SocketInterface.cs | 62 +++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Facepunch.Steamworks/Classes/ConnectionInterface.cs create mode 100644 Facepunch.Steamworks/Classes/SocketInterface.cs diff --git a/Facepunch.Steamworks/Classes/ConnectionInterface.cs b/Facepunch.Steamworks/Classes/ConnectionInterface.cs new file mode 100644 index 0000000..f58a96e --- /dev/null +++ b/Facepunch.Steamworks/Classes/ConnectionInterface.cs @@ -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; + } + } + + /// + /// We're trying to connect! + /// + public virtual void OnConnecting( ConnectionInfo data ) + { + + } + + /// + /// Client is connected. They move from connecting to Connections + /// + public virtual void OnConnected( ConnectionInfo data ) + { + Connected = true; + } + + /// + /// The connection has been closed remotely or disconnected locally. Check data.State for details. + /// + public virtual void OnDisconnected( ConnectionInfo data ) + { + Connected = false; + } + } +} \ No newline at end of file diff --git a/Facepunch.Steamworks/Classes/SocketInterface.cs b/Facepunch.Steamworks/Classes/SocketInterface.cs new file mode 100644 index 0000000..48331a1 --- /dev/null +++ b/Facepunch.Steamworks/Classes/SocketInterface.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using Steamworks.Data; + +namespace Steamworks +{ + public class SocketInterface + { + public List Connecting = new List(); + public List Connected = new List(); + 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; + } + } + + /// + /// Default behaviour is to accept every connection + /// + public virtual void OnConnecting( NetConnection connection, ConnectionInfo data ) + { + connection.Accept(); + Connecting.Add( connection ); + } + + /// + /// Client is connected. They move from connecting to Connections + /// + public virtual void OnConnected( NetConnection connection, ConnectionInfo data ) + { + Connecting.Remove( connection ); + Connected.Add( connection ); + } + + /// + /// The connection has been closed remotely or disconnected locally. Check data.State for details. + /// + public virtual void OnDisconnected( NetConnection connection, ConnectionInfo data ) + { + connection.Close(); + + Connecting.Remove( connection ); + Connected.Remove( connection ); + } + } +} \ No newline at end of file