private IConnection BuildDuplicatedNamedPipeConnection(NamedPipeDuplicateContext duplicateContext, int connectionBufferSize)
            {
                if (DiagnosticUtility.ShouldTraceVerbose)
                {
                    TraceUtility.TraceEvent(TraceEventType.Verbose, 0xa0002, System.ServiceModel.SR.GetString("TraceCodePortSharingDuplicatedPipe"));
                }
                PipeHandle     pipe            = new PipeHandle(duplicateContext.Handle);
                PipeConnection innerConnection = new PipeConnection(pipe, connectionBufferSize, false, true);

                return(new NamedPipeValidatingConnection(new PreReadConnection(innerConnection, duplicateContext.ReadData), this));
            }
Example #2
0
        private IConnection TryConnect(Uri remoteUri, string resolvedAddress, BackoffTimeoutHelper backoffHelper)
        {
            bool flag = backoffHelper.IsExpired();
            int  dwFlagsAndAttributes = 0x40000000;

            if (this.includeSecurityIdentity)
            {
                dwFlagsAndAttributes |= 0x110000;
            }
            PipeHandle handle    = UnsafeNativeMethods.CreateFile(resolvedAddress, -1073741824, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero);
            int        errorCode = Marshal.GetLastWin32Error();

            if (handle.IsInvalid)
            {
                handle.SetHandleAsInvalid();
            }
            else
            {
                int mode = 2;
                if (UnsafeNativeMethods.SetNamedPipeHandleState(handle, ref mode, IntPtr.Zero, IntPtr.Zero) == 0)
                {
                    errorCode = Marshal.GetLastWin32Error();
                    handle.Close();
                    PipeException exception = new PipeException(System.ServiceModel.SR.GetString("PipeModeChangeFailed", new object[] { PipeError.GetErrorString(errorCode) }), errorCode);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.CreateConnectFailedException(remoteUri, exception));
                }
                return(new PipeConnection(handle, this.bufferSize, false, true));
            }
            if ((errorCode == 2) || (errorCode == 0xe7))
            {
                TimeoutException exception3;
                if (!flag)
                {
                    return(null);
                }
                Exception exception2  = new PipeException(System.ServiceModel.SR.GetString("PipeConnectAddressFailed", new object[] { resolvedAddress, PipeError.GetErrorString(errorCode) }), errorCode);
                string    absoluteUri = remoteUri.AbsoluteUri;
                if (errorCode == 0xe7)
                {
                    exception3 = new TimeoutException(System.ServiceModel.SR.GetString("PipeConnectTimedOutServerTooBusy", new object[] { absoluteUri, backoffHelper.OriginalTimeout }), exception2);
                }
                else
                {
                    exception3 = new TimeoutException(System.ServiceModel.SR.GetString("PipeConnectTimedOut", new object[] { absoluteUri, backoffHelper.OriginalTimeout }), exception2);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception3);
            }
            PipeException innerException = new PipeException(System.ServiceModel.SR.GetString("PipeConnectAddressFailed", new object[] { resolvedAddress, PipeError.GetErrorString(errorCode) }), errorCode);

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.CreateConnectFailedException(remoteUri, innerException));
        }
        public IConnection EndAccept(IAsyncResult result)
        {
            PendingAccept accept = result as PendingAccept;

            if (accept == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("result", System.ServiceModel.SR.GetString("InvalidAsyncResult"));
            }
            PipeHandle pipe = accept.End();

            if (pipe == null)
            {
                return(null);
            }
            return(new PipeConnection(pipe, this.bufferSize, accept.IsBoundToCompletionPort, accept.IsBoundToCompletionPort));
        }
