Example #1
0
        public void Start()
        {
            lock (mutex) {
                CheckDisposed();

                if (Started)
                {
                    throw new InvalidOperationException("A browse operation is already running. Cancel it first.");
                }
                else if (String.IsNullOrEmpty(service_type))
                {
                    throw new ArgumentNullException("ServiceType");
                }

                socket = new SsdpSocket(/*false*/);
                socket.BeginSendTo(Protocol.CreateDiscoveryRequest(service_type, MX), OnBrowseRequestFinished);

                // wait for 4 times MX
                if (AutoStop)
                {
                    timout_id = client.Dispatcher.Add(TimeSpan.FromSeconds(MX * 3), OnTimeout);
                }

                if (!client.Started)
                {
                    client.Start(false);
                }
            }
        }
Example #2
0
        public void Stop()
        {
            lock (mutex) {
                CheckDisposed();

                if (!started)
                {
                    return;
                }

                WaitHandle[] handles = new WaitHandle[announcers.Count];
                int          i       = 0;
                foreach (Announcer announcer in announcers.Values)
                {
                    handles[i++] = announcer.StopAsync();
                }

                request_listener.Stop();
                respondSocket.Close();
                respondSocket = null;
                WaitHandle.WaitAll(handles);
                announceSocket.Close();
                announceSocket = null;
                started        = false;
            }
        }
Example #3
0
        public void Start(bool startAnnouncers)
        {
            lock (mutex) {
                CheckDisposed();

                if (started)
                {
                    throw new InvalidOperationException("The Server is already started.");
                }

                started = true;
                request_listener.Start();
                announceSocket = new SsdpSocket();
                announceSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
                respondSocket = new SsdpSocket(false);
                respondSocket.Bind(new IPEndPoint(IPAddress.Any, Protocol.Port));

                if (startAnnouncers)
                {
                    foreach (Announcer announcer in announcers.Values)
                    {
                        if (!announcer.Started)
                        {
                            announcer.Start();
                        }
                    }
                }
            }
        }
Example #4
0
        private void OnBrowseRequestFinished(IAsyncResult asyncResult)
        {
            SsdpSocket socket = (SsdpSocket)asyncResult.AsyncState;

            socket.EndSendTo(asyncResult);

            AsyncReadResult(socket);
        }
Example #5
0
        private void OnByeBye(IAsyncResult asyncResult)
        {
            SsdpSocket socket = (SsdpSocket)asyncResult.AsyncState;

            try {
                socket.EndSendTo(asyncResult);
            } catch {
            }
            started = false;
        }
Example #6
0
        public void Stop()
        {
            lock (mutex) {
                CheckDisposed();

                if (timout_id != 0)
                {
                    client.Dispatcher.Remove(timout_id);
                    timout_id = 0;
                }

                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }
            }
        }
Example #7
0
        private void OnFinished(IAsyncResult asyncResult)
        {
            SsdpSocket socket = (SsdpSocket)asyncResult.AsyncState;

            socket.EndSendTo(asyncResult);
        }
Example #8
0
 internal void AsyncReadResult(SsdpSocket socket)
 {
     AsyncReadResult(new AsyncReceiveBuffer(socket));
 }