ctrlX Data Layer API for .NET 5  2.1.0
DatalayerSystem.cs
1 using Datalayer.Internal;
2 using System;
3 using System.Collections.Generic;
4 
5 namespace 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();
23  private readonly List<Provider> _providers = new();
24 
30  public DatalayerSystem(string ipcPath = "")
31  {
32  _ipcPath = ipcPath ?? throw new ArgumentNullException(nameof(ipcPath));
33 
34  unsafe
35  {
36  _nativePtr = NativeMethods.Datalayer.DLR_systemCreate(IpcPath.ToSBytePtr());
37  }
38  }
39 
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  unsafe
231  {
232  NativeMethods.Datalayer.DLR_systemSetBfbsPath(_nativePtr, value.ToSBytePtr());
233  }
234  }
235  }
236 
237  #endregion
238 
239  #region Public Methods
240 
253  public void Start(bool startBroker = false)
254  {
255  if (IsDisposed)
256  {
257  throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
258  }
259 
260  unsafe
261  {
262  NativeMethods.Datalayer.DLR_systemStart(_nativePtr, Convert.ToByte(startBroker));
263  }
264  _isStarted = true;
265  }
266 
271  public void Stop()
272  {
273  if (IsDisposed)
274  {
275  throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
276  }
277 
278  //We have to delete the children BEFORE stopping, else stop never returns (hang)
279  DeleteChildren();
280 
281  unsafe
282  {
283  NativeMethods.Datalayer.DLR_systemStop(_nativePtr, Convert.ToByte(true));
284  _isStarted = false;
285  }
286  }
287 
293  public IFactory Factory
294  {
295  get
296  {
297  if (IsDisposed)
298  {
299  throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
300  }
301 
302  if (_factory == null)
303  {
304  //System has to be started to get valid factory pointer
305  if (!_isStarted)
306  {
307  throw new InvalidOperationException("DatalayerSystem not started");
308  }
309 
310  unsafe
311  {
312  void* factoryPtr = NativeMethods.Datalayer.DLR_systemFactory(_nativePtr);
313  _factory = new Factory(this, factoryPtr);
314  }
315  }
316  return _factory;
317  }
318  }
319 
324  public IConverter Converter
325  {
326  get
327  {
328  if (IsDisposed)
329  {
330  throw new ObjectDisposedException(Utils.Strings.ErrorObjectDisposed);
331  }
332 
333  if (_converter == null)
334  {
335  unsafe
336  {
337  void* converterPtr = NativeMethods.Datalayer.DLR_systemJsonConverter(_nativePtr);
338  _converter = new Converter(converterPtr);
339  }
340  }
341  return _converter;
342  }
343  }
344 
345  #endregion
346  }
347 }
Datalayer.DatalayerSystem.DefaultProviderPort
static readonly int DefaultProviderPort
Gets the default Provider port
Definition: DatalayerSystem.cs:159
Datalayer.DatalayerSystem.DatalayerSystem
DatalayerSystem(string ipcPath="")
Creates a new DatalayerSystem
Definition: DatalayerSystem.cs:30
Datalayer.IDatalayerSystem
The datalayer system interface.
Definition: IDatalayerSystem.cs:8
Datalayer.DatalayerSystem.IpcPath
string IpcPath
Gets the path for interprocess communication.
Definition: DatalayerSystem.cs:182
Datalayer.DatalayerSystem.IsDisposed
bool IsDisposed
Definition: DatalayerSystem.cs:61
Datalayer.DatalayerSystem.Dispose
void Dispose()
Definition: DatalayerSystem.cs:96
Datalayer.DatalayerSystem.BfbsPath
string BfbsPath
Sets the binary Flatbuffer path, which contains *.bfbs files.
Definition: DatalayerSystem.cs:217
Datalayer.DatalayerSystem.Stop
void Stop()
Stops the DatalayerSystem
Definition: DatalayerSystem.cs:271
Datalayer.DatalayerSystem.ProtocolSchemeTcp
static readonly string ProtocolSchemeTcp
Gets the protocol scheme for TCP communication. Recommended to connect to a DatalayerSystem not runni...
Definition: DatalayerSystem.cs:165
Datalayer.IFactory
The factory interface.
Definition: IFactory.cs:8
Datalayer.DatalayerSystem.IsStarted
bool IsStarted
Indicates whether the DatalayerSystem is started
Definition: DatalayerSystem.cs:199
Datalayer.DatalayerSystem.Dispose
virtual void Dispose(bool disposing)
Definition: DatalayerSystem.cs:67
Datalayer
Definition: DatalayerSystem.cs:5
Datalayer.DatalayerSystem.Start
void Start(bool startBroker=false)
Starts the DatalayerSystem
Definition: DatalayerSystem.cs:253
Datalayer.IConverter
The converter interface.
Definition: IConverter.cs:6
Datalayer.DatalayerSystem.DefaultClientPort
static readonly int DefaultClientPort
Gets the default Client port
Definition: DatalayerSystem.cs:154
Datalayer.DatalayerSystem.Factory
IFactory Factory
Gets the Factory to create Clients and Providers
Definition: DatalayerSystem.cs:294
Datalayer.DatalayerSystem.ProtocolSchemeIpc
static readonly string ProtocolSchemeIpc
Gets the protocol scheme for IPC communication. Recommended to connect to a DatalayerSystem running o...
Definition: DatalayerSystem.cs:171
Datalayer.DatalayerSystem.Converter
IConverter Converter
Gets the Converter for Variant to JSON conversions
Definition: DatalayerSystem.cs:325
Datalayer.DatalayerSystem
Definition: DatalayerSystem.cs:10