Example #4
0
        public PipeConnection(PipeHandle pipe, int connectionBufferSize, bool isBoundToCompletionPort, bool autoBindToCompletionPort)
        {
            if (pipe == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pipe");
            if (pipe.IsInvalid)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pipe");

            this.closeState = CloseState.Open;
            this.exceptionEventType = TraceEventType.Error;
            this.isBoundToCompletionPort = isBoundToCompletionPort;
            this.autoBindToCompletionPort = autoBindToCompletionPort;
            this.pipe = pipe;
            this.readBufferSize = connectionBufferSize;
            this.writeBufferSize = connectionBufferSize;
            this.readOverlapped = new OverlappedContext();
            this.asyncReadBuffer = DiagnosticUtility.Utility.AllocateByteArray(connectionBufferSize);
            this.writeOverlapped = new OverlappedContext();
            this.atEOFEvent = new ManualResetEvent(false);
            this.onAsyncReadComplete = new OverlappedIOCompleteCallback(OnAsyncReadComplete);
            this.onAsyncWriteComplete = new OverlappedIOCompleteCallback(OnAsyncWriteComplete);
        }
 public PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort, AsyncCallback callback, object state) : base(callback, state)
 {
     this.pipeHandle              = pipeHandle;
     this.result                  = pipeHandle;
     this.listener                = listener;
     this.onAcceptComplete        = new OverlappedIOCompleteCallback(this.OnAcceptComplete);
     this.overlapped              = new OverlappedContext();
     this.isBoundToCompletionPort = isBoundToCompletionPort;
     if (!Thread.CurrentThread.IsThreadPoolThread)
     {
         if (onStartAccept == null)
         {
             onStartAccept = new Action <object>(PipeConnectionListener.PendingAccept.OnStartAccept);
         }
         ActionItem.Schedule(onStartAccept, this);
     }
     else
     {
         this.StartAccept(true);
     }
 }
 public IAsyncResult BeginAccept(AsyncCallback callback, object state)
 {
     lock (this.ThisLock)
     {
         if (this.isDisposed)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException("", System.ServiceModel.SR.GetString("PipeListenerDisposed")));
         }
         if (!this.isListening)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("PipeListenerNotListening")));
         }
         PipeHandle    pipeHandle = this.CreatePipe();
         PendingAccept item       = new PendingAccept(this, pipeHandle, this.useCompletionPort, callback, state);
         if (!item.CompletedSynchronously)
         {
             this.pendingAccepts.Add(item);
         }
         return(item);
     }
 }
 internal static extern unsafe int ConnectNamedPipe(PipeHandle handle, NativeOverlapped *lpOverlapped);
 internal static extern int SetNamedPipeHandleState(PipeHandle handle, ref int mode, IntPtr collectionCount, IntPtr collectionDataTimeout);
Example #9
0
            public static unsafe PipeException GetOverlappedReadException(PipeHandle pipe,
                NativeOverlapped* nativeOverlapped, out int bytesRead)
            {
                if (UnsafeNativeMethods.GetOverlappedResult(pipe.DangerousGetHandle(), nativeOverlapped, out bytesRead, 0) == 0)
                {
                    int error = Marshal.GetLastWin32Error();
                    if (error == UnsafeNativeMethods.ERROR_MORE_DATA)
                    {
                        return null;
                    }

                    else
                    {
                        return Exceptions.CreateReadException(error);
                    }
                }
                else
                {
                    return null;
                }
            }
 internal static extern int SetNamedPipeHandleState
 (
     PipeHandle handle,
     ref int mode,
     IntPtr collectionCount,
     IntPtr collectionDataTimeout
 );
