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
{
public enum QueryEndReason
{
EndOfRefresh,
TimeOut,
CancelledOrChangedRequest,
InvalidClient,
}
public abstract class Base : IDisposable
{
@ -51,7 +59,7 @@ namespace Steamworks.ServerList
/// Query the server list. Task result will be true when finished
/// </summary>
/// <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();
@ -60,6 +68,8 @@ namespace Steamworks.ServerList
var thisRequest = request;
QueryEndReason ret = QueryEndReason.EndOfRefresh;
while ( IsRefreshing )
{
await Task.Delay( 33 );
@ -68,10 +78,10 @@ namespace Steamworks.ServerList
// The request has been cancelled or changed in some way
//
if ( request.Value == IntPtr.Zero || thisRequest.Value != request.Value )
return false;
return QueryEndReason.CancelledOrChangedRequest;
if ( !SteamClient.IsValid )
return false;
return QueryEndReason.InvalidClient;
var r = Responsive.Count;
@ -84,13 +94,16 @@ namespace Steamworks.ServerList
}
if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds )
{
ret = QueryEndReason.TimeOut;
break;
}
}
MovePendingToUnresponsive();
InvokeChanges();
return true;
return ret;
}
public virtual void Cancel() => Internal.CancelQuery( request );
@ -194,4 +207,4 @@ namespace Steamworks.ServerList
Unresponsive.Add( serverInfo );
}
}
}
}

View File

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