ctrlX Data Layer .NET API  4.3.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
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 Factory _factory;
15 private Converter _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
95 public void Dispose()
96 {
97 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
98 Dispose(disposing: true);
99 GC.SuppressFinalize(this);
100 }
101
105 private void Delete()
106 {
107 unsafe
108 {
109 if (_nativePtr == null)
110 {
111 return;
112 }
113
114 //Stop implicitly, first
115 if (!IsDisposed)
116 {
117 Stop();
118 }
119
120 NativeMethods.Datalayer.DLR_systemDelete(_nativePtr);
121 _isStarted = false;
122 _nativePtr = null;
123 _disposedValue = true;
124 }
125 }
126
130 private void DeleteChildren()
131 {
132 //Delete clients
133 foreach (var client in _clients)
134 {
135 client.Delete();
136 }
137 _clients.Clear();
138
139 //Delete providers
140 foreach (var provider in _providers)
141 {
142 provider.Delete();
143 }
144 _providers.Clear();
145
146 //Delete Factory / Converter and force recreation on next access.
147 if (_factory != null)
148 {
149 _factory.Delete();
150 _factory = null;
151 }
152
153 if (_converter != null)
154 {
155 _converter.Delete();
156 _converter = null;
157 }
158 }
159 #endregion
160
161 #region Public Consts
162
166 public static readonly int DefaultClientPort = 2069;
167
171 public static readonly int DefaultProviderPort = 2070;
172
177 public static readonly string ProtocolSchemeTcp = "tcp://";
178
183 public static readonly string ProtocolSchemeIpc = "ipc://";
184
185 #endregion
186
187 #region Public Properties
188
193 public string IpcPath
194 {
195 get
196 {
197 if (IsDisposed)
198 {
199 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
200 }
201
202 return _ipcPath;
203 }
204 }
205
210 public bool IsStarted
211 {
212 get
213 {
214 if (IsDisposed)
215 {
216 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
217 }
218
219 return _isStarted;
220 }
221 }
222
228 public string BfbsPath
229 {
230 set
231 {
232 if (IsDisposed)
233 {
234 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
235 }
236
237 if (value == null)
238 {
239 throw new ArgumentNullException(nameof(value));
240 }
241
242 var valueBuffer = StringBuffer.FromString(value);
243 unsafe
244 {
245
246 NativeMethods.Datalayer.DLR_systemSetBfbsPath(_nativePtr, valueBuffer.ToNativePtr());
247 }
248 }
249 }
250
251 #endregion
252
253 #region Public Methods
254
267 public void Start(bool startBroker = false)
268 {
269 if (IsDisposed)
270 {
271 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
272 }
273
274 unsafe
275 {
276 NativeMethods.Datalayer.DLR_systemStart(_nativePtr, Convert.ToByte(startBroker));
277 }
278 _isStarted = true;
279 }
280
285 public void Stop()
286 {
287 //If the system has been stopped, it's completely useless
288
289 if (IsDisposed)
290 {
291 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
292 }
293
294 //We have to delete the children BEFORE stopping, else stop never returns (hang)
295 DeleteChildren();
296
297 unsafe
298 {
299 NativeMethods.Datalayer.DLR_systemStop(_nativePtr, Convert.ToByte(true));
300 }
301 _isStarted = false;
302 }
303
310 {
311 get
312 {
313 if (IsDisposed)
314 {
315 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
316 }
317
318 if (_factory == null)
319 {
320 //System has to be started to get valid factory pointer
321 if (!_isStarted)
322 {
323 throw new InvalidOperationException("DatalayerSystem not started");
324 }
325
326 unsafe
327 {
328 void* factoryPtr = NativeMethods.Datalayer.DLR_systemFactory(_nativePtr);
329 _factory = new Factory(this, factoryPtr);
330 }
331 }
332 return _factory;
333 }
334 }
335
341 {
342 get
343 {
344 if (IsDisposed)
345 {
346 throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
347 }
348
349 if (_converter == null)
350 {
351 unsafe
352 {
353 void* converterPtr = NativeMethods.Datalayer.DLR_systemJsonConverter(_nativePtr);
354 _converter = new Converter(this, converterPtr);
355 }
356 }
357 return _converter;
358 }
359 }
360
361 #endregion
362 }
363}
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