Added QueryEndReason enum for ServerList queries

This commit is contained in:
SeaFood 2023-09-13 15:53:46 +02:00
parent df075f3f50
commit 9488b5ca6b
2 changed files with 25 additions and 10 deletions

View File

@ -7,6 +7,14 @@ using Steamworks.Data;
namespace Steamworks.ServerList namespace Steamworks.ServerList
{ {
public enum QueryEndReason
{
EndOfRefresh,
TimeOut,
CancelledOrChangedRequest,
InvalidClient,
}
public abstract class Base : IDisposable public abstract class Base : IDisposable
{ {
@ -51,7 +59,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 virtual async Task<bool> RunQueryAsync( float timeoutSeconds = 10 ) public virtual async Task<QueryEndReason> RunQueryAsync( float timeoutSeconds = 10 )
{ {
var stopwatch = System.Diagnostics.Stopwatch.StartNew(); var stopwatch = System.Diagnostics.Stopwatch.StartNew();
@ -60,6 +68,8 @@ namespace Steamworks.ServerList
var thisRequest = request; var thisRequest = request;
QueryEndReason ret = QueryEndReason.EndOfRefresh;
while ( IsRefreshing ) while ( IsRefreshing )
{ {
await Task.Delay( 33 ); await Task.Delay( 33 );
@ -68,10 +78,10 @@ namespace Steamworks.ServerList
// The request has been cancelled or changed in some way // The request has been cancelled or changed in some way
// //
if ( request.Value == IntPtr.Zero || thisRequest.Value != request.Value ) if ( request.Value == IntPtr.Zero || thisRequest.Value != request.Value )
return false; return QueryEndReason.CancelledOrChangedRequest;
if ( !SteamClient.IsValid ) if ( !SteamClient.IsValid )
return false; return QueryEndReason.InvalidClient;
var r = Responsive.Count; var r = Responsive.Count;
@ -84,13 +94,16 @@ namespace Steamworks.ServerList
} }
if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds ) if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds )
{
ret = QueryEndReason.TimeOut;
break; break;
} }
}
MovePendingToUnresponsive(); MovePendingToUnresponsive();
InvokeChanges(); InvokeChanges();
return true; return ret;
} }
public virtual void Cancel() => Internal.CancelQuery( request ); public virtual void Cancel() => Internal.CancelQuery( request );

View File

@ -23,13 +23,15 @@ namespace Steamworks.ServerList
Ips.AddRange( list ); Ips.AddRange( list );
} }
public override async Task<bool> RunQueryAsync( float timeoutSeconds = 10 ) public override async Task<QueryEndReason> RunQueryAsync( float timeoutSeconds = 10 )
{ {
int blockSize = 16; int blockSize = 16;
int pointer = 0; int pointer = 0;
var ips = Ips.ToArray(); var ips = Ips.ToArray();
QueryEndReason ret = QueryEndReason.EndOfRefresh;
while ( true ) while ( true )
{ {
var sublist = ips.Skip( pointer ).Take( blockSize ); var sublist = ips.Skip( pointer ).Take( blockSize );
@ -45,10 +47,10 @@ namespace Steamworks.ServerList
list.AddFilter( "gameaddr", server ); list.AddFilter( "gameaddr", server );
} }
await list.RunQueryAsync( timeoutSeconds ); ret = await list.RunQueryAsync( timeoutSeconds );
if ( wantsCancel ) if ( wantsCancel )
return false; return QueryEndReason.CancelledOrChangedRequest;
Responsive.AddRange( list.Responsive ); Responsive.AddRange( list.Responsive );
Responsive = Responsive.Distinct().ToList(); Responsive = Responsive.Distinct().ToList();
@ -61,7 +63,7 @@ namespace Steamworks.ServerList
InvokeChanges(); InvokeChanges();
} }
return true; return ret;
} }
public override void Cancel() public override void Cancel()