コード例 #1
0
        //public event EventHandler<PacketReceivedEventArgs> PacketReceived;

        /**
         * Notifies subscribed XBee packet listeners that a new XBee packet has
         * been received.
         *
         * @param packet The received XBee packet.
         *
         * @see com.digi.xbee.api.packet.XBeeAPIPacket
         * @see com.digi.xbee.api.packet.XBeePacket
         */
        private void NotifyPacketReceived(XBeePacket packet)
        {
            logger.DebugFormat(connectionInterface.ToString() + "Packet received: \n{0}", packet.ToPrettyString());

            try
            {
                lock (packetReceiveListeners)
                {
                    var removeListeners = new List <IPacketReceiveListener>();

                    var tasks = new ConcurrentBag <Task>();
                    foreach (IPacketReceiveListener listener in packetReceiveListeners.Keys)
                    {
                        tasks.Add(Task.Factory.StartNew((state) =>
                        {
                            var listener0 = (IPacketReceiveListener)state;
                            lock (listener0)
                            {
                                if (packetReceiveListeners[listener0] == ALL_FRAME_IDS)
                                {
                                    listener0.PacketReceived(packet);
                                }
                                else if (((XBeeAPIPacket)packet).NeedsAPIFrameID &&
                                         ((XBeeAPIPacket)packet).FrameID == packetReceiveListeners[listener0])
                                {
                                    listener0.PacketReceived(packet);
                                    removeListeners.Add(listener0);
                                }
                            }
                        }, listener));
                    }
                    Task.WaitAll(tasks.ToArray());
                    //executor.shutdown();
                    // Remove required listeners.
                    foreach (IPacketReceiveListener listener in removeListeners)
                    {
                        int value;
                        packetReceiveListeners.TryRemove(listener, out value);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message, e);
            }
        }
コード例 #2
0
        /// <summary>
        /// Notifies subscribed XBee packet event handlers that a new XBee Packet has been received.
        /// </summary>
        /// <param name="packet">The received XBee packet.</param>
        /// <seealso cref="XBeeAPIPacket"/>
        /// <seealso cref="XBeePacket"/>
        private void NotifyPacketReceived(XBeePacket packet)
        {
            logger.DebugFormat(connectionInterface.ToString() + "Packet received: \n{0}", packet.ToPrettyString());

            try
            {
                lock (packetReceivedHandlers)
                {
                    var           args      = new PacketReceivedEventArgs(packet);
                    XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;
                    List <CustomPacketReceivedEventHandler> handlersToRemove = new List <CustomPacketReceivedEventHandler>();

                    // Need to go over the list of Packet received handlers to
                    // verify which ones need to be notified of the received packet.
                    foreach (var packetHandler in packetReceivedHandlers)
                    {
                        if (packetHandler.Handler != null)
                        {
                            if (packetHandler.FrameId == ALL_FRAME_IDS)
                            {
                                packetHandler.Handler.DynamicInvoke(this, args);
                            }
                            else if (apiPacket.NeedsAPIFrameID &&
                                     apiPacket.FrameID == packetHandler.FrameId)
                            {
                                packetHandler.Handler.DynamicInvoke(this, args);
                                handlersToRemove.Add(packetHandler);
                            }
                        }
                    }
                    foreach (CustomPacketReceivedEventHandler handlerToRemove in handlersToRemove)
                    {
                        RemovePacketReceivedHandler(handlerToRemove.Handler);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message, e);
            }
        }