当前位置: 首页>>代码示例>>C#>>正文


C# UdpClient.ReceiveWithTimeoutAfter方法代码示例

本文整理汇总了C#中System.Net.Sockets.UdpClient.ReceiveWithTimeoutAfter方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.ReceiveWithTimeoutAfter方法的具体用法?C# UdpClient.ReceiveWithTimeoutAfter怎么用?C# UdpClient.ReceiveWithTimeoutAfter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.UdpClient的用法示例。


在下文中一共展示了UdpClient.ReceiveWithTimeoutAfter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetParsedServers

        public async Task<List<IPEndPoint>> GetParsedServers(CancellationToken cancelToken, int limit = -1,
            IPEndPoint remote = null, int tried = -1) {
            var cur = -1;
            var servers = new List<IPEndPoint>();
            if (remote == null) {
                var hosts = await Dns.GetHostAddressesAsync("hl2master.steampowered.com").ConfigureAwait(false);
                if (tried == -1)
                    cur = new Random().Next(hosts.Length);
                else
                    cur = (tried + 1)%hosts.Length;
                var host = hosts[cur];
                const int port = 27011;

                remote = new IPEndPoint(host, port); //TODO: alternate ip and ports availability
            }
            var e = new IPEndPoint(IPAddress.Any, 0);
            var udpClient = new UdpClient(e) {
                Client = {ReceiveTimeout = Consts.DefaultReceiveTimeout, SendTimeout = Consts.DefaultSendTimeout }
            };

            var i = 0;
            const int maxIterations = 30;
            string lastSeed;
            var seed = "0.0.0.0:0";
            do {
                cancelToken.ThrowIfCancellationRequested();
                lastSeed = seed;
                var msg = BuildMessage(seed);

                if (await udpClient.SendAsync(msg, msg.Length, remote).ConfigureAwait(false) != msg.Length)
                    throw new Exception("Send failed.");
                byte[] response = null;
                var timedOut = false;
                try {
                    response =
                        (await
                                udpClient.ReceiveWithTimeoutAfter(Consts.DefaultReceiveTimeout, cancelToken).ConfigureAwait(false))
                            .Buffer;
                } catch (TimeoutException) {
                    timedOut = true;
                    if ((cur == -1) || (tried != -1) || (i != 0)) {
                        throw new TimeoutException(string.Format("Received timeout on try: {0} packet: {1}",
                            tried == -1 ? 1 : 2, i));
                    }
                }
                //only retries when remote was not passed in, on the first try, and when the first packet was never received
                if (timedOut)
                    return await GetParsedServers(cancelToken, limit, null, cur).ConfigureAwait(false);
                seed = ParseResponse(servers, response, limit);
                if (seed == null)
                    throw new Exception("Bad packet recieved.");
                i++;
            } while ((i < maxIterations) && !seed.Equals("0.0.0.0:0") && !seed.Equals(lastSeed));
            return servers;
        }
开发者ID:SIXNetworks,项目名称:withSIX.Desktop,代码行数:55,代码来源:SourceMasterQuery.cs

示例2: RetrieveAsync

        protected async Task<List<ServerAddress>> RetrieveAsync(int limit, IPEndPoint remote = null, int tried = -1) {
            var cur = -1;
            var servers = new List<ServerAddress>();
            if (remote == null) {
                var hosts = Dns.GetHostAddresses("hl2master.steampowered.com");
                if (tried == -1)
                    cur = new Random().Next(hosts.Length);
                else
                    cur = (tried + 1)%hosts.Length;
                var host = hosts[cur];
                const int port = 27011;
#if DEBUG
                this.Logger().Debug("Contacting master server {0}:{1}", host, port);
#endif
                remote = new IPEndPoint(host, port); //TODO: alternate ip and ports availability
            }
            var e = new IPEndPoint(IPAddress.Any, 0);
            var udpClient = new UdpClient(e) {
                Client = {ReceiveTimeout = DefaultReceiveTimeout, SendTimeout = DefaultSendTimeout}
            };
            udpClient.Connect(remote);

            var i = 0;
            const int maxIterations = 30;
            string lastSeed;
            var seed = "0.0.0.0:0";
            do {
                lastSeed = seed;
                var msg = BuildMessage(seed);

                if (await udpClient.SendAsync(msg, msg.Length).ConfigureAwait(false) != msg.Length)
                    throw new Exception("Send failed.");
                byte[] response = null;
                var timedOut = false;
                try {
                    response =
                        (await udpClient.ReceiveWithTimeoutAfter(DefaultReceiveTimeout).ConfigureAwait(false)).Buffer;
                } catch (TimeoutException) {
                    timedOut = true;
                    if (cur == -1 || tried != -1 || i != 0) {
                        throw new TimeoutException(String.Format("Received timeout on try: {0} packet: {1}",
                            tried == -1 ? 1 : 2, i));
                    }
                }
                //only retries when remote was not passed in, on the first try, and when the first packet was never received
                if (timedOut)
                    return await RetrieveAsync(limit, null, cur);
                seed = ParseResponse(servers, response, limit);
                if (seed == null)
                    throw new Exception("Bad packet recieved.");
                i++;
            } while (i < maxIterations && !seed.Equals("0.0.0.0:0") && !seed.Equals(lastSeed));
#if DEBUG
            this.Logger().Debug("Found {0} servers in {1} packets.", servers.Count, i);
#endif
            return servers;
        }
开发者ID:MaHuJa,项目名称:withSIX.Desktop,代码行数:57,代码来源:SourceMasterQuery.cs


注:本文中的System.Net.Sockets.UdpClient.ReceiveWithTimeoutAfter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。