internal static void End(IAsyncResult result)
            {
                WriteAsyncResult thisPtr = (WriteAsyncResult)result;

                thisPtr.InternalWaitForCompletion();
                System.Diagnostics.Debug.Assert(thisPtr.written == thisPtr.count);
            }
Esempio n. 2
0
            internal static void End(IAsyncResult result)
            {
                WriteAsyncResult thisPtr = (WriteAsyncResult)result;

                thisPtr.InternalWaitForCompletion();
                Debug.Assert(thisPtr._written == thisPtr._count);
            }
Esempio n. 3
0
            static void OnWriteComplete(TransportAsyncCallbackArgs args)
            {
                WriteAsyncResult thisPtr = (WriteAsyncResult)args.UserToken;

                thisPtr.completedSynchronously = args.CompletedSynchronously;
                thisPtr.Complete();
            }
Esempio n. 4
0
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            var result = new WriteAsyncResult { AsyncState = state };
            var delayed = _write(new ArraySegment<byte>(buffer, offset, count));

            if (delayed)
            {
                delayed = _flush(() =>
                {
                    result.IsCompleted = true;
                    if (callback != null)
                    {
                        try { callback(result); }
                        catch { }
                    }
                });
            }

            if (!delayed)
            {
                result.CompletedSynchronously = true;
                result.IsCompleted = true;
                if (callback != null)
                {
                    try { callback(result); }
                    catch { }
                }
            }

            return result;
        }
Esempio n. 5
0
 public override void EndWrite(IAsyncResult asyncResult)
 {
     if (asyncResult == null)
     {
         throw new ArgumentNullException("asyncResult");
     }
     WriteAsyncResult.End(asyncResult);
 }
Esempio n. 6
0
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback?callback, object?state)
        {
            ValidateBufferArguments(buffer, offset, count);

            WriteAsyncResult result = new WriteAsyncResult(this, buffer, offset, count, callback, state);

            result.Write();
            return(result);
        }
Esempio n. 7
0
        public WebSocketNetworkStream(Socket socket) : base(socket, ownsSocket: true)
        {
            m_socket = socket;

            m_readAsyncResult     = new ReadAsyncResult(this);
            m_readArgs            = new SocketAsyncEventArgs();
            m_readArgs.Completed += OnReadCompleted;

            m_writeAsyncResult     = new WriteAsyncResult(this);
            m_writeArgs            = new SocketAsyncEventArgs();
            m_writeArgs.Completed += OnWriteCompleted;
        }
Esempio n. 8
0
 private static void OnWrite(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         WriteAsyncResult thisPtr = (WriteAsyncResult)result.AsyncState;
         try
         {
             thisPtr.CompleteWrite(result);
             thisPtr.Write();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if ((offset < 0) || (offset > buffer.Length))
     {
         throw new ArgumentOutOfRangeException("offset");
     }
     if ((offset + count) > buffer.Length)
     {
         throw new ArgumentOutOfRangeException("count");
     }
     WriteAsyncResult result = new WriteAsyncResult(this, buffer, offset, count, callback, state);
     result.Write();
     return result;
 }
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if ((offset < 0) || (offset > buffer.Length))
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if ((offset + count) > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            WriteAsyncResult result = new WriteAsyncResult(this, buffer, offset, count, callback, state);

            result.Write();
            return(result);
        }
Esempio n. 11
0
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            var result = new WriteAsyncResult(this, buffer, offset, count, callback, state);

            result.Write();
            return(result);
        }
Esempio n. 12
0
            static void OnWriteComplete(IAsyncResult result)
            {
                if (result.CompletedSynchronously)
                {
                    return;
                }
                WriteAsyncResult thisPtr             = result.AsyncState as WriteAsyncResult;
                Exception        completionException = null;

                try
                {
                    thisPtr.HandleWriteComplete(result);
                }
                catch (Exception ex)
                {
                    completionException = ex;
                }

                thisPtr.Complete(false, completionException);
            }
Esempio n. 13
0
            public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
            {
                if (buffer == null)
                {
                    throw new ArgumentNullException("buffer");
                }
                if (offset >= buffer.Length)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }
                if (offset + count > buffer.Length)
                {
                    throw new ArgumentOutOfRangeException("count");
                }

                WriteAsyncResult ar = new WriteAsyncResult(callback, state);

                this.owner.TryCompleteWriteRequest(buffer, offset, count, ar);
                return(ar);
            }
Esempio n. 14
0
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            var result = new WriteAsyncResult(this, buffer, offset, count, callback, state);
            result.Write();
            return result;
        }
