public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
            {
                Task task = this.WriteAsync(buffer, offset, count);

                if (task.IsSuccess())
                {
                    // write+flush completed synchronously (and successfully)
                    var result = new SynchronousAsyncResult <int>
                    {
                        AsyncState = state
                    };
                    callback?.Invoke(result);
                    return(result);
                }
                else
                {
                    if (callback is object || state != task.AsyncState)
                    {
                        Debug.Assert(_writeCompletion is null);
                        _writeCallback = callback;
                        var tcs = _owner.CapturedContext.NewPromise(state);
                        _writeCompletion = tcs;
                        _ = task.ContinueWith(s_writeCompleteCallback, this, TaskContinuationOptions.ExecuteSynchronously);
                        return(tcs.Task);
                    }
                    else
                    {
                        return(task);
                    }
                }
            }
Example #2
0
        //| <include file='doc\Stream.uex' path='docs/doc[@for="Stream.EndWrite"]/*' />
        public virtual void EndWrite(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;

            if (ar == null || !ar.IsWrite)
            {
                __Error.WrongAsyncResult();
                assume false; // never gets here
            }
            if (ar._EndXxxCalled)
            {
                __Error.EndWriteCalledTwice();
                assume false; // never gets here
            }
            ar._EndXxxCalled = true;

            if (ar._exception != null)
            {
                throw ar._exception;
            }
        }
Example #3
0
        //| <include file='doc\Stream.uex' path='docs/doc[@for="Stream.BeginRead"]/*' />
        public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object _state)
        {
            if (!CanRead)
            {
                __Error.ReadNotSupported();
            }

            // To avoid a race with a stream's position pointer & generating race
            // conditions with internal buffer indexes in our own streams that
            // don't natively support async IO operations when there are multiple
            // async requests outstanding, we will block the application's main
            // thread and do the IO synchronously.
            SynchronousAsyncResult asyncResult = new SynchronousAsyncResult(_state, false);

            try {
                int numRead = Read(buffer, offset, count);
                asyncResult._numRead     = numRead;
                asyncResult._isCompleted = true;
                asyncResult._waitHandle.Set();
            }
            catch (IOException e) {
                asyncResult._exception = e;
            }

            if (callback != null)
            {
                callback(asyncResult);
            }

            return(asyncResult);
        }
Example #4
0
            public override int EndRead(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult <int> syncResult = this.syncReadResult;

                if (ReferenceEquals(asyncResult, syncResult))
                {
                    return(syncResult.Result);
                }

                Contract.Assert(!((Task <int>)asyncResult).IsCanceled);

                try
                {
                    return(((Task <int>)asyncResult).Result);
                }
                catch (AggregateException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    throw; // unreachable
                }
                finally
                {
                    this.readCompletionSource = null;
                    this.readCallback         = null;
                    this.sslOwnedBuffer       = default(ArraySegment <byte>);
                }
            }
Example #5
0
        internal IAsyncResult BlockingBeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            Contract.Ensures(Contract.Result <IAsyncResult>() != null);

            // To avoid a race with a stream's position pointer & generating ----
            // conditions with internal buffer indexes in our own streams that
            // don't natively support async IO operations when there are multiple
            // async requests outstanding, we will block the application's main
            // thread and do the IO synchronously.
            // This can't perform well - use a different approach.
            SynchronousAsyncResult asyncResult;

            try
            {
                Write(buffer, offset, count);
                asyncResult = new SynchronousAsyncResult(state);
            }
            catch (IOException ex)
            {
                asyncResult = new SynchronousAsyncResult(ex, state, isWrite: true);
            }

            if (callback != null)
            {
                callback(asyncResult);
            }

            return(asyncResult);
        }
