src/server/daemon/httplistener.c

changeset 577
4f5ccaea4a92
parent 571
0fa595aefd0f
child 579
e10457d74fe1
equal deleted inserted replaced
576:5c31cc844c68 577:4f5ccaea4a92
75 */ 75 */
76 static CxMap *listener_socket_map; 76 static CxMap *listener_socket_map;
77 77
78 static pthread_mutex_t listener_mutex = PTHREAD_MUTEX_INITIALIZER; 78 static pthread_mutex_t listener_mutex = PTHREAD_MUTEX_INITIALIZER;
79 79
80 static void wssocket_free(WSSocket *s) {
81 close(s->socket);
82 free(s);
83 }
80 84
81 int http_listener_global_init(void) { 85 int http_listener_global_init(void) {
82 listener_socket_map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4); 86 listener_socket_map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4);
83 if(!listener_socket_map) { 87 if(!listener_socket_map) {
84 return 1; 88 return 1;
85 } 89 }
90 listener_socket_map->simple_destructor = (cx_destructor_func)wssocket_free;
86 91
87 return 0; 92 return 0;
93 }
94
95 void http_listener_global_shutdown(void) {
96 cxMapDestroy(listener_socket_map);
88 } 97 }
89 98
90 int start_all_listener() { 99 int start_all_listener() {
91 ServerConfiguration *conf = cfgmgr_get_server_config(); 100 ServerConfiguration *conf = cfgmgr_get_server_config();
92 CxList *ls = conf->listeners; 101 CxList *ls = conf->listeners;
426 log_ereport(LOG_DEBUG, "destroy http listener: %s config: %p", listener->name.ptr, listener->cfg); 435 log_ereport(LOG_DEBUG, "destroy http listener: %s config: %p", listener->name.ptr, listener->cfg);
427 436
428 if(listener->shutdown) { 437 if(listener->shutdown) {
429 pthread_mutex_destroy(&listener->shutdown_mutex); 438 pthread_mutex_destroy(&listener->shutdown_mutex);
430 pthread_cond_destroy(&listener->shutdown_cond); 439 pthread_cond_destroy(&listener->shutdown_cond);
440
441 free(listener->acceptors);
442 free(listener->acceptors6);
431 } 443 }
432 } 444 }
433 445
434 int http_listener_start(HttpListener *listener) { 446 int http_listener_start(HttpListener *listener) {
435 if(listener->running) { 447 if(listener->running) {
554 struct timespec ts; 566 struct timespec ts;
555 clock_gettime(CLOCK_REALTIME, &ts); 567 clock_gettime(CLOCK_REALTIME, &ts);
556 ts.tv_sec += 60; 568 ts.tv_sec += 60;
557 pthread_cond_timedwait(&listener->shutdown_cond, &listener->shutdown_mutex, &ts); 569 pthread_cond_timedwait(&listener->shutdown_cond, &listener->shutdown_mutex, &ts);
558 570
571
559 pthread_mutex_unlock(&listener->shutdown_mutex); 572 pthread_mutex_unlock(&listener->shutdown_mutex);
560 } 573 }
561 574
562 575
563 Acceptor* acceptor_new(HttpListener *listener) { 576 Acceptor* acceptor_new(HttpListener *listener) {
675 acceptor_exit = 1; 688 acceptor_exit = 1;
676 } 689 }
677 690
678 // create Connection object 691 // create Connection object
679 Connection *conn = malloc(sizeof(Connection)); 692 Connection *conn = malloc(sizeof(Connection));
680 uint32_t conn_id;
681 RAND_bytes((unsigned char*)&conn->id, sizeof(conn->id)); 693 RAND_bytes((unsigned char*)&conn->id, sizeof(conn->id));
682 conn->id <<= 16; 694 conn->id <<= 16;
683 conn->address = ca; 695 conn->address = ca;
684 conn->addr_type = addr_type; 696 conn->addr_type = addr_type;
685 conn->fd = clientfd; 697 conn->fd = clientfd;
757 769
758 acceptor->running = FALSE; 770 acceptor->running = FALSE;
759 771
760 cfg_unref(acceptor->listener->cfg); 772 cfg_unref(acceptor->listener->cfg);
761 773
774 free(acceptor);
775
762 return NULL; 776 return NULL;
763 } 777 }
764 778
765 void wssocket_ref(WSSocket *ws) { 779 void wssocket_ref(WSSocket *ws) {
766 ws_atomic_inc32(&ws->ref); 780 ws_atomic_inc32(&ws->ref);

mercurial