/// <summary> /// Reads the next available node for reading into the specified struct array /// </summary> /// <typeparam name="T">The structure type to be read</typeparam> /// <param name="buffer">Reference to the buffer</param> /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for reading (default 1000ms)</param> /// <returns>The number of bytes read</returns> /// <remarks>The maximum number of bytes that can be read is the minimum of the length of <paramref name="buffer"/> multiplied by <code>Marshal.SizeOf(typeof(T))</code> and <see cref="NodeBufferSize"/>.</remarks> public virtual int Read <T>(T[] buffer, int timeout = 1000) where T : struct { Node *node = GetNodeForReading(timeout); if (node == null) { return(0); } // Copy the data using the FastStructure class (much faster than the MemoryMappedViewAccessor ReadArray<T> method) int amount = Math.Min(buffer.Length, NodeBufferSize / FastStructure.SizeOf <T>()); base.ReadArray <T>(buffer, node->Offset, 0, amount); // Return the node for further writing ReturnNode(node); return(amount); }
/// <summary> /// Writes the struct array buffer to the next available node for writing /// </summary> /// <param name="buffer">Reference to the buffer to write</param> /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for writing (default 1000ms)</param> /// <returns>The number of bytes written</returns> /// <remarks>The maximum number of bytes that can be written is the minimum of the length of <paramref name="buffer"/> multiplied by <code>Marshal.SizeOf(typeof(T))</code> and <see cref="NodeBufferSize"/>.</remarks> public virtual int Write <T>(T[] buffer, int timeout = 1000) where T : struct { // Grab a node for writing Node *node = GetNodeForWriting(timeout); if (node == null) { return(0); } // Write the data using the FastStructure class (much faster than the MemoryMappedViewAccessor WriteArray<T> method) int amount = Math.Min(buffer.Length, NodeBufferSize / FastStructure.SizeOf <T>()); base.WriteArray <T>(node->Offset, buffer, 0, amount); // Writing is complete, make node readable PostNode(node); return(amount); }