Fixed ms.GetBuffer() usage (for netcore)

This commit is contained in:
Garry Newman 2017-04-05 16:23:13 +01:00
parent 412d13cbbc
commit fd82d26367
2 changed files with 14 additions and 14 deletions

View File

@ -26,12 +26,14 @@ public void PeerToPeerSend()
// //
client.Networking.SetListenChannel( 0, true ); 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( str, TestString );
Assert.AreEqual( steamid, client.SteamId ); Assert.AreEqual( steamid, client.SteamId );
OutputReceived = true; OutputReceived = true;
Console.WriteLine( "Got: " + str );
}; };
client.Networking.OnIncomingConnection = ( steamid ) => client.Networking.OnIncomingConnection = ( steamid ) =>

View File

@ -8,13 +8,16 @@ namespace Facepunch.Steamworks
{ {
public class Networking : IDisposable public class Networking : IDisposable
{ {
public Action<ulong, MemoryStream, int> 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<ulong, bool> OnIncomingConnection; public Func<ulong, bool> OnIncomingConnection;
public Action<ulong, SessionError> OnConnectionFailed; public Action<ulong, SessionError> OnConnectionFailed;
private List<int> ListenChannels = new List<int>(); private List<int> ListenChannels = new List<int>();
private MemoryStream ReceiveBuffer = new MemoryStream();
private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew(); private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew();
internal SteamNative.SteamNetworking networking; internal SteamNative.SteamNetworking networking;
@ -170,21 +173,16 @@ private unsafe bool ReadP2PPacket( int channel )
if ( !networking.IsP2PPacketAvailable( out DataAvailable, channel ) || DataAvailable == 0 ) if ( !networking.IsP2PPacketAvailable( out DataAvailable, channel ) || DataAvailable == 0 )
return false; return false;
if ( ReceiveBuffer.Capacity < DataAvailable ) if ( ReceiveBuffer.Length < DataAvailable )
ReceiveBuffer.Capacity = (int) DataAvailable; ReceiveBuffer = new byte[ DataAvailable + 1024 ];
ReceiveBuffer.Position = 0; fixed ( byte* p = ReceiveBuffer )
ReceiveBuffer.SetLength( DataAvailable );
fixed ( byte* p = ReceiveBuffer.GetBuffer() )
{ {
SteamNative.CSteamID steamid = 1; 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; return false;
ReceiveBuffer.SetLength( DataAvailable ); OnP2PData?.Invoke( steamid, ReceiveBuffer, (int) DataAvailable, channel );
OnP2PData?.Invoke( steamid, ReceiveBuffer, channel );
return true; return true;
} }
} }