Beispiel #1
0
            static void OnWriteComplete(TransportAsyncCallbackArgs args)
            {
                WriteAsyncResult thisPtr = (WriteAsyncResult)args.UserToken;

                thisPtr.completedSynchronously = args.CompletedSynchronously;
                thisPtr.Complete();
            }
Beispiel #2
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);
            }
 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;
 }