Example #1
0
        public static IObservable<Unit> WriteAsync(this Stream This, byte[] data, int start, int length)
        {
            var ret = new AsyncSubject<Unit>();

            try
            {
                This.BeginWrite(data, start, length, result =>
                {
                    try
                    {
                        This.EndWrite(result);
                        ret.OnNext(Unit.Default);
                        ret.OnCompleted();
                    }
                    catch (Exception ex)
                    {
                        ret.OnError(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                ret.OnError(ex);
            }

            return ret;
        }
Example #2
0
        public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            var tcs = new TaskCompletionSource<object>();
            var sr = stream.BeginWrite(buffer, offset, count, ar =>
            {
                if (ar.CompletedSynchronously)
                {
                    return;
                }
                try
                {
                    stream.EndWrite(ar);
                    tcs.SetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);

            if (sr.CompletedSynchronously)
            {
                try
                {
                    stream.EndWrite(sr);
                    tcs.SetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }
            return tcs.Task;
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// sqlfilestream.BeginWrite(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this SqlFileStream sqlfilestream, Byte[] buffer, AsyncCallback callback)
        {
            if(sqlfilestream == null) throw new ArgumentNullException("sqlfilestream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return sqlfilestream.BeginWrite(buffer, 0, buffer.Length, callback);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// networkstream.BeginWrite(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this NetworkStream networkstream, Byte[] buffer, AsyncCallback callback)
        {
            if(networkstream == null) throw new ArgumentNullException("networkstream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return networkstream.BeginWrite(buffer, 0, buffer.Length, callback);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// deflatestream.BeginWrite(array, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this DeflateStream deflatestream, Byte[] array, AsyncCallback asyncCallback)
        {
            if(deflatestream == null) throw new ArgumentNullException("deflatestream");

            if(array == null) throw new ArgumentNullException("array");

            return deflatestream.BeginWrite(array, 0, array.Length, asyncCallback);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// isolatedstoragefilestream.BeginWrite(buffer, userCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this IsolatedStorageFileStream isolatedstoragefilestream, Byte[] buffer, AsyncCallback userCallback)
        {
            if(isolatedstoragefilestream == null) throw new ArgumentNullException("isolatedstoragefilestream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return isolatedstoragefilestream.BeginWrite(buffer, 0, buffer.Length, userCallback);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// pipestream.BeginWrite(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this AnonymousPipeClientStream pipestream, Byte[] buffer, AsyncCallback callback)
        {
            if(pipestream == null) throw new ArgumentNullException("pipestream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return pipestream.BeginWrite(buffer, 0, buffer.Length, callback);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// stream.BeginWrite(buffer, callback, state);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this Stream stream, Byte[] buffer, AsyncCallback callback, Object state)
        {
            if(stream == null) throw new ArgumentNullException("stream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return stream.BeginWrite(buffer, 0, buffer.Length, callback, state);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// gzipstream.BeginWrite(array, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this GZipStream gzipstream, Byte[] array, AsyncCallback asyncCallback)
        {
            if(gzipstream == null) throw new ArgumentNullException("gzipstream");

            if(array == null) throw new ArgumentNullException("array");

            return gzipstream.BeginWrite(array, 0, array.Length, asyncCallback);
        }
Example #10
0
        /// <summary>
        /// <paramref name="buffer"/> 내용을 지정된 스트림에 비동기 방식으로 씁니다.
        /// </summary>
        public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count) {
            if(IsDebugEnabled)
                log.Debug("버퍼의 내용을 비동기 방식으로 스트림에 씁니다... offset=[{0}], count=[{1}]", offset, count);

            // Silverlight용 TPL에서는 지원하지 3개 이상의 인자를 지원하지 않는다.
            var ar = stream.BeginWrite(buffer, offset, count, null, null);
            return Task.Factory.StartNew(() => stream.EndWrite(ar));
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// filestream.BeginWrite(array, userCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this FileStream filestream, Byte[] array, AsyncCallback userCallback)
        {
            if(filestream == null) throw new ArgumentNullException("filestream");

            if(array == null) throw new ArgumentNullException("array");

            return filestream.BeginWrite(array, 0, array.Length, userCallback);
        }
Example #12
0
        /// <include file='../_Doc/mscorlib.xml' path='doc/members/member[@name="M:System.IO.Stream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)"]/*' />
        /// <param name="stream">Stream on which to begin writing.</param>
        public static IAsyncResult BeginWrite(this Stream stream, byte[] buffer, int offset, int count,
                                              AsyncCallback callback, object state)
        {
#if DOTNET || WINDOWS_PHONE
            return stream.BeginWrite(buffer, offset, count, callback, state);
#else
            return
                new TaskFactory().StartNew(asyncState => stream.Write(buffer, offset, count), state)
                                 .ContinueWith(task => callback(task));
#endif
        }
Example #13
0
        public static IYield NioWrite(this Stream stream, byte[] buffer, int offset, int count)
        {
            var waitEvent = new WaitEvent();
            stream.BeginWrite(buffer, offset, count, ar =>
                                                         {
                                                             stream.EndWrite(ar);
                                                             ((WaitEvent) ar.AsyncState).Set();
                                                         }, waitEvent);

            return Microthread.Wait(waitEvent);
        }
Example #14
0
        public static void Write(this Stream stream, byte[] buffer, int offset, int size, Action continuation)
        {
            var daemon = Daemons.Current;

            stream.BeginWrite(buffer, offset, size,
                             asyncResult =>
                             {
                                 stream.EndWrite(asyncResult);
                                 ((IDaemon)asyncResult.AsyncState).Schedule(continuation);
                             }, daemon);
        }
 public static Task WriteAsync(this Stream stream, byte[] buffer)
 {
     try
     {
         return Task.Factory.FromAsync((cb, state) => stream.BeginWrite(buffer, 0, buffer.Length, cb, state), ar => stream.EndWrite(ar), null);
     }
     catch (System.Exception ex)
     {
         return TaskAsyncHelper.FromError(ex);
     }
 }
Example #16
0
 public static Task WriteAsync(this Stream stream, byte[] buffer)
 {
     #if NETFX_CORE
     return stream.WriteAsync(buffer, 0, buffer.Length);
     #else
     try
     {
         return Task.Factory.FromAsync((cb, state) => stream.BeginWrite(buffer, 0, buffer.Length, cb, state), ar => stream.EndWrite(ar), null);
     }
     catch (Exception ex)
     {
         return TaskAsyncHelper.FromError(ex);
     }
     #endif
 }
Example #17
0
        public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, 
            CancellationToken cancel = default(CancellationToken))
        {
            cancel.ThrowIfCancellationRequested();
            var tcs = new TaskCompletionSource<object>();
            var sr = stream.BeginWrite(buffer, offset, count, ar =>
            {
                if (ar.CompletedSynchronously)
                {
                    return;
                }
                try
                {
                    stream.EndWrite(ar);
                    tcs.SetResult(null);
                }
                catch (Exception ex)
                {
                    // Assume errors were caused by cancelation.
                    if (cancel.IsCancellationRequested)
                    {
                        tcs.TrySetCanceled();
                    }

                    tcs.SetException(ex);
                }
            }, null);

            if (sr.CompletedSynchronously)
            {
                try
                {
                    stream.EndWrite(sr);
                    tcs.SetResult(null);
                }
                catch (Exception ex)
                {
                    // Assume errors were caused by cancelation.
                    if (cancel.IsCancellationRequested)
                    {
                        tcs.TrySetCanceled();
                    }

                    tcs.SetException(ex);
                }
            }
            return tcs.Task;
        }
Example #18
0
 public static void AsyncWrite(this Stream dest, byte[] buff, Action completed, Action<Exception> excepted)
 {
     dest.BeginWrite(buff, 0, buff.Length, (ar) =>
     {
         try
         {
             dest.EndWrite(ar);
         }
         catch (Exception err)
         {
             excepted(err);
             return;
         }
         completed();
     }, null);
 }
        /// <summary>
        /// Write the buffer to the stream asynchronously, returning a Task to use for completion
        /// </summary>
        /// <param name="stream">The target stream</param>
        /// <param name="buffer">The buffer to write</param>
        /// <param name="offset">The offset into the buffer</param>
        /// <param name="count">The count of bytes to write</param>
        /// <param name="cancellationToken">The cancellation token</param>
        public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (cancellationToken.IsCancellationRequested)
                return TaskUtil.Cancelled();

            var source = new TaskCompletionSource<object>();

            AsyncCallback complete = asyncResult =>
                {
                    try
                    {
                        stream.EndWrite(asyncResult);
                        source.SetResult(null);
                    }
                    catch (OperationCanceledException ex)
                    {
                        if (cancellationToken.IsCancellationRequested)
                            source.TrySetCanceled();
                        else
                            source.SetException(ex);
                    }
                    catch (Exception ex)
                    {
                        source.SetException(ex);
                    }
                };

            AsyncCallback callback = asyncResult =>
                {
                    if (asyncResult.CompletedSynchronously)
                        return;

                    complete(asyncResult);
                };

            IAsyncResult result = stream.BeginWrite(buffer, offset, count, callback, null);
            if (result.CompletedSynchronously)
                complete(result);

            return source.Task;
        }
Example #20
0
        public static SignalFuture AsyncWrite (this Stream stream, byte[] buffer, int offset, int count) {
#if XBOX
            return Future.RunInThread(() => stream.Write(buffer, offset, count));
#else
            var f = new SignalFuture();
            try {
                stream.BeginWrite(buffer, offset, count, (ar) => {
                    try {
                        lock (stream)
                            stream.EndWrite(ar);
                        f.Complete();
                    } catch (FutureHandlerException) {
                        throw;
                    } catch (Exception ex) {
                        f.Fail(ex);
                    }
                }, stream);
            } catch (Exception ex) {
                f.Fail(ex);
            }
            return f;
#endif
        }
 public static Task WriteAsync(this Stream stream,
                               byte[] buffer,
                               int offset,
                               int count,
                               CancellationToken cancellationToken = default(CancellationToken)) {
     if (cancellationToken.IsCancellationRequested) {
         return TaskHelper.Canceled();
     }
     var completionSource = new TaskCompletionSource<int>();
     var processSync = stream.BeginWrite(buffer,
                                         offset,
                                         count,
                                         processAsync => {
                                             if (processAsync.CompletedSynchronously) {
                                                 return;
                                             }
                                             FinishWriteAsync(stream, processAsync, completionSource);
                                         },
                                         null);
     if (processSync.CompletedSynchronously) {
         FinishWriteAsync(stream, processSync, completionSource);
     }
     return completionSource.Task;
 }
        /// <summary>
        /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// pipestream.BeginWrite(buffer, offset, count, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this NamedPipeServerStream pipestream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback)
        {
            if(pipestream == null) throw new ArgumentNullException("pipestream");

            return pipestream.BeginWrite(buffer, offset, count, callback, null);
        }
        /// <summary>
        /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// gzipstream.BeginWrite(array, offset, count, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this GZipStream gzipstream, Byte[] array, Int32 offset, Int32 count, AsyncCallback asyncCallback)
        {
            if(gzipstream == null) throw new ArgumentNullException("gzipstream");

            return gzipstream.BeginWrite(array, offset, count, asyncCallback, null);
        }
 public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
 {
     return Task.Factory.FromAsync((cb, state) => stream.BeginWrite(buffer, offset, count, cb, state), ar => stream.EndWrite(ar), null);
 }
 internal static Task<NullTaskReturn> WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
 {
     return new APMTask(
         (callback, st) => stream.BeginWrite(buffer, offset, count, callback, st),
         (res) => { stream.EndWrite(res); });
 }
Example #26
0
 internal static Task WriteAsync(this System.Net.Security.SslStream source,
     byte[] buffer, int offset, int count)
 {
     return Task.Factory.FromAsync(
         (c, s) => source.BeginWrite(buffer, offset, count, c, s),
         (r) => source.EndWrite(r),
         null);
 }
Example #27
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="buffer"></param>
 /// <param name="callback"></param>
 /// <returns></returns>
 public static IAsyncResult BeginWrite(this Stream stream, byte[] buffer, AsyncCallback callback) => stream.BeginWrite(buffer, 0, buffer.Length, callback, null);
        /// <summary>
        /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// filestream.BeginWrite(array, offset, numBytes, userCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this FileStream filestream, Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback)
        {
            if(filestream == null) throw new ArgumentNullException("filestream");

            return filestream.BeginWrite(array, offset, numBytes, userCallback, null);
        }
        /// <summary>
        /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// sslstream.BeginWrite(buffer, offset, count, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this SslStream sslstream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback)
        {
            if(sslstream == null) throw new ArgumentNullException("sslstream");

            return sslstream.BeginWrite(buffer, offset, count, asyncCallback, null);
        }
        /// <summary>
        /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// sslstream.BeginWrite(buffer, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginWrite(this SslStream sslstream, Byte[] buffer, AsyncCallback asyncCallback)
        {
            if(sslstream == null) throw new ArgumentNullException("sslstream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return sslstream.BeginWrite(buffer, 0, buffer.Length, asyncCallback);
        }