C# Class SharedMemory.SharedBuffer

Inheritance: IDisposable
Mostrar archivo Open project: McManning/Coherence

Protected Properties

Property Type Description
BufferStartPtr byte*
Header SharedHeader*
Mmf MemoryMappedFile
View MemoryMappedViewAccessor
ViewPtr byte*

Public Methods

Method Description
Close ( ) : void

Sets the ShuttingDown flag, and disposes of the MemoryMappedFile and MemoryMappedViewAccessor.
Attempting to read/write to the buffer after closing will result in a System.NullReferenceException.

Dispose ( ) : void

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Protected Methods

Method Description
Dispose ( bool disposeManagedResources ) : void

IDisposable pattern - dispose of managed/unmanaged resources

DoClose ( ) : void

Any classes that inherit from SharedBuffer should implement any Close logic here, Mmf and View are still active at this point. There is no need to call base.DoClose() from these classes.

It is possible for Close to be called before Open has completed successfully, in this situation DoClose should fail gracefully.

DoOpen ( ) : bool

Allows any classes that inherit from SharedBuffer to perform additional open logic. There is no need to call base.DoOpen() from these implementations.

By throwing an exception or returning false, the call to Open will fail and Close will be called.

InitialiseHeader ( ) : void

Initialises the header within the shared memory. Only applicable if IsOwnerOfSharedMemory is true.

Read ( Action readFunc, long bufferPosition ) : void

Prepares an IntPtr to the buffer position and calls readFunc to perform the reading.

Read ( IntPtr destination, int length, long bufferPosition ) : void

Reads length bytes into the memory location destination from the buffer region of the shared memory.

Read ( &data, long bufferPosition ) : void

Reads an instance of T from the buffer

Read ( Array destination, long bufferPosition ) : void

Reads an array of T from the buffer.

ReadArray ( Array destination, int index, int count, long bufferPosition ) : void

Reads a number of elements from a memory location into the provided buffer starting at the specified index.

SharedBuffer ( string name, long bufferSize, bool ownsSharedMemory ) : System

Create a new SharedBuffer instance with the specified name and buffer size

The maximum total shared memory size is dependent upon the system and current memory fragmentation.

The shared memory layout on 32-bit and 64-bit is:
| Header | Buffer |
| 16-bytes | bufferSize |

Write ( Action writeFunc, long bufferPosition ) : void

Prepares an IntPtr to the buffer position and calls writeFunc to perform the writing.

Write ( IntPtr source, int length, long bufferPosition ) : void

Writes length bytes from the source into the shared memory buffer.

Write ( &source, long bufferPosition ) : void

Writes an instance of T into the buffer

Write ( Array source, int index, long bufferPosition ) : void

Writes an array of T into the buffer

Write ( Array source, long bufferPosition ) : void

Writes an array of T into the buffer

WriteArray ( Array source, int index, int count, long bufferPosition ) : void

Writes an array of T into the buffer

Private Methods

Method Description
Open ( ) : bool

Method Details

Close() public method

Sets the ShuttingDown flag, and disposes of the MemoryMappedFile and MemoryMappedViewAccessor.
Attempting to read/write to the buffer after closing will result in a System.NullReferenceException.
public Close ( ) : void
return void

Dispose() public method

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public Dispose ( ) : void
return void

Dispose() protected method

IDisposable pattern - dispose of managed/unmanaged resources
protected Dispose ( bool disposeManagedResources ) : void
disposeManagedResources bool true to dispose of managed resources as well as unmanaged.
return void

DoClose() protected method

Any classes that inherit from SharedBuffer should implement any Close logic here, Mmf and View are still active at this point. There is no need to call base.DoClose() from these classes.
It is possible for Close to be called before Open has completed successfully, in this situation DoClose should fail gracefully.
protected DoClose ( ) : void
return void

DoOpen() protected method

Allows any classes that inherit from SharedBuffer to perform additional open logic. There is no need to call base.DoOpen() from these implementations.
By throwing an exception or returning false, the call to Open will fail and Close will be called.
protected DoOpen ( ) : bool
return bool

InitialiseHeader() protected method

Initialises the header within the shared memory. Only applicable if IsOwnerOfSharedMemory is true.
protected InitialiseHeader ( ) : void
return void

Read() protected method

