private async Task ProcessWrites(UvLoopHandle loop, UvTcpHandle handle) { var writeReq = new UvWriteReq(); writeReq.Init(loop); try { while (true) { await _output; var buffer = _output.BeginRead(); if (buffer.IsEmpty && _output.Completion.IsCompleted) { break; } // Up the reference count of the buffer so that we own the disposal of it var cloned = buffer.Clone(); _outgoing.Enqueue(cloned); writeReq.Write(handle, ref cloned, _writeCallback, this); _output.EndRead(buffer); } } catch (Exception ex) { _output.CompleteReading(ex); } finally { _output.CompleteReading(); // There's pending writes happening if (_outgoing.Count > 0) { _connectionCompleted = new TaskCompletionSource <object>(); await _connectionCompleted.Task; } writeReq.Dispose(); handle.Dispose(); } }
private async Task ProcessSends() { while (true) { await _output; var buffer = _output.BeginRead(); if (buffer.IsEmpty && _output.Completion.IsCompleted) { break; } var enumerator = buffer.GetEnumerator(); if (enumerator.MoveNext()) { var current = enumerator.Current; while (enumerator.MoveNext()) { var next = enumerator.Current; await SendAsync(current, endOfMessage : false); current = next; } await PreviousSendingComplete; _sendingBuffer = buffer.Clone(); await SendAsync(current, endOfMessage : true); } _output.EndRead(buffer); } _output.CompleteReading(); }