/// <summary> /// Writes the raw data of the supplied struct to this stream asynchronously. /// </summary> /// <typeparam name="T">The type of the struct.</typeparam> /// <param name="stream">The stream that should be written to.</param> /// <param name="struct">The struct that should be written to the stream.</param> /// <returns>A task that represents the asynchronous write operation.</returns> public static async Task WriteRawDataAsync <T>(this Stream stream, T @struct) where T : struct { int size = Marshal.SizeOf(typeof(T)); byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size); await stream.WriteAsync(data, 0, size); }
T GetStructFromRawData <T>( #if USE_EXTENSIONS this #endif byte[] rawData) where T : struct { return(RawSerializer.GetStructFromRawDataInternal <T>(rawData, 0)); }
byte[] GetRawData <T>( #if USE_EXTENSIONS this #endif T @struct) where T : struct { return(RawSerializer.GetRawDataInternal <T>(@struct, Marshal.SizeOf(typeof(T)))); }
/// <summary> /// Writes the raw data of the supplied struct to this stream asynchronously and monitors cancellation requests. /// </summary> /// <typeparam name="T">The type of the struct.</typeparam> /// <param name="stream">The stream that should be written to.</param> /// <param name="struct">The struct that should be written to the stream.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.</param> /// <returns>A task that represents the asynchronous write operation.</returns> public static async Task WriteRawDataAsync <T>(this Stream stream, T @struct, CancellationToken cancellationToken) where T : struct { cancellationToken.ThrowIfCancellationRequested(); int size = Marshal.SizeOf(typeof(T)); byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size); await stream.WriteAsync(data, 0, size, cancellationToken); }
/// <summary> /// Reads the raw data of a struct of the supplied type from this stream asynchronously, reconstructs the instance and monitors cancellation requests. /// </summary> /// <typeparam name="T">The type of the struct that should be reconstructed.</typeparam> /// <param name="stream">The stream that should be read from.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.</param> /// <returns>A task that represents the asynchronous read operation. The value of the TResult parameter contains the reconstructed struct.</returns> public static async Task <T> ReadStructFromRawDataAsync <T>(this Stream stream, CancellationToken cancellationToken) where T : struct { int size = Marshal.SizeOf(typeof(T)); byte[] rawData = new byte[size]; await stream.ReadAsync(rawData, 0, size, cancellationToken); return(RawSerializer.GetStructFromRawDataInternal <T>(rawData, 0)); }
/// <summary> /// Writes the raw data of the supplied struct to this BinaryWriter. /// </summary> /// <typeparam name="T">The type of the struct.</typeparam> /// <param name="writer">The BinaryWriter that should be written to</param> /// <param name="struct">The struct that should be written to the BinaryWriter.</param> public static void WriteRawData <T>( #if USE_EXTENSIONS this #endif BinaryWriter writer, T @struct) where T : struct { int size = Marshal.SizeOf(typeof(T)); byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size); writer.Write(data, 0, size); }
/// <summary> /// Reads the raw data of a struct of the supplied type from this BinaryReader and reconstructs the instance. /// </summary> /// <typeparam name="T">The type of the struct that should be reconstructed.</typeparam> /// <param name="reader">The BinaryReader that should be read from.</param> /// <returns>An instance of the struct specified by the generic parameter which was reconstructed from the supplied raw data.</returns> public static T ReadStructFromRawData <T>( #if USE_EXTENSIONS this #endif BinaryReader reader) where T : struct { int size = Marshal.SizeOf(typeof(T)); byte[] rawData = new byte[size]; reader.Read(rawData, 0, size); return(RawSerializer.GetStructFromRawDataInternal <T>(rawData, 0)); }