Esempio n. 15
0
 public TransportStream(TransportBase transport)
 {
     this.transport   = transport;
     this.readResult  = new ReadAsyncResult(this.transport);
     this.writeResult = new WriteAsyncResult(this.transport);
 }
Esempio n. 16
0
            public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
            {
                if (buffer == null)
                {
                    throw new ArgumentNullException("buffer");
                }
                if (offset >= buffer.Length)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }
                if (offset + count > buffer.Length)
                {
                    throw new ArgumentOutOfRangeException("count");
                }

                WriteAsyncResult ar = new WriteAsyncResult(callback, state);
                this.owner.TryCompleteWriteRequest(buffer, offset, count, ar);
                return ar;
            }
Esempio n. 17
0
 static void OnWriteComplete(IAsyncResult result)
 {
     WriteAsyncResult.End(result);
 }
Esempio n. 18
0
 public TransportStream(TransportBase transport)
 {
     this.transport = transport;
     this.readResult = new ReadAsyncResult(this.transport);
     this.writeResult = new WriteAsyncResult(this.transport);
 }
 private unsafe void OnAsyncWriteComplete(bool haveResult, int error, int numBytes)
 {
     WriteAsyncResult writeAsyncResult = this.writeAsyncResult;
     this.writeAsyncResult = null;
     if (writeAsyncResult == null)
     {
         throw Fx.AssertAndThrow("Write completed with no WriteAsyncResult available.");
     }
     Exception e = null;
     this.WriteTimer.Cancel();
     lock (this.writeLock)
     {
         try
         {
             try
             {
                 if (this.closeState == CloseState.HandleClosed)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.CreatePipeClosedException(TransferOperation.Write));
                 }
                 if (!haveResult)
                 {
                     if (UnsafeNativeMethods.GetOverlappedResult(this.pipe.DangerousGetHandle(), this.writeOverlapped.NativeOverlapped, out numBytes, 0) == 0)
                     {
                         error = Marshal.GetLastWin32Error();
                     }
                     else
                     {
                         error = 0;
                     }
                 }
                 if (error != 0)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Exceptions.CreateWriteException(error));
                 }
                 if (numBytes != writeAsyncResult.WriteSize)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PipeException(System.ServiceModel.SR.GetString("PipeWriteIncomplete")));
                 }
             }
             catch (PipeException exception2)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertPipeException(exception2, TransferOperation.Write), this.ExceptionEventType);
             }
         }
         catch (Exception exception3)
         {
             if (Fx.IsFatal(exception3))
             {
                 throw;
             }
             e = exception3;
         }
         finally
         {
             this.isWriteOutstanding = false;
             this.WriteIOCompleted();
             this.ExitWritingState();
         }
     }
     writeAsyncResult.Complete(false, e);
 }
 public unsafe IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state)
 {
     IAsyncResult result2;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     this.FinishPendingWrite(timeout);
     ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
     if (this.autoBindToCompletionPort && !this.isBoundToCompletionPort)
     {
         lock (this.readLock)
         {
             lock (this.writeLock)
             {
                 this.ValidateEnterWritingState(true);
                 this.EnsureBoundToCompletionPort();
             }
         }
     }
     lock (this.writeLock)
     {
         try
         {
             this.ValidateEnterWritingState(true);
             if (this.isWriteOutstanding)
             {
                 throw Fx.AssertAndThrow("Write I/O already pending when BeginWrite called.");
             }
             WriteAsyncResult result = new WriteAsyncResult(callback, state, size);
             try
             {
                 this.writeTimeout = timeout;
                 this.WriteTimer.Set(helper.RemainingTime());
                 this.writeAsyncResult = result;
                 this.isWriteOutstanding = true;
                 this.writeOverlapped.StartAsyncOperation(buffer, this.onAsyncWriteComplete, this.isBoundToCompletionPort);
                 if (UnsafeNativeMethods.WriteFile(this.pipe.DangerousGetHandle(), this.writeOverlapped.BufferPtr + offset, size, IntPtr.Zero, this.writeOverlapped.NativeOverlapped) == 0)
                 {
                     int error = Marshal.GetLastWin32Error();
                     if (error != 0x3e5)
                     {
                         this.isWriteOutstanding = false;
                         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Exceptions.CreateWriteException(error));
                     }
                 }
             }
             finally
             {
                 if (!this.isWriteOutstanding)
                 {
                     this.writeOverlapped.CancelAsyncOperation();
                     this.writeAsyncResult = null;
                     this.WriteTimer.Cancel();
                 }
             }
             if (!this.isWriteOutstanding)
             {
                 int num2;
                 Exception exception = Exceptions.GetOverlappedWriteException(this.pipe, this.writeOverlapped.NativeOverlapped, out num2);
                 if ((exception == null) && (num2 != size))
                 {
                     exception = new PipeException(System.ServiceModel.SR.GetString("PipeWriteIncomplete"));
                 }
                 if (exception != null)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
                 }
                 result.Complete(true);
             }
             else
             {
                 this.EnterWritingState();
             }
             result2 = result;
         }
         catch (PipeException exception2)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertPipeException(exception2, TransferOperation.Write), this.ExceptionEventType);
         }
     }
     return result2;
 }
