コード例 #1
0
        public void GetNextServerCandidate_MarkIterateAllBadCandidates()
        {
            serverList.GetAllEndPoints();

            var recordA = ServerRecord.CreateWebSocketServer("10.0.0.1:27030");
            var recordB = ServerRecord.CreateWebSocketServer("10.0.0.2:27030");
            var recordC = ServerRecord.CreateWebSocketServer("10.0.0.3:27030");

            // Add all candidates and mark them bad
            serverList.ReplaceList(new List <ServerRecord>()
            {
                recordA, recordB, recordC
            });
            serverList.TryMark(recordA.EndPoint, ProtocolTypes.WebSocket, ServerQuality.Bad);
            serverList.TryMark(recordB.EndPoint, ProtocolTypes.WebSocket, ServerQuality.Bad);
            serverList.TryMark(recordC.EndPoint, ProtocolTypes.WebSocket, ServerQuality.Bad);

            var candidatesReturned = new HashSet <ServerRecord>();

            void DequeueAndMarkCandidate()
            {
                var candidate = serverList.GetNextServerCandidate(ProtocolTypes.WebSocket);

                Assert.True(candidatesReturned.Add(candidate), $"Candidate {candidate.EndPoint} already seen");
                Thread.Sleep(TimeSpan.FromMilliseconds(10));
                serverList.TryMark(candidate.EndPoint, ProtocolTypes.WebSocket, ServerQuality.Bad);
            }

            // We must dequeue all candidates from a bad list
            DequeueAndMarkCandidate();
            DequeueAndMarkCandidate();
            DequeueAndMarkCandidate();
            Assert.True(candidatesReturned.Count == 3, "All candidates returned");
        }
コード例 #2
0
ファイル: SteamDirectory.cs プロジェクト: umaim/SteamKit
        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());
        }
コード例 #3
0
            internal CMListCallback(CMsgClientCMList cmMsg)
            {
                var cmList = cmMsg.cm_addresses
                             .Zip(cmMsg.cm_ports, (addr, port) => ServerRecord.CreateSocketServer(new IPEndPoint(NetHelpers.GetIPAddress(addr), ( int )port)));

                var websocketList = cmMsg.cm_websocket_addresses.Select((addr) => ServerRecord.CreateWebSocketServer(addr));

                Servers = new ReadOnlyCollection <ServerRecord>(cmList.Concat(websocketList).ToList());
            }
コード例 #4
0
            public ServerRecord GetServerRecord()
            {
                if (IsWebSocket)
                {
                    return(ServerRecord.CreateWebSocketServer(Address));
                }

                ServerRecord.TryCreateSocketServer(Address, out var record);
                return(record);
            }
コード例 #5
0
ファイル: ServerRecordFacts.cs プロジェクト: zjh4473/SteamKit
        public void NullIsNotEqual()
        {
            var s = ServerRecord.CreateWebSocketServer("host:1");

            Assert.True(s != null);
            Assert.True(null != s);
            Assert.False(s.Equals(null));
            Assert.False(s == null);
            Assert.False(null == s);
        }
コード例 #6
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)
        {
            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));
        }
コード例 #7
0
        public void GetNextServerCandidate_OnlyReturnsMatchingServerOfType()
        {
            var record = ServerRecord.CreateWebSocketServer("localhost:443");

            serverList.ReplaceList(new List <ServerRecord>()
            {
                record
            });

            var endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Tcp);

            Assert.Null(endPoint);
            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Udp);
            Assert.Null(endPoint);
            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Tcp | ProtocolTypes.Udp);
            Assert.Null(endPoint);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.WebSocket);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.WebSocket, endPoint.ProtocolTypes);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.All);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.WebSocket, endPoint.ProtocolTypes);

            record = ServerRecord.CreateSocketServer(new IPEndPoint(IPAddress.Loopback, 27015));
            serverList.ReplaceList(new List <ServerRecord>()
            {
                record
            });

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.WebSocket);
            Assert.Null(endPoint);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Tcp);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.Tcp, endPoint.ProtocolTypes);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Udp);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.Udp, endPoint.ProtocolTypes);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.Tcp | ProtocolTypes.Udp);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.Tcp, endPoint.ProtocolTypes);

            endPoint = serverList.GetNextServerCandidate(ProtocolTypes.All);
            Assert.Equal(record.EndPoint, endPoint.EndPoint);
            Assert.Equal(ProtocolTypes.Tcp, endPoint.ProtocolTypes);
        }
コード例 #8
0
ファイル: CMClient.cs プロジェクト: oxters168/SteamKit
        void HandleCMList(IPacketMsg packetMsg)
        {
            var cmMsg = new ClientMsgProtobuf <CMsgClientCMList>(packetMsg);

            DebugLog.Assert(cmMsg.Body.cm_addresses.Count == cmMsg.Body.cm_ports.Count, "CMClient", "HandleCMList received malformed message");

            var cmList = cmMsg.Body.cm_addresses
                         .Zip(cmMsg.Body.cm_ports, (addr, port) => ServerRecord.CreateSocketServer(new IPEndPoint(NetHelpers.GetIPAddress(addr), ( int )port)));

            var webSocketList = cmMsg.Body.cm_websocket_addresses.Select(addr => ServerRecord.CreateWebSocketServer(addr));

            // update our list with steam's list of CMs
            Servers.ReplaceList(cmList.Concat(webSocketList));
        }
コード例 #9
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);
        }