Example #11
0
            public unsafe PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort,
                AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.pipeHandle = pipeHandle;
                this.result = pipeHandle;
                this.listener = listener;
                onAcceptComplete = new OverlappedIOCompleteCallback(OnAcceptComplete);
                overlapped = new OverlappedContext();
                this.isBoundToCompletionPort = isBoundToCompletionPort;

                if (TD.PipeConnectionAcceptStartIsEnabled())
                {
                    this.eventTraceActivity = new EventTraceActivity();
                    TD.PipeConnectionAcceptStart(this.eventTraceActivity, this.listener.pipeUri != null ? this.listener.pipeUri.ToString() : string.Empty);
                }

                if (!Thread.CurrentThread.IsThreadPoolThread)
                {
                    if (onStartAccept == null)
                    {
                        onStartAccept = new Action<object>(OnStartAccept);
                    }
                    ActionItem.Schedule(onStartAccept, this);
                }
                else
                {
                    StartAccept(true);
                }
            }
 internal unsafe static extern int ConnectNamedPipe
 (
     PipeHandle handle,
     NativeOverlapped* lpOverlapped
 );
 internal static extern bool DuplicateHandle(
     IntPtr hSourceProcessHandle,
     PipeHandle hSourceHandle,
     SafeCloseHandle hTargetProcessHandle,
     out IntPtr lpTargetHandle,
     int dwDesiredAccess,
     bool bInheritHandle,
     int dwOptions
 );
 public PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort, AsyncCallback callback, object state) : base(callback, state)
 {
     this.pipeHandle = pipeHandle;
     this.result = pipeHandle;
     this.listener = listener;
     this.onAcceptComplete = new OverlappedIOCompleteCallback(this.OnAcceptComplete);
     this.overlapped = new OverlappedContext();
     this.isBoundToCompletionPort = isBoundToCompletionPort;
     if (!Thread.CurrentThread.IsThreadPoolThread)
     {
         if (onStartAccept == null)
         {
             onStartAccept = new Action<object>(PipeConnectionListener.PendingAccept.OnStartAccept);
         }
         ActionItem.Schedule(onStartAccept, this);
     }
     else
     {
         this.StartAccept(true);
     }
 }
 public void Abort()
 {
     this.result = null;
     this.pipeHandle.Close();
 }
 public static unsafe PipeException GetOverlappedWriteException(PipeHandle pipe, NativeOverlapped* nativeOverlapped, out int bytesWritten)
 {
     if (UnsafeNativeMethods.GetOverlappedResult(pipe.DangerousGetHandle(), nativeOverlapped, out bytesWritten, 0) == 0)
     {
         return CreateWriteException(Marshal.GetLastWin32Error());
     }
     return null;
 }
 public static unsafe PipeException GetOverlappedReadException(PipeHandle pipe, NativeOverlapped* nativeOverlapped, out int bytesRead)
 {
     if (UnsafeNativeMethods.GetOverlappedResult(pipe.DangerousGetHandle(), nativeOverlapped, out bytesRead, 0) != 0)
     {
         return null;
     }
     int error = Marshal.GetLastWin32Error();
     if (error == 0xea)
     {
         return null;
     }
     return CreateReadException(error);
 }
Example #18
0
 // Must be called in PipeConnectionListener's lock.
 public void Abort()
 {
     this.result = null; // we need to return null after an abort
     pipeHandle.Close();
 }
 internal static extern int DisconnectNamedPipe(PipeHandle handle);
 internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, PipeHandle hSourceHandle, SafeCloseHandle hTargetProcessHandle, out IntPtr lpTargetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions);
 public void Abort()
 {
     this.result = null;
     this.pipeHandle.Close();
 }
 internal static extern bool GetNamedPipeClientProcessId(PipeHandle handle, out int id);
 internal unsafe static extern int DisconnectNamedPipe
 (
     PipeHandle handle
 );
 internal static extern bool GetNamedPipeServerProcessId(PipeHandle handle, out int id);
 unsafe internal static extern int GetOverlappedResult
 (
     PipeHandle handle,
     NativeOverlapped* overlapped,
     out int bytesTransferred,
     int wait
 );
 internal static extern unsafe int GetOverlappedResult(PipeHandle handle, NativeOverlapped *overlapped, out int bytesTransferred, int wait);
 internal static unsafe extern bool GetNamedPipeServerProcessId(PipeHandle handle, out int id);
 private IConnection BuildDuplicatedNamedPipeConnection(NamedPipeDuplicateContext duplicateContext, int connectionBufferSize)
 {
     if (DiagnosticUtility.ShouldTraceVerbose)
     {
         TraceUtility.TraceEvent(TraceEventType.Verbose, 0xa0002, System.ServiceModel.SR.GetString("TraceCodePortSharingDuplicatedPipe"));
     }
     PipeHandle pipe = new PipeHandle(duplicateContext.Handle);
     PipeConnection innerConnection = new PipeConnection(pipe, connectionBufferSize, false, true);
     return new NamedPipeValidatingConnection(new PreReadConnection(innerConnection, duplicateContext.ReadData), this);
 }