public void OnWriterCompletedThrowsNotSupportedException()
        {
            PipeReader reader = PipeReader.Create(Stream.Null);

            Assert.Throws <NotSupportedException>(() => reader.OnWriterCompleted((_, __) => { }, null));
            reader.Complete();
        }
        public void OnWriterCompletedNoops()
        {
            bool       fired  = false;
            PipeReader reader = PipeReader.Create(Stream.Null);

            reader.OnWriterCompleted((_, __) => { fired = true; }, null);
            reader.Complete();
            Assert.False(fired);
        }
Example #3
0
        public void OnWriterCompletedThrowsNotSupportedException()
        {
            bool       fired  = false;
            PipeReader reader = PipeReader.Create(Stream.Null);

            reader.OnWriterCompleted((_, __) => { fired = true; }, null);
            reader.Complete();
            Assert.False(fired);
        }
        private async Task CloneAsync(string path, PipeReader from, PipeWriter to)
        {
            try {
                to.OnReaderCompleted((ex, o) => {
                    // if (ex != null) Console.Error.WriteLine(ex);
                    ((PipeReader)o).Complete(ex);
                }, from);
                from.OnWriterCompleted((ex, o) =>
                {
                    // if (ex != null) Console.Error.WriteLine(ex);
                    ((PipeWriter)o).Complete(ex);
                }, to);
                try { File.Delete(path); } catch {}

                while (true)
                {
                    var result = await from.ReadAsync();

                    var buffer = result.Buffer;
                    if (result.IsCompleted && buffer.IsEmpty)
                    {
                        break;
                    }

                    using (var file = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        foreach (var segment in buffer)
                        {
                            // append it to the file
                            bool leased = false;
                            if (!MemoryMarshal.TryGetArray(segment, out var arr))
                            {
                                var tmp = ArrayPool <byte> .Shared.Rent(segment.Length);

                                segment.CopyTo(tmp);
                                arr    = new ArraySegment <byte>(tmp, 0, segment.Length);
                                leased = true;
                            }
                            await file.WriteAsync(arr.Array, arr.Offset, arr.Count);

                            await file.FlushAsync();

                            if (leased)
                            {
                                ArrayPool <byte> .Shared.Return(arr.Array);
                            }

                            // and flush it upstream
                            await to.WriteAsync(segment);
                        }
                    }
                    from.AdvanceTo(buffer.End);
                }
            }
            catch { }
        }
        public void OnWriterCompletedNoops()
        {
            bool       fired  = false;
            PipeReader reader = PipeReader.Create(Stream.Null);

#pragma warning disable CS0618 // Type or member is obsolete
            reader.OnWriterCompleted((_, __) => { fired = true; }, null);
#pragma warning restore CS0618 // Type or member is obsolete
            reader.Complete();
            Assert.False(fired);
        }
        public LibuvOutputConsumer(
            PipeReader pipe,
            LibuvThread thread,
            UvStreamHandle socket,
            string connectionId,
            ILibuvTrace log)
        {
            _pipe         = pipe;
            _thread       = thread;
            _socket       = socket;
            _connectionId = connectionId;
            _log          = log;

            _pipe.OnWriterCompleted(OnWriterCompleted, this);
        }
        internal static Task WaitForWriterCompletionAsync(this PipeReader reader)
        {
            Requires.NotNull(reader, nameof(reader));

            var writerDone = new TaskCompletionSource <object?>();

            reader.OnWriterCompleted(
                (ex, wdObject) =>
            {
                var wd = (TaskCompletionSource <object?>)wdObject;
                if (ex != null)
                {
                    wd.SetException(ex);
                }
                else
                {
                    wd.SetResult(null);
                }
            },
                writerDone);
            return(writerDone.Task);
        }
Example #8
0
        internal static Task WaitForWriterCompletionAsync(this PipeReader reader)
        {
            Requires.NotNull(reader, nameof(reader));

            var writerDone = new TaskCompletionSource <object?>();

#pragma warning disable CS0618 // Type or member is obsolete
            reader.OnWriterCompleted(
                (ex, wdObject) =>
            {
                var wd = (TaskCompletionSource <object?>)wdObject;
                if (ex != null)
                {
                    wd.SetException(ex);
                }
                else
                {
                    wd.SetResult(null);
                }
            },
                writerDone);
#pragma warning restore CS0618 // Type or member is obsolete
            return(writerDone.Task);
        }
Example #9
0
 // note - consider deprecated: https://github.com/dotnet/corefx/issues/38362
 public override void OnWriterCompleted(Action <Exception, object> callback, object state)
 => _reader.OnWriterCompleted(callback, state);
Example #10
0
 public override void OnWriterCompleted(Action <Exception?, object> callback, object state)
 {
     _pipeReader.OnWriterCompleted(callback, state);
 }