Пример #1
0
        /// <summary>
        /// Helper method to construct an asyncResult object and use it to
        /// call NativeControl(). Called by BeginAsyncControl().
        /// </summary>
        private static DeviceAsyncResult <T> AsyncControl <T>(
            SafeFileHandle device,
            int controlCode,
            T outBuffer,
            AsyncCallback asyncCallback,
            object state
            )
        {
            SafePinnedObject outDeviceBuffer = null;

            if (outBuffer != null)
            {
                outDeviceBuffer = new SafePinnedObject(outBuffer);
            }

            // Create the async result object
            DeviceAsyncResult <T> asyncResult = new DeviceAsyncResult <T>(outDeviceBuffer,
                                                                          asyncCallback,
                                                                          state
                                                                          );

            unsafe
            {
                uint bytesReturned = 0;
                NativeAsyncControl(device,
                                   controlCode,
                                   outDeviceBuffer,
                                   ref bytesReturned,
                                   asyncResult.GetNativeOverlapped()
                                   );
            }

            return(asyncResult);
        }
Пример #2
0
        /// <summary>
        /// Method to initiate an ASYNCHRONOUS device I/O control operation to
        /// get a packet from the driver, whenever it may arrive.
        ///
        /// The device handle MUST have been opened with the async option set
        /// to TRUE prior to calling this method.
        /// </summary>
        /// <returns></returns>
        public static IAsyncResult BeginGetPacketFromDriverAsync <TResult>(
            SafeFileHandle device,
            int controlCode,
            Int32 maxElements,
            AsyncCallback asyncCallback,
            object state
            )
        {
            // Error checking
            if (device == null)
            {
                throw new InvalidEnumArgumentException("Device handle was null");
            }

            if (maxElements > 1280)
            {
                return(null);
            }
            // Construct the output buffer; that is, the packet. Shouldn't be
            // more than 1280 bytes or the driver will reject the request.
            byte[] packet = new byte[maxElements];
            DeviceAsyncResult <byte[]> asyncResult = AsyncControl(device,
                                                                  controlCode,
                                                                  packet,
                                                                  asyncCallback,
                                                                  state
                                                                  );

            return(asyncResult);
        }
Пример #3
0
        /// <summary>
        /// Ends asynchronous device I/O control operation to get a packet from
        /// the driver.
        ///
        /// The reciprocal of BeginGetPacketFromDriverAsync().
        /// </summary>
        /// <param name="result"></param>
        public static byte[] EndGetPacketFromDriverAsync <TResult>(
            IAsyncResult result
            )
        {
            DeviceAsyncResult <byte[]> asyncResult = (DeviceAsyncResult <byte[]>)result;

            byte[] packet = asyncResult.EndInvoke();
            return(packet);
        }