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

91 lines
2.5 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
{
[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
{
2020-02-22 23:29:37 +03:00
SteamNetworkingUtils.InitRelayNetworkAccess();
// 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()
{
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" );
2019-05-07 19:42:03 +03:00
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-02 22:41:45 +03:00
}
2020-02-22 23:29:37 +03:00
}