예제 #1
0
        /// <summary>
        /// Returns an array of Wintab data packets from the packet queue.
        /// </summary>
        /// <param name="maxPkts_I">Specifies the maximum number of packets to return.</param>
        /// <param name="remove_I">If true, returns data packets and removes them from the queue.</param>
        /// <param name="numPkts_O">Number of packets actually returned.</param>
        /// <returns>Returns the next maxPkts_I from the list.  Note that if remove_I is false, then
        /// repeated calls will return the same packets.  If remove_I is true, then packets will be
        /// removed and subsequent calls will get different packets (if any).</returns>
        public WintabPacket[] GetDataPackets(UInt32 maxPkts_I, bool remove_I, ref UInt32 numPkts_O)
        {
            WintabPacket[] packets = null;

            try
            {
                CheckForValidHCTX("GetDataPackets");

                if (maxPkts_I == 0)
                {
                    throw new Exception("GetDataPackets - maxPkts_I is zero.");
                }

                // Packet array is used whether we're just looking or buying.
                int    size = (int)(maxPkts_I * Marshal.SizeOf(new WintabPacket()));
                IntPtr buf  = CMemUtils.AllocUnmanagedBuf(size);

                if (remove_I)
                {
                    // Return data packets and remove packets from queue.
                    numPkts_O = CWintabFuncs.WTPacketsGet(m_context.HCtx, maxPkts_I, buf);

                    if (numPkts_O > 0)
                    {
                        packets = CMemUtils.MarshalDataPackets(numPkts_O, buf);
                    }

                    //System.Diagnostics.Debug.WriteLine("GetDataPackets: numPkts_O: " + numPkts_O);
                }
                else
                {
                    // Return data packets, but leave on queue.  (Peek mode)
                    UInt32 pktIDOldest = 0;
                    UInt32 pktIDNewest = 0;

                    // Get oldest and newest packet identifiers in the queue.  These will bound the
                    // packets that are actually returned.
                    if (CWintabFuncs.WTQueuePacketsEx(m_context.HCtx, ref pktIDOldest, ref pktIDNewest))
                    {
                        UInt32 pktIDStart = pktIDOldest;
                        UInt32 pktIDEnd   = pktIDNewest;

                        if (pktIDStart == 0)
                        {
                            throw new Exception("WTQueuePacketsEx reports zero start packet identifier");
                        }

                        if (pktIDEnd == 0)
                        {
                            throw new Exception("WTQueuePacketsEx reports zero end packet identifier");
                        }

                        // Peek up to the max number of packets specified.
                        UInt32 numFoundPkts = CWintabFuncs.WTDataPeek(m_context.HCtx, pktIDStart, pktIDEnd, maxPkts_I, buf, ref numPkts_O);

                        System.Diagnostics.Debug.WriteLine("GetDataPackets: WTDataPeek - numFoundPkts: " + numFoundPkts + ", numPkts_O: " + numPkts_O);

                        if (numFoundPkts > 0 && numFoundPkts < numPkts_O)
                        {
                            throw new Exception("WTDataPeek reports more packets returned than actually exist in queue.");
                        }

                        packets = CMemUtils.MarshalDataPackets(numPkts_O, buf);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("FAILED GetPacketDataRange: " + ex.ToString());
            }

            return(packets);
        }