Facepunch.Steamworks/Facepunch.Steamworks.Test/NetworkingSockets.cs

288 lines
7.2 KiB
C#
Raw Normal View History

2019-05-02 22:41:45 +03:00
using System;
2019-05-03 17:08:48 +03:00
using System.Collections.Generic;
2019-05-02 22:41:45 +03:00
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
2019-05-03 17:08:48 +03:00
using Steamworks.Data;
2019-05-02 22:41:45 +03:00
namespace Steamworks
{
[TestClass]
[DeploymentItem( "steam_api64.dll" )]
public class NetworkingSocketsTest
{
[TestMethod]
public async Task CreateRelayServer()
{
2019-05-03 17:08:48 +03:00
var si = SteamNetworkingSockets.CreateRelaySocket<TestSocketInterface>();
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
Console.WriteLine( $"Created Socket: {si}" );
2019-05-02 22:41:45 +03:00
// Give it a second for something to happen
2019-05-03 17:08:48 +03:00
await Task.Delay( 1000 );
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
si.Close();
2019-05-02 22:41:45 +03:00
}
[TestMethod]
public async Task CreateNormalServer()
{
2019-05-06 15:34:41 +03:00
var si = SteamNetworkingSockets.CreateNormalSocket<TestSocketInterface>( Data.NetAddress.AnyIp( 21893 ) );
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
Console.WriteLine( $"Created Socket: {si}" );
2019-05-02 22:41:45 +03:00
// Give it a second for something to happen
2019-05-03 17:08:48 +03:00
await Task.Delay( 1000 );
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
si.Close();
2019-05-02 22:41:45 +03:00
}
[TestMethod]
2019-05-03 17:08:48 +03:00
public async Task RelayEndtoEnd()
2019-05-02 22:41:45 +03:00
{
2019-05-03 17:08:48 +03:00
var socket = SteamNetworkingSockets.CreateRelaySocket<TestSocketInterface>( 7788 );
var server = socket.RunAsync();
2019-05-02 22:41:45 +03:00
2019-05-02 23:40:39 +03:00
await Task.Delay( 1000 );
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
var connection = SteamNetworkingSockets.ConnectRelay<TestConnectionInterface>( SteamClient.SteamId, 7788 );
var client = connection.RunAsync();
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
await Task.WhenAll( server, client );
}
2019-05-02 22:41:45 +03:00
2019-05-07 19:42:03 +03:00
[TestMethod]
public async Task NormalEndtoEnd()
{
var socket = SteamNetworkingSockets.CreateNormalSocket<TestSocketInterface>( NetAddress.AnyIp( 12445 ) );
var server = socket.RunAsync();
await Task.Delay( 1000 );
var connection = SteamNetworkingSockets.ConnectNormal<TestConnectionInterface>( NetAddress.From( System.Net.IPAddress.Parse( "127.0.0.1" ), 12445 ) );
var client = connection.RunAsync();
await Task.WhenAll( server, client );
}
2019-05-03 17:08:48 +03:00
private class TestConnectionInterface : ConnectionInterface
{
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" );
2019-05-07 19:42:03 +03:00
var sw = System.Diagnostics.Stopwatch.StartNew();
while ( Connecting )
{
2019-05-03 17:08:48 +03:00
await Task.Delay( 10 );
2019-05-07 19:42:03 +03:00
if ( sw.Elapsed.TotalSeconds > 30 )
break;
}
if ( !Connected )
{
Console.WriteLine( "[Connection] Couldn't connect!" );
Console.WriteLine( Connection.DetailedStatus() );
return;
}
2019-05-03 17:08:48 +03:00
Console.WriteLine( "[Connection] Hey We're Connected!" );
2019-05-06 14:33:29 +03:00
2019-05-07 19:42:03 +03:00
sw = System.Diagnostics.Stopwatch.StartNew();
2019-05-03 17:08:48 +03:00
while ( Connected )
{
Receive();
2019-05-06 14:33:29 +03:00
await Task.Delay( 100 );
2019-05-08 13:12:39 +03:00
if ( sw.Elapsed.TotalSeconds > 10 )
2019-05-06 14:33:29 +03:00
{
2019-05-06 15:26:37 +03:00
Assert.Fail( "Client Took Too Long" );
2019-05-06 14:33:29 +03:00
break;
}
2019-05-03 17:08:48 +03:00
}
}
2019-05-06 14:02:36 +03:00
2019-05-06 15:26:37 +03:00
public override unsafe void OnMessage( IntPtr data, int size, long messageNum, long recvTime, int channel )
2019-05-06 14:02:36 +03:00
{
// 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}\"" );
2019-05-06 14:33:29 +03:00
if ( str.Contains( "Hello" ) )
2019-05-06 14:02:36 +03:00
{
Connection.SendMessage( "Hello, How are you!?" );
2019-05-06 14:33:29 +03:00
Connection.SendMessage( "How do you like 20 messages in a row?" );
for ( int i=0; i<20; i++ )
{
Connection.SendMessage( $"BLAMMO!" );
}
}
2019-05-06 15:26:37 +03:00
if ( str.Contains( "status" ))
{
Console.WriteLine( Connection.DetailedStatus() );
}
2019-05-06 14:33:29 +03:00
if ( str.Contains( "how about yourself" ) )
{
Connection.SendMessage( "I'm great, but I have to go now, bye." );
}
if ( str.Contains( "hater" ) )
{
Close();
2019-05-06 14:02:36 +03:00
}
}
2019-05-02 22:41:45 +03:00
}
2019-05-03 17:08:48 +03:00
private class TestSocketInterface : SocketInterface
{
public bool HasFinished = false;
2019-05-06 15:34:41 +03:00
public override void OnConnectionChanged( Connection connection, ConnectionInfo data )
2019-05-03 17:08:48 +03:00
{
Console.WriteLine( $"[Socket{Socket}][{connection}] [{data.State}]" );
2019-05-02 22:41:45 +03:00
2019-05-03 17:08:48 +03:00
base.OnConnectionChanged( connection, data );
}
2019-05-06 15:34:41 +03:00
public override void OnConnecting( Connection connection, ConnectionInfo data )
2019-05-03 17:08:48 +03:00
{
Console.WriteLine( $" - OnConnecting" );
base.OnConnecting( connection, data );
}
/// <summary>
/// Client is connected. They move from connecting to Connections
/// </summary>
2019-05-06 15:34:41 +03:00
public override void OnConnected( Connection connection, ConnectionInfo data )
2019-05-03 17:08:48 +03:00
{
Console.WriteLine( $" - OnConnected" );
base.OnConnected( connection, data );
}
/// <summary>
/// The connection has been closed remotely or disconnected locally. Check data.State for details.
/// </summary>
2019-05-06 15:34:41 +03:00
public override void OnDisconnected( Connection connection, ConnectionInfo data )
2019-05-03 17:08:48 +03:00
{
Console.WriteLine( $" - OnDisconnected" );
base.OnDisconnected( connection, data );
}
internal async Task RunAsync()
{
2019-05-07 19:42:03 +03:00
var sw = System.Diagnostics.Stopwatch.StartNew();
2019-05-03 17:08:48 +03:00
while ( Connected.Count == 0 )
2019-05-07 19:42:03 +03:00
{
2019-05-03 17:08:48 +03:00
await Task.Delay( 10 );
2019-05-07 19:42:03 +03:00
if ( sw.Elapsed.TotalSeconds > 2 )
{
Assert.Fail( "Client Took Too Long To Connect" );
break;
}
}
2019-05-03 17:08:48 +03:00
await Task.Delay( 1000 );
var singleClient = Connected.First();
2019-05-06 14:02:36 +03:00
singleClient.SendMessage( "Hey?" );
2019-05-06 15:26:37 +03:00
await Task.Delay( 100 );
2019-05-06 14:02:36 +03:00
singleClient.SendMessage( "Anyone?" );
await Task.Delay( 100 );
singleClient.SendMessage( "What's this?" );
2019-05-06 15:26:37 +03:00
await Task.Delay( 100 );
singleClient.SendMessage( "What's your status?" );
2019-05-06 14:02:36 +03:00
await Task.Delay( 10 );
singleClient.SendMessage( "Greetings!!??" );
2019-05-06 15:26:37 +03:00
await Task.Delay( 100 );
2019-05-06 14:02:36 +03:00
singleClient.SendMessage( "Hello Client!?" );
2019-05-03 17:08:48 +03:00
2019-05-07 19:42:03 +03:00
sw = System.Diagnostics.Stopwatch.StartNew();
2019-05-06 14:33:29 +03:00
while ( Connected.Contains( singleClient ) )
{
Receive();
await Task.Delay( 100 );
2019-05-03 17:08:48 +03:00
2019-05-08 13:12:39 +03:00
if ( sw.Elapsed.TotalSeconds > 10 )
2019-05-06 14:33:29 +03:00
{
2019-05-08 13:12:39 +03:00
Assert.Fail( "Socket Took Too Long" );
2019-05-06 14:33:29 +03:00
break;
}
}
2019-05-03 17:08:48 +03:00
await Task.Delay( 1000 );
Close();
}
2019-05-06 14:33:29 +03:00
2019-05-06 15:34:41 +03:00
public override unsafe void OnMessage( Connection connection, NetIdentity identity, IntPtr data, int size, long messageNum, long recvTime, int channel )
2019-05-06 14:33:29 +03:00
{
// 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." );
2019-05-08 13:12:39 +03:00
connection.Flush();
2019-05-06 14:33:29 +03:00
connection.Close( true, 10, "Said Bye" );
}
}
2019-05-03 17:08:48 +03:00
}
2019-05-02 22:41:45 +03:00
}
}