示例#1
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;
            }
        }
示例#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, 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;
            }
        }