Пример #1
0
        /// <summary>
        /// Pack the given message body into a message array with a message
        /// header prepended.
        /// </summary>
        /// <remarks>
        /// The message body must have a
        /// <seealso cref="SimpleMessageIDAttribute"/> for this method to
        /// succeed.
        /// </remarks>
        /// <typeparam name="T">The type of structure that is passed in as
        /// message body.</typeparam>
        /// <param name="body">The message body.</param>
        /// <returns>An array holding the whole message.</returns>
        public static byte[] Pack <T>(T body)
            where T : struct
        {
            SimpleMessageIDAttribute att = null;

            try {
                att = (from a in body.GetType().GetCustomAttributes(true)
                       where a is SimpleMessageIDAttribute
                       select a as SimpleMessageIDAttribute).Single();
            } catch {
            }

            if (att == null)
            {
                throw new ArgumentException("The message body must have a "
                                            + "SimpleMessageIDAttribute.", "body");
            }

            return(SimpleMessagePacker.Pack(new SimpleMessageHeader(att.ID,
                                                                    (UInt32)Marshal.SizeOf(body.GetType())), body));
        }
Пример #2
0
 /// <summary>
 /// Pack the given message header and body into a single message array.
 /// </summary>
 /// <remarks>
 /// The method will update the body size in the header to match the body
 /// size specified in the parameters. The message ID, however, will
 /// remain unchanged.
 /// </remarks>
 /// <param name="header">The message header.</param>
 /// <param name="body">The message body.</param>
 /// <param name="offset">The offset into <paramref name="body"/> to copy
 /// the body data from.</param>
 /// <returns>An array holding the whole message.</returns>
 public static byte[] Pack(SimpleMessageHeader header, byte[] body,
                           int offset)
 {
     return(SimpleMessagePacker.Pack(header, body, offset,
                                     body.Length - offset));
 }
Пример #3
0
 /// <summary>
 /// Pack the given message header and body into a single message array.
 /// </summary>
 /// <remarks>
 /// The method will update the body size in the header to match the body
 /// size specified in the parameters. The message ID, however, will
 /// remain unchanged.
 /// </remarks>
 /// <param name="header">The message header.</param>
 /// <param name="body">The message body.</param>
 /// <returns>An array holding the whole message.</returns>
 public static byte[] Pack(SimpleMessageHeader header, byte[] body)
 {
     return(SimpleMessagePacker.Pack(header, body, 0));
 }