Facepunch.Steamworks/Facepunch.Steamworks.Test/Client/Networking.cs

109 lines
3.6 KiB
C#
Raw Normal View History

2016-07-13 19:14:16 +03:00
using System;
2016-10-05 13:41:53 +03:00
using System.Diagnostics;
2016-07-13 19:14:16 +03:00
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Facepunch.Steamworks.Test
{
[TestClass]
[DeploymentItem( Config.LibraryName + ".dll" )]
[DeploymentItem( "steam_appid.txt" )]
public partial class Networking
2016-07-13 19:14:16 +03:00
{
[TestMethod]
public void PeerToPeerSend()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
var TestString = "This string will be transformed to bytes, sent over the Steam P2P network, then converted back to a string.";
var OutputReceived = false;
var data = Encoding.UTF8.GetBytes( TestString );
client.Networking.OnP2PData = ( steamid, ms, channel ) =>
{
var str = Encoding.UTF8.GetString( ms.GetBuffer() );
Assert.AreEqual( str, TestString );
Assert.AreEqual( steamid, client.SteamId );
OutputReceived = true;
};
2016-10-05 13:41:53 +03:00
client.Networking.OnIncomingConnection = ( steamid ) =>
{
Console.WriteLine( "Incoming P2P Connection: " + steamid );
return true;
};
client.Networking.OnConnectionFailed = ( steamid, error ) =>
{
Console.WriteLine( "Connection Error: " + steamid + " - " + error );
};
2016-07-13 19:14:16 +03:00
client.Networking.SendP2PPacket( client.SteamId, data, data.Length );
while( true )
{
Thread.Sleep( 10 );
client.Update();
if ( OutputReceived )
break;
}
}
}
2016-10-05 13:41:53 +03:00
[TestMethod]
public void PeerToPeerFailure()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
var TestString = "This string will be transformed to bytes, sent over the Steam P2P network, then converted back to a string.";
var TimeoutReceived = false;
var data = Encoding.UTF8.GetBytes( TestString );
client.Networking.OnIncomingConnection = ( steamid ) =>
{
Console.WriteLine( "Incoming P2P Connection: " + steamid );
return true;
};
client.Networking.OnConnectionFailed = ( steamid, error ) =>
{
Console.WriteLine( "Connection Error: " + steamid + " - " + error );
TimeoutReceived = true;
};
ulong rand = (ulong) new Random().Next( 1024 * 16 );
// Send to an invalid, not listening steamid
if ( !client.Networking.SendP2PPacket( client.SteamId + rand, data, data.Length ) )
{
Console.WriteLine( "Couldn't send packet" );
return;
}
var sw = Stopwatch.StartNew();
while ( true )
{
Thread.Sleep( 10 );
client.Update();
//
// Timout is usually around 15 seconds
//
if ( TimeoutReceived )
break;
if ( sw.Elapsed.TotalSeconds > 30 )
{
Assert.Fail( "Didn't time out" );
}
}
}
}
2016-07-13 19:14:16 +03:00
}
}