最近在开发im服务器,需要大并发链接。QT默认的是使用select模型的,这种轮询方式非常慢。在高并发连接,我们需要epoll才能发挥linux服务器的性能.而且使用简单,整个服务端代码架构无需修改,设置QT的分发事件就可以使用了,只要在main里面添加 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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文件添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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源码下载地址:qt_libevent.zip
发表评论
要发表评论,您必须先登录。