void print_features (struct event_base *base) { const char *method=event_base_get_method(base); printf ("--- method <%s> ---\n", method); int cnt = event_base_get_features(base); if (cnt & EV_FEATURE_ET) { /* see man for more information about edge-triggered and level-triggered */ printf("support edge-triggered events with EV_ET\n"); } if(cnt & EV_FEATURE_O1) { /* poll and select is O(n) */ printf("having one event triggered among many is an O(1) operation.\n"); } if(cnt & EV_FEATURE_FDS) { /* epoll not support it */ printf("allows file descriptors as well as sockets. \n"); } if (cnt & EV_FEATURE_EARLY_CLOSE) { /* detect connection close without the necessity of reading all the pending data. */ printf ("EV_FEATURE_EARLY_CLOSE\n"); } printf ("that is all\n\n"); }
CIOServer::CIOServer() : m_evbase(event_base_new()) , m_status(status_null) , m_binit(false) , m_id(gen_ioserverid()) , m_curpost( 0 ) , m_haspostdata( false ) { //static CNetInit netinit; if ( !m_evbase ) { XH_LOG_ERROR(::xhnet::logname_base, "Create EventBase Failed, Check OS config"); } else { XH_LOG_DEBUG(::xhnet::logname_base, "Using LibEvent with backend method " << event_base_get_method(m_evbase)); // 设置m_evbase的优先级范围0-10 //event_base_priority_init(m_evbase, 10); //event_priority_set 设置event的优先级 //event_base_get_npriorities // 2.1.1 #ifdef _DEBUG int f = event_base_get_features(m_evbase); if ((f & EV_FEATURE_ET)) XH_LOG_DEBUG(::xhnet::logname_base, " Edge-triggered events are supported."); if ((f & EV_FEATURE_O1)) XH_LOG_DEBUG(::xhnet::logname_base, " O(1) event notification is supported."); if ((f & EV_FEATURE_FDS)) XH_LOG_DEBUG(::xhnet::logname_base, " All FD types are supported."); #endif } }
int main(int argc, char*argv[]) { printf("Hello, world!\n"); event_config *conf = event_config_new(); #ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); return 1; } evthread_use_windows_threads(); event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP); #endif base = event_base_new_with_config(conf); const char ** methods = event_get_supported_methods(); int loop; std::cout << "Version: " << event_get_version() << std::endl; std::cout << "Method: " << event_base_get_method(base) << std::endl; std::cout << "Features: 0x" << std::hex << event_base_get_features(base) << std::endl; std::cout << "Base: " << base << std::endl; while (*methods) { std::cout << "Method: " << *methods++ << std::endl; } event_set_log_callback(_log_cb); /* The caller has already set up fd1, fd2 somehow, and make them nonblocking. */ if (0) { evutil_socket_t fd1 = 1; evutil_socket_t fd2 = 1; struct timeval five_seconds = { 5, 0 }; struct event *ev1 = event_new(base, fd1, EV_TIMEOUT | EV_READ/*|EV_PERSIST*/, cb_func, (char*) "Reading event"); struct event *ev2 = event_new(base, fd2, EV_WRITE/*|EV_PERSIST*/, cb_func, (char*) "Writing event"); event_add(ev1, &five_seconds); event_add(ev2, NULL); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; } // http server evhttp *ev_http = evhttp_new(base); int http_port = 9090; // evhttp_bind_socket expects its PORT param in host byte order. Sigh. int r = evhttp_bind_socket(ev_http, "0.0.0.0", http_port); // This return value is undocumented (!), but this seems to work. if (r == -1) { std::cerr << "could not open port " << http_port << std::endl; return 3; } evhttp_set_gencb(ev_http, http_handle_generic, 0); //evhttp_set_cb(ev_http, "/", http_handle_root); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; evhttp_free(ev_http); event_base_free(base); event_config_free(conf); return 0; }