struct bb_session *bb_session_new(struct bb_connection *bbc) { struct bb_session *bbs = malloc(sizeof(struct bb_session)); if (!bbs) { bb_error("malloc()"); return NULL; } memset(bbs, 0, sizeof(struct bb_session)); // put the session in the hashtable bb_sht_add(bbs); // prepare for allocating a new request bbs->new_request = 1; // link to the connection bbs->connection = bbc; if (!bbc->sessions_head) { bbc->sessions_head = bbs; bbc->sessions_tail = bbs; } else { bbs->conn_prev = bbc->sessions_tail; bbc->sessions_tail = bbs; bbs->conn_prev->next = bbs; } return bbs; }
static void accept_callback(struct ev_loop *loop, struct ev_io *w, int revents) { struct bb_acceptor *acceptor = (struct bb_acceptor *) w; struct sockaddr_in sin; socklen_t sin_len = sizeof(sin); int client = accept(w->fd, (struct sockaddr *)&sin, &sin_len); if (client < 0) { perror("accept()"); return; } if (bb_nonblock(client)) { close(client); return; } struct bb_session *bbs = malloc(sizeof(struct bb_session)); if (!bbs) { perror("malloc()"); close(client); return; } memset(bbs, 0, sizeof(struct bb_session)); bb_sht_add(bbs); bbs->fd = client; bbs->acceptor = acceptor; // ssl context ? if (bbs->acceptor->ctx) { bbs->ssl = SSL_new(acceptor->ctx); SSL_set_fd(bbs->ssl, bbs->fd); SSL_set_accept_state(bbs->ssl); } bbs->new_request = 1; ev_io_init(&bbs->reader.reader, read_callback, client, EV_READ); bbs->reader.session = bbs; ev_io_init(&bbs->writer.writer, bb_wq_callback, client, EV_WRITE); bbs->writer.session = bbs; ev_io_start(loop, &bbs->reader.reader); }