ctrlX Data Layer .NET API  4.2.0
Loading...
Searching...
No Matches
DatalayerSystem.cs
1using Datalayer.Internal;
2using System;
3using System.Collections.Generic;
4
5namespace Datalayer
6{
10 public class DatalayerSystem : IDatalayerSystem, INative
11 {
12 private unsafe void* _nativePtr;
13
14 private IFactory _factory;
15 private IConverter _converter;
16
17 private readonly string _ipcPath;
18
19 private bool _disposedValue;
20 private bool _isStarted;
21
22 private readonly List<Client> _clients = new List<Client>();
23 private readonly List<Provider> _providers = new List<Provider>();
24
30 public DatalayerSystem(string ipcPath = "")
31 {
32 _ipcPath = ipcPath ?? throw new ArgumentNullException(nameof(ipcPath));
33
34 var ipcPathBuffer = StringBuffer.FromString(_ipcPath);
35 unsafe
36 {
37 _nativePtr = NativeMethods.Datalayer.DLR_systemCreate(ipcPathBuffer.ToNativePtr());
38 }
39 }
40
44 unsafe void* INative.NativePtr => _nativePtr;
45
49 internal List<Client> Clients => _clients;
50
54 internal List<Provider> Providers => _providers;
55
56 #region Disposing
57
61 public bool IsDisposed => _disposedValue;
62
67 protected virtual void Dispose(bool disposing)
68 {
69 if (!_disposedValue)
70 {
71 if (disposing)
72 {
73 // dispose managed state (managed objects)
74 }
75
76 // free unmanaged resources (unmanaged objects) and override finalizer
77 Delete();
78 // set large fields to null
79 _disposedValue = true;
80 }
81 }
82
87 {
88 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
89 Dispose(disposing: false);
90 }
91
92
96 public void Dispose()
97 {
98 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
99 Dispose(disposing: true);
100 GC.SuppressFinalize(this);
101 }
102
106 private void Delete()
107 {
108 unsafe
109 {
110 if (_nativePtr == null)
111 {
112 return;
113 }
114
115 //Stop implicitly, first
116 if (!IsDisposed)
117 {
118 Stop();
119 }
120
121 NativeMethods.Datalayer.DLR_systemDelete(_nativePtr);
122 _isStarted = false;
123 _nativePtr = null;
124 _disposedValue = true;
125 }
126 }
127
131 private void DeleteChildren()
132 {
133 //Delete clients
134 foreach (var client in _clients)
135 {
136 client.Delete();
137 }
138 _clients.Clear();
139
140 //Delete providers
141 foreach (var provider in _providers)
142 {
143 provider.Delete();
144 }
145 _providers.Clear();
146 }
147 #endregion
148
149 #region Public Consts
150
154 public static readonly int DefaultClientPort = 2069;
155
159 public static readonly int DefaultProviderPort = 2070;
160
165 public static readonly string ProtocolSchemeTcp = "tcp://";
166
171 public static readonly string ProtocolSchemeIpc = "ipc://";
172
173 #endregion
174
175 #region Public Properties
176
181 public string IpcPath
182 {
183 get
184 {
185 if (IsDisposed)
186 {
187 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
188 }
189
190 return _ipcPath;
191 }
192 }
193
198 public bool IsStarted
199 {
200 get
201 {
202 if (IsDisposed)
203 {
204 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
205 }
206
207 return _isStarted;
208 }
209 }
210
216 public string BfbsPath
217 {
218 set
219 {
220 if (IsDisposed)
221 {
222 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
223 }
224
225 if (value == null)
226 {
227 throw new ArgumentNullException(nameof(value));
228 }
229
230 var valueBuffer = StringBuffer.FromString(value);
231 unsafe
232 {
233
234 NativeMethods.Datalayer.DLR_systemSetBfbsPath(_nativePtr, valueBuffer.ToNativePtr());
235 }
236 }
237 }
238
239 #endregion
240
241 #region Public Methods
242
255 public void Start(bool startBroker = false)
256 {
257 if (IsDisposed)
258 {
259 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
260 }
261
262 unsafe
263 {
264 NativeMethods.Datalayer.DLR_systemStart(_nativePtr, Convert.ToByte(startBroker));
265 }
266 _isStarted = true;
267 }
268
273 public void Stop()
274 {
275 //If the system has been stopped, it's completely useless
276
277 if (IsDisposed)
278 {
279 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
280 }
281
282 //We have to delete the children BEFORE stopping, else stop never returns (hang)
283 DeleteChildren();
284
285 unsafe
286 {
287 NativeMethods.Datalayer.DLR_systemStop(_nativePtr, Convert.ToByte(true));
288 _isStarted = false;
289 }
290 }
291
298 {
299 get
300 {
301 if (IsDisposed)
302 {
303 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
304 }
305
306 if (_factory == null)
307 {
308 //System has to be started to get valid factory pointer
309 if (!_isStarted)
310 {
311 throw new InvalidOperationException("DatalayerSystem not started");
312 }
313
314 unsafe
315 {
316 void* factoryPtr = NativeMethods.Datalayer.DLR_systemFactory(_nativePtr);
317 _factory = new Factory(this, factoryPtr);
318 }
319 }
320 return _factory;
321 }
322 }
323
329 {
330 get
331 {
332 if (IsDisposed)
333 {
334 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
335 }
336
337 if (_converter == null)
338 {
339 unsafe
340 {
341 void* converterPtr = NativeMethods.Datalayer.DLR_systemJsonConverter(_nativePtr);
342 _converter = new Converter(converterPtr);
343 }
344 }
345 return _converter;
346 }
347 }
348
349 #endregion
350 }
351}
Provides the implementation for IDatalayerSystem.
string IpcPath
Gets the path for interprocess communication.
DatalayerSystem(string ipcPath="")
Initializes a new instance of the DatalayerSystem class.
void Stop()
Stops the DatalayerSystem.
static readonly string ProtocolSchemeTcp
Gets the protocol scheme for TCP communication. Recommended to connect to a DatalayerSystem not runni...
static readonly int DefaultProviderPort
Gets the default Provider port.
void Dispose()
Dispose the instance.
string BfbsPath
Sets the binary Flatbuffer path, which contains *.bfbs files.
static readonly int DefaultClientPort
Gets the default Client port.
virtual void Dispose(bool disposing)
Disposes the instance.
bool IsStarted
Gets a value that indicates whether the DatalayerSystem is started.
static readonly string ProtocolSchemeIpc
Gets the protocol scheme for IPC communication. Recommended to connect to a DatalayerSystem running o...
IConverter Converter
Gets the Converter for Variant to JSON conversions.
bool IsDisposed
Gets a value that indicates whether the instance is already disposed and useless.
IFactory Factory
Gets the Factory to create Clients and Providers.
void Start(bool startBroker=false)
Starts the DatalayerSystem.
The IConverter interface.
Definition: IConverter.cs:9
The IDatalayerSystem interface.
The IFactory interface.
Definition: IFactory.cs:9