using System; using System.Collections.Generic; namespace Facepunch.Steamworks.Data { /// /// Used as a base to create your client connection. This creates a socket /// to a single connection. /// /// You can override all the virtual functions to turn it into what you /// want it to do. /// public struct Connection { public uint Id { get; set; } public override string ToString() => Id.ToString(); public static implicit operator Connection( uint value ) => new Connection() { Id = value }; public static implicit operator uint( Connection value ) => value.Id; /// /// Accept an incoming connection that has been received on a listen socket. /// public Result Accept() { return SteamNetworkingSockets.Internal.AcceptConnection( this ); } /// /// Disconnects from the remote host and invalidates the connection handle. Any unread data on the connection is discarded.. /// reasonCode is defined and used by you. /// public bool Close( bool linger = false, int reasonCode = 0, string debugString = "Closing Connection" ) { return SteamNetworkingSockets.Internal.CloseConnection( this, reasonCode, debugString, linger ); } /// /// Get/Set connection user data /// public long UserData { get => SteamNetworkingSockets.Internal.GetConnectionUserData( this ); set => SteamNetworkingSockets.Internal.SetConnectionUserData( this, value ); } /// /// A name for the connection, used mostly for debugging /// public string ConnectionName { get { if ( !SteamNetworkingSockets.Internal.GetConnectionName( this, out var strVal ) ) return "ERROR"; return strVal; } set => SteamNetworkingSockets.Internal.SetConnectionName( this, value ); } /// /// This is the best version to use. /// public Result SendMessage( IntPtr ptr, int size, SendType sendType = SendType.Reliable ) { long messageNumber = 0; return SteamNetworkingSockets.Internal.SendMessageToConnection( this, ptr, (uint) size, (int)sendType, ref messageNumber ); } /// /// Ideally should be using an IntPtr version unless you're being really careful with the byte[] array and /// you're not creating a new one every frame (like using .ToArray()) /// public unsafe Result SendMessage( byte[] data, SendType sendType = SendType.Reliable ) { fixed ( byte* ptr = data ) { return SendMessage( (IntPtr)ptr, data.Length, sendType ); } } /// /// Ideally should be using an IntPtr version unless you're being really careful with the byte[] array and /// you're not creating a new one every frame (like using .ToArray()) /// public unsafe Result SendMessage( byte[] data, int offset, int length, SendType sendType = SendType.Reliable ) { fixed ( byte* ptr = data ) { return SendMessage( (IntPtr)ptr + offset, length, sendType ); } } /// /// This creates a ton of garbage - so don't do anything with this beyond testing! /// public unsafe Result SendMessage( string str, SendType sendType = SendType.Reliable ) { var bytes = System.Text.Encoding.UTF8.GetBytes( str ); return SendMessage( bytes, sendType ); } /// /// Flush any messages waiting on the Nagle timer and send them at the next transmission /// opportunity (often that means right now). /// public Result Flush() => SteamNetworkingSockets.Internal.FlushMessagesOnConnection( this ); /// /// Returns detailed connection stats in text format. Useful /// for dumping to a log, etc. /// /// Plain text connection info public string DetailedStatus() { if ( SteamNetworkingSockets.Internal.GetDetailedConnectionStatus( this, out var strVal ) != 0 ) return null; return strVal; } } }