Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maintain state between callback function

Tags:

haskell

pcap

To Rephrase

I am newbie to haskell. I am trying to parse a pcap file using network pcap package.

I have byte stream as an input, so I used dispatchBS function with my own callback function.

My goal is to get the concatenated result from all callbacks. [a] of all the packets parsed. How do I keep track of all the packets got so far when dispatchBS has nothing in its signature to maintain state of all packets parsed so far?

http://hackage.haskell.org/packages/archive/pcap/0.4.5/doc/html/Network-Pcap.html#t:CallbackBS

like image 283
Ak Magnolia Avatar asked Apr 26 '26 21:04

Ak Magnolia


1 Answers

For example, you might use the "loop" function and provide your own callback. This callback has to be an IO action, so one simple solution would be to have it append its packet argument to a sequence (see Data.Sequence) held in an IORef. Once you have received enough packets, just read the IOVar. Or you could do something more sophisticated with threads and queues, depending on what you are trying to achieve.

like image 146
Paul Johnson Avatar answered Apr 29 '26 18:04

Paul Johnson