Пример #1
0
        private static IEnumerable <Future> PipeToIterator(this Stream input, Stream output, byte[] buffer)
        {
            bool readAsync  = input.CanReadAsync();
            bool writeAsync = output.CanWriteAsync();

            while (true)
            {
                int read;
                if (readAsync)
                {
                    var readFuture = Future.FromAsyncPattern <int>(
                        ac => input.BeginRead(buffer, 0, buffer.Length, ac, null),
                        input.EndRead);
                    yield return(readFuture);

                    read = readFuture.Result;
                }
                else
                {
                    read = input.Read(buffer, 0, buffer.Length);
                }

                if (read == 0)
                {
                    yield break;
                }

                if (writeAsync)
                {
                    yield return(Future.FromAsyncPattern(
                                     ac => output.BeginWrite(buffer, 0, read, ac, null),
                                     ar =>
                    {
                        output.EndWrite(ar);
                        return 0;
                    }));
                }
                else
                {
                    output.Write(buffer, 0, read);
                }
            }
        }