예제 #1
0
        /// <summary>
        /// Get available Webservices
        /// </summary>
        ///
        internal async Task <int> GetAvailableWSEndpointsInternal()
        {
            int ret = 0;

            try
            {
                this.Status = -2;
                m_endPoints.Clear();

                IsNotWorking = false;

                using (var socket = new Windows.Networking.Sockets.DatagramSocket())
                {
                    // Set the callback for servers' responses
                    //     await m_MessageReceive.WaitAsync();

                    socket.MessageReceived += SocketOnMessageReceived;
                    // Start listening for servers' responses
                    await socket.BindServiceNameAsync(m_ListenPort.ToString());

                    // Send a search message
                    await SendMessage(socket);

                    bool            bTimeOut = false;
                    ThreadPoolTimer timer    = ThreadPoolTimer.CreateTimer((t) =>
                    {
                        bTimeOut = true;
                        m_signal.Release();
                    }, TimeSpan.FromSeconds(m_timeOut + 2)); // Timeout +2
                    await m_signal.WaitAsync();

                    timer.Cancel();
                    timer = null;

                    if (bTimeOut)
                    {
                        ret = -1;
                        await socket.CancelIOAsync();
                    }
                    else
                    {
                        ret = 1;
                    }
                }
            }
            catch (Exception)
            {
                m_signal.Release();
                ret = -1;
            }
            IsNotWorking = true;
            this.Status  = ret;
            return(ret);
        }
예제 #2
0
        private async void Search(string st, TimeSpan? timeout = null)
        {
            Log("Search - " + st);

            var ssdp_data = new StringBuilder()
                .Append("M-SEARCH * HTTP/1.1").Append("\r\n")
                .Append("HOST: ").Append(MULTICAST_ADDRESS).Append(":").Append(SSDP_PORT.ToString()).Append("\r\n")
                .Append("MAN: ").Append("\"ssdp:discover\"").Append("\r\n")
                .Append("MX: ").Append(MX.ToString()).Append("\r\n")
                .Append("ST: ").Append(st).Append("\r\n")
                .Append("\r\n")
                .ToString();

            var adapters = TargetNetworkAdapters ?? await GetActiveAdaptersAsync().ConfigureAwait(false);

            await Task.WhenAll(adapters.Select(async adapter =>
            {
                using (var socket = new DatagramSocket())
                {
                    socket.Control.DontFragment = true;
                    socket.MessageReceived += OnDatagramSocketMessageReceived;

                    try
                    {
                        await socket.BindServiceNameAsync("", adapter);

                        using (var output = await socket.GetOutputStreamAsync(MULTICAST_HOST, SSDP_PORT.ToString()))
                        {
                            using (var writer = new DataWriter(output))
                            {
                                writer.WriteString(ssdp_data);
                                await writer.StoreAsync();
                            }
                        }

                        await Task.Delay(timeout ?? DEFAULT_TIMEOUT).ConfigureAwait(false);
                        Log("Search Timeout");
                        await socket.CancelIOAsync();
                    }
                    catch (Exception e)
                    {
                        Log("Failed to send multicast: " + e.StackTrace);
                    }

                    socket.MessageReceived -= OnDatagramSocketMessageReceived;
                }
            })).ConfigureAwait(false);

            Finished?.Invoke(this, new EventArgs());
        }