mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-01-13 15:18:07 +03:00
Updated Socket Tests
This commit is contained in:
parent
9d363de6b8
commit
91083d5d16
@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
@ -15,54 +17,149 @@ namespace Steamworks
|
||||
[TestMethod]
|
||||
public async Task CreateRelayServer()
|
||||
{
|
||||
var socket = SteamNetworkingSockets.CreateRelaySocket();
|
||||
var si = SteamNetworkingSockets.CreateRelaySocket<TestSocketInterface>();
|
||||
|
||||
Console.WriteLine( $"{socket}" );
|
||||
Console.WriteLine( $"Created Socket: {si}" );
|
||||
|
||||
// Give it a second for something to happen
|
||||
await Task.Delay( 5000 );
|
||||
await Task.Delay( 1000 );
|
||||
|
||||
Console.WriteLine( $"{socket}" );
|
||||
|
||||
socket.Close();
|
||||
si.Close();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateNormalServer()
|
||||
{
|
||||
var socket = SteamNetworkingSockets.CreateNormalSocket( Data.NetworkAddress.AnyIp( 21893 ) );
|
||||
var si = SteamNetworkingSockets.CreateNormalSocket<TestSocketInterface>( Data.NetworkAddress.AnyIp( 21893 ) );
|
||||
|
||||
Console.WriteLine( $"{socket}" );
|
||||
|
||||
// Give it a second for something to happen
|
||||
await Task.Delay( 5000 );
|
||||
|
||||
Console.WriteLine( $"{socket}" );
|
||||
|
||||
socket.Close();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ConnectToRelayServer()
|
||||
{
|
||||
var socket = SteamNetworkingSockets.CreateRelaySocket( 7788 );
|
||||
Console.WriteLine( $"Created {socket}" );
|
||||
//await Task.Delay( 1000 );
|
||||
|
||||
var connection = SteamNetworkingSockets.ConnectRelay( SteamClient.SteamId, 7788 );
|
||||
connection.ConnectionName = "Connected To Self";
|
||||
connection.UserData = 69;
|
||||
Console.WriteLine( $"Created Socket: {si}" );
|
||||
|
||||
// Give it a second for something to happen
|
||||
await Task.Delay( 1000 );
|
||||
|
||||
connection.Close();
|
||||
|
||||
Console.WriteLine( $"{socket}" );
|
||||
|
||||
socket.Close();
|
||||
si.Close();
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task RelayEndtoEnd()
|
||||
{
|
||||
var socket = SteamNetworkingSockets.CreateRelaySocket<TestSocketInterface>( 7788 );
|
||||
var server = socket.RunAsync();
|
||||
|
||||
await Task.Delay( 1000 );
|
||||
|
||||
var connection = SteamNetworkingSockets.ConnectRelay<TestConnectionInterface>( SteamClient.SteamId, 7788 );
|
||||
var client = connection.RunAsync();
|
||||
|
||||
await Task.WhenAll( server, client );
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
await Task.Delay( 10 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class TestSocketInterface : SocketInterface
|
||||
{
|
||||
public bool HasFinished = false;
|
||||
|
||||
public override void OnConnectionChanged( NetConnection connection, ConnectionInfo data )
|
||||
{
|
||||
Console.WriteLine( $"[Socket{Socket}][{connection}] [{data.State}]" );
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,10 @@
|
||||
return SteamNetworkingSockets.Internal.CloseListenSocket( this );
|
||||
}
|
||||
|
||||
// GetListenSocketAddress
|
||||
|
||||
public SocketInterface Interface
|
||||
{
|
||||
get => SteamNetworkingSockets.GetSocketInterface( Id );
|
||||
set => SteamNetworkingSockets.SetSocketInterface( Id, value );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user