/// <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(DiscoveryUtils.DiscoveryType[] types, 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 }; } XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); int j = 0; string probeTypes = string.Empty; bool first = true; if (types != null) { foreach (DiscoveryUtils.DiscoveryType type in types) { string ns = type.Namespace; string t = type.Type; string prefix; if (string.IsNullOrEmpty(type.Prefix)) { prefix = string.Format("ns{0}", j); j++; } else { prefix = type.Prefix; } namespaces.Add(prefix, ns); if (first) { first = false; } else { probeTypes += " "; } probeTypes += prefix + ":" + t; } } probe.Types = probeTypes; //probe.Types = "dn:" + DiscoveryUtils.ONVIF_DISCOVER_TYPES; //namespaces.Add("dn", DiscoveryUtils.ONVIF_NETWORK_WSDL_URL); return(DiscoverySoapBuilder.BuildMessage(probe, Encoding.UTF8, new DiscoveryHeaderBuilder(), namespaces)); }
/// <summary> /// Parse UDP packet /// </summary> /// <typeparam name="T">Type of expected object</typeparam> /// <param name="e">Event arguments, containing endpoint address and incoming bytes</param> /// <param name="action">Post-processing action</param> protected void ProcessIncomingPacket <T>(DiscoverySocketEventArgs e, Action action) where T : class { if ((_listenAddress == null) || DiscoveryUtils.CompareAddresses(e.Source.Address, _listenAddress)) { try { //try to parse message according to expected type SoapMessage <T> message = DiscoverySoapBuilder.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); } action(); } } catch (SoapFaultException ex) { if (ex.Message != null) { if (IsExpectedMessage <Fault>(ex.FaultMessage)) { if (SoapFaultReceived != null) { SoapFaultReceived(this, new DiscoveryErrorEventArgs(ex, ex.Fault)); } action(); } } } catch (UnxpectedElementException ex) { if ((_listenMessages.Count() > 0) && 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)); } action(); 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(); } } }