Facepunch.Steamworks/Facepunch.Steamworks.Test/NetworkingSocketsTest.TestConnectionInterface.cs

125 lines
3.1 KiB
C#
Raw Normal View History

2020-02-22 23:29:37 +03:00
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Steamworks.Data;
namespace Steamworks
{
public partial class NetworkingSocketsTest
{
private class TestConnectionInterface : ConnectionManager
2020-02-22 23:29:37 +03:00
{
public override void OnConnectionChanged( ConnectionInfo data )
{
Console.WriteLine( $"[Connection][{Connection}] [{data.State}]" );
base.OnConnectionChanged( data );
}
public override void OnConnecting( ConnectionInfo data )
{
Console.WriteLine( $" - OnConnecting" );
base.OnConnecting( data );
}
/// <summary>
/// Client is connected. They move from connecting to Connections
/// </summary>
public override void OnConnected( ConnectionInfo data )
{
Console.WriteLine( $" - OnConnected" );
base.OnConnected( data );
}
/// <summary>
/// The connection has been closed remotely or disconnected locally. Check data.State for details.
/// </summary>
public override void OnDisconnected( ConnectionInfo data )
{
Console.WriteLine( $" - OnDisconnected" );
base.OnDisconnected( data );
}
internal async Task RunAsync()
{
Console.WriteLine( "[Connection] RunAsync" );
var sw = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine( "[Connection] Connecting" );
2020-02-22 23:29:37 +03:00
while ( Connecting )
{
await Task.Delay( 10 );
if ( sw.Elapsed.TotalSeconds > 10 )
2020-02-22 23:29:37 +03:00
break;
}
if ( !Connected )
{
Console.WriteLine( "[Connection] Couldn't connect!" );
Console.WriteLine( Connection.DetailedStatus() );
return;
}
Console.WriteLine( "[Connection] Hey We're Connected!" );
sw = System.Diagnostics.Stopwatch.StartNew();
while ( Connected )
{
Receive();
await Task.Delay( 100 );
if ( sw.Elapsed.TotalSeconds > 10 )
{
Assert.Fail( "Client Took Too Long" );
break;
}
}
}
public override unsafe void OnMessage( IntPtr data, int size, long messageNum, long 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( $"[Connection][{messageNum}][{recvTime}][{channel}] \"{str}\"" );
if ( str.Contains( "Hello" ) )
{
Console.WriteLine( $"[Connection][{messageNum}][{recvTime}][{channel}] Sending: Hello, How are you!?" );
2020-02-22 23:29:37 +03:00
Connection.SendMessage( "Hello, How are you!?" );
Console.WriteLine( $"[Connection][{messageNum}][{recvTime}][{channel}] Sending: How do you like 20 messages in a row?" );
2020-02-22 23:29:37 +03:00
Connection.SendMessage( "How do you like 20 messages in a row?" );
for ( int i=0; i<20; i++ )
{
Connection.SendMessage( $"BLAMMO!" );
}
Connection.Flush();
2020-02-22 23:29:37 +03:00
}
if ( str.Contains( "status" ))
{
Console.WriteLine( Connection.DetailedStatus() );
}
if ( str.Contains( "how about yourself" ) )
{
Connection.SendMessage( "I'm great, but I have to go now, bye." );
}
if ( str.Contains( "hater" ) )
{
Close();
}
}
}
}
}