int swReactor_create(swReactor *reactor, int max_event) { int ret; bzero(reactor, sizeof(swReactor)); #ifdef HAVE_EPOLL ret = swReactorEpoll_create(reactor, max_event); #elif defined(HAVE_KQUEUE) ret = swReactorKqueue_create(reactor, max_event); #elif defined(HAVE_POLL) ret = swReactorPoll_create(reactor, max_event); #else ret = swReactorSelect_create(reactor); #endif reactor->running = 1; reactor->setHandle = swReactor_setHandle; reactor->onFinish = swReactor_onFinish; reactor->onTimeout = swReactor_onTimeout; reactor->write = swReactor_write; reactor->defer = swReactor_defer; reactor->close = swReactor_close; reactor->socket_array = swArray_new(1024, sizeof(swConnection)); if (!reactor->socket_array) { swWarn("create socket array failed."); return SW_ERR; } return ret; }
int swReactor_create(swReactor *reactor, int max_event) { int ret; bzero(reactor, sizeof(swReactor)); //event less than SW_REACTOR_MINEVENTS, use poll/select if (max_event <= SW_REACTOR_MINEVENTS) { #ifdef SW_MAINREACTOR_USE_POLL ret = swReactorPoll_create(reactor, SW_REACTOR_MINEVENTS); #else ret = swReactorSelect_create(reactor); #endif } //use epoll or kqueue else { #ifdef HAVE_EPOLL ret = swReactorEpoll_create(reactor, max_event); #elif defined(HAVE_KQUEUE) ret = swReactorKqueue_create(reactor, max_event); #elif defined(SW_MAINREACTOR_USE_POLL) ret = swReactorPoll_create(reactor, max_event); #else ret = swReactorSelect_create(reactor); #endif } reactor->running = 1; reactor->setHandle = swReactor_setHandle; reactor->onFinish = swReactor_onFinish; reactor->onTimeout = swReactor_onTimeout; reactor->write = swReactor_write; reactor->close = swReactor_close; reactor->socket_array = swArray_new(1024, sizeof(swConnection), 0); if (!reactor->socket_array) { swWarn("create socket array failed."); return SW_ERR; } return ret; }
/** * proxy模式 * 在单独的n个线程中接受维持TCP连接 */ static int swServer_start_proxy(swServer *serv) { int ret; swReactor *main_reactor = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swReactor)); #ifdef SW_MAINREACTOR_USE_POLL ret = swReactorPoll_create(main_reactor, 10); #else ret = swReactorSelect_create(main_reactor); #endif if (ret < 0) { swWarn("Swoole reactor create fail"); return SW_ERR; } ret = swReactorThread_start(serv, main_reactor); if (ret < 0) { swWarn("Swoole poll thread start fail"); return SW_ERR; } SwooleG.main_reactor = main_reactor; main_reactor->id = serv->reactor_num; //设为一个特别的ID main_reactor->ptr = serv; main_reactor->setHandle(main_reactor, SW_FD_LISTEN, swServer_master_onAccept); main_reactor->setHandle(main_reactor, (SW_FD_USER+2), swServer_master_onClose); main_reactor->onFinish = swServer_master_onReactorFinish; main_reactor->onTimeout = swServer_master_onReactorTimeout; #ifdef HAVE_SIGNALFD if (SwooleG.use_signalfd) { swSignalfd_setup(main_reactor); } #endif main_reactor->add(main_reactor, serv->main_pipe.getFd(&serv->main_pipe, 0), (SW_FD_USER+2)); //no use //SW_START_SLEEP; if (serv->onStart != NULL) { serv->onStart(serv); } struct timeval tmo; tmo.tv_sec = SW_MAINREACTOR_TIMEO; tmo.tv_usec = 0; //先更新一次时间 swServer_update_time(); return main_reactor->wait(main_reactor, &tmo); }
/** * 自动适配reactor */ int swReactor_auto(swReactor *reactor, int max_event) { int ret; #ifdef HAVE_EPOLL ret = swReactorEpoll_create(reactor, max_event); #elif defined(HAVE_KQUEUE) ret = swReactorKqueue_create(reactor, max_event); #elif defined(SW_MAINREACTOR_USE_POLL) ret = swReactorPoll_create(reactor, max_event); #else ret = swReactorSelect_create(SwooleG.main_reactor) #endif return ret; }