示例#1
0
        /// <summary>
        ///     Creates a new instance of <see cref="NotifyMessage"/> from notify message.
        /// </summary>
        /// <param name="message">
        ///     The notify message received from a device.
        /// </param>
        /// <returns>
        ///     A new instance of <see cref="NotifyMessage"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        ///     <paramref name="message"/> is not valid notify message.
        /// </exception>
        internal static NotifyMessage Create(string message)
        {
            var notifyMessage = new NotifyMessage();

            var lines = message.SplitIntoLines();

            if (lines.Count() > 1)
            {
                var statusString = lines[0];
                var headers      = ParseHeaders(lines.Skip(1));

                if (statusString.StartsWith("notify", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        notifyMessage.Host                = headers.GetValue <string>("HOST");
                        notifyMessage.NotificationType    = headers.GetValue <string>("NT");
                        notifyMessage.NotificationSubtype = ParseNotifyType(headers.GetValue <string>("NTS"));
                        notifyMessage.USN = headers.GetValue <string>("USN");

                        notifyMessage.Location   = headers.GetValueOrDefault <string>("LOCATION");
                        notifyMessage.MaxAge     = ParseMaxAge(headers.GetValueOrDefault <string>("CACHE-CONTROL"));
                        notifyMessage.Server     = headers.GetValueOrDefault <string>("SERVER");
                        notifyMessage.BootId     = headers.GetValueOrDefault <int>("BOOTID.UPNP.ORG");
                        notifyMessage.NextBootId = headers.GetValueOrDefault <int>("NEXTBOOTID.UPNP.ORG");
                        notifyMessage.ConfigId   = headers.GetValueOrDefault <int>("CONFIGID.UPNP.ORG");
                        notifyMessage.SearchPort = headers.GetValueOrDefault <int>("SEARCHPORT.UPNP.ORG");
                    }
                    catch (KeyNotFoundException ex)
                    {
                        throw new ArgumentException("The given message is not valid notify message", "message", ex);
                    }
                    catch (FormatException ex)
                    {
                        throw new ArgumentException("The given message is not valid notify message", "message", ex);
                    }
                }
            }

            return(notifyMessage);
        }
示例#2
0
		/// <summary>
		///     Creates a new instance of <see cref="NotifyMessage"/> from notify message.
		/// </summary>
		/// <param name="message">
		///     The notify message received from a device.
		/// </param>
		/// <returns>
		///     A new instance of <see cref="NotifyMessage"/>.
		/// </returns>
		/// <exception cref="ArgumentException">
		///     <paramref name="message"/> is not valid notify message.
		/// </exception>
		internal static NotifyMessage Create(string message)
		{
			var notifyMessage = new NotifyMessage();

			var lines = message.SplitIntoLines();
			if (lines.Count() > 1)
			{
				var statusString = lines[0];
				var headers = ParseHeaders(lines.Skip(1));

				if (statusString.StartsWith("notify", StringComparison.OrdinalIgnoreCase))
				{
					try
					{
						notifyMessage.Host 				  = headers.GetValue<string>("HOST");
						notifyMessage.NotificationType 	  = headers.GetValue<string>("NT");
						notifyMessage.NotificationSubtype = ParseNotifyType(headers.GetValue<string>("NTS"));
						notifyMessage.USN 				  = headers.GetValue<string>("USN");

						notifyMessage.Location 	 = headers.GetValueOrDefault<string>("LOCATION");
						notifyMessage.MaxAge 	 = ParseMaxAge(headers.GetValueOrDefault<string>("CACHE-CONTROL"));
						notifyMessage.Server 	 = headers.GetValueOrDefault<string>("SERVER");
						notifyMessage.BootId 	 = headers.GetValueOrDefault<int>("BOOTID.UPNP.ORG");
						notifyMessage.NextBootId = headers.GetValueOrDefault<int>("NEXTBOOTID.UPNP.ORG");
						notifyMessage.ConfigId 	 = headers.GetValueOrDefault<int>("CONFIGID.UPNP.ORG");
						notifyMessage.SearchPort = headers.GetValueOrDefault<int>("SEARCHPORT.UPNP.ORG");
					}
					catch (KeyNotFoundException ex)
					{
						throw new ArgumentException("The given message is not valid notify message", "message", ex);
					}
					catch (FormatException ex)
					{
						throw new ArgumentException("The given message is not valid notify message", "message", ex);
					}
				}
			}

			return notifyMessage;
		}