示例#1
0
        private async Task ProcessWrites()
        {
            var writeReq = new UvWriteReq();

            writeReq.Init(_thread.Loop);

            try
            {
                while (true)
                {
                    var buffer = await _output;

                    // Make sure we're on the libuv thread
                    await _thread;

                    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);

                    buffer.Consumed();
                }
            }
            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();
            }
        }
示例#2
0
        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();
            }
        }
示例#3
0
        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();
        }