Exemple #1
0
        static async Task <IReadOnlyCollection <ServerRecord> > LoadCoreAsync(SteamConfiguration configuration, int?maxNumServers, CancellationToken cancellationToken)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var directory = configuration.GetAsyncWebAPIInterface("ISteamDirectory");
            var args      = new Dictionary <string, object?>
            {
                ["cellid"] = configuration.CellID.ToString(CultureInfo.InvariantCulture)
            };

            if (maxNumServers.HasValue)
            {
                args["maxcount"] = maxNumServers.Value.ToString(CultureInfo.InvariantCulture);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var response = await directory.CallAsync(HttpMethod.Get, "GetCMList", version : 1, args : args).ConfigureAwait(false);

            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 (child.Value is null || !ServerRecord.TryCreateSocketServer(child.Value, out var record))
                {
                    continue;
                }

                serverRecords.Add(record);
            }

            foreach (var child in websocketList.Children)
            {
                if (child.Value is null)
                {
                    continue;
                }

                serverRecords.Add(ServerRecord.CreateWebSocketServer(child.Value));
            }

            return(serverRecords.AsReadOnly());
        }
        /// <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)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var directory = configuration.GetAsyncWebAPIInterface("ISteamDirectory");
            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));
        }
Exemple #3
0
        static async Task <IReadOnlyCollection <CDN.Server> > LoadCoreAsync(SteamConfiguration configuration, int?cellId, int?maxNumServers, CancellationToken cancellationToken)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var directory = configuration.GetAsyncWebAPIInterface("IContentServerDirectoryService");
            var args      = new Dictionary <string, object?>();

            if (cellId.HasValue)
            {
                args["cell_id"] = cellId.Value.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                args["cell_id"] = configuration.CellID.ToString(CultureInfo.InvariantCulture);
            }

            if (maxNumServers.HasValue)
            {
                args["max_servers"] = maxNumServers.Value.ToString(CultureInfo.InvariantCulture);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var response = await directory.CallProtobufAsync <CContentServerDirectory_GetServersForSteamPipe_Response>(
                HttpMethod.Get,
                nameof( IContentServerDirectory.GetServersForSteamPipe ),
                version : 1,
                args : args
                ).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            return(ConvertServerList(response));
        }
        static async Task <IReadOnlyCollection <CDNClient.Server> > LoadCoreAsync(SteamConfiguration configuration, int?cellId, int?maxNumServers, CancellationToken cancellationToken)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var directory = configuration.GetAsyncWebAPIInterface("IContentServerDirectoryService");
            var args      = new Dictionary <string, object>();

            if (cellId.HasValue)
            {
                args["cell_id"] = cellId.Value.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                args["cell_id"] = configuration.CellID.ToString(CultureInfo.InvariantCulture);
            }

            if (maxNumServers.HasValue)
            {
                args["max_servers"] = maxNumServers.Value.ToString(CultureInfo.InvariantCulture);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var response = await directory.CallAsync(HttpMethod.Get, "GetServersForSteamPipe", version : 1, args : args).ConfigureAwait(false);

            var result = ( EResult )response["result"].AsInteger(( int )EResult.OK);

            if (result != EResult.OK || response["servers"] == KeyValue.Invalid)
            {
                throw new InvalidOperationException(string.Format("Steam Web API returned EResult.{0}", result));
            }

            var serverList = response["servers"];

            cancellationToken.ThrowIfCancellationRequested();

            var serverRecords = new List <CDNClient.Server>(capacity: serverList.Children.Count);

            foreach (var child in serverList.Children)
            {
                var httpsSupport = child["https_support"].AsString();
                var protocol     = (httpsSupport == "optional" || httpsSupport == "mandatory") ? CDNClient.Server.ConnectionProtocol.HTTPS : CDNClient.Server.ConnectionProtocol.HTTP;

                serverRecords.Add(new CDNClient.Server
                {
                    Protocol = protocol,
                    Host     = child["host"].AsString(),
                    VHost    = child["vhost"].AsString(),
                    Port     = protocol == CDNClient.Server.ConnectionProtocol.HTTPS ? 443 : 80,

                    Type     = child["type"].AsString(),
                    SourceID = child["source_id"].AsInteger(),
                    CellID   = (uint)child["cell_id"].AsInteger(),

                    Load         = child["load"].AsInteger(),
                    WeightedLoad = child["weighted_load"].AsInteger(),
                    NumEntries   = child["num_entries_in_client_list"].AsInteger(1)
                }
                                  );
            }

            return(serverRecords.AsReadOnly());
        }