public void EndWriteMessage()
        {
            // This is just a temporary and ignored array used on the WriteFile calls.
            byte[] _numReadWritten = new byte[4];

            _writer.Flush();

            uint len = (uint)_stream.Length;

            //TODO: Replace BitConverter
            bool fOk = PipeNative.WriteFile(_handle,
                                            BitConverter.GetBytes(len),
                                            4,
                                            _numReadWritten,
                                            0);

            if (fOk)
            {
                fOk = PipeNative.WriteFile(_handle,
                                           _stream.GetBuffer(),
                                           len,
                                           _numReadWritten,
                                           0);
            }

            if (!fOk)
            {
                throw new PipeIOException("Error writing to pipe " + _handle + ": error " + PipeNative.GetLastError());
            }

            Flush();

            _stream = null;
            _writer = null;
        }
        private void Connect()
        {
            while (true)
            {
                _handle = PipeNative.CreateFile(_pipeName,
                                                PipeNative.GENERIC_READ | PipeNative.GENERIC_WRITE,
                                                0,
                                                null,
                                                PipeNative.OPEN_EXISTING,
                                                0,
                                                0);

                if (_handle != PipeNative.INVALID_HANDLE_VALUE)
                {
                    return;
                }

                if (PipeNative.GetLastError() != PipeNative.ERROR_PIPE_BUSY)
                {
                    throw new PipeIOException("Could not open pipe: " + _pipeName);
                }

                if (!PipeNative.WaitNamedPipe(_pipeName, 20000))
                {
                    throw new PipeIOException("Specified pipe was over-burdened: " + _pipeName);
                }
            }
        }
        public void Dispose()
        {
            if (_handle != 0)
            {
                PipeNative.FlushFileBuffers(_handle);
                PipeNative.DisconnectNamedPipe(_handle);
                PipeNative.CloseHandle(_handle);

                _handle = 0;

                DBG.Info("PipeChannel.Dispose", _pipeName);
            }
        }
        public void BeginReadMessage()
        {
            // This is just a temporary and ignored array used on the WriteFile calls.
            byte[] _numReadWritten = new byte[4];

            // TODO: Fix the pinvoke to eliminate these byte arrays
            byte[] intBytes = new byte[4];
            byte[] msgBytes = null;
            int    len;

            bool fOk = PipeNative.ReadFile(_handle,
                                           intBytes,
                                           4,
                                           _numReadWritten,
                                           0);

            if (fOk)
            {
                len = BitConverter.ToInt32(intBytes, 0);

                msgBytes = new byte[len];

                fOk = PipeNative.ReadFile(_handle,
                                          msgBytes,
                                          (uint)len,
                                          _numReadWritten,
                                          0);
            }

            if (!fOk)
            {
                throw new PipeIOException("Error reading from pipe " + _handle + ": error " + PipeNative.GetLastError());
            }

            _stream = new MemoryStream(msgBytes, false);

            if (_stream.CanRead)
            {
                string s = GetStringFromStream(_stream);
                if (s.Contains("Exception"))
                {
                    RemotingException rex = new RemotingException(s);
                    throw rex;
                }
            }

            _stream.Position = 0;
            _reader          = new BinaryReader(_stream, Encoding.UTF8);
        }
        private void Create(IntPtr pipeSecurityDescriptor)
        {
            _handle = PipeNative.CreateNamedPipe(_pipeName,
                                                 PipeNative.PIPE_ACCESS_DUPLEX,
                                                 PipeNative.PIPE_TYPE_BYTE | PipeNative.PIPE_READMODE_BYTE | PipeNative.PIPE_WAIT,
                                                 PipeNative.PIPE_UNLIMITED_INSTANCES,
                                                 8192,
                                                 8192,
                                                 PipeNative.NMPWAIT_WAIT_FOREVER,
                                                 pipeSecurityDescriptor);

            if (_handle == PipeNative.INVALID_HANDLE_VALUE)
            {
                throw new PipeIOException("Could not create the pipe (" + _pipeName + ") - os returned " + PipeNative.GetLastError());
            }
        }
 public uint GetLastError()
 {
     return(PipeNative.GetLastError());
 }
 public void Flush()
 {
     PipeNative.FlushFileBuffers(_handle);
 }
        //////////////////////////////////////////////////////////////

        public bool WaitForConnect()
        {
            bool fRet = PipeNative.ConnectNamedPipe(_handle, null);

            return(fRet ? true : (PipeNative.GetLastError() == PipeNative.ERROR_PIPE_CONNECTED));
        }