private static void OnSyncCompletion(object state)
        {
            PipeOverlappedAsyncResult asyncResult = (PipeOverlappedAsyncResult)state;

            // invoke client callback
            asyncResult.AsyncCallback(asyncResult);
        }
Example #2
0
        /// <summary>
        /// Completes send operation
        /// </summary>
        public void EndSend(IAsyncResult result)
        {
            // parameters validation
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            // get async result
            PipeOverlappedAsyncResult asyncResult = (result as PipeOverlappedAsyncResult);

            if (asyncResult == null)
            {
                throw new ArgumentException("result");
            }

            // wait for completion
            int error = asyncResult.WaitForCompletion();

            if (error != Win32.ERROR_SUCCESS)
            {
                // error occured
                throw new PipeIOException(error, "Could not write data to the pipe: " + _instance.Name);
            }
        }
Example #3
0
        /// <summary>
        /// Completes receive operation
        /// </summary>
        public int EndReceive(IAsyncResult result)
        {
            // parameters validation
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            // get async result
            PipeOverlappedAsyncResult asyncResult = (result as PipeOverlappedAsyncResult);

            if (asyncResult == null)
            {
                throw new ArgumentException("result");
            }

            // wait for completion
            int error = asyncResult.WaitForCompletion();

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

            return(asyncResult.TotalBytes);
        }
Example #4
0
        /// <summary>
        /// Initiates send operation
        /// </summary>
        public IAsyncResult BeginSend(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");
            }

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

            int error = asyncResult.CheckForCompletion(PipeNative.WriteFile(
                                                           _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 write data to the pipe: " + _instance.Name);
            }

            return(asyncResult);
        }
        private static void OnAsyncCompletion(object state, bool signaled)
        {
            PipeOverlappedAsyncResult asyncResult = (PipeOverlappedAsyncResult)state;

            asyncResult.SetCompleted(false);

            if (asyncResult.AsyncCallback != null)
            {
                // invoke client callback
                asyncResult.AsyncCallback(asyncResult);
            }
        }
Example #6
0
        public void WaitForClientConnection()
        {
            // check object state
            if (_isConnected)
            {
                throw new InvalidOperationException("Pipe is already connected");
            }

            // connect to the client
            PipeOverlappedAsyncResult asyncResult = new PipeOverlappedAsyncResult();
            int error = asyncResult.CheckForCompletion(PipeNative.ConnectNamedPipe(
                                                           _handle,
                                                           asyncResult.OverlappedHandle));

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

            case PipeNative.ERROR_PIPE_CONNECTED:
                // client already connected
                break;

            case PipeNative.ERROR_IO_PENDING:
                // async operation was pended
                asyncResult.WaitForCompletion();
                break;

            default:
                // error occured
                throw new PipeIOException(error, "Unable to connect client to the pipe: " + _pipeName);
            }

            _isConnected = true;
        }
Example #7
0
		/// <summary>
		/// Initiates send operation
		/// </summary>
		public IAsyncResult BeginSend(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");

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

			int error = asyncResult.CheckForCompletion(PipeNative.WriteFile(
				_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 write data to the pipe: " + _instance.Name);
			}

			return asyncResult;
		}
Example #8
0
		public void WaitForClientConnection()
		{
			// check object state
			if (_isConnected)
				throw new InvalidOperationException("Pipe is already connected");

			// connect to the client
			PipeOverlappedAsyncResult asyncResult = new PipeOverlappedAsyncResult();
			int error = asyncResult.CheckForCompletion(PipeNative.ConnectNamedPipe(
				_handle,
				asyncResult.OverlappedHandle));

			switch (error)
			{
				case Win32.ERROR_SUCCESS:
					// operation completed synchronously
					break;
				case PipeNative.ERROR_PIPE_CONNECTED:
					// client already connected
					break;
				case PipeNative.ERROR_IO_PENDING:
					// async operation was pended
					asyncResult.WaitForCompletion();
					break;
				default:
					// error occured
					throw new PipeIOException(error, "Unable to connect client to the pipe: " + _pipeName);
			}

			_isConnected = true;
		}