Message send/recv tests

This commit is contained in:
Garry Newman 2019-05-06 12:33:29 +01:00
parent f77332a41f
commit cdeccc5afe
4 changed files with 121 additions and 6 deletions

View File

@ -30,6 +30,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -39,6 +40,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
@ -48,6 +50,7 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
@ -57,6 +60,7 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
@ -66,6 +70,7 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
@ -75,6 +80,7 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">

View File

@ -96,10 +96,19 @@ namespace Steamworks
Console.WriteLine( "[Connection] Hey We're Connected!" );
var sw = System.Diagnostics.Stopwatch.StartNew();
while ( Connected )
{
Receive();
await Task.Delay( 10 );
await Task.Delay( 100 );
if ( sw.Elapsed.TotalSeconds > 5 )
{
Console.WriteLine( "CLIENT ERROR!!!!! TIMED OUT" );
break;
}
}
}
@ -110,9 +119,26 @@ namespace Steamworks
Console.WriteLine( $"[Connection][{messageNum}][{recvTime}][{channel}] \"{str}\"" );
if ( str.StartsWith( "Hello" ) )
if ( str.Contains( "Hello" ) )
{
Connection.SendMessage( "Hello, How are you!?" );
Connection.SendMessage( "How do you like 20 messages in a row?" );
for ( int i=0; i<20; i++ )
{
Connection.SendMessage( $"BLAMMO!" );
}
}
if ( str.Contains( "how about yourself" ) )
{
Connection.SendMessage( "I'm great, but I have to go now, bye." );
}
if ( str.Contains( "hater" ) )
{
Close();
}
}
@ -173,14 +199,43 @@ namespace Steamworks
await Task.Delay( 300 );
singleClient.SendMessage( "Hello Client!?" );
await Task.Delay( 1000 );
var sw = System.Diagnostics.Stopwatch.StartNew();
singleClient.Close();
while ( Connected.Contains( singleClient ) )
{
Receive();
await Task.Delay( 100 );
if ( sw.Elapsed.TotalSeconds > 5 )
{
Assert.Fail( "Took too long" );
break;
}
}
await Task.Delay( 1000 );
Close();
}
public override unsafe void OnMessage( NetConnection connection, NetworkIdentity identity, IntPtr data, int size, long messageNum, SteamNetworkingMicroseconds recvTime, int channel )
{
// We're only sending strings, so it's fine to read this like this
var str = UTF8Encoding.UTF8.GetString( (byte*)data, size );
Console.WriteLine( $"[SOCKET][{connection}[{identity}][{messageNum}][{recvTime}][{channel}] \"{str}\"" );
if ( str.Contains( "Hello, How are you" ) )
{
connection.SendMessage( "I'm great thanks, how about yourself?" );
}
if ( str.Contains( "bye" ) )
{
connection.SendMessage( "See you later, hater." );
connection.Close( true, 10, "Said Bye" );
}
}
}
}

View File

@ -37,6 +37,7 @@ namespace Steamworks
break;
case ConnectionState.ClosedByPeer:
case ConnectionState.ProblemDetectedLocally:
case ConnectionState.None:
OnDisconnected( data );
break;
}
@ -78,7 +79,8 @@ namespace Steamworks
for ( int i = 0; i < processed; i++ )
{
ReceiveMessage( Marshal.ReadIntPtr( messageBuffer, i ) );
// #32bit
ReceiveMessage( Marshal.ReadIntPtr( messageBuffer, i * 8) );
}
}
finally

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Steamworks.Data;
namespace Steamworks
@ -25,6 +27,7 @@ namespace Steamworks
break;
case ConnectionState.ClosedByPeer:
case ConnectionState.ProblemDetectedLocally:
case ConnectionState.None:
OnDisconnected( connection, data );
break;
}
@ -58,5 +61,54 @@ namespace Steamworks
Connecting.Remove( connection );
Connected.Remove( connection );
}
public void Receive( int bufferSize = 32 )
{
// #32bit
int processed = 0;
IntPtr messageBuffer = Marshal.AllocHGlobal( 8 * bufferSize );
try
{
processed = SteamNetworkingSockets.Internal.ReceiveMessagesOnListenSocket( Socket, messageBuffer, bufferSize );
for ( int i = 0; i < processed; i++ )
{
// #32bit
ReceiveMessage( Marshal.ReadIntPtr( messageBuffer, i * 8 ) );
}
}
finally
{
Marshal.FreeHGlobal( messageBuffer );
}
//
// Overwhelmed our buffer, keep going
//
if ( processed == bufferSize )
Receive( bufferSize );
}
internal unsafe void ReceiveMessage( IntPtr msgPtr )
{
var msg = Marshal.PtrToStructure<NetMsg>( msgPtr );
try
{
OnMessage( msg.Connection, msg.Identity, msg.DataPtr, msg.DataSize, msg.TimeRecv, msg.MessageNumber, msg.Channel );
}
finally
{
//
// Releases the message
//
msg.Release( msgPtr );
}
}
public virtual void OnMessage( NetConnection connection, NetworkIdentity identity, IntPtr data, int size, long messageNum, SteamNetworkingMicroseconds recvTime, int channel )
{
}
}
}