コード例 #1
0
        private static async Task OpenSharedChannelAsync()
        {
            await _mutex.WaitAsync().ConfigureAwait(false);

            if (IsJoined || !_shouldJoinChannel)
            {
                return;
            }

            CloseSharedChannel();

            _log.Info("Opening shared MDNS channel...");

            while (!IsJoined && _shouldJoinChannel)
            {
                try
                {
                    _client = new UdpAnySourceMulticastClient(IPAddress.Parse(BonjourUtility.MulticastDNSAddress), BonjourUtility.MulticastDNSPort);
                    _log.Debug("Joining multicast group...");
                    await _client.JoinGroupAsync().ConfigureAwait(false);

                    IsJoined = true;

                    BeginReceiveMessageLoop();
                }
                catch (Exception e)
                {
                    _log.Warning("Caught exception while opening UDP socket");
                    _log.Debug("Exception details: " + e.ToString());
                    CloseSharedChannel();
                }

                if (!IsJoined)
                {
                    await TaskEx.Delay(1000).ConfigureAwait(false);
                }
                else
                {
                    _log.Info("Successfully opened shared MDNS channel.");
                }
            }

            _mutex.Release();
        }