Skip to content

Commit 6540833

Browse files
Improved usage consistency by changing Buffer.Open to protected
1 parent ef2ac91 commit 6540833

File tree

16 files changed

+346
-151
lines changed

16 files changed

+346
-151
lines changed

Examples/ClientTest/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ static void Main(string[] args)
4444
Console.WriteLine("Open existing shared memory circular buffer");
4545
using (SharedMemory.CircularBuffer theClient = new SharedMemory.CircularBuffer("TEST"))
4646
{
47-
theClient.Open();
48-
4947
Console.WriteLine("Buffer {0} opened, NodeBufferSize: {1}, NodeCount: {2}", theClient.Name, theClient.NodeBufferSize, theClient.NodeCount);
5048

5149
long bufferSize = theClient.NodeBufferSize;

Examples/ServerTest/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ static void Main(string[] args)
6060

6161

6262
Console.WriteLine("Create shared memory circular buffer");
63-
using (var theServer = new SharedMemory.CircularBuffer("TEST", count, size, true))
63+
using (var theServer = new SharedMemory.CircularBuffer("TEST", count, size))
6464
{
65-
theServer.Open();
66-
6765
Console.WriteLine("Circular buffer producer created.");
6866
Console.WriteLine("Ready for client...");
6967
Thread.Sleep(1000);

Examples/SingleProcess/Program.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ static void Main(string[] args)
5050

5151
long bytesRead = 0;
5252

53-
var server = new SharedMemory.CircularBuffer("TEST", count, size, true);
54-
if (!server.Open())
55-
{
56-
Console.WriteLine("Unable to allocate memory.");
57-
Console.ReadLine();
58-
return;
59-
}
53+
var server = new SharedMemory.CircularBuffer("TEST", count, size);
6054

6155
Stopwatch sw = Stopwatch.StartNew();
6256

@@ -65,7 +59,6 @@ static void Main(string[] args)
6559
byte[] testData = new byte[size];
6660

6761
var client = new SharedMemory.CircularBuffer("TEST");
68-
client.Open();
6962

7063
Stopwatch clientTime = new Stopwatch();
7164
clientTime.Start();

SharedMemory/Array.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Array(string name, int length)
8787
/// Opens an existing shared memory array with the name as specified by <paramref name="name"/>.
8888
/// </summary>
8989
/// <param name="name">The name of the shared memory array to open.</param>
90-
/// <exception cref="ArgumentOutOfRangeException">If the shared memory location specified by <paramref name="name"/> does not have a <see cref="BufferSize"/> that is evenly divisable by the size of <typeparamref name="T"/>.</exception>
90+
/// <exception cref="ArgumentOutOfRangeException">If the shared memory location specified by <paramref name="name"/> does not have a <see cref="Buffer.BufferSize"/> that is evenly divisable by the size of <typeparamref name="T"/>.</exception>
9191
public Array(string name)
9292
: base(name, 0, false)
9393
{
@@ -98,9 +98,12 @@ public Array(string name)
9898

9999
#endregion
100100

101+
/// <summary>
102+
/// Perform any initialisation required when opening the shared memory array
103+
/// </summary>
104+
/// <returns>true if successful</returns>
101105
protected override bool DoOpen()
102106
{
103-
104107
if (!IsOwnerOfSharedMemory)
105108
{
106109
if (BufferSize % _elementSize != 0)
@@ -131,7 +134,7 @@ public void Write(ref T data, int index)
131134
/// </summary>
132135
/// <param name="buffer">The source array to copy elements from.</param>
133136
/// <param name="startIndex">The zero-based index of the shared memory array element to begin writing to.</param>
134-
/// <exception cref="IndexOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- <see cref="buffer.Length"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
137+
/// <exception cref="IndexOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- length of <paramref name="buffer"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
135138
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> must not be null</exception>
136139
public void Write(T[] buffer, int startIndex = 0)
137140
{
@@ -167,7 +170,7 @@ public void Read(out T data, int index)
167170
/// </summary>
168171
/// <param name="buffer">The destination array to copy the elements into.</param>
169172
/// <param name="startIndex">The zero-based index of the shared memory array element to begin reading from.</param>
170-
/// <exception cref="IndexOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- <see cref="buffer.Length"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
173+
/// <exception cref="IndexOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- length of <paramref name="buffer"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
171174
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> must not be null</exception>
172175
public void CopyTo(T[] buffer, int startIndex = 0)
173176
{

SharedMemory/Buffer.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,21 @@ protected virtual long BufferOffset
112112

113113
#region Protected field members
114114

115+
/// <summary>
116+
/// Memory mapped file
117+
/// </summary>
115118
protected MemoryMappedFile Mmf;
119+
/// <summary>
120+
/// Memory mapped view
121+
/// </summary>
116122
protected MemoryMappedViewAccessor View;
123+
/// <summary>
124+
/// Pointer to the memory mapped view
125+
/// </summary>
117126
protected byte* ViewPtr = null;
127+
/// <summary>
128+
/// Pointer to the header within shared memory
129+
/// </summary>
118130
protected Header* Header = null;
119131

120132
#endregion
@@ -158,6 +170,9 @@ protected Buffer(string name, long bufferSize, bool ownsSharedMemory)
158170
}
159171
}
160172

173+
/// <summary>
174+
/// Destructor - for Dispose(false)
175+
/// </summary>
161176
~Buffer()
162177
{
163178
Dispose(false);
@@ -168,15 +183,17 @@ protected Buffer(string name, long bufferSize, bool ownsSharedMemory)
168183
#region Open / Close
169184

170185
/// <summary>
171-
/// Create a new or open an existing shared memory buffer with the name of <see cref="Name"/>.
186+
/// Creates a new or opens an existing shared memory buffer with the name of <see cref="Name"/> depending on the value of <see cref="IsOwnerOfSharedMemory"/>.
172187
/// </summary>
173188
/// <returns>True if the memory was successfully mapped</returns>
174189
/// <remarks>If <see cref="IsOwnerOfSharedMemory"/> is true then the shared memory buffer will be created, opening will fail in this case if the shared memory already exists. Otherwise if IsOwnerOfSharedMemory is false then the shared memory buffer will be opened, which will fail if it doesn't already exist.</remarks>
175190
/// <exception cref="System.IO.IOException">If trying to create a new shared memory buffer with a duplicate name as buffer owner.</exception>
176191
/// <exception cref="System.IO.FileNotFoundException">If trying to open a new shared memory buffer that does not exist as a consumer of existing buffer.</exception>
177192
/// <exception cref="System.ArgumentOutOfRangeException">If trying to create a new shared memory buffer with a size larger than the logical addressable space.</exception>
178-
public bool Open()
193+
protected bool Open()
179194
{
195+
Close();
196+
180197
try
181198
{
182199
// Attempts to create or open the shared memory with a name of this.Name
@@ -325,7 +342,7 @@ protected virtual void Write<T>(ref T data, long bufferPosition = 0)
325342
/// Writes an array of <typeparamref name="T"/> into the buffer
326343
/// </summary>
327344
/// <typeparam name="T">A structure type</typeparam>
328-
/// <param name="data">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
345+
/// <param name="buffer">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
329346
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to write to.</param>
330347
protected virtual void Write<T>(T[] buffer, long bufferPosition = 0)
331348
where T : struct
@@ -374,7 +391,7 @@ protected virtual void Read<T>(out T data, long bufferPosition = 0)
374391
/// Reads an array of <typeparamref name="T"/> from the buffer
375392
/// </summary>
376393
/// <typeparam name="T">A structure type</typeparam>
377-
/// <param name="data">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
394+
/// <param name="buffer">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
378395
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to read from.</param>
379396
protected virtual void Read<T>(T[] buffer, long bufferPosition = 0)
380397
where T : struct
@@ -407,11 +424,18 @@ protected virtual void Read(Action<IntPtr> readFunc, long bufferPosition = 0)
407424

408425
#region IDisposable
409426

427+
/// <summary>
428+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
429+
/// </summary>
410430
public void Dispose()
411431
{
412432
Dispose(true);
413433
}
414434

435+
/// <summary>
436+
/// IDisposable pattern - dispose of managed/unmanaged resources
437+
/// </summary>
438+
/// <param name="disposeManagedResources">true to dispose of managed resources as well as unmanaged.</param>
415439
protected virtual void Dispose(bool disposeManagedResources)
416440
{
417441
if (disposeManagedResources)
@@ -425,7 +449,7 @@ protected virtual void Dispose(bool disposeManagedResources)
425449
#region Native Imports
426450

427451
/// <summary>
428-
/// Allow copying memory from one IntPtr to another. Required as the <see cref="Marshal.Copy"/> implementation does not provide an appropriate override.
452+
/// Allow copying memory from one IntPtr to another. Required as the <see cref="System.Runtime.InteropServices.Marshal.Copy(System.IntPtr, System.IntPtr[], int, int)"/> implementation does not provide an appropriate override.
429453
/// </summary>
430454
/// <param name="dest"></param>
431455
/// <param name="src"></param>

SharedMemory/BufferReadWrite.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,25 @@ public unsafe class BufferReadWrite : BufferWithLocks
3939
{
4040
#region Constructors
4141

42-
public BufferReadWrite(string name, int bufferSize, bool ownsSharedMemory)
43-
: base(name, bufferSize, ownsSharedMemory)
42+
/// <summary>
43+
/// Creates a new shared memory buffer with the specified name and size
44+
/// </summary>
45+
/// <param name="name">The name of the shared memory to create</param>
46+
/// <param name="bufferSize">The size of the buffer</param>
47+
public BufferReadWrite(string name, int bufferSize)
48+
: base(name, bufferSize, true)
4449
{
50+
Open();
4551
}
4652

53+
/// <summary>
54+
/// Opens an existing shared memory buffer with the specified name
55+
/// </summary>
56+
/// <param name="name">The name of the shared memory to open</param>
4757
public BufferReadWrite(string name)
4858
: base(name, 0, false)
4959
{
60+
Open();
5061
}
5162

5263
#endregion
@@ -69,7 +80,7 @@ public BufferReadWrite(string name)
6980
/// Writes an array of <typeparamref name="T"/> into the buffer
7081
/// </summary>
7182
/// <typeparam name="T">A structure type</typeparam>
72-
/// <param name="data">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
83+
/// <param name="buffer">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
7384
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to write to.</param>
7485
new public void Write<T>(T[] buffer, long bufferPosition = 0)
7586
where T : struct
@@ -118,7 +129,7 @@ public BufferReadWrite(string name)
118129
/// Reads an array of <typeparamref name="T"/> from the buffer
119130
/// </summary>
120131
/// <typeparam name="T">A structure type</typeparam>
121-
/// <param name="data">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
132+
/// <param name="buffer">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
122133
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to read from.</param>
123134
new public void Read<T>(T[] buffer, long bufferPosition = 0)
124135
where T : struct

SharedMemory/BufferWithLocks.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace SharedMemory
3636
/// <summary>
3737
/// <para>Extends <see cref="Buffer"/> to support simple thread-synchronisation for read/write
3838
/// to the buffer by allowing callers to acquire and release read/write locks.</para>
39-
/// <para>All buffer read/write operations have been overloaded to first perform a <see cref="EventWaitHandle.WaitOne"/>
39+
/// <para>All buffer read/write operations have been overloaded to first perform a <see cref="System.Threading.WaitHandle.WaitOne()"/>
4040
/// using the <see cref="ReadWaitEvent"/> and <see cref="WriteWaitEvent"/> respectively.</para>
4141
/// <para>By default all read/write operations will not block, it is necessary to first acquire locks
4242
/// through calls to <see cref="AcquireReadLock"/> and <see cref="AcquireWriteLock"/> as appropriate, with corresponding
@@ -74,7 +74,7 @@ protected BufferWithLocks(string name, long bufferSize, bool ownsSharedMemory)
7474
#region Synchronisation
7575

7676
/// <summary>
77-
/// Blocks the current thread until it is able to acquire a read lock. If succesfull all subsequent writes will be blocked until after a call to <paramref name="ReleaseReadLock"/>.
77+
/// Blocks the current thread until it is able to acquire a read lock. If succesfull all subsequent writes will be blocked until after a call to <see cref="ReleaseReadLock"/>.
7878
/// </summary>
7979
/// <param name="millisecondsTimeout">The number of milliseconds to wait, or <see cref="System.Threading.Timeout.Infinite" /> (-1) to wait indefinitely.</param>
8080
/// <returns>true if the read lock was able to be acquired, otherwise false.</returns>
@@ -97,7 +97,7 @@ public void ReleaseReadLock()
9797
}
9898

9999
/// <summary>
100-
/// Blocks the current thread until it is able to acquire a write lock. If succesfull all subsequent reads will be blocked until after a call to <paramref name="ReleaseWriteLock"/>.
100+
/// Blocks the current thread until it is able to acquire a write lock. If succesfull all subsequent reads will be blocked until after a call to <see cref="ReleaseWriteLock"/>.
101101
/// </summary>
102102
/// <param name="millisecondsTimeout">The number of milliseconds to wait, or System.Threading.Timeout.Infinite (-1) to wait indefinitely.</param>
103103
/// <returns>true if the write lock was able to be acquired, otherwise false.</returns>
@@ -139,7 +139,7 @@ protected override void Write<T>(ref T data, long bufferPosition = 0)
139139
/// Writes an array of <typeparamref name="T"/> into the buffer
140140
/// </summary>
141141
/// <typeparam name="T">A structure type</typeparam>
142-
/// <param name="data">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
142+
/// <param name="buffer">An array of <typeparamref name="T"/> to be written. The length of this array controls the number of elements to be written.</param>
143143
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to write to.</param>
144144
protected override void Write<T>(T[] buffer, long bufferPosition = 0)
145145
{
@@ -190,7 +190,7 @@ protected override void Read<T>(out T data, long bufferPosition = 0)
190190
/// Reads an array of <typeparamref name="T"/> from the buffer
191191
/// </summary>
192192
/// <typeparam name="T">A structure type</typeparam>
193-
/// <param name="data">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
193+
/// <param name="buffer">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param>
194194
/// <param name="bufferPosition">The offset within the buffer region of the shared memory to read from.</param>
195195
protected override void Read<T>(T[] buffer, long bufferPosition = 0)
196196
{
@@ -225,6 +225,10 @@ protected override void Read(Action<IntPtr> readFunc, long bufferPosition = 0)
225225

226226
#region IDisposable
227227

228+
/// <summary>
229+
/// IDisposable pattern
230+
/// </summary>
231+
/// <param name="disposeManagedResources">true to release managed resources</param>
228232
protected override void Dispose(bool disposeManagedResources)
229233
{
230234
if (disposeManagedResources)

0 commit comments

Comments
 (0)