Libtins:不能使用 class 方法作为 sniff_loop 中的数据包处理程序,即使使用 c++11
Libtins: cannot use class method as packet handler within the sniff_loop even with c++11
我正在研究一个使用 libtins 的小型 UDP 流量嗅探示例。一切正常,直到我开始将所有内容封装到 class 中。根据 tutorial(循环嗅探部分),snifer_loop 将模板函子作为参数,并且:
The call to Sniffer::sniff_loop will make the sniffer start processing packets. The functor will be called using each processed packet as its argument. If at some point, you want to stop sniffing, then your functor should return false. Otherwise return true and the Sniffer object will keep looping.
The functor object will be copy constructed, so it must implement copy semantics. There is a helper template function which takes a pointer to an object of a template parameter type, and a member function, and returns a HandlerProxy. That object implements the required operator, in which it forwards the call to the member function pointer provided, using the object pointer given:
如果我使用 PDU 数据类型,或者如果我将 Packet 类型与外部函数一起使用,一切正常,但是一旦我将 Packet 类型与方法一起使用,我就会遇到编译器错误:
c++ -Ihandler@sha -I. -I.. -I/usr/local/include/tins -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=c++11 -g -fPIC -MD -MQ 'handler@sh
a/handler.cpp.o' -MF 'handler@sha/handler.cpp.o.d' -o 'handler@sha/handler.cpp.o' -c ../handler.cpp
../handler.cpp: In constructor 'Reader::Reader()':
../handler.cpp:24:77: error: cannot convert 'bool (Reader::*)(Tins::Packet&)' to 'Tins::HandlerProxy<Reader>::fun_type {aka bool (Reader::*)(Tins::PDU&)}' for argument '2' to 'Tins::Handl
erProxy<T> Tins::make_sniffer_handler(T*, typename Tins::HandlerProxy<T>::fun_type) [with T = Reader; typename Tins::HandlerProxy<T>::fun_type = bool (Reader::*)(Tins::PDU&)]'
sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPacket));
教程指定:
Packets can also be accepted on the functor object used on Sniffer::sniff_loop, but only when you are compiling in C++11 mode.
我正在使用 c++11 开关。
#include <tins/tins.h>
using namespace Tins;
bool callbackPDU(PDU &pdu) {
return true;
}
bool callbackPacket(Packet &packet) {
return true;
}
class Reader
{
Reader()
{
Sniffer sniffer("wlp3s0");
// sniffer.sniff_loop(callbackPDU); // This works
// sniffer.sniff_loop(callbackPacket); // This also works
// sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPDU)); // This also works
sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPacket)); // This doesn't work
}
bool handlerPDU(PDU &pdu)
{
return true;
}
bool handlerPacket(Packet &packet)
{
return true;
}
};
int main()
{
Reader reader();
}
class Functor
{
Functor(){}
bool operator()(Packet &packet)
{
return true;
}
};
好的,根据库的主要开发人员,我应该使用这个:
sniffer.sniff_loop(std::bind(&Reader::handlerPacket, this, std::placeholders::_1));
这就像一个魅力。
我正在研究一个使用 libtins 的小型 UDP 流量嗅探示例。一切正常,直到我开始将所有内容封装到 class 中。根据 tutorial(循环嗅探部分),snifer_loop 将模板函子作为参数,并且:
The call to Sniffer::sniff_loop will make the sniffer start processing packets. The functor will be called using each processed packet as its argument. If at some point, you want to stop sniffing, then your functor should return false. Otherwise return true and the Sniffer object will keep looping.
The functor object will be copy constructed, so it must implement copy semantics. There is a helper template function which takes a pointer to an object of a template parameter type, and a member function, and returns a HandlerProxy. That object implements the required operator, in which it forwards the call to the member function pointer provided, using the object pointer given:
如果我使用 PDU 数据类型,或者如果我将 Packet 类型与外部函数一起使用,一切正常,但是一旦我将 Packet 类型与方法一起使用,我就会遇到编译器错误:
c++ -Ihandler@sha -I. -I.. -I/usr/local/include/tins -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=c++11 -g -fPIC -MD -MQ 'handler@sh
a/handler.cpp.o' -MF 'handler@sha/handler.cpp.o.d' -o 'handler@sha/handler.cpp.o' -c ../handler.cpp
../handler.cpp: In constructor 'Reader::Reader()':
../handler.cpp:24:77: error: cannot convert 'bool (Reader::*)(Tins::Packet&)' to 'Tins::HandlerProxy<Reader>::fun_type {aka bool (Reader::*)(Tins::PDU&)}' for argument '2' to 'Tins::Handl
erProxy<T> Tins::make_sniffer_handler(T*, typename Tins::HandlerProxy<T>::fun_type) [with T = Reader; typename Tins::HandlerProxy<T>::fun_type = bool (Reader::*)(Tins::PDU&)]'
sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPacket));
教程指定:
Packets can also be accepted on the functor object used on Sniffer::sniff_loop, but only when you are compiling in C++11 mode.
我正在使用 c++11 开关。
#include <tins/tins.h>
using namespace Tins;
bool callbackPDU(PDU &pdu) {
return true;
}
bool callbackPacket(Packet &packet) {
return true;
}
class Reader
{
Reader()
{
Sniffer sniffer("wlp3s0");
// sniffer.sniff_loop(callbackPDU); // This works
// sniffer.sniff_loop(callbackPacket); // This also works
// sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPDU)); // This also works
sniffer.sniff_loop(make_sniffer_handler(this, &Reader::handlerPacket)); // This doesn't work
}
bool handlerPDU(PDU &pdu)
{
return true;
}
bool handlerPacket(Packet &packet)
{
return true;
}
};
int main()
{
Reader reader();
}
class Functor
{
Functor(){}
bool operator()(Packet &packet)
{
return true;
}
};
好的,根据库的主要开发人员,我应该使用这个:
sniffer.sniff_loop(std::bind(&Reader::handlerPacket, this, std::placeholders::_1));
这就像一个魅力。