mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-01-12 14:48:02 +03:00
Optimized Networking
This commit is contained in:
parent
480daeb673
commit
44dcf4d150
@ -90,11 +90,15 @@ namespace Facepunch.Steamworks.Test
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
|
var sw = new Stopwatch();
|
||||||
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
|
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
|
||||||
{
|
{
|
||||||
for( int i=0; i<1024; i++ )
|
for( int i=0; i<1024; i++ )
|
||||||
{
|
{
|
||||||
|
sw.Restart();
|
||||||
client.Update();
|
client.Update();
|
||||||
|
Console.WriteLine( $"{sw.Elapsed.TotalMilliseconds}ms" );
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,11 @@ namespace Facepunch.Steamworks.Test
|
|||||||
var OutputReceived = false;
|
var OutputReceived = false;
|
||||||
var data = Encoding.UTF8.GetBytes( TestString );
|
var data = Encoding.UTF8.GetBytes( TestString );
|
||||||
|
|
||||||
|
//
|
||||||
|
// Enable listening on this channel
|
||||||
|
//
|
||||||
|
client.Networking.SetListenChannel( 0, true );
|
||||||
|
|
||||||
client.Networking.OnP2PData = ( steamid, ms, channel ) =>
|
client.Networking.OnP2PData = ( steamid, ms, channel ) =>
|
||||||
{
|
{
|
||||||
var str = Encoding.UTF8.GetString( ms.GetBuffer() );
|
var str = Encoding.UTF8.GetString( ms.GetBuffer() );
|
||||||
|
@ -12,6 +12,10 @@ namespace Facepunch.Steamworks
|
|||||||
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 MemoryStream ReceiveBuffer = new MemoryStream();
|
||||||
|
|
||||||
internal SteamNative.SteamNetworking networking;
|
internal SteamNative.SteamNetworking networking;
|
||||||
|
|
||||||
internal Networking( BaseSteamworks steamworks, SteamNative.SteamNetworking networking )
|
internal Networking( BaseSteamworks steamworks, SteamNative.SteamNetworking networking )
|
||||||
@ -29,23 +33,39 @@ namespace Facepunch.Steamworks
|
|||||||
OnIncomingConnection = null;
|
OnIncomingConnection = null;
|
||||||
OnConnectionFailed = null;
|
OnConnectionFailed = null;
|
||||||
OnP2PData = null;
|
OnP2PData = null;
|
||||||
|
ListenChannels.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal void Update()
|
internal void Update()
|
||||||
{
|
{
|
||||||
for ( int i = 0; i < 32; i++ )
|
if ( OnP2PData == null )
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach ( var channel in ListenChannels )
|
||||||
{
|
{
|
||||||
// POOL ME
|
while ( ReadP2PPacket( channel ) )
|
||||||
using ( var ms = new MemoryStream() )
|
|
||||||
{
|
{
|
||||||
while( ReadP2PPacket( ms, i ) )
|
// Nothing Here.
|
||||||
{
|
|
||||||
// Nothing Here.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enable or disable listening on a specific channel.
|
||||||
|
/// If you donp't enable the channel we won't listen to it,
|
||||||
|
/// so you won't be able to receive messages on it.
|
||||||
|
/// </summary>
|
||||||
|
public void SetListenChannel( int ChannelId, bool Listen )
|
||||||
|
{
|
||||||
|
ListenChannels.RemoveAll( x => x == ChannelId );
|
||||||
|
|
||||||
|
if ( Listen )
|
||||||
|
{
|
||||||
|
ListenChannels.Add( ChannelId );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void onP2PConnectionRequest( SteamNative.P2PSessionRequest_t o, bool b )
|
private void onP2PConnectionRequest( SteamNative.P2PSessionRequest_t o, bool b )
|
||||||
{
|
{
|
||||||
if ( OnIncomingConnection != null )
|
if ( OnIncomingConnection != null )
|
||||||
@ -132,28 +152,28 @@ namespace Facepunch.Steamworks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private unsafe bool ReadP2PPacket( MemoryStream ms, int channel = 0 )
|
private unsafe bool ReadP2PPacket( int channel )
|
||||||
{
|
{
|
||||||
uint DataAvailable = 0;
|
uint DataAvailable = 0;
|
||||||
|
|
||||||
if ( !networking.IsP2PPacketAvailable( out DataAvailable, channel ) || DataAvailable == 0 )
|
if ( !networking.IsP2PPacketAvailable( out DataAvailable, channel ) || DataAvailable == 0 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ( ms.Capacity < DataAvailable )
|
if ( ReceiveBuffer.Capacity < DataAvailable )
|
||||||
ms.Capacity = (int) DataAvailable;
|
ReceiveBuffer.Capacity = (int) DataAvailable;
|
||||||
|
|
||||||
ms.Position = 0;
|
ReceiveBuffer.Position = 0;
|
||||||
ms.SetLength( DataAvailable );
|
ReceiveBuffer.SetLength( DataAvailable );
|
||||||
|
|
||||||
fixed ( byte* p = ms.GetBuffer() )
|
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, (uint)DataAvailable, out DataAvailable, out steamid, channel ) || DataAvailable == 0 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
ms.SetLength( DataAvailable );
|
ReceiveBuffer.SetLength( DataAvailable );
|
||||||
|
|
||||||
OnP2PData?.Invoke( steamid, ms, channel );
|
OnP2PData?.Invoke( steamid, ReceiveBuffer, channel );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user