コード例 #1
0
        /// <summary>
        /// Load a list of servers from the Steam Directory.
        /// </summary>
        /// <param name="configuration">Configuration Object</param>
        /// <param name="cancellationToken">Cancellation Token</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> with the Result set to an enumerable list of <see cref="ServerRecord"/>s.</returns>
        public static Task <IReadOnlyCollection <ServerRecord> > LoadAsync(SteamConfiguration configuration, CancellationToken cancellationToken)
        {
            var directory = new WebAPI.AsyncInterface(configuration.WebAPIBaseAddress, "ISteamDirectory", null);
            var args      = new Dictionary <string, string>
            {
                ["cellid"] = configuration.CellID.ToString(CultureInfo.InvariantCulture)
            };

            cancellationToken.ThrowIfCancellationRequested();

            var task = directory.CallAsync(HttpMethod.Get, "GetCMList", version: 1, args: args);

            return(task.ContinueWith(t =>
            {
                var response = task.Result;
                var result = ( EResult )response["result"].AsInteger(( int )EResult.Invalid);
                if (result != EResult.OK)
                {
                    throw new InvalidOperationException(string.Format("Steam Web API returned EResult.{0}", result));
                }

                var socketList = response["serverlist"];
                var websocketList = response["serverlist_websockets"];

                cancellationToken.ThrowIfCancellationRequested();

                var serverRecords = new List <ServerRecord>(capacity: socketList.Children.Count + websocketList.Children.Count);

                foreach (var child in socketList.Children)
                {
                    if (!NetHelpers.TryParseIPEndPoint(child.Value, out var endpoint))
                    {
                        continue;
                    }

                    serverRecords.Add(ServerRecord.CreateSocketServer(endpoint));
                }

                foreach (var child in websocketList.Children)
                {
                    serverRecords.Add(ServerRecord.CreateWebSocketServer(child.Value));
                }

                return (IReadOnlyCollection <ServerRecord>)serverRecords;
            }, cancellationToken, TaskContinuationOptions.NotOnCanceled | TaskContinuationOptions.NotOnFaulted, TaskScheduler.Current));
        }
コード例 #2
0
ファイル: SteamDirectory.cs プロジェクト: ranhuan/SteamKit
        /// <summary>
        /// Load a list of servers from the Steam Directory.
        /// </summary>
        /// <param name="cellid">Cell ID</param>
        /// <param name="cancellationToken">Cancellation Token</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> with the Result set to an enumerable list of <see cref="System.Net.IPEndPoint"/>s.</returns>
        public static Task <IEnumerable <IPEndPoint> > LoadAsync(uint cellid, CancellationToken cancellationToken)
        {
            var directory = new WebAPI.AsyncInterface("ISteamDirectory", null);
            var args      = new Dictionary <string, string>
            {
                { "cellid", cellid.ToString() }
            };

            cancellationToken.ThrowIfCancellationRequested();

            var task = directory.CallAsync(HttpMethod.Get, "GetCMList", version: 1, args: args, secure: true);

            return(task.ContinueWith(t =>
            {
                var response = task.Result;
                var result = ( EResult )response["result"].AsInteger(( int )EResult.Invalid);
                if (result != EResult.OK)
                {
                    throw new InvalidOperationException(string.Format("Steam Web API returned EResult.{0}", result));
                }

                var list = response["serverlist"];

                cancellationToken.ThrowIfCancellationRequested();

                var endPoints = new List <IPEndPoint>(capacity: list.Children.Count);

                foreach (var child in list.Children)
                {
                    IPEndPoint endpoint;
                    if (!NetHelpers.TryParseIPEndPoint(child.Value, out endpoint))
                    {
                        continue;
                    }

                    endPoints.Add(endpoint);
                }

                return endPoints.AsEnumerable();
            }, cancellationToken, TaskContinuationOptions.NotOnCanceled | TaskContinuationOptions.NotOnFaulted, TaskScheduler.Current));
        }
コード例 #3
0
        /// <summary>
        /// Load a list of servers from the Steam Directory.
        /// </summary>
        /// <param name="cellid">Cell ID</param>
        /// <param name="cancellationToken">Cancellation Token</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> with the Result set to an enumerable list of <see cref="System.Net.IPEndPoint"/>s.</returns>
        public static Task<IEnumerable<IPEndPoint>> LoadAsync( uint cellid, CancellationToken cancellationToken )
        {
            var directory = new WebAPI.AsyncInterface( "ISteamDirectory", null );
            var args = new Dictionary<string, string>
            {
                { "cellid", cellid.ToString() }
            };

            cancellationToken.ThrowIfCancellationRequested();

            var task = directory.Call( "GetCMList", version: 1, args: args, secure: true );
            return task.ContinueWith( t =>
            {
                var response = task.Result;
                var result = ( EResult )response[ "result" ].AsInteger( ( int )EResult.Invalid );
                if ( result != EResult.OK )
                {
                    throw new InvalidOperationException( string.Format( "Steam Web API returned EResult.{0}", result ) );
                }

                var list = response[ "serverlist" ];

                cancellationToken.ThrowIfCancellationRequested();

                var endPoints = new List<IPEndPoint>( capacity: list.Children.Count );

                foreach ( var child in list.Children )
                {
                    IPEndPoint endpoint;
                    if ( !NetHelpers.TryParseIPEndPoint( child.Value, out endpoint ) )
                    {
                        continue;
                    }

                    endPoints.Add( endpoint );
                }

                return endPoints.AsEnumerable();
            }, cancellationToken, TaskContinuationOptions.NotOnCanceled | TaskContinuationOptions.NotOnFaulted, TaskScheduler.Current );
        }