예제 #1
0
        /// <summary>
        /// Writes the structure array buffer to the next available node for writing.
        /// </summary>
        /// <param name="source">Reference to the buffer to write.</param>
        /// <param name="startIndex">The index within the buffer to start writing from.</param>
        /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for writing. Defaults to 1000 (ms).</param>
        /// <returns>The number of elements written.</returns>
        /// <remarks><![CDATA[
        /// The maximum number of elements that can be written can be calculated by the following formula:
        /// ```C#
        /// Math.Min(source - startIndex, NodeBufferSize) / FastStructure.SizeOf<T>()
        /// ```
        /// ]]></remarks>
        public virtual int Write <T>(T[] source, int startIndex = 0, 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 count = Math.Min(source.Length - startIndex, NodeBufferSize / FastStructure.SizeOf <T>());

            base.WriteArray <T>(source, startIndex, count, node->Offset);
            node->AmountWritten = count * FastStructure.SizeOf <T>();

            // Writing is complete, make node readable
            PostNode(node);

            return(count);
        }
예제 #2
0
        /// <summary>
        /// Reads the next available node for reading into the specified structure array.
        /// </summary>
        /// <typeparam name="T">The structure type to be read.</typeparam>
        /// <param name="destination">Reference to the buffer.</param>
        /// <param name="startIndex">The index within the destination to start writing to.</param>
        /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for reading. Defaults to 1000 (ms).</param>
        /// <returns>The number of elements read into destination.</returns>
        /// <remarks><![CDATA[
        /// The maximum number of elements that can be read can be determined by the following formula:
        ///
        /// ```C#
        /// Math.Min(destination.Length - startIndex, Node.AmountWritten / FastStructure.SizeOf<T>())
        /// ```
        /// ]]></remarks>
        public virtual int Read <T>(T[] destination, int startIndex = 0, 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 count = Math.Min(destination.Length - startIndex, node->AmountWritten / FastStructure.SizeOf <T>());

            base.ReadArray <T>(destination, startIndex, count, node->Offset);

            // Return the node for further writing
            ReturnNode(node);

            return(count);
        }