Пример #1
0
 private void OnServerDiscovered(MulticastClient sender, EndpointInformation args)
 {
     _ = CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         MessagesListBox.Items.Add($"Discovered {args.Name} at {args.Address}");
     });
 }
Пример #2
0
        private async Task ReceiveFromAsync(MulticastClient client)
        {
            while (IsRunning)
            {
                try
                {
                    var result = await client.Udp.ReceiveAsync();

                    var message = MessageParser.Decode(result.Buffer);

                    if (message.Type == MessageType.Query &&
                        message.Questions.Any(q => q.QType == QType.PTR && q.QName == $"{ServiceType}.{Domain}."))
                    {
                        var response = CreateMessage(message.Id, 120, client.Adapter.Ipv4Address);
                        var packet   = MessageParser.Encode(response);
                        await client.Udp.SendAsync(packet, packet.Length, result.RemoteEndPoint);
                    }
                }
                catch (ObjectDisposedException e)
                {
                    if (e.InnerException is TaskCanceledException)
                    {
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Пример #3
0
        private Task SendGoodbyeMessage(MulticastClient client)
        {
            var goodbye = CreateMessage(0, 0, client.Adapter.Ipv4Address);
            var data    = MessageParser.Encode(goodbye);

            _logger.Trace($"Publishing goodbye\n{goodbye}");
            return(client.Udp.SendAsync(data, data.Length, Zeroconf.BroadcastEndpoint));
        }
Пример #4
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            string localAddress = e.Parameter as string;

            client = new MulticastClient();
            await client.Bind(localAddress, "224.1.0.0", "9000");

            client.ServerDiscovered += OnServerDiscovered;
        }
Пример #5
0
        private async Task PublishAsync(MulticastClient client)
        {
            while (IsRunning)
            {
                try
                {
                    if (DateTimeOffset.UtcNow > _nextPublishTime)
                    {
                        var hello = CreateMessage(0, 120, client.Adapter.Ipv4Address);
                        var bytes = MessageParser.Encode(hello);
                        _logger.Trace($"Publishing hello\n{hello}");
                        await client.Udp.SendAsync(bytes, bytes.Length, Zeroconf.BroadcastEndpoint);

                        _nextPublishTime = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(110);
                    }
                    await Task.Delay(TimeSpan.FromSeconds(9));
                }
                catch (Exception)
                {
                }
            }
        }