diff --git a/ReaderApp/Program.cs b/ReaderApp/Program.cs index d442ad7..042a117 100644 --- a/ReaderApp/Program.cs +++ b/ReaderApp/Program.cs @@ -1,12 +1,14 @@  -using SharedMemory; - try { - using var numbers = new SharedArray("MySharedArray"); - Console.WriteLine(numbers.Length); - Console.WriteLine(numbers[0]); - Console.WriteLine(numbers[^1]); + using var consumer = new WriterApp.SharedBuffer("MySharedBuffer"); + consumer.BufferAction((span) => + { + for (var i = 0; i < span.Length; i++) + { + Console.WriteLine($"Read {span[i]} from the shared buffer"); + } + }); } catch (FileNotFoundException) { diff --git a/ReaderApp/ReaderApp.csproj b/ReaderApp/ReaderApp.csproj index 1748380..1531814 100644 --- a/ReaderApp/ReaderApp.csproj +++ b/ReaderApp/ReaderApp.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/WriterApp/Program.cs b/WriterApp/Program.cs index 421b9f2..112724e 100644 --- a/WriterApp/Program.cs +++ b/WriterApp/Program.cs @@ -7,12 +7,15 @@ using var producer = new WriterApp.SharedBuffer("MySharedBuffer", length: 1 var intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; producer.Write(intArray); using var consumer = new WriterApp.SharedBuffer("MySharedBuffer"); -var intSpan = consumer.DataSpan; -for (var i = 0; i < intSpan.Length; i++) +consumer.BufferAction((span) => { - Debug.Assert(intSpan[i] == intArray[i]); + for (var i = 0; i < span.Length; i++) + { + Debug.Assert(span[i] == intArray[i]); + } +}); + -} Console.WriteLine("Start the second program, then press Enter here to close the array"); Console.ReadLine(); diff --git a/WriterApp/SharedBuffer.cs b/WriterApp/SharedBuffer.cs index 4959ddb..2a3eccf 100644 --- a/WriterApp/SharedBuffer.cs +++ b/WriterApp/SharedBuffer.cs @@ -26,12 +26,6 @@ public class SharedBuffer : BufferReadWrite where T : struct if (!WriteWaitEvent.WaitOne(ReadWriteTimeout)) throw new TimeoutException("The write operation timed out waiting for the write lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); } - public void Write(T[] array) - { - WriteWait(); - var span = DataSpan; - array.AsSpan().CopyTo(span); - } /// /// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within milliseconds /// @@ -40,20 +34,14 @@ public class SharedBuffer : BufferReadWrite where T : struct if (!ReadWaitEvent.WaitOne(ReadWriteTimeout)) throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); } - public void Read(T[] array) - { - ReadWait(); - var span = DataSpan; - span.CopyTo(array.AsSpan()); - } - public Span ByteSpan + private Span ByteSpan { get { return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes; } } - public Span DataSpan + private Span DataSpan { get { @@ -61,5 +49,44 @@ public class SharedBuffer : BufferReadWrite where T : struct return spanT; } } + public void Write(T[] array) + { + WriteWait(); + var span = DataSpan; + if (span.Length < array.Length) + throw new ArgumentException($"The array length {array.Length} is greater than the buffer length {span.Length}"); + array.AsSpan().CopyTo(span); + } + public void Read(T[] array) + { + ReadWait(); + var span = DataSpan; + if (span.Length > array.Length) + throw new ArgumentException($"The array length {array.Length} is less than the buffer length {span.Length}"); + span.CopyTo(array.AsSpan()); + } + public void Write(ReadOnlySpan span) + { + WriteWait(); + var dataSpan = DataSpan; + if (dataSpan.Length < span.Length) + throw new ArgumentException($"The span length {span.Length} is greater than the buffer length {dataSpan.Length}"); + span.CopyTo(dataSpan); + } + + public void Read(Span span) + { + ReadWait(); + var dataSpan = DataSpan; + if (dataSpan.Length > span.Length) + throw new ArgumentException($"The span length {span.Length} is less than the buffer length {dataSpan.Length}"); + dataSpan.CopyTo(span); + } + public void BufferAction(Action> action) + { + ReadWait(); + var dataSpan = DataSpan; + action(dataSpan); + } public T First => DataSpan[0]; }