|
Boost Users : |
Subject: [Boost-users] pcap_loop callback with boost::function and boost::bind
From: Clemens Gruber (philistion.dev_at_[hidden])
Date: 2011-07-28 12:01:17
Hi,
I am writing a statistical protocol analyzer running under Windows with
WinPcap.
The problem is: The pcap_loop function for starting to capture the
packets wants a C style callback function, but I need that callback
function to have access to several class variables, so this callback
function should be a member function.
I know that it is not possible to use a member function directly,
because of the additional parameter, the pointer to the class.
I thought, maybe I could use boost::function and boost::bind, but I am
not sure if my solution is correct, so it would be very nice if some of
you guys could just look over it and tell me if this is a good way to do
this.
This is my code for starting the capture:
void MyClass::startCapturing()
{
boost::function < void (u_char *, const struct pcap_pkthdr *, const
u_char *) > f;
f = boost::bind(&MyClass::packetHandler, this, _1, _2, _3);
pcap_handler p = *(f.target<pcap_handler>());
pcap_loop(hnd, 0, p, NULL);
}
This the callback function I want pcap_loop to call:
void MyClass::packetHandler(u_char *param, const struct pcap_pkthdr
*header, const u_char *pkt_data)
{
/* Do something with the packet.. */
}
And this is the signature of the callback argument in the pcap_loop
function, as defined in pcap.h:
typedef void(*) pcap_handler(u_char *param, const struct pcap_pkthdr
*pkt_header, const u_char *pkt_data)
If you have an alternative, I would be glad to hear it! Maybe there's a
much simpler solution, I didn't think about.
Best regards,
Clemens
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net