From fd82d263677386d5615c83e1de2b081004d0bd49 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 5 Apr 2017 16:23:13 +0100 Subject: [PATCH] Fixed ms.GetBuffer() usage (for netcore) --- .../Client/Networking.cs | 6 +++-- Facepunch.Steamworks/Interfaces/Networking.cs | 22 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Facepunch.Steamworks.Test/Client/Networking.cs b/Facepunch.Steamworks.Test/Client/Networking.cs index 882ead6..1200b81 100644 --- a/Facepunch.Steamworks.Test/Client/Networking.cs +++ b/Facepunch.Steamworks.Test/Client/Networking.cs @@ -26,12 +26,14 @@ public void PeerToPeerSend() // client.Networking.SetListenChannel( 0, true ); - client.Networking.OnP2PData = ( steamid, ms, channel ) => + client.Networking.OnP2PData = ( steamid, bytes, length, channel ) => { - var str = Encoding.UTF8.GetString( ms.GetBuffer() ); + var str = Encoding.UTF8.GetString( bytes, 0, length ); Assert.AreEqual( str, TestString ); Assert.AreEqual( steamid, client.SteamId ); OutputReceived = true; + + Console.WriteLine( "Got: " + str ); }; client.Networking.OnIncomingConnection = ( steamid ) => diff --git a/Facepunch.Steamworks/Interfaces/Networking.cs b/Facepunch.Steamworks/Interfaces/Networking.cs index 1e9c438..c5b3245 100644 --- a/Facepunch.Steamworks/Interfaces/Networking.cs +++ b/Facepunch.Steamworks/Interfaces/Networking.cs @@ -8,13 +8,16 @@ namespace Facepunch.Steamworks { public class Networking : IDisposable { - public Action OnP2PData; + private static byte[] ReceiveBuffer = new byte[1024 * 64]; + + public delegate void OnRecievedP2PData( ulong steamid, byte[] data, int dataLength, int channel ); + + public OnRecievedP2PData OnP2PData; public Func OnIncomingConnection; public Action OnConnectionFailed; private List ListenChannels = new List(); - private MemoryStream ReceiveBuffer = new MemoryStream(); private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew(); internal SteamNative.SteamNetworking networking; @@ -170,21 +173,16 @@ private unsafe bool ReadP2PPacket( int channel ) if ( !networking.IsP2PPacketAvailable( out DataAvailable, channel ) || DataAvailable == 0 ) return false; - if ( ReceiveBuffer.Capacity < DataAvailable ) - ReceiveBuffer.Capacity = (int) DataAvailable; + if ( ReceiveBuffer.Length < DataAvailable ) + ReceiveBuffer = new byte[ DataAvailable + 1024 ]; - ReceiveBuffer.Position = 0; - ReceiveBuffer.SetLength( DataAvailable ); - - fixed ( byte* p = ReceiveBuffer.GetBuffer() ) + fixed ( byte* p = ReceiveBuffer ) { SteamNative.CSteamID steamid = 1; - if ( !networking.ReadP2PPacket( (IntPtr)p, (uint)DataAvailable, out DataAvailable, out steamid, channel ) || DataAvailable == 0 ) + if ( !networking.ReadP2PPacket( (IntPtr)p, DataAvailable, out DataAvailable, out steamid, channel ) || DataAvailable == 0 ) return false; - ReceiveBuffer.SetLength( DataAvailable ); - - OnP2PData?.Invoke( steamid, ReceiveBuffer, channel ); + OnP2PData?.Invoke( steamid, ReceiveBuffer, (int) DataAvailable, channel ); return true; } }