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-03 17:08:48 +03:00
|
|
|
|
var si = SteamNetworkingSockets.CreateNormalSocket<TestSocketInterface>( Data.NetworkAddress.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
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 17:08:48 +03:00
|
|
|
|
|
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-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" );
|
|
|
|
|
|
|
|
|
|
while ( !Connected )
|
|
|
|
|
await Task.Delay( 10 );
|
|
|
|
|
|
|
|
|
|
Console.WriteLine( "[Connection] Hey We're Connected!" );
|
|
|
|
|
|
|
|
|
|
while ( Connected )
|
|
|
|
|
{
|
2019-05-03 18:47:29 +03:00
|
|
|
|
Receive();
|
2019-05-03 17:08:48 +03:00
|
|
|
|
await Task.Delay( 10 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 22:41:45 +03:00
|
|
|
|
}
|
2019-05-03 17:08:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class TestSocketInterface : SocketInterface
|
|
|
|
|
{
|
|
|
|
|
public bool HasFinished = false;
|
|
|
|
|
|
|
|
|
|
public override void OnConnectionChanged( NetConnection connection, ConnectionInfo data )
|
|
|
|
|
{
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnConnecting( NetConnection connection, ConnectionInfo data )
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine( $" - OnConnecting" );
|
|
|
|
|
base.OnConnecting( connection, data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Client is connected. They move from connecting to Connections
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void OnConnected( NetConnection connection, ConnectionInfo data )
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine( $" - OnConnected" );
|
|
|
|
|
base.OnConnected( connection, data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The connection has been closed remotely or disconnected locally. Check data.State for details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void OnDisconnected( NetConnection connection, ConnectionInfo data )
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine( $" - OnDisconnected" );
|
|
|
|
|
base.OnDisconnected( connection, data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task RunAsync()
|
|
|
|
|
{
|
|
|
|
|
while ( Connected.Count == 0 )
|
|
|
|
|
await Task.Delay( 10 );
|
|
|
|
|
|
|
|
|
|
await Task.Delay( 1000 );
|
|
|
|
|
|
|
|
|
|
var singleClient = Connected.First();
|
|
|
|
|
|
|
|
|
|
singleClient.SendMessage( "Hello Client" );
|
|
|
|
|
|
|
|
|
|
await Task.Delay( 1000 );
|
|
|
|
|
|
|
|
|
|
singleClient.Close();
|
|
|
|
|
|
|
|
|
|
await Task.Delay( 1000 );
|
|
|
|
|
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 22:41:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|