Пример #1
0
//#define MULTI_THREAD_SUPPORT 1
int httpserver_start(int port, int nthreads, int backlog) {
	int r, i;
	int nfd = httpserver_bindsocket(port, backlog);
	if (nfd < 0) return -1;

#ifdef MULTI_THREAD_SUPPORT
	pthread_t ths[nthreads];
	for (i=0; i<nthreads; i++) {
		struct event_base *base = event_init();
		if (base == NULL) return -1;
		struct evhttp *httpd = evhttp_new(base);
		if (httpd == NULL) return -1;
		r = evhttp_accept_socket(httpd, nfd);
		if (r != 0) return -1;
		 evhttp_set_gencb(httpd, httpserver_generic_handler, NULL);
		r = pthread_create(&ths[i], NULL, httpserver_dispatch, base);
		if (r != 0) return -1;
	}
	
	for (i=0; i<nthreads; i++) {
		pthread_join(ths[i], NULL);
	}
#else
	struct event_base *base = event_init();
	if (base == NULL) return -1;
	struct evhttp *httpd = evhttp_new(base);
	if (httpd == NULL) return -1;
	r = evhttp_accept_socket(httpd, nfd);
	evhttp_set_gencb(httpd, httpserver_generic_handler, NULL);
	httpserver_dispatch(base);
#endif
}
Пример #2
0
int httpserver_start(int port, int backlog)  
{  
    int ret;  
    int fd = httpserver_bindsocket(port, backlog);  
    if (fd < 0)   
    {     
        return -1;  
        printf("httpserver_bindsocket error !\n");  
    }  
    struct event_base *base = event_init();  
    if (base == NULL)   
    {  
        printf("event_init error !\n");  
        return -1;        
    }  
    struct evhttp *httpd = evhttp_new(base);  
    if (httpd == NULL)   
    {  
        printf("enhttp_new error !\n");  
        return -1;  
    }  
    ret = evhttp_accept_socket(httpd, fd);  
    if (ret != 0)   
    {  
        printf("evhttp_accept_socket error \n");  
        return -1;  
    }  
    evhttp_set_gencb(httpd, httpserver_handler, NULL);  
    event_base_dispatch(base);  
    return 0;  
}