Example #6
0
            public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
            {
                Task task = this.WriteAsync(buffer, offset, count);

                switch (task.Status)
                {
                case TaskStatus.RanToCompletion:
                    // write+flush completed synchronously (and successfully)
                    var result = new SynchronousAsyncResult <int>();
                    result.AsyncState = state;
                    callback?.Invoke(result);
                    return(result);

                default:
                    if (callback != null || state != task.AsyncState)
                    {
                        Contract.Assert(this.writeCompletion == null);
                        this.writeCallback = callback;
                        var tcs = new TaskCompletionSource(state);
                        this.writeCompletion = tcs;
                        task.ContinueWith(WriteCompleteCallback, this, TaskContinuationOptions.ExecuteSynchronously);
                        return(tcs.Task);
                    }
                    else
                    {
                        return(task);
                    }
                }
            }
            IAsyncResult PrepareSyncReadResult(int readBytes, object state)
            {
                // it is safe to reuse sync result object as it can't lead to leak (no way to attach to it via handle)
                SynchronousAsyncResult <int> result = this.syncReadResult ?? (this.syncReadResult = new SynchronousAsyncResult <int>());

                result.Result     = readBytes;
                result.AsyncState = state;
                return(result);
            }
            IAsyncResult PrepareSyncReadResult(int readBytes, object state)
            {
                //Program.Trace.WriteLine($"[MediationStream] PrepareSyncReadResult - readBytes {readBytes}");
                // it is safe to reuse sync result object as it can't lead to leak (no way to attach to it via handle)
                SynchronousAsyncResult <int> result = this.syncReadResult;

                result.Result     = readBytes;
                result.AsyncState = state;
                return(result);
            }
Example #9
0
        internal static void BlockingEndWrite(IAsyncResult asyncResult)
        {
            SynchronousAsyncResult result = asyncResult as SynchronousAsyncResult;

            if ((result == null) || !result.IsWrite)
            {
                __Error.WrongAsyncResult();
            }
            if (result._EndXxxCalled)
            {
                __Error.EndWriteCalledTwice();
            }
            result._EndXxxCalled = true;
            if (result._exception != null)
            {
                throw result._exception;
            }
        }
Example #10
0
            internal static void EndWrite(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;

                if (ar == null || !ar._isWrite)
                {
                    throw new ArgumentException("WrongAsyncResult");
                }

                if (ar._endXxxCalled)
                {
                    throw new InvalidOperationException();
                }

                ar._endXxxCalled = true;

                ar.ThrowIfError();
            }
Example #11
0
            internal static void EndWrite(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;

                if (ar == null || !ar._isWrite)
                {
                    __Error.WrongAsyncResult();
                }

                if (ar._endXxxCalled)
                {
                    __Error.EndWriteCalledTwice();
                }

                ar._endXxxCalled = true;

                ar.ThrowIfError();
            }
Example #12
0
            internal static Int32 EndRead(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;

                if (ar == null || ar._isWrite)
                {
                    throw new InvalidOperationException();
                }

                if (ar._endXxxCalled)
                {
                    throw new InvalidOperationException();
                }

                ar._endXxxCalled = true;

                ar.ThrowIfError();
                return(ar._bytesRead);
            }
Example #13
0
        internal static int BlockingEndRead(IAsyncResult asyncResult)
        {
            SynchronousAsyncResult result = asyncResult as SynchronousAsyncResult;

            if ((result == null) || result.IsWrite)
            {
                __Error.WrongAsyncResult();
            }
            if (result._EndXxxCalled)
            {
                __Error.EndReadCalledTwice();
            }
            result._EndXxxCalled = true;
            if (result._exception != null)
            {
                throw result._exception;
            }
            return(result._numRead);
        }
Example #14
0
            internal static int EndRead(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult;

                if (ar == null || ar._isWrite)
                {
                    __Error.WrongAsyncResult();
                }

                if (ar._endXxxCalled)
                {
                    __Error.EndReadCalledTwice();
                }

                ar._endXxxCalled = true;

                ar.ThrowIfError();
                return(ar._bytesRead);
            }
