private void StartWrite(int offset, int count) { try { _ssl.Write(_sendBuffer, offset, count); OnMsgSent?.Invoke(_sendBuffer, offset, count); TrySendNextMsg(); } catch (Exception ex) { Close(TcpConnectionCloseType.Unexpected, "Error when writing to SSL stream"); throw new Exception("Error when writing to SSL stream", ex); } }
private void TrySendNextMessageThread() { new Thread(() => { while (!_stopSendThread) { try { _canSend = _msgSendQueue.TryDequeue(out _outgoingMessage); if (_canSend) { // NOTE: resulting format will be : version (1) + outgoing message length + outgoing message _outgoingMessageRemainingBytes = _outgoingMessage.Length; AddHeader(_outgoingMessage.Length); int num = 0; while (_outgoingMessageRemainingBytes > 0) { int num2 = Math.Min(_outgoingMessageRemainingBytes, (num == 0) ? 5115 : 5120); Array.Copy(_outgoingMessage, num, _sendBuffer, (num == 0) ? 5 : 0, num2); _ssl.Write(_sendBuffer, 0, (num == 0) ? (num2 + 5) : num2); OnMsgSent?.Invoke(_sendBuffer, 0, (num == 0) ? (num2 + 5) : num2); _outgoingMessageRemainingBytes -= num2; num += num2; } } else { Thread.Sleep(1); } } catch (ObjectDisposedException) { break; } catch (Exception ex) { Close(TcpConnectionCloseType.Unexpected, "Unexpected error while trying to send next message in queue"); throw new Exception("Unexpected error while trying to send next message in queue", ex); } } }).Start(); }