/// <summary> /// Read from the LSL stream for server status /// </summary> async Task RunReadStatusPortAsync(CancellationToken cancelToken) { ReportNetworkTimeInterval.Restart(); StreamInlet inlet = null; try { inlet = new StreamInlet(StatusStream); cancelToken.Register(() => inlet.close_stream()); inlet.open_stream(); SyncedTime = false; SetBoardProperties(inlet); Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadStatusPortAsync", $"Create LSL stream for status on host {HostName}.", LogLevel.DEBUG)); HatConnectionChanged?.Invoke(this, new HatConnectionEventArgs(HatConnectionState.Discovered, HostName, "", BoardId, SampleRate)); string[,] samples = new string[32, 1]; double[] timestamps = new double[32]; // spin until canceled while (!cancelToken.IsCancellationRequested) { await Task.Delay(StatusReadingDelay); try { var sampleCount = inlet.pull_chunk(samples, timestamps); if (sampleCount > 0 && (samples[sampleCount - 1, 0].Length > 0)) { await ParseServerStatus(samples[sampleCount - 1, 0]); } } catch (ObjectDisposedException) { } catch (Exception ex) { Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadStatusPortAsync", ex, LogLevel.WARN)); } } } catch (OperationCanceledException) { } catch (Exception e) { Log?.Invoke(this, new LogEventArgs(this, "RunReadStatusPortAsync", e, LogLevel.FATAL)); } finally { if (inlet != null) { inlet.close_stream(); } } }
/// <summary> /// Run function for reading data on LSL multicast data port /// </summary> async Task RunReadDataPortAsync(CancellationToken cancelToken) { StreamInlet inlet = null; try { int maxChunkLen = (int)((SampleRate * 1.5) / ReadingDelay); inlet = new StreamInlet(DataStream, 360, maxChunkLen); cancelToken.Register(() => inlet.close_stream()); inlet.open_stream(); Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadDataPortAsync", $"Create LSL data stream for host {HostName}.", LogLevel.DEBUG)); double[,] buffer = new double[512, SampleSize]; double[] timestamps = new double[512]; // spin until canceled while (!cancelToken.IsCancellationRequested) { await Task.Delay(ReadingDelay); try { int num = inlet.pull_chunk(buffer, timestamps); ProcessChunk(buffer, num); } catch (ObjectDisposedException) { } catch (Exception ex) { Log?.Invoke(this, new LogEventArgs(HostName, this, "RunReadDataPortAsync", ex, LogLevel.WARN)); } } } catch (OperationCanceledException) { } catch (Exception e) { Log?.Invoke(this, new LogEventArgs(this, "RunReadDataPortAsync", e, LogLevel.FATAL)); } finally { if (inlet != null) { inlet.close_stream(); } } }