Example #15
0
            public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
            {
                Task task = this.owner.capturedContext.WriteAndFlushAsync(Unpooled.WrappedBuffer(buffer, offset, count));

                switch (task.Status)
                {
                case TaskStatus.RanToCompletion:
                    // write+flush completed synchronously (and successfully)
                    var result = new SynchronousAsyncResult <int>();
                    result.AsyncState = state;
                    callback(result);
                    return(result);

                default:
                    this.writeCallback = callback;
                    var tcs = new TaskCompletionSource(state);
                    this.writeCompletion = tcs;
                    task.ContinueWith(WriteCompleteCallback, this, TaskContinuationOptions.ExecuteSynchronously);
                    return(tcs.Task);
                }
            }
            public override int EndRead(IAsyncResult asyncResult)
            {
                SynchronousAsyncResult <int> syncResult = this.syncReadResult;

                if (ReferenceEquals(asyncResult, syncResult))
                {
                    return(syncResult.Result);
                }

                Debug.Assert(this.readCompletionSource == null || this.readCompletionSource.Task == asyncResult);
                Contract.Assert(!((Task <int>)asyncResult).IsCanceled);

                try
                {
                    return(((Task <int>)asyncResult).Result);
                }
                catch (AggregateException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    throw; // unreachable
                }
            }
Example #17
0
        internal IAsyncResult BlockingBeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            SynchronousAsyncResult ar = new SynchronousAsyncResult(state, true);

            try
            {
                this.Write(buffer, offset, count);
            }
            catch (IOException exception)
            {
                ar._exception = exception;
            }
            finally
            {
                ar._isCompleted = true;
                ar._waitHandle.Set();
            }
            if (callback != null)
            {
                callback(ar);
            }
            return(ar);
        }
Example #18
0
        /// <include file='doc\Stream.uex' path='docs/doc[@for="Stream.BeginRead"]/*' />
        public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            if (!CanRead) __Error.ReadNotSupported();

            // To avoid a race with a stream's position pointer & generating race 
            // conditions with internal buffer indexes in our own streams that 
            // don't natively support async IO operations when there are multiple 
            // async requests outstanding, we will block the application's main
            // thread and do the IO synchronously.  
            SynchronousAsyncResult asyncResult = new SynchronousAsyncResult(state, false);
            try {
                int numRead = Read(buffer, offset, count);
                asyncResult._numRead = numRead;
                asyncResult._isCompleted = true;
                asyncResult._waitHandle.Set();
            }
            catch (IOException e) {
                asyncResult._exception = e;
            }
            
            if (callback != null)
                callback(asyncResult);

            return asyncResult;
        }
Example #19
0
 internal static void BlockingEndWrite(IAsyncResult asyncResult)
 {
     SynchronousAsyncResult.EndWrite(asyncResult);
 }
Example #20
0
 internal static int BlockingEndRead(IAsyncResult asyncResult)
 {
     return(SynchronousAsyncResult.EndRead(asyncResult));
 }
Example #21
0
        internal IAsyncResult BlockingBeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
        {
            // To avoid a race with a stream's position pointer & generating ---- 
            // conditions with internal buffer indexes in our own streams that 
            // don't natively support async IO operations when there are multiple 
            // async requests outstanding, we will block the application's main
            // thread and do the IO synchronously.  
            // This can't perform well - use a different approach.
            SynchronousAsyncResult asyncResult;
            try
            {
                Write(buffer, offset, count);
                asyncResult = new SynchronousAsyncResult(state);
            }
            catch (IOException ex)
            {
                asyncResult = new SynchronousAsyncResult(ex, state, isWrite: true);
            }

            if (callback != null)
            {
                callback(asyncResult);
            }

            return asyncResult;
        }
Example #22
0
 public MediationStream(TlsHandler owner)
 {
     this.syncReadResult = new SynchronousAsyncResult <int>();
     this.owner          = owner;
 }
 public MediationStream(CustomTlsHandler owner)
 {
     //Program.Trace.WriteLine($"[MediationStream] Ctor");
     this.syncReadResult = new SynchronousAsyncResult <int>();
     this.owner          = owner;
 }
Example #24
0
        internal static int BlockingEndRead(IAsyncResult asyncResult)
        {
            Contract.Ensures(Contract.Result <int>() >= 0);

            return(SynchronousAsyncResult.EndRead(asyncResult));
        }
 internal IAsyncResult BlockingBeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     SynchronousAsyncResult ar = new SynchronousAsyncResult(state, false);
     try
     {
         int num = this.Read(buffer, offset, count);
         ar._numRead = num;
     }
     catch (IOException exception)
     {
         ar._exception = exception;
     }
     finally
     {
         ar._isCompleted = true;
         ar._waitHandle.Set();
     }
     if (callback != null)
     {
         callback(ar);
     }
     return ar;
 }