From 5c11508f41a638eb27398234c29ec1cdf051465d Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Wed, 20 Jan 2021 09:57:07 -0500 Subject: [PATCH] Make SendMessage call SendMessages instead of SendMessageToConnection --- .../Networking/BroadcastBufferManager.cs | 8 +++--- Facepunch.Steamworks/Networking/Connection.cs | 25 ++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Facepunch.Steamworks/Networking/BroadcastBufferManager.cs b/Facepunch.Steamworks/Networking/BroadcastBufferManager.cs index a6e63b5..502f641 100644 --- a/Facepunch.Steamworks/Networking/BroadcastBufferManager.cs +++ b/Facepunch.Steamworks/Networking/BroadcastBufferManager.cs @@ -48,7 +48,7 @@ public bool Decrement() } } - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer( CallingConvention.Cdecl )] private delegate void FreeFn( NetMsg* msg ); private static readonly Stack ReferenceCounterPool = @@ -246,7 +246,7 @@ private static void FreeBuffer( IntPtr ptr, int size ) } } - private const int Bucket512 = 512; + private const int Bucket512 = 512; private const int Bucket1Kb = 1 * 1024; private const int Bucket4Kb = 4 * 1024; private const int Bucket16Kb = 16 * 1024; @@ -254,8 +254,8 @@ private static void FreeBuffer( IntPtr ptr, int size ) private const int Bucket256Kb = 256 * 1024; private static int GetBucketSize( int size ) - { - if ( size <= Bucket512 ) return Bucket512; + { + if ( size <= Bucket512 ) return Bucket512; if ( size <= Bucket1Kb ) return Bucket1Kb; if ( size <= Bucket4Kb ) return Bucket4Kb; if ( size <= Bucket16Kb ) return Bucket16Kb; diff --git a/Facepunch.Steamworks/Networking/Connection.cs b/Facepunch.Steamworks/Networking/Connection.cs index e2843cd..a424cd2 100644 --- a/Facepunch.Steamworks/Networking/Connection.cs +++ b/Facepunch.Steamworks/Networking/Connection.cs @@ -65,11 +65,30 @@ public string ConnectionName /// /// This is the best version to use. /// - public Result SendMessage( IntPtr ptr, int size, SendType sendType = SendType.Reliable ) + public unsafe Result SendMessage( IntPtr ptr, int size, SendType sendType = SendType.Reliable ) { + if ( ptr == IntPtr.Zero ) + throw new ArgumentNullException( nameof( ptr ) ); + if ( size == 0 ) + throw new ArgumentException( "`size` cannot be zero", nameof( size ) ); + + var copyPtr = BroadcastBufferManager.Get( size, 1 ); + Buffer.MemoryCopy( (void*)ptr, (void*)copyPtr, size, size ); + + var message = SteamNetworkingUtils.AllocateMessage(); + message->Connection = this; + message->Flags = sendType; + message->DataPtr = copyPtr; + message->DataSize = size; + message->FreeDataPtr = BroadcastBufferManager.FreeFunctionPointer; + long messageNumber = 0; - return SteamNetworkingSockets.Internal.SendMessageToConnection( this, ptr, (uint) size, (int)sendType, ref messageNumber ); - } + SteamNetworkingSockets.Internal.SendMessages( 1, &message, &messageNumber ); + + return messageNumber >= 0 + ? Result.OK + : (Result)(-messageNumber); + } /// /// Ideally should be using an IntPtr version unless you're being really careful with the byte[] array and