-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathSharedArray.cs
More file actions
316 lines (276 loc) · 12.1 KB
/
Copy pathSharedArray.cs
File metadata and controls
316 lines (276 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// SharedMemory (File: SharedMemory\SharedArray.cs)
// Copyright (c) 2014 Justin Stenning
// http://spazzarama.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// The SharedMemory library is inspired by the following Code Project article:
// "Fast IPC Communication Using Shared Memory and InterlockedCompareExchange"
// http://www.codeproject.com/Articles/14740/Fast-IPC-Communication-Using-Shared-Memory-and-Int
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace SharedMemory
{
/// <summary>
/// A generic fixed-length shared memory array of structures with support for simple inter-process read/write synchronisation.
/// </summary>
/// <typeparam name="T">The structure type that will be stored in the elements of this fixed array buffer.</typeparam>
#if NETFULL
[PermissionSet(SecurityAction.LinkDemand)]
[PermissionSet(SecurityAction.InheritanceDemand)]
#endif
public class SharedArray<T> : BufferWithLocks, IList<T>
where T : struct
{
/// <summary>
/// Gets a 32-bit integer that represents the total number of elements in the <see cref="SharedArray{T}"/>
/// </summary>
public int Length { get; private set; }
/// <summary>
/// Gets or sets the element at the specified index
/// </summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <returns>The element at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 -or- index is equal to or greater than <see cref="Length"/>.</exception>
public T this[int index]
{
get
{
T item;
Read(out item, index);
return item;
}
set
{
Write(ref value, index);
}
}
private int _elementSize;
#region Constructors
/// <summary>
/// Creates the shared memory array with the name specified by <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the shared memory array to be created.</param>
/// <param name="length">The number of elements to make room for within the shared memory array.</param>
#if !(NET35)
/// <param name="openExisting">Indicates whether to allow opening an existing if already exists.</param>
#endif
public SharedArray(string name, int length
#if !(NET35)
, bool openExisting = false
#endif
)
: base(name, Marshal.SizeOf(typeof(T)) * length, true)
{
Length = length;
_elementSize = Marshal.SizeOf(typeof(T));
Open(
#if !(NET35)
openExisting
#endif
);
}
/// <summary>
/// Opens an existing shared memory array with the name as specified by <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the shared memory array to open.</param>
/// <exception cref="ArgumentOutOfRangeException">If the shared memory location specified by <paramref name="name"/> does not have a <see cref="SharedBuffer.BufferSize"/> that is evenly divisible by the size of <typeparamref name="T"/>.</exception>
public SharedArray(string name)
: base(name, 0, false)
{
_elementSize = Marshal.SizeOf(typeof(T));
Open();
}
#endregion
/// <summary>
/// Perform any initialisation required when opening the shared memory array
/// </summary>
/// <returns>true if successful</returns>
protected override bool DoOpen()
{
if (!IsOwnerOfSharedMemory)
{
if (BufferSize % _elementSize != 0)
throw new ArgumentOutOfRangeException("name", "BufferSize is not evenly divisible by the size of " + typeof(T).Name);
Length = (int)(BufferSize / _elementSize);
}
return true;
}
#region Writing
/// <summary>
/// Copy <paramref name="data"/> to the shared memory array element at index <paramref name="index"/>.
/// </summary>
/// <param name="data">The data to be written.</param>
/// <param name="index">The zero-based index of the element to set.</param>
public void Write(ref T data, int index)
{
if (index > Length - 1 || index < 0)
throw new ArgumentOutOfRangeException("index");
base.Write(ref data, index * _elementSize);
}
/// <summary>
/// Copy the elements of the array <paramref name="buffer"/> into the shared memory array starting at index <paramref name="startIndex"/>.
/// </summary>
/// <param name="buffer">The source array to copy elements from.</param>
/// <param name="startIndex">The zero-based index of the shared memory array element to begin writing to.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- length of <paramref name="buffer"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> must not be null</exception>
public void Write(T[] buffer, int startIndex = 0)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (buffer.Length + startIndex > Length || startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex");
base.Write(buffer, startIndex * _elementSize);
}
#endregion
#region Reading
/// <summary>
/// Reads a single element from the shared memory array into <paramref name="data"/> located at <paramref name="index"/>.
/// </summary>
/// <param name="data">The element at the specified index.</param>
/// <param name="index">The zero-based index of the element to get.</param>
/// <returns>The element at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 -or- index is equal to or greater than <see cref="Length"/>.</exception>
public void Read(out T data, int index)
{
if (index > Length - 1 || index < 0)
throw new ArgumentOutOfRangeException("index");
base.Read(out data, index * _elementSize);
}
/// <summary>
/// Reads buffer.Length elements from the shared memory array into <paramref name="buffer"/> starting at the shared memory array element located at <paramref name="startIndex"/>.
/// </summary>
/// <param name="buffer">The destination array to copy the elements into.</param>
/// <param name="startIndex">The zero-based index of the shared memory array element to begin reading from.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than 0 -or- length of <paramref name="buffer"/> + <paramref name="startIndex"/> is greater than <see cref="Length"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> must not be null</exception>
public void CopyTo(T[] buffer, int startIndex = 0)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (buffer.Length + startIndex > Length || startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex");
base.Read(buffer, startIndex * _elementSize);
}
#endregion
#region IEnumerable<T>
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="System.Collections.Generic.IEnumerator{T}"/> instance that can be used to iterate through the collection</returns>
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Length; i++)
{
yield return this[i];
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>An <see cref="System.Collections.IEnumerator"/> object that can be used to iterate through the collection.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IList<T>
/// <summary>
/// Operation not supported. Throws <see cref="System.NotImplementedException"/>
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
throw new NotImplementedException();
}
/// <summary>
/// Operation not supported. Throws <see cref="System.NotImplementedException"/>
/// </summary>
public void Clear()
{
throw new NotImplementedException();
}
/// <summary>
/// Checks if the list contains the specified item.
/// </summary>
/// <param name="item"></param>
/// <returns>True if found</returns>
public bool Contains(T item)
{
return IndexOf(item) >= 0;
}
/// <summary>
/// Operation not supported. Throws <see cref="System.NotImplementedException"/>
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(T item)
{
throw new NotImplementedException();
}
/// <summary>
/// The number of elements in the array
/// </summary>
public int Count
{
get { return Length; }
}
/// <summary>
/// The elements are not read-only
/// </summary>
public bool IsReadOnly
{
get { return true; }
}
/// <summary>
/// Return the index of the specified item.
/// </summary>
/// <param name="item"></param>
/// <returns>The index of the item if found, otherwise -1.</returns>
public int IndexOf(T item)
{
for (var i = 0; i < Count; i++)
{
if (this[i].Equals(item)) return i;
}
return -1;
}
/// <summary>
/// Operation not supported. Throws <see cref="System.NotImplementedException"/>
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
/// <summary>
/// Operation not supported. Throws <see cref="System.NotImplementedException"/>
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
#endregion
}
}