/// <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> /// <param name="length">The number of bytes to copy from /// <paramref name="body"/>.</param> /// <returns>An array holding the whole message.</returns> /// <exception cref="ArgumentException">In case /// <paramref name="offset"/> is negative.</exception> /// <exception cref="ArgumentException">In case /// <paramref name="length"/> is less than one.</exception> /// <exception cref="ArgumentException">In case <paramref name="body"/> /// is not enough to copy <paramref name="length"/> bytes starting at /// offset <paramref name="offset"/>.</exception> public static byte[] Pack(SimpleMessageHeader header, byte[] body, int offset, int length) { if (offset < 0) { throw new ArgumentException("The offset must not be negative.", "offset"); } if (length < 1) { throw new ArgumentException("The body data must not be empty.", "length"); } if (body.Length < offset + length) { throw new ArgumentException("The message body is too short.", "body"); } header.BodySize = (UInt32)length; byte[] retval = new byte[SimpleMessageHeader.Size + header.BodySize]; StructureConverter.StructureToBytes(retval, header.Data); Buffer.BlockCopy(body, offset, retval, SimpleMessageHeader.Size, length); return(retval); }
/// <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> /// <typeparam name="T">The type of structure that is passed in as /// message body.</typeparam> /// <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 <T>(SimpleMessageHeader header, T body) where T : struct { header.BodySize = (UInt32)Marshal.SizeOf(body.GetType()); byte[] retval = new byte[SimpleMessageHeader.Size + header.BodySize]; StructureConverter.StructureToBytes(retval, header.Data); StructureConverter.StructureToBytes(retval, SimpleMessageHeader.Size, body); return(retval); }
/// <summary> /// Update the header data from the given byte array. /// </summary> /// <param name="data">Byte representation of the header data /// structure.</param> public void UpdateData(byte[] data) { this.data = StructureConverter.BytesToStructure < SimpleMessageHeaderData>(data); }
/// <summary> /// Convert a structure to a byte array. /// </summary> /// <typeparam name="T">The type of structure to be converted into a /// byte array.</typeparam> /// <param name="dst">The array that will receive the structure. The /// array must have at least <c>Marshal.SizeOf(obj)</c> elements. /// </param> /// <param name="obj">The structure to be converted.</param> public static void StructureToBytes <T>(byte[] dst, T obj) where T : struct { StructureConverter.StructureToBytes(dst, 0, obj); }
/// <summary> /// Convert a byte array to a structure of the given type. /// </summary> /// <typeparam name="T">The type of structure that is contained in /// <paramref name="bytes"/>.</typeparam> /// <param name="bytes">An array holding the bytes of the /// structure.</param> /// <returns>The structure contained in <paramref name="bytes"/>. /// </returns> public static T BytesToStructure <T>(byte[] bytes) where T : struct { return(StructureConverter.BytesToStructure <T>(bytes, 0)); }