static inline void close_conn(client_t *cli, picoev_loop* loop) { client_t *new_client; if(!cli->response_closed){ close_response(cli); } picoev_del(loop, cli->fd); clean_cli(cli); #ifdef DEBUG printf("start close client:%p fd:%d status_code %d \n", cli, cli->fd, cli->status_code); printf("picoev_del client:%p fd:%d \n", cli, cli->fd); printf("remain http pipeline size :%d \n", cli->request_queue->size); #endif if(cli->request_queue->size > 0){ if(check_status_code(cli) > 0){ //process pipeline prepare_call_wsgi(cli); call_wsgi_app(cli, loop); } return ; } if(cli->http != NULL){ PyMem_Free(cli->http); } free_request_queue(cli->request_queue); if(!cli->keep_alive){ close(cli->fd); #ifdef DEBUG printf("close client:%p fd:%d status_code %d \n", cli, cli->fd, cli->status_code); #endif }else{ disable_cork(cli); new_client = new_client_t(cli->fd, cli->remote_addr, cli->remote_port); new_client->keep_alive = 1; init_parser(new_client, server_name, server_port); picoev_add(main_loop, new_client->fd, PICOEV_READ, keep_alive_timeout, r_callback, (void *)new_client); } //PyMem_Free(cli); dealloc_client(cli); #ifdef DEBUG printf("********************\n\n"); #endif }
static void accept_callback(picoev_loop* loop, int fd, int events, void* cb_arg) { int client_fd; client_t *client; struct sockaddr_in client_addr; if ((events & PICOEV_TIMEOUT) != 0) { // time out // next turn or other process return; }else if ((events & PICOEV_READ) != 0) { socklen_t client_len = sizeof(client_addr); #ifdef linux client_fd = accept4(fd, (struct sockaddr *)&client_addr, &client_len, SOCK_NONBLOCK | SOCK_CLOEXEC); #else client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len); #endif if (client_fd != -1) { #ifdef DEBUG printf("accept fd %d \n", client_fd); #endif setup_sock(client_fd); client = new_client_t(client_fd, client_addr); client->environ = Qnil; rb_gc_register_address(&client->environ); init_parser(client, server_name, server_port); picoev_add(loop, client_fd, PICOEV_READ, READ_LONG_TIMEOUT_SECS, r_callback, (void *)client); }else{ if (errno != EAGAIN && errno != EWOULDBLOCK) { // TODO: // raise exception from errno /* rb_raise(); */ /* write_error_log(__FILE__, __LINE__); */ // die loop_done = 0; } } } }