Ejemplo n.º 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());
        }
Ejemplo n.º 2
0
            public ServerRecord GetServerRecord()
            {
                if (IsWebSocket)
                {
                    return(ServerRecord.CreateWebSocketServer(Address));
                }

                ServerRecord.TryCreateSocketServer(Address, out var record);
                return(record);
            }
Ejemplo n.º 3
0
        public void CanTryCreateSocketServer()
        {
            Assert.True(ServerRecord.TryCreateSocketServer("127.0.0.1:1234", out var record));
            Assert.NotNull(record);
            Assert.Equal(new IPEndPoint(IPAddress.Loopback, 1234), record.EndPoint);
            Assert.Equal(ProtocolTypes.Tcp | ProtocolTypes.Udp, record.ProtocolTypes);

            Assert.True(ServerRecord.TryCreateSocketServer("192.168.0.1:5678", out record));
            Assert.NotNull(record);
            Assert.Equal(new IPEndPoint(IPAddress.Parse("192.168.0.1"), 5678), record.EndPoint);
            Assert.Equal(ProtocolTypes.Tcp | ProtocolTypes.Udp, record.ProtocolTypes);
        }
Ejemplo n.º 4
0
        private static async Task <List <ServerRecord> > LoadChinaCMList(SteamConfiguration configuration)
        {
            var directory = configuration.GetAsyncWebAPIInterface("ISteamDirectory");
            var args      = new Dictionary <string, object>
            {
                ["cellid"]     = "47", // Shanghai
                ["maxcount"]   = int.MaxValue.ToString(),
                ["steamrealm"] = "steamchina",
            };

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

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

            if (result != EResult.OK)
            {
                throw new InvalidOperationException($"Steam Web API returned EResult.{result}");
            }

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

            var serverRecords = new List <ServerRecord>(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);
        }
Ejemplo n.º 5
0
        public void CannotTryCreateSocketServer()
        {
            Assert.False(ServerRecord.TryCreateSocketServer("127.0.0.1", out var record));
            Assert.Null(record);

            Assert.False(ServerRecord.TryCreateSocketServer("127.0.0.1:123456789", out record));
            Assert.Null(record);

            Assert.False(ServerRecord.TryCreateSocketServer("127.0.0.1:-1234", out record));
            Assert.Null(record);

            Assert.False(ServerRecord.TryCreateSocketServer("127.0.0.1:notanint", out record));
            Assert.Null(record);

            Assert.False(ServerRecord.TryCreateSocketServer("volvopls.valvesoftware.com:1234", out record));
            Assert.Null(record);
        }