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

164 lines
4.7 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
{
2020-02-22 23:29:37 +03:00
[TestClass]
2019-05-02 22:41:45 +03:00
[DeploymentItem( "steam_api64.dll" )]
2020-02-22 23:29:37 +03:00
[DeploymentItem( "steam_api.dll" )]
public partial class NetworkingSocketsTest
2019-05-02 22:41:45 +03:00
{
void DebugOutput( NetDebugOutput type, string text )
{
Console.WriteLine( $"[NET:{type}]\t\t{text}" );
}
2019-05-02 22:41:45 +03:00
[TestMethod]
public async Task CreateRelayServer()
{
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Everything;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
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()
{
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Everything;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
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
}
2021-11-23 17:46:07 +03:00
[TestMethod]
public async Task CreateRelayServerFakeIP()
{
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Everything;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
var si = SteamNetworkingSockets.CreateRelaySocketFakeIP<TestSocketInterface>();
Console.WriteLine( $"Created Socket: {si}" );
// Give it a second for something to happen
await Task.Delay( 1000 );
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
{
2020-02-22 23:29:37 +03:00
SteamNetworkingUtils.InitRelayNetworkAccess();
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Warning;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
2020-02-22 23:29:37 +03:00
// For some reason giving steam a couple of seconds here
// seems to prevent it returning null connections from ConnectNormal
await Task.Delay( 2000 );
Console.WriteLine( $"----- Creating Socket Relay Socket.." );
var socket = SteamNetworkingSockets.CreateRelaySocket<TestSocketInterface>( 6 );
2019-05-03 17:08:48 +03:00
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
2020-02-22 23:29:37 +03:00
Console.WriteLine( $"----- Connecting To Socket via SteamId ({SteamClient.SteamId})" );
var connection = SteamNetworkingSockets.ConnectRelay<TestConnectionInterface>( SteamClient.SteamId, 6 );
2019-05-03 17:08:48 +03:00
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()
{
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Everything;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
2020-02-22 23:29:37 +03:00
// For some reason giving steam a couple of seconds here
// seems to prevent it returning null connections from ConnectNormal
await Task.Delay( 2000 );
//
// Start the server
//
Console.WriteLine( "CreateNormalSocket" );
2019-05-07 19:42:03 +03:00
var socket = SteamNetworkingSockets.CreateNormalSocket<TestSocketInterface>( NetAddress.AnyIp( 12445 ) );
var server = socket.RunAsync();
2020-02-22 23:29:37 +03:00
//
// Start the client
//
Console.WriteLine( "ConnectNormal" );
2020-02-26 16:49:46 +03:00
var connection = SteamNetworkingSockets.ConnectNormal<TestConnectionInterface>( NetAddress.From( "127.0.0.1", 12445 ) );
2019-05-07 19:42:03 +03:00
var client = connection.RunAsync();
await Task.WhenAll( server, client );
}
2020-02-26 18:28:52 +03:00
2021-11-23 17:46:07 +03:00
[TestMethod]
public async Task RelayEndtoEndFakeIP()
{
SteamNetworkingUtils.InitRelayNetworkAccess();
SteamNetworkingUtils.DebugLevel = NetDebugOutput.Warning;
SteamNetworkingUtils.OnDebugOutput += DebugOutput;
// For some reason giving steam a couple of seconds here
// seems to prevent it returning null connections from ConnectNormal
await Task.Delay( 2000 );
Console.WriteLine( $"----- Creating Socket Relay Socket.." );
var socket = SteamNetworkingSockets.CreateRelaySocketFakeIP<TestSocketInterface>();
var server = socket.RunAsync();
await Task.Delay( 1000 );
Console.WriteLine( $"----- Retrieving Fake IP.." );
SteamNetworkingSockets.GetFakeIP( 0, out NetAddress address );
Console.WriteLine( $"----- Connecting To Socket via Fake IP ({address})" );
var connection = SteamNetworkingSockets.ConnectNormal<TestConnectionInterface>( address );
var client = connection.RunAsync();
await Task.WhenAll( server, client );
}
2020-02-26 18:28:52 +03:00
[TestMethod]
2020-02-28 15:10:53 +03:00
public void NetAddressTest()
2020-02-26 18:28:52 +03:00
{
{
var n = NetAddress.From( "127.0.0.1", 12445 );
Assert.AreEqual( n.ToString(), "127.0.0.1:12445" );
}
{
var n = NetAddress.AnyIp( 5543 );
Assert.AreEqual( n.ToString(), "[::]:5543" );
}
}
2019-05-02 22:41:45 +03:00
}
2021-11-23 17:46:07 +03:00
}