Restructuring of the files
This commit is contained in:
197
Facepunch.Steamworks/ServerList/Base.cs
Normal file
197
Facepunch.Steamworks/ServerList/Base.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Facepunch.Steamworks.Data;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public abstract class Base : IDisposable
|
||||
{
|
||||
|
||||
#region ISteamMatchmakingServers
|
||||
internal static ISteamMatchmakingServers Internal => SteamMatchmakingServers.Internal;
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Which app we're querying. Defaults to the current app.
|
||||
/// </summary>
|
||||
public AppId AppId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When a new server is added, this function will get called
|
||||
/// </summary>
|
||||
public event Action OnChanges;
|
||||
|
||||
/// <summary>
|
||||
/// Called for every responsive server
|
||||
/// </summary>
|
||||
public event Action<ServerInfo> OnResponsiveServer;
|
||||
|
||||
/// <summary>
|
||||
/// A list of servers that responded. If you're only interested in servers that responded since you
|
||||
/// last updated, then simply clear this list.
|
||||
/// </summary>
|
||||
public List<ServerInfo> Responsive = new List<ServerInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// A list of servers that were in the master list but didn't respond.
|
||||
/// </summary>
|
||||
public List<ServerInfo> Unresponsive = new List<ServerInfo>();
|
||||
|
||||
|
||||
public Base()
|
||||
{
|
||||
AppId = SteamClient.AppId; // Default AppId is this
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query the server list. Task result will be true when finished
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<bool> RunQueryAsync( float timeoutSeconds = 10 )
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
|
||||
Reset();
|
||||
LaunchQuery();
|
||||
|
||||
var thisRequest = request;
|
||||
|
||||
while ( IsRefreshing )
|
||||
{
|
||||
await Task.Delay( 33 );
|
||||
|
||||
//
|
||||
// The request has been cancelled or changed in some way
|
||||
//
|
||||
if ( request.Value == IntPtr.Zero || thisRequest.Value != request.Value )
|
||||
return false;
|
||||
|
||||
if ( !SteamClient.IsValid )
|
||||
return false;
|
||||
|
||||
var r = Responsive.Count;
|
||||
|
||||
UpdatePending();
|
||||
UpdateResponsive();
|
||||
|
||||
if ( r != Responsive.Count )
|
||||
{
|
||||
InvokeChanges();
|
||||
}
|
||||
|
||||
if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds )
|
||||
break;
|
||||
}
|
||||
|
||||
MovePendingToUnresponsive();
|
||||
InvokeChanges();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void Cancel() => Internal.CancelQuery( request );
|
||||
|
||||
// Overrides
|
||||
internal abstract void LaunchQuery();
|
||||
|
||||
internal HServerListRequest request;
|
||||
|
||||
#region Filters
|
||||
|
||||
internal List<MatchMakingKeyValuePair> filters = new List<MatchMakingKeyValuePair>();
|
||||
internal virtual MatchMakingKeyValuePair[] GetFilters() => filters.ToArray();
|
||||
|
||||
public void AddFilter( string key, string value )
|
||||
{
|
||||
filters.Add( new MatchMakingKeyValuePair { Key = key, Value = value } );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
internal int Count => Internal.GetServerCount( request );
|
||||
internal bool IsRefreshing => request.Value != IntPtr.Zero && Internal.IsRefreshing( request );
|
||||
internal List<int> watchList = new List<int>();
|
||||
internal int LastCount = 0;
|
||||
|
||||
void Reset()
|
||||
{
|
||||
ReleaseQuery();
|
||||
LastCount = 0;
|
||||
watchList.Clear();
|
||||
}
|
||||
|
||||
void ReleaseQuery()
|
||||
{
|
||||
if ( request.Value != IntPtr.Zero )
|
||||
{
|
||||
Cancel();
|
||||
Internal.ReleaseRequest( request );
|
||||
request = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ReleaseQuery();
|
||||
}
|
||||
|
||||
internal void InvokeChanges()
|
||||
{
|
||||
OnChanges?.Invoke();
|
||||
}
|
||||
|
||||
void UpdatePending()
|
||||
{
|
||||
var count = Count;
|
||||
if ( count == LastCount ) return;
|
||||
|
||||
for ( int i = LastCount; i < count; i++ )
|
||||
{
|
||||
watchList.Add( i );
|
||||
}
|
||||
|
||||
LastCount = count;
|
||||
}
|
||||
|
||||
public void UpdateResponsive()
|
||||
{
|
||||
watchList.RemoveAll( x =>
|
||||
{
|
||||
var info = Internal.GetServerDetails( request, x );
|
||||
if ( info.HadSuccessfulResponse )
|
||||
{
|
||||
OnServer( ServerInfo.From( info ), info.HadSuccessfulResponse );
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} );
|
||||
}
|
||||
|
||||
void MovePendingToUnresponsive()
|
||||
{
|
||||
watchList.RemoveAll( x =>
|
||||
{
|
||||
var info = Internal.GetServerDetails( request, x );
|
||||
OnServer( ServerInfo.From( info ), info.HadSuccessfulResponse );
|
||||
return true;
|
||||
} );
|
||||
}
|
||||
|
||||
private void OnServer( ServerInfo serverInfo, bool responded )
|
||||
{
|
||||
if ( responded )
|
||||
{
|
||||
Responsive.Add( serverInfo );
|
||||
OnResponsiveServer?.Invoke( serverInfo );
|
||||
return;
|
||||
}
|
||||
|
||||
Unresponsive.Add( serverInfo );
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Facepunch.Steamworks/ServerList/Favourites.cs
Normal file
17
Facepunch.Steamworks/ServerList/Favourites.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public class Favourites : Base
|
||||
{
|
||||
internal override void LaunchQuery()
|
||||
{
|
||||
var filters = GetFilters();
|
||||
request = Internal.RequestFavoritesServerList( AppId.Value, ref filters, (uint)filters.Length, IntPtr.Zero );
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Facepunch.Steamworks/ServerList/Friends.cs
Normal file
17
Facepunch.Steamworks/ServerList/Friends.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public class Friends : Base
|
||||
{
|
||||
internal override void LaunchQuery()
|
||||
{
|
||||
var filters = GetFilters();
|
||||
request = Internal.RequestFriendsServerList( AppId.Value, ref filters, (uint)filters.Length, IntPtr.Zero );
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Facepunch.Steamworks/ServerList/History.cs
Normal file
17
Facepunch.Steamworks/ServerList/History.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public class History : Base
|
||||
{
|
||||
internal override void LaunchQuery()
|
||||
{
|
||||
var filters = GetFilters();
|
||||
request = Internal.RequestHistoryServerList( AppId.Value, ref filters, (uint)filters.Length, IntPtr.Zero );
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Facepunch.Steamworks/ServerList/Internet.cs
Normal file
18
Facepunch.Steamworks/ServerList/Internet.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public class Internet : Base
|
||||
{
|
||||
internal override void LaunchQuery()
|
||||
{
|
||||
var filters = GetFilters();
|
||||
|
||||
request = Internal.RequestInternetServerList( AppId.Value, ref filters, (uint)filters.Length, IntPtr.Zero );
|
||||
}
|
||||
}
|
||||
}
|
||||
72
Facepunch.Steamworks/ServerList/IpList.cs
Normal file
72
Facepunch.Steamworks/ServerList/IpList.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Facepunch.Steamworks.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Facepunch.Steamworks/ServerList/LocalNetwork.cs
Normal file
16
Facepunch.Steamworks/ServerList/LocalNetwork.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Facepunch.Steamworks.ServerList
|
||||
{
|
||||
public class LocalNetwork : Base
|
||||
{
|
||||
internal override void LaunchQuery()
|
||||
{
|
||||
request = Internal.RequestLANServerList( AppId.Value, IntPtr.Zero );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user