powered by Jive Software

Packet filters and packet listeners

When I call addPacketListener on a connection, my expectation is that for each packet that comes in, the packet filter is called immediately before the packet listener. This turns out not to be true; in fact, the filter and listener are called from separate threads (the parser thread and listener thread, respectively, in PacketReader); in particular, this means that if two packets arrive in quick succession, they could both be processed by the packet filter before either one gets processed by the packet listener. This can cause problems if the packet filter depends on the current state based on what packets have arrived, which is true in my program. I rewrote my program to check the state-based condition in the listener instead of the filter, but this seems inelegant to me since the whole point of the filter is to filter out packets that don’'t need to be listened to.

Is this design intentional? That is, should packet filters always be functional (independent of state)? If so, this should be documented as part of the contract of PacketFilter. Otherwise, I think this is pretty easy to fix: store the packet filter in the PacketReader.ListenerWrapper, and check it explicitly before calling the packet listener, rather than making it part of the packet collector.