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
|
|
|
|
|
{
|
2016-07-18 18:01:52 +03:00
|
|
|
|
[TestClass]
|
2016-10-25 12:29:35 +03:00
|
|
|
|
[DeploymentItem( "steam_api.dll" )]
|
|
|
|
|
[DeploymentItem( "steam_api64.dll" )]
|
2016-07-18 18:01:52 +03:00
|
|
|
|
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 );
|
|
|
|
|
|
2016-11-03 15:09:35 +03:00
|
|
|
|
//
|
|
|
|
|
// Enable listening on this channel
|
|
|
|
|
//
|
|
|
|
|
client.Networking.SetListenChannel( 0, true );
|
|
|
|
|
|
2017-04-05 18:23:13 +03:00
|
|
|
|
client.Networking.OnP2PData = ( steamid, bytes, length, channel ) =>
|
2016-07-13 19:14:16 +03:00
|
|
|
|
{
|
2017-04-05 18:23:13 +03:00
|
|
|
|
var str = Encoding.UTF8.GetString( bytes, 0, length );
|
2016-07-13 19:14:16 +03:00
|
|
|
|
Assert.AreEqual( str, TestString );
|
|
|
|
|
Assert.AreEqual( steamid, client.SteamId );
|
|
|
|
|
OutputReceived = true;
|
2017-04-05 18:23:13 +03:00
|
|
|
|
|
|
|
|
|
Console.WriteLine( "Got: " + str );
|
2016-07-13 19:14:16 +03:00
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|