Prepares an IntPtr to the buffer position and calls readFunc to perform the reading.
protected Read ( Action readFunc, long bufferPosition ) : void
readFunc Action A function used to read from the buffer. The IntPtr parameter is a pointer to the buffer offset by .
bufferPosition long The offset within the buffer region of the shared memory to read from.
return void

Read() protected method

Reads length bytes into the memory location destination from the buffer region of the shared memory.
protected Read ( IntPtr destination, int length, long bufferPosition ) : void
destination IntPtr A managed pointer to the memory location to copy data into from the buffer
length int The number of bytes to be copied
bufferPosition long The offset within the buffer region of the shared memory to read from.
return void

Read() protected method

Reads an instance of T from the buffer
protected Read ( &data, long bufferPosition ) : void
data Output parameter that will contain the value read from the buffer
bufferPosition long The offset within the buffer region of the shared memory to read from.
return void

Read() protected method

Reads an array of T from the buffer.
protected Read ( Array destination, long bufferPosition ) : void
destination Array Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.
bufferPosition long The offset within the buffer region of the shared memory to read from.
return void

ReadArray() protected method

Reads a number of elements from a memory location into the provided buffer starting at the specified index.
protected ReadArray ( Array destination, int index, int count, long bufferPosition ) : void
destination Array The destination buffer.
index int The start index within .
count int The number of elements to read.
bufferPosition long The source offset within the buffer region of the shared memory.
return void

SharedBuffer() protected method

Create a new SharedBuffer instance with the specified name and buffer size

The maximum total shared memory size is dependent upon the system and current memory fragmentation.

The shared memory layout on 32-bit and 64-bit is:
| Header | Buffer |
| 16-bytes | bufferSize |

protected SharedBuffer ( string name, long bufferSize, bool ownsSharedMemory ) : System
name string The name of the shared memory
bufferSize long The buffer size in bytes. The total shared memory size will be Marshal.SizeOf(SharedMemory.SharedHeader) + bufferSize
ownsSharedMemory bool Whether or not the current instance owns the shared memory. If true a new shared memory will be created and initialised otherwise an existing one is opened.
return System

Write() protected method

Prepares an IntPtr to the buffer position and calls writeFunc to perform the writing.
protected Write ( Action writeFunc, long bufferPosition ) : void
writeFunc Action A function used to write to the buffer. The IntPtr parameter is a pointer to the buffer location offset by .
bufferPosition long The offset within the buffer region to start writing to.
return void

Write() protected method

Writes length bytes from the source into the shared memory buffer.
protected Write ( IntPtr source, int length, long bufferPosition ) : void
source IntPtr A managed pointer to the memory location to be copied into the buffer
length int The number of bytes to be copied
bufferPosition long The offset within the buffer region of the shared memory to write to.
return void

Write() protected method

Writes an instance of T into the buffer
protected Write ( &source, long bufferPosition ) : void
source A reference to an instance of to be written into the buffer
bufferPosition long The offset within the buffer region of the shared memory to write to.
return void

Write() protected method

Writes an array of T into the buffer
protected Write ( Array source, int index, long bufferPosition ) : void
source Array An array of to be written. The length of this array controls the number of elements to be written.
index int The index within the array to start writing from.
bufferPosition long The offset within the buffer region of the shared memory to write to.
return void

Write() protected method

Writes an array of T into the buffer
protected Write ( Array source, long bufferPosition ) : void
source Array An array of to be written. The length of this array controls the number of elements to be written.
bufferPosition long The offset within the buffer region of the shared memory to write to.
return void

WriteArray() protected method

Writes an array of T into the buffer
protected WriteArray ( Array source, int index, int count, long bufferPosition ) : void
source Array The source data to be written to the buffer
index int The start index within .
count int The number of elements to write.
bufferPosition long The offset within the buffer region of the shared memory to write to.
return void

Property Details

BufferStartPtr protected_oe property

Pointer to the start of the buffer region of the memory mapped view
protected byte* BufferStartPtr
return byte*

Header protected_oe property

Pointer to the header within shared memory
protected SharedHeader* Header
return SharedHeader*

Mmf protected_oe property

Memory mapped file
protected MemoryMappedFile Mmf
return MemoryMappedFile

View protected_oe property

Memory mapped view
protected MemoryMappedViewAccessor View
return MemoryMappedViewAccessor

ViewPtr protected_oe property

Pointer to the memory mapped view
protected byte* ViewPtr
return byte*