public Task <HTSMessage> SendMessage(HTSMessage message, CancellationToken cancellationToken) { // loop the sequence number if (_seq == int.MaxValue) { _seq = int.MinValue; } else { _seq++; } message.putField("seq", _seq); var taskCompletionSource = new TaskCompletionSource <HTSMessage>(); cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()); _responseHandlers.Add(_seq, taskCompletionSource); try { byte[] data2send = message.BuildBytes(); int bytesSent = _socket.Send(data2send); if (bytesSent != data2send.Length) { _logger.Error("[TVHclient] SendingHandler: Sending not complete! \nBytes sent: " + bytesSent + "\nMessage bytes: " + data2send.Length + "\nMessage: " + message.ToString()); } return(taskCompletionSource.Task); } catch (Exception ex) { _logger.Error("[TVHclient] SendingHandler caught exception : {0}", ex.ToString()); if (_listener != null) { _listener.onError(ex); } else { _logger.ErrorException("[TVHclient] SendingHandler caught exception : {0} but no error listener is configured!!!", ex, ex.ToString()); } throw; } }
private void SendingHandler() { Boolean threadOk = true; while (_connected && threadOk) { if (_sendingHandlerThreadTokenSource.IsCancellationRequested) { return; } try { HTSMessage message = _messagesForSendQueue.Dequeue(); byte[] data2send = message.BuildBytes(); int bytesSent = _socket.Send(data2send); if (bytesSent != data2send.Length) { _logger.LogError("[TVHclient] HTSConnectionAsync.SendingHandler: sending data not completed\nBytes sent: {txbytes}\nMessage bytes: " + "{msgbytes}\nMessage: {msg}", bytesSent, data2send.Length, message.ToString()); } } catch (Exception ex) { threadOk = false; _logger.LogError(ex, "[TVHclient] HTSConnectionAsync.SendingHandler: exception caught"); if (_listener != null) { _listener.onError(ex); } else { _logger.LogError(ex, "[TVHclient] HTSConnectionAsync.SendingHandler: exception caught, but no error listener is configured"); } } } }
private void SendingHandler() { Boolean threadOk = true; while (_connected && threadOk) { if (_sendingHandlerThreadTokenSource.IsCancellationRequested) { return; } try { HTSMessage message = _messagesForSendQueue.Dequeue(); byte[] data2send = message.BuildBytes(); int bytesSent = _socket.Send(data2send); if (bytesSent != data2send.Length) { _logger.Error("[TVHclient] SendingHandler: Sending not complete! \nBytes sent: " + bytesSent + "\nMessage bytes: " + data2send.Length + "\nMessage: " + message.ToString()); } } catch (Exception ex) { threadOk = false; _logger.Error("[TVHclient] SendingHandler caught exception : {0}", ex.ToString()); if (_listener != null) { _listener.onError(ex); } else { _logger.ErrorException("[TVHclient] SendingHandler caught exception : {0} but no error listener is configured!!!", ex, ex.ToString()); } } } }