最近在开发im服务器,需要大并发链接。QT默认的是使用select模型的,这种轮询方式非常慢。在高并发连接,我们需要epoll才能发挥linux服务器的性能.而且使用简单,整个服务端代码架构无需修改,设置QT的分发事件就可以使用了,只要在main里面添加 :
int main(int argc, char *argv[])
{
#ifdef Q_OS_LINUX
QCoreApplication::setEventDispatcher(new EventDispatcherLibEvent);
// qInstallMessageHandler(customMessageHandler);
#endif
QCoreApplication a(argc, argv);
auto *ser=new ConfigServer;
ser->startServer();
return a.exec();
}
在.pro文件添加
linux{
LIBS += -levent_core
SOURCES += ../common/eventdispatcher_libevent/eventdispatcher_libevent.cpp \
../common/eventdispatcher_libevent/eventdispatcher_libevent_config.cpp \
../common/eventdispatcher_libevent/eventdispatcher_libevent_p.cpp \
../common/eventdispatcher_libevent/socknot_p.cpp \
../common/eventdispatcher_libevent/tco_eventfd.cpp \
../common/eventdispatcher_libevent/tco_pipe.cpp \
../common/eventdispatcher_libevent/tco.cpp \
../common/eventdispatcher_libevent/timers_p.cpp
HEADERS += ../common/eventdispatcher_libevent/common.h \
../common/eventdispatcher_libevent/eventdispatcher_libevent.h \
../common/eventdispatcher_libevent/eventdispatcher_libevent_config.h \
../common/eventdispatcher_libevent/eventdispatcher_libevent_config_p.h \
../common/eventdispatcher_libevent/eventdispatcher_libevent_p.h \
../common/eventdispatcher_libevent/libevent2-emul.h \
../common/eventdispatcher_libevent/qt4compat.h \
../common/eventdispatcher_libevent/tco.h \
../common/eventdispatcher_libevent/wsainit.h
}
可以直接跨平台了使用了
附上qt libevent源码下载地址:http://download.csdn.net/detail/rushroom/7968573