Esempio n. 21
0
        public override void EndWrite(IAsyncResult asyncResult)
        {
            ArgumentNullException.ThrowIfNull(asyncResult);

            WriteAsyncResult.End(asyncResult);
        }
Esempio n. 22
0
 public override void EndWrite(IAsyncResult asyncResult)
 {
     WriteAsyncResult.End(asyncResult);
 }
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            Debug.WriteLine("BNS ({0}): BeginWrite (buffer, {1}, {2}) [m_outputBuffer_pos = {3}]", NetworkStream.InternalSocket.InternalRemoteEndPoint, offset, count,
                            m_outputBuffer_pos);

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer is null");
            }

            int len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
            }
            if (count < 0 || offset + count > len)
            {
                throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
            }

            CheckObjectDisposedException();

            if (!m_stream.CanWrite)
            {
                throw new NotSupportedException(Locale.GetText("Cannot write to stream"));
            }

            if (m_nagleTimer == null)
            {
                return(m_stream.BeginWrite(buffer, offset, count, callback, state));
            }

            var avail = m_bufferSize - m_outputBuffer_pos;

            if (count <= avail)
            {
                EnsureOutputBuffer();

                m_lockFlush.Acquire();
                try
                {
                    Buffer.BlockCopy(buffer, offset, m_outputBuffer, m_outputBuffer_pos, count);
                    m_outputBuffer_pos += count;

                    if (m_outputBuffer_pos == m_bufferSize)
                    {
                        m_nagleTimer.Stop();

                        m_outputBuffer_pos = 0;

                        return(m_stream.BeginWrite(m_outputBuffer, 0, m_bufferSize, callback, state));
                    }

                    m_nagleTimer.Reset(200);

                    var wsr = new WriteSyncResult
                    {
                        AsyncState = state
                    };

                    if (callback != null)
                    {
                        callback.BeginInvokeEx(wsr, null, null);
                    }

                    return(wsr);
                }
                finally
                {
                    m_lockFlush.Release();
                }
            }

            if (m_outputBuffer_pos == 0 && count > m_bufferSize)
            {
                return(m_stream.BeginWrite(buffer, offset, count, callback, state));
            }

            if (m_inputBuffer_pos != 0)
            {
                m_lockFlush.Acquire();
                try
                {
                    if (m_outputBuffer_pos != 0)
                    {
                        m_nagleTimer.Stop();

                        if (count <= m_bufferSize)
                        {
                            var tbuffer = new byte[m_outputBuffer_pos + count];
                            Buffer.BlockCopy(m_outputBuffer, 0, tbuffer, 0, m_outputBuffer_pos);
                            Buffer.BlockCopy(buffer, offset, tbuffer, m_outputBuffer_pos, count);

                            m_outputBuffer_pos = 0;

                            return(m_stream.BeginWrite(tbuffer, 0, tbuffer.Length, callback, state));
                        }

                        var war = new WriteAsyncResult
                        {
                            AsyncState = state
                        };

                        m_stream.BeginWrite(m_outputBuffer, 0, m_outputBuffer_pos, iar =>
                        {
                            var ws = (WriteState)iar.AsyncState;

                            m_stream.EndWrite(iar);

                            m_stream.BeginWrite(ws.buffer, ws.offset, ws.count, iar2 =>
                            {
                                var ws2 = (WriteState)iar.AsyncState;

                                m_stream.EndWrite(iar2);

                                ws2.writeAsyncResult.IsCompleted = true;
                                ((CEvent)ws2.writeAsyncResult.AsyncWaitHandle).Set();

                                if (ws2.callback != null)
                                {
                                    try
                                    {
                                        ws2.callback(ws2.writeAsyncResult);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }, ws);
                        }, new WriteState
                        {
                            writeAsyncResult = war,
                            callback         = callback,
                            buffer           = buffer,
                            offset           = offset,
                            count            = count,
                        });

                        m_outputBuffer_pos = 0;

                        return(war);
                    }
                }
                finally
                {
                    m_lockFlush.Release();
                }
            }

            return(m_stream.BeginWrite(buffer, offset, count, callback, state));
        }