Beispiel #1
0
        private async Task AnnounceServiceStopPublishingAsync()
        {
            Message message = GetPublishMessage(PublishMessageType.Stop);

            if (_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);

            await TaskUtility.Delay(FirstRebroadcastInterval).ConfigureAwait(false);

            if (_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);

            await TaskUtility.Delay(SecondRebroadcastInterval).ConfigureAwait(false);

            if (_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);
        }
Beispiel #2
0
        private async Task AnnounceServicePublishAsync()
        {
            if (!_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            var initialMessage = GetPublishMessage(PublishMessageType.Initial);

            _currentServicePublishMessage = GetPublishMessage(PublishMessageType.Normal);

            // Send initial message immediately followed by the "normal" message
            await MulticastDNSChannel.SendMessageAsync(initialMessage).ConfigureAwait(false);

            await SendServicePublishMessageAsync().ConfigureAwait(false);

            await TaskUtility.Delay(FirstRebroadcastInterval).ConfigureAwait(false);

            if (!_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            await SendServicePublishMessageAsync().ConfigureAwait(false);

            await TaskUtility.Delay(SecondRebroadcastInterval).ConfigureAwait(false);

            if (!_publishing || !MulticastDNSChannel.IsJoined)
            {
                return;
            }

            await SendServicePublishMessageAsync().ConfigureAwait(false);
        }
Beispiel #3
0
        public async Task <bool> ResolveAsync()
        {
            if (_browser == null)
            {
                throw new InvalidOperationException("This operation is only valid on services that were generated by a NetServiceBrowser.");
            }

            // Making sure that the associated NetServiceBrowser is still active ensures that the MulticastDNSChannel has not been shut down.
            if (!_browser.IsRunning)
            {
                throw new InvalidOperationException("The associated NetServiceBrowser has been stopped.");
            }

            // Resolve the service
            _log.Info("Resolving service '{0}'...", FullServiceInstanceName);
            Message message = GetResolveMessage();

            var tcs = new TaskCompletionSource <object>();

            _resolveTaskCompletionSource = tcs;

            // First resolution attempt
            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);

            Task delayTask = TaskUtility.Delay(FirstRebroadcastInterval);
            await TaskUtility.WhenAny(delayTask, tcs.Task).ConfigureAwait(false);

            if (tcs.Task.IsCompleted)
            {
                return(true);
            }

            // Second resolution attempt
            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);

            delayTask = TaskUtility.Delay(SecondRebroadcastInterval);
            await TaskUtility.WhenAny(delayTask, tcs.Task).ConfigureAwait(false);

            if (tcs.Task.IsCompleted)
            {
                return(true);
            }

            // Third resolution attempt
            await MulticastDNSChannel.SendMessageAsync(message).ConfigureAwait(false);

            delayTask = TaskUtility.Delay(ServiceResolveTimeout);
            await TaskUtility.WhenAny(delayTask, tcs.Task).ConfigureAwait(false);

            if (tcs.Task.IsCompleted)
            {
                return(true);
            }

            return(false);
        }
Beispiel #4
0
        private async Task SendServicePublishMessageAsync()
        {
            if (!_publishing || _currentServicePublishMessage == null)
            {
                return;
            }

            _log.Debug("Sending publish message for service \"{0}\"...", FullServiceInstanceName);
            await MulticastDNSChannel.SendMessageAsync(_currentServicePublishMessage).ConfigureAwait(false);

            _lastServicePublishBroadcast = DateTime.Now;
        }
        public void Stop()
        {
            IsRunning = false;

            MulticastDNSChannel.RemoveListener(this);
            _discoveredServices.Clear();
            _discoveredIPs.Clear();
            _currentServiceSearchMessage = null;

            _log.Info("Stopped search for service type '{0}'.", _currentServiceType);
            _currentServiceType = null;
        }
        private async Task SendServiceSearchMessageAsync()
        {
            if (!MulticastDNSChannel.IsJoined || _currentServiceSearchMessage == null)
            {
                return;
            }

            _log.Debug("Sending search message for '{0}'...", _currentServiceType);
            await MulticastDNSChannel.SendMessageAsync(_currentServiceSearchMessage).ConfigureAwait(false);

            _lastServiceBroadcast = DateTime.Now;
        }
Beispiel #7
0
        public async Task StopPublishingAsync()
        {
            if (!_publishing)
            {
                return;
            }

            _publishing = false;
            _currentServicePublishMessage = null;

            await AnnounceServiceStopPublishingAsync().ConfigureAwait(false);

            MulticastDNSChannel.RemoveListener(this);
            _log.Info("Stopped publishing service '{0}'.", FullServiceInstanceName);
        }
Beispiel #8
0
        public async void Publish()
        {
            if (_browser != null)
            {
                throw new InvalidOperationException("Cannot publish services that were discovered by NetServiceBrowser.");
            }

            _log.Info("Publishing service '{0}'...", FullServiceInstanceName);

            _publishing = true;
            await MulticastDNSChannel.AddListenerAsync(this).ConfigureAwait(false);

            await AnnounceServicePublishAsync().ConfigureAwait(false);

            StartRunLoop();
        }
        /// <summary>
        /// Searches for services on the local network.
        /// </summary>
        /// <param name="type">The type of the service to search for, e.g., "_touch-able._tcp.local."</param>
        public async void SearchForServices(string type)
        {
            _discoveredServices.Clear();

            _currentServiceType = BonjourUtility.FormatLocalHostname(type);

            _log.Info("Searching for service type '{0}'...", _currentServiceType);

            // Create the DNS message to send
            _currentServiceSearchMessage = new Message();
            _currentServiceSearchMessage.Questions.Add(new Question(_currentServiceType, ResourceRecordType.PTR));

            IsRunning = true;

            // Listen for MDNS messages and notifications
            await MulticastDNSChannel.AddListenerAsync(this).ConfigureAwait(false);

            await SendServiceSearchMessageAsync().ConfigureAwait(false);

            await TaskUtility.Delay(FirstRebroadcastInterval).ConfigureAwait(false);

            if (!IsRunning)
            {
                return;
            }
            await SendServiceSearchMessageAsync().ConfigureAwait(false);

            await TaskUtility.Delay(SecondRebroadcastInterval).ConfigureAwait(false);

            if (!IsRunning)
            {
                return;
            }
            await SendServiceSearchMessageAsync().ConfigureAwait(false);

            StartRunLoop();
        }