Пример #1
0
        /// <summary> Returns a list of service infos of the specified type.
        ///
        /// </summary>
        /// <param name="type">Service type name, such as <code>_http._tcp.local.</code>.
        /// </param>
        /// <returns> An array of service instance names.
        /// </returns>
        public async virtual System.Threading.Tasks.Task <ServiceInfo[]> List(string type)
        {
            // Implementation note: The first time a list for a given type is
            // requested, a ServiceCollector is created which collects service
            // infos. This greatly speeds up the performance of subsequent calls
            // to this method. The caveats are, that 1) the first call to this method
            // for a given type is slow, and 2) we spawn a ServiceCollector
            // instance for each service type which increases network traffic a
            // little.

            ServiceCollector collector;

            bool newCollectorCreated;

            lock (serviceCollectors.SyncRoot)
            {
                // TODO: check this
                collector = (ServiceCollector)serviceCollectors[type];
                if (collector == null)
                {
                    collector = new ServiceCollector(type);
                    serviceCollectors[type] = collector;
                    AddServiceListener(type, collector);
                    newCollectorCreated = true;
                }
                else
                {
                    newCollectorCreated = false;
                }
            }

            // After creating a new ServiceCollector, we collect service infos for
            // 200 milliseconds. This should be enough time, to get some service
            // infos from the network.
            if (newCollectorCreated)
            {
                try
                {
                    await System.Threading.Tasks.Task.Delay(new TimeSpan((System.Int64) 10000 * 200));
                }
                catch
                {
                }
            }

            return(collector.list());
        }
Пример #2
0
		/// <summary> Returns a list of service infos of the specified type.
		/// 
		/// </summary>
		/// <param name="type">Service type name, such as <code>_http._tcp.local.</code>.
		/// </param>
		/// <returns> An array of service instance names.
		/// </returns>
		public async virtual System.Threading.Tasks.Task<ServiceInfo[]> List(string type)
		{
			// Implementation note: The first time a list for a given type is
			// requested, a ServiceCollector is created which collects service
			// infos. This greatly speeds up the performance of subsequent calls
			// to this method. The caveats are, that 1) the first call to this method
			// for a given type is slow, and 2) we spawn a ServiceCollector
			// instance for each service type which increases network traffic a
			// little.
			
			ServiceCollector collector;
			
			bool newCollectorCreated;
			lock (serviceCollectors.SyncRoot)
			{
				// TODO: check this
				collector = (ServiceCollector) serviceCollectors[type];
				if (collector == null)
				{
					collector = new ServiceCollector(type);
					serviceCollectors[type] = collector;
					AddServiceListener(type, collector);
					newCollectorCreated = true;
				}
				else
				{
					newCollectorCreated = false;
				}
			}
			
			// After creating a new ServiceCollector, we collect service infos for
			// 200 milliseconds. This should be enough time, to get some service
			// infos from the network.
			if (newCollectorCreated)
			{
				try
				{
                    await System.Threading.Tasks.Task.Delay(new TimeSpan((System.Int64)10000 * 200));
				}
				catch
				{
				}
			}
			
			return collector.list();
		}