Exemplo n.º 1
0
        /// <summary>
        /// Accepts a pending connection request
        /// </summary>
        /// <remarks>
        /// Accept is a blocking method that returns a NamedPipeSocket you can use to send and receive data.
        /// </remarks>
        /// <returns>The NamedPipeSocket.</returns>
        public NamedPipeSocket Accept()
        {
            IntPtr hPipe = NamedPipeHelper.CreateNamedPipe(
                pipeName,
                NamedPipeHelper.PIPE_ACCESS_DUPLEX | NamedPipeHelper.FILE_FLAG_OVERLAPPED,
                NamedPipeHelper.PIPE_TYPE_MESSAGE
                | NamedPipeHelper.PIPE_READMODE_MESSAGE
                | NamedPipeHelper.PIPE_WAIT,
                NamedPipeHelper.PIPE_UNLIMITED_INSTANCES,
                DefaultBufferSize,
                DefaultBufferSize,
                NamedPipeHelper.NMPWAIT_USE_DEFAULT_WAIT,
                IntPtr.Zero
                );

            if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE)
            {
                throw new NamedPipeException(Marshal.GetLastWin32Error());
            }

            // Connect the named pipe with overlapped structure
            // in order to make it altertable. This way we will
            // wake up when someone aborts a thread waiting
            // for this pipe.
            NativeOverlapped overlapped = new NativeOverlapped();
            bool             canConnect = NamedPipeHelper.ConnectNamedPipe(hPipe, ref overlapped);

            int lastError = Marshal.GetLastWin32Error();

            if (!canConnect)
            {
                if (lastError == NamedPipeHelper.ERROR_IO_PENDING)
                {
                    uint bytesTransferred = 0;
                    if (!GetOverlappedResultEx(hPipe, ref overlapped, out bytesTransferred, Timeout.Infinite, true))
                    {
                        lastError = Marshal.GetLastWin32Error();
                        NamedPipeHelper.CloseHandle(hPipe);
                        throw new NamedPipeException(lastError);
                    }
                    canConnect = true;
                }
                else if (lastError == NamedPipeHelper.ERROR_PIPE_CONNECTED)
                {
                    canConnect = true;
                }
            }

            if (!canConnect)
            {
                NamedPipeHelper.CloseHandle(hPipe);
                throw new NamedPipeException(lastError);
            }

            return(new NamedPipeSocket(hPipe));
        }
        /// <summary>
        /// Disposes the object.
        /// </summary>
        void IDisposable.Dispose()
        {
            if (hPipe != IntPtr.Zero)
            {
                try
                {
                    // disconnect the pipe
                    if (Info.IsServer)
                    {
                        NamedPipeHelper.FlushFileBuffers(hPipe);
                        NamedPipeHelper.DisconnectNamedPipe(hPipe);
                    }
                }
                catch (NamedPipeException)
                {
                }

                NamedPipeHelper.CloseHandle(hPipe);
                hPipe = IntPtr.Zero;
                GC.SuppressFinalize(this);
            }
        }
        /// <summary>
        /// Accepts a pending connection request
        /// </summary>
        /// <remarks>
        /// Accept is a blocking method that returns a NamedPipeSocket you can use to send and receive data.
        /// </remarks>
        /// <returns>The NamedPipeSocket.</returns>
        public NamedPipeSocket Accept()
        {
            IntPtr hPipe = NamedPipeHelper.CreateNamedPipe(
                pipeName,
                NamedPipeHelper.PIPE_ACCESS_DUPLEX,
                NamedPipeHelper.PIPE_TYPE_MESSAGE
                | NamedPipeHelper.PIPE_READMODE_MESSAGE
                | NamedPipeHelper.PIPE_WAIT,
                NamedPipeHelper.PIPE_UNLIMITED_INSTANCES,
                DefaultBufferSize,
                DefaultBufferSize,
                NamedPipeHelper.NMPWAIT_USE_DEFAULT_WAIT,
                IntPtr.Zero
                );

            if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE)
            {
                throw new NamedPipeException();
            }

            bool canConnect = NamedPipeHelper.ConnectNamedPipe(hPipe, IntPtr.Zero);
            int  lastError  = Marshal.GetLastWin32Error();

            if (!canConnect && lastError == NamedPipeHelper.ERROR_PIPE_CONNECTED)
            {
                canConnect = true;
            }

            if (canConnect)
            {
                return(new NamedPipeSocket(hPipe));
            }
            else
            {
                NamedPipeHelper.CloseHandle(hPipe);
                throw new NamedPipeException(lastError);
            }
        }