public void ReceiveData() { _cancelListenerSource = new CancellationTokenSource(); _listenerTask = new Task(async() => { var running = true; while (running) { try { var readTask = _reader.ReadAsync(_readBuffer, 0, MAX_BUFFER_SIZE); readTask.Wait(_cancelListenerSource.Token); var bytesRead = await readTask; var byteBuffer = _readBuffer.ToByteArray(0, bytesRead); _parser.Parse(byteBuffer); } catch (OperationCanceledException) { running = false; /* Task Cancellation */ } catch (Exception ex) { running = false; _logger.NotifyUserError("Client_Listening", ex.Message); } } }); _listenerTask.Start(); }
public async void Listen() { try { // keep reading the serial input while (true) { await ReadAsync(_readCancellationTokenSource.Token); } } catch (TaskCanceledException) { _logger.NotifyUserInfo("SerialChannel_Listen", "Task Cancelled - Closing"); } catch (Exception ex) { _logger.NotifyUserError("SerialChannel_Listen", ex.Message); } finally { // Cleanup once complete if (_dataReaderObject != null) { _dataReaderObject.DetachStream(); _dataReaderObject = null; } } }
public async override Task <bool> ConnectAsync() { try { await _port.OpenAsync(); State = States.Connecting; _reader = new StreamReader(_port.InputStream); _writer = new StreamWriter(_port.OutputStream); ReceiveDataAsync(_reader); State = States.Connected; return(true); } catch (Exception ex) { _logger.NotifyUserError("SerialPortChannel_ConnectAsync", ex.Message); return(false); } }
public async void StartListening() { try { _logger.NotifyUserInfo("TCPIP Listener", $"Started Listening on Port {_port}"); await _listener.BindServiceNameAsync(_port.ToString()); } catch (Exception ex) { _logger.NotifyUserError("TCPIP Listener", ex.Message); } }
public override async Task <bool> ConnectAsync() { _socket = new StreamSocket(); State = States.Connecting; try { await _socket.ConnectAsync(_deviceService.ConnectionHostName, _deviceService.ConnectionServiceName); _writer = new DataWriter(_socket.OutputStream); var dataReader = new DataReader(_socket.InputStream); dataReader.InputStreamOptions = InputStreamOptions.Partial; ReceiveDataAsync(dataReader); State = States.Connected; ConnectCommand.Enabled = false; _logger.NotifyUserInfo("BT Channel", "Connected!"); return(true); } catch (Exception ex) { _logger.NotifyUserError("BT Channel", ex.Message); return(false); } }
private async void SendMessage(mBlockOutgingMessage msg) { lock (_messageHandlers) { if (msg.Handler != null) { _messageHandlers.Add(msg); } } try { OutgoingMessages.Add(msg); // Debug.WriteLine(String.Format("{0:000000} >>>", (DateTime.Now - _start).TotalMilliseconds) + msg.MessageHexString); _logger.NotifyUserInfo("mBlock", ">>> " + msg.MessageHexString); await _channel.WriteBuffer(msg.Buffer); } catch (Exception ex) { _logger.NotifyUserError("mBlock_SendMessage", ex.Message); _logger.NotifyUserInfo("mBlock", "DISCONNECTED!"); _channel.State = States.Disconnected; } }