예제 #1
0
        /// <summary>
        /// Closes socket after timeout or answer from proper device
        /// </summary>
        /// <param name="state">Socket listening timeout</param>
        protected void CloseConnection(object state)
        {
            int timeout = ((CloseConnectionState)state).Timeout;

            System.Diagnostics.Trace.WriteLine(string.Format("{0} CloseConnection - started, timeout={1}", DateTime.Now.ToLongTimeString(), timeout));
            System.Diagnostics.Trace.Flush();
            bool timeoutOccured = false;

            if ((_listenAddress == null) && (_listenDevice == null))
            {
                //listen all devices
                Thread.Sleep(timeout);
                timeoutOccured = true;
            }
            else
            {
                //listen specified device only
                timeoutOccured = !_stopListenEvent.WaitOne(timeout);
            }
            System.Diagnostics.Trace.WriteLine(string.Format("{0} CloseConnection - awaken {1}", DateTime.Now.ToLongTimeString(), timeoutOccured ? "after timeout" : "by event"));
            System.Diagnostics.Trace.Flush();
            lock (_socketSync)
            {
                if (_socket != null)
                {
                    //close socket
                    _socket.Close();
                    _socket = null;
                }
            }
            if (DiscoveryFinished != null)
            {
                DiscoveryFinished(this, EventArgs.Empty);
            }
        }
예제 #2
0
        /// <summary>
        /// Sends multicast or unicast probe message and begins listening for answer from specified address and device
        /// </summary>
        /// <param name="multicast">if true, multicast message will be sent</param>
        /// <param name="address">Address to listen</param>
        /// <param name="deviceId">Device to listen</param>
        /// <param name="timeout">>Time to listen</param>
        /// <param name="scopes">Scopes to probe</param>
        /// <param name="matchRule">Scope matching rule</param>
        public void Probe(bool multicast, IPAddress address, string deviceId, int timeout, DiscoveryUtils.DiscoveryType[][] types, string[] scopes, string matchRule)
        {
            _socket = new DiscoverySocket(_endPointLocal);
            _socket.MessageReceived += OnMessageReceived <WSD.ProbeMatchesType>;

            System.Diagnostics.Debug.WriteLine(string.Format("New socket [{0}]", _socket.GetHashCode()));

            string messageId = string.Empty;

            try
            {
                if (multicast)
                {
                    JoinDiscoveryMutlicastGroup(_socket);
                }

                IPEndPoint target = multicast
                    ? new IPEndPoint(GetDiscoveryMulticastAddress(), WS_DISCOVER_PORT)
                    : new IPEndPoint(address, WS_DISCOVER_PORT);
                List <byte[]> messages   = new List <byte[]>();
                List <string> messageIds = new List <string>();
                if (types != null)
                {
                    foreach (DiscoveryUtils.DiscoveryType[] typesSet in types)
                    {
                        byte[] message = BuildProbeMessage(typesSet, scopes, matchRule);
                        messageIds.Add(DiscoveryUtils.ExtractMessageId(message));
                        messages.Add(_processMessageMethod != null ? _processMessageMethod(message) : message);
                    }
                }
                else
                {
                    byte[] message = BuildProbeMessage(null, scopes, matchRule);
                    messageIds.Add(DiscoveryUtils.ExtractMessageId(message));
                    messages.Add(_processMessageMethod != null ? _processMessageMethod(message) : message);
                }
                messages.Reverse();

                _socket.Send(target, messages);

                StartListen(timeout, address, deviceId, messageIds.ToArray(), true);

                foreach (byte[] message in messages)
                {
                    if (MessageSent != null)
                    {
                        string dump = Encoding.UTF8.GetString(message);
                        MessageSent(this, new MessageEventArgs()
                        {
                            Message = dump
                        });
                    }
                }
            }
            catch (Exception exc)
            {
                _socket.Close();
                throw;
            }
        }
예제 #3
0
        /// <summary>
        /// Sends multicast or unicast probe message and begins listening for answer from specified address and device
        /// </summary>
        /// <param name="multicast">if true, multicast message will be sent</param>
        /// <param name="address">Address to listen</param>
        /// <param name="deviceId">Device to listen</param>
        /// <param name="timeout">>Time to listen</param>
        /// <param name="scopes">Scopes to probe</param>
        /// <param name="matchRule">Scope matching rule</param>
        public void Probe(bool multicast, IPAddress address, string deviceId, int timeout, string[] scopes, string matchRule)
        {
            _socket = new DiscoverySocket(_endPointLocal);
            _socket.MessageReceived += OnMessageReceived <WSD.ProbeMatchesType>;

            string messageId = string.Empty;

            try
            {
                if (multicast)
                {
                    JoinDiscoveryMutlicastGroup(_socket);
                }
                byte[] message = BuildProbeMessage(scopes, matchRule);
                messageId = DiscoveryUtils.ExtractMessageId(message);

                IPEndPoint target = multicast ? new IPEndPoint(GetDiscoveryMulticastAddress(), WS_DISCOVER_PORT) :
                                    new IPEndPoint(address, WS_DISCOVER_PORT);

                _socket.Send(target, message);

                StartListen(timeout, address, deviceId, messageId);
            }
            catch
            {
                _socket.Close();
                throw;
            }
        }
예제 #4
0
        /// <summary>
        /// Closes socket after timeout or answer from proper device
        /// </summary>
        /// <param name="state">Socket listening timeout</param>
        protected void CloseConnection(object state)
        {
            int timeout = ((CloseConnectionState)state).Timeout;

            System.Diagnostics.Trace.WriteLine(string.Format("{0} CloseConnection - started, timeout={1}", DateTime.Now.ToLongTimeString(), timeout));
            System.Diagnostics.Trace.Flush();
            bool timeoutOccured = false;

            if ((_listenAddress == null) && (_listenDevice == null))
            {
                //listen all devices
                Thread.Sleep(timeout);
                timeoutOccured = true;
            }
            else
            {
                //listen specified device only
                timeoutOccured = !_stopListenEvent.WaitOne(timeout);
            }
            System.Diagnostics.Trace.WriteLine(string.Format("{0} CloseConnection - awaken {1}", DateTime.Now.ToLongTimeString(), timeoutOccured ? "after timeout" : "by event"));
            System.Diagnostics.Trace.Flush();

            lock (_socketSync)
            {
                if (_socket != null)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Close socket [{0}]", _socket.GetHashCode()));

                    //close socket
                    _socket.Close();
                    _socket = null;
                }
            }

            //process received packets
            foreach (DiscoverySocketEventArgs e in _packetsReceived)
            {
                ProcessIncomingPacket <WSD.ProbeMatchesType>(e, () => {});
            }

            if (DiscoveryFinished != null)
            {
                DiscoveryFinished(this, EventArgs.Empty);
            }
        }