Exemple #1
0
        /// <summary>
        /// Build device discovery probe message
        /// </summary>
        /// <param name="scopes">Scopes to probe</param>
        /// <param name="matchRule">Scope matching rule</param>
        /// <returns>Probe message</returns>
        protected byte[] BuildProbeMessage(string[] scopes, string matchRule)
        {
            WSD.ProbeType probe = new WSD.ProbeType();
            //Scope.MatchBy = string.IsNullOrEmpty(matchRule) ? SCOPE_MATCH_BY_URL : matchRule;
            probe.Scopes         = new WSD.ScopesType();
            probe.Scopes.MatchBy = matchRule;
            if (scopes != null)
            {
                string strScopes = string.Empty;
                for (int i = 0; i < scopes.Length; i++)
                {
                    strScopes += scopes[i];
                    if (i < (scopes.Length - 1))
                    {
                        strScopes += " ";
                    }
                }
                probe.Scopes.Text = new string[] { strScopes };
            }
            probe.Types = "dn:" + DiscoveryUtils.ONVIF_DISCOVER_TYPES;
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("dn", DiscoveryUtils.ONVIF_NETWORK_WSDL_URL);
            return(SoapBuilder.BuildMessage(probe, Encoding.UTF8, new DiscoveryHeaderBuilder(), namespaces));
        }
Exemple #2
0
 /// <summary>
 /// Processed incoming UDP packet
 /// </summary>
 /// <typeparam name="T">Type of expected object</typeparam>
 /// <param name="sender">Discovery socket</param>
 /// <param name="e">Event arguments, containing endpoint address and incoming bytes</param>
 protected void OnMessageReceived <T>(object sender, DiscoverySocketEventArgs e)
     where T : class
 {
     if ((_listenAddress == null) || DiscoveryUtils.CompareAddresses(e.Source.Address, _listenAddress))
     {
         try
         {
             //try to parse message according to expected type
             SoapMessage <T> message = SoapBuilder.ParseMessage <T>(e.Message, _discoverySchemas);
             if (IsExpectedMessage <T>(message))
             {
                 EventHandler <DiscoveryMessageEventArgs> handler = GetHandler(message.Object.GetType());
                 DiscoveryMessageEventArgs args = new DiscoveryMessageEventArgs(
                     message.ToSoapMessage <object>(),
                     e.Source.Address);
                 if (handler != null)
                 {
                     handler(this, args);
                 }
                 _stopListenEvent.Set();
             }
         }
         catch (SoapFaultException ex)
         {
             if (ex.Message != null)
             {
                 if (IsExpectedMessage <Fault>(ex.FaultMessage))
                 {
                     if (SoapFaultReceived != null)
                     {
                         SoapFaultReceived(this, new DiscoveryErrorEventArgs(ex, ex.Fault));
                     }
                     _stopListenEvent.Set();
                 }
             }
         }
         catch (UnxpectedElementException ex)
         {
             if (!string.IsNullOrEmpty(_listenMessage) && IsExpectedMessageHeader(ex.Headers))
             {
                 //throw this  exception only is message contains proper RelatedTo, otherwise just ignore message
                 if (ReceiveError != null)
                 {
                     ReceiveError(this, new DiscoveryErrorEventArgs(ex, null));
                 }
                 _stopListenEvent.Set();
                 throw;
             }
         }
         catch (Exception ex)
         {
             if (ReceiveError != null)
             {
                 ReceiveError(this, new DiscoveryErrorEventArgs(ex, null));
             }
             System.Diagnostics.Trace.WriteLine(string.Format("Discovery::OnMessageReceived error [{0}]",
                                                              ex.Message));
             System.Diagnostics.Trace.Flush();
         }
     }
 }