コード例 #1
0
        private async void LookupServers(CancellationToken token)
        {
            var discovery = new MonoServerDiscovery();

            while (!token.IsCancellationRequested)
            {
                token.ThrowIfCancellationRequested();
                MonoServerInformation server = await discovery.SearchServer(token);

                if (server != null)
                {
                    MonoServerInformation exists = Servers.FirstOrDefault(x => Equals(x.IpAddress, server.IpAddress));
                    if (exists == null)
                    {
                        Servers.Add(server);
                        server.LastMessage = DateTime.Now;
                    }
                    else
                    {
                        exists.LastMessage = DateTime.Now;
                    }
                }

                foreach (
                    MonoServerInformation deadServer in
                    Servers.Where(x => ((DateTime.Now - x.LastMessage).TotalSeconds > 5)).ToList())
                {
                    Servers.Remove(deadServer);
                }
            }
        }
コード例 #2
0
        private async void LookupServers(CancellationToken token)
        {
            var discovery = new MonoServerDiscovery();

            try
            {
                while (!token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                    MonoServerInformation server = await discovery.SearchServer(token);

                    if (server != null)
                    {
                        MonoServerInformation exists = Servers.FirstOrDefault(x => Equals(x.IpAddress, server.IpAddress));
                        if (exists == null)
                        {
                            Servers.Add(server);
                            server.LastMessage = DateTime.Now;
                        }
                        else
                        {
                            exists.LastMessage = DateTime.Now;
                        }
                    }
                    else
                    {
                        await Task.Delay(1000);
                    }

                    foreach (
                        MonoServerInformation deadServer in
                        Servers.Where(x => ((DateTime.Now - x.LastMessage).TotalSeconds > 5)).ToList())
                    {
                        Servers.Remove(deadServer);
                    }
                }
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
                {
                    MessageBox.Show("Port 15000 is in use.");
                }
                else
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }