Exemple #1
0
        /// <summary>
        /// Initiates receive operation
        /// </summary>
        public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (_instance == null)
            {
                throw new InvalidOperationException("Pipe is not connected");
            }

            // parameters validation
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("size");
            }

            // read data from the pipe
            GCHandle gcBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            PipeOverlappedAsyncResult asyncResult = new PipeOverlappedAsyncResult(gcBuffer, callback, state);

            int error = asyncResult.CheckForCompletion(PipeNative.ReadFile(
                                                           _instance.Handle,
                                                           Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset),
                                                           size,
                                                           IntPtr.Zero,
                                                           asyncResult.OverlappedHandle));

            switch (error)
            {
            case Win32.ERROR_SUCCESS:
                // operation completed synchronously
                break;

            case PipeNative.ERROR_IO_PENDING:
                // async operation was pended
                break;

            default:
                // error occured
                asyncResult.Dispose();
                throw new PipeIOException(error, "Could not read data from the pipe: " + _instance.Name);
            }

            return(asyncResult);
        }
Exemple #2
0
        /// <summary>
        /// Receives data from the pipe
        /// </summary>
        public int Receive(byte[] buffer, int offset, int size)
        {
            // check object state
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (_instance == null)
            {
                throw new InvalidOperationException("Pipe is not connected");
            }

            // parameters validation
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("size");
            }

            // read data from the pipe
            GCHandle gcBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            int      bytesRead;

            bool error = PipeNative.ReadFile(
                _instance.Handle,
                Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset),
                size,
                out bytesRead,
                IntPtr.Zero);

            gcBuffer.Free();

            if (!error)
            {
                // error occured
                throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not read data from the pipe: " + _instance.Name);
            }

            return(bytesRead);
        }