mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-01-27 05:58:07 +03:00
Added Steamworks.ServerList.IpList for querying an array of Ips
This commit is contained in:
parent
b85fe5ffd8
commit
10c099d910
@ -165,5 +165,65 @@ namespace Steamworks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ServerListIps()
|
||||||
|
{
|
||||||
|
var ips = new string[]
|
||||||
|
{
|
||||||
|
"31.186.251.76",
|
||||||
|
"31.186.251.76",
|
||||||
|
"31.186.251.76",
|
||||||
|
"31.186.251.76",
|
||||||
|
"31.186.251.76",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"139.99.144.70",
|
||||||
|
"139.99.144.70",
|
||||||
|
"139.99.144.70",
|
||||||
|
"139.99.144.70",
|
||||||
|
"139.99.144.70",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"74.91.119.142",
|
||||||
|
"95.172.92.176",
|
||||||
|
"95.172.92.176",
|
||||||
|
"95.172.92.176",
|
||||||
|
"95.172.92.176",
|
||||||
|
"95.172.92.176",
|
||||||
|
"164.132.205.154",
|
||||||
|
"164.132.205.154",
|
||||||
|
"164.132.205.154",
|
||||||
|
"164.132.205.154",
|
||||||
|
"164.132.205.154",
|
||||||
|
};
|
||||||
|
|
||||||
|
using ( var list = new ServerList.IpList( ips ) )
|
||||||
|
{
|
||||||
|
var success = await list.RunQueryAsync();
|
||||||
|
|
||||||
|
Console.WriteLine( $"success {success}" );
|
||||||
|
Console.WriteLine( $"Found {list.Responsive.Count} Responsive Servers" );
|
||||||
|
Console.WriteLine( $"Found {list.Unresponsive.Count} Unresponsive Servers" );
|
||||||
|
|
||||||
|
Assert.AreNotEqual( list.Responsive.Count, 0 );
|
||||||
|
|
||||||
|
foreach ( var server in list.Responsive )
|
||||||
|
{
|
||||||
|
Console.WriteLine( $"[{server.Address}:{server.ConnectionPort}] - {server.Name}" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ namespace Steamworks.ServerList
|
|||||||
/// Query the server list. Task result will be true when finished
|
/// Query the server list. Task result will be true when finished
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<bool> RunQueryAsync( float timeoutSeconds = 10 )
|
public virtual async Task<bool> RunQueryAsync( float timeoutSeconds = 10 )
|
||||||
{
|
{
|
||||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ namespace Steamworks.ServerList
|
|||||||
|
|
||||||
if ( r != Responsive.Count )
|
if ( r != Responsive.Count )
|
||||||
{
|
{
|
||||||
OnChanges?.Invoke();
|
InvokeChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds )
|
if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds )
|
||||||
@ -100,12 +100,12 @@ namespace Steamworks.ServerList
|
|||||||
}
|
}
|
||||||
|
|
||||||
MovePendingToUnresponsive();
|
MovePendingToUnresponsive();
|
||||||
OnChanges?.Invoke();
|
InvokeChanges();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Cancel() => Internal.CancelQuery( request );
|
public virtual void Cancel() => Internal.CancelQuery( request );
|
||||||
|
|
||||||
// Overrides
|
// Overrides
|
||||||
internal abstract void LaunchQuery();
|
internal abstract void LaunchQuery();
|
||||||
@ -115,7 +115,7 @@ namespace Steamworks.ServerList
|
|||||||
#region Filters
|
#region Filters
|
||||||
|
|
||||||
internal List<MatchMakingKeyValuePair_t> filters = new List<MatchMakingKeyValuePair_t>();
|
internal List<MatchMakingKeyValuePair_t> filters = new List<MatchMakingKeyValuePair_t>();
|
||||||
internal MatchMakingKeyValuePair_t[] GetFilters() => filters.ToArray();
|
internal virtual MatchMakingKeyValuePair_t[] GetFilters() => filters.ToArray();
|
||||||
|
|
||||||
public void AddFilter( string key, string value )
|
public void AddFilter( string key, string value )
|
||||||
{
|
{
|
||||||
@ -151,6 +151,11 @@ namespace Steamworks.ServerList
|
|||||||
ReleaseQuery();
|
ReleaseQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void InvokeChanges()
|
||||||
|
{
|
||||||
|
OnChanges?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
void UpdatePending()
|
void UpdatePending()
|
||||||
{
|
{
|
||||||
var count = Count;
|
var count = Count;
|
||||||
|
72
Facepunch.Steamworks/ServerList/IpList.cs
Normal file
72
Facepunch.Steamworks/ServerList/IpList.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using Steamworks.Data;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Steamworks.ServerList
|
||||||
|
{
|
||||||
|
public class IpList : Internet
|
||||||
|
{
|
||||||
|
public List<string> Ips = new List<string>();
|
||||||
|
bool wantsCancel;
|
||||||
|
|
||||||
|
public IpList( IEnumerable<string> list )
|
||||||
|
{
|
||||||
|
Ips.AddRange( list );
|
||||||
|
}
|
||||||
|
|
||||||
|
public IpList( params string[] list )
|
||||||
|
{
|
||||||
|
Ips.AddRange( list );
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<bool> RunQueryAsync( float timeoutSeconds = 10 )
|
||||||
|
{
|
||||||
|
int blockSize = 16;
|
||||||
|
int pointer = 0;
|
||||||
|
|
||||||
|
var ips = Ips.ToArray();
|
||||||
|
|
||||||
|
while ( true )
|
||||||
|
{
|
||||||
|
var sublist = ips.Skip( pointer ).Take( blockSize );
|
||||||
|
if ( sublist.Count() == 0 )
|
||||||
|
break;
|
||||||
|
|
||||||
|
using ( var list = new ServerList.Internet() )
|
||||||
|
{
|
||||||
|
list.AddFilter( "or", sublist.Count().ToString() );
|
||||||
|
|
||||||
|
foreach ( var server in sublist )
|
||||||
|
{
|
||||||
|
list.AddFilter( "gameaddr", server );
|
||||||
|
}
|
||||||
|
|
||||||
|
await list.RunQueryAsync( timeoutSeconds );
|
||||||
|
|
||||||
|
if ( wantsCancel )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Responsive.AddRange( list.Responsive );
|
||||||
|
Responsive = Responsive.Distinct().ToList();
|
||||||
|
Unresponsive.AddRange( list.Unresponsive );
|
||||||
|
Unresponsive = Unresponsive.Distinct().ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer += sublist.Count();
|
||||||
|
|
||||||
|
InvokeChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Cancel()
|
||||||
|
{
|
||||||
|
wantsCancel = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Steamworks.Data
|
namespace Steamworks.Data
|
||||||
{
|
{
|
||||||
public struct ServerInfo
|
public struct ServerInfo : IEquatable<ServerInfo>
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public int Ping { get; set; }
|
public int Ping { get; set; }
|
||||||
@ -118,5 +118,15 @@ namespace Steamworks.Data
|
|||||||
{
|
{
|
||||||
//Client.native.matchmaking.RemoveFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite );
|
//Client.native.matchmaking.RemoveFavoriteGame( AppId, Utility.IpToInt32( Address ), (ushort)ConnectionPort, (ushort)QueryPort, k_unFavoriteFlagFavorite );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Equals( ServerInfo other )
|
||||||
|
{
|
||||||
|
return this.GetHashCode() == other.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Address.GetHashCode() + SteamId.GetHashCode() + ConnectionPort.GetHashCode() + QueryPort.GetHashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user