void mk_server_loop(int server_fd) { int ret; int remote_fd; /* Activate TCP_DEFER_ACCEPT */ if (mk_socket_set_tcp_defer_accept(server_fd) != 0) { mk_warn("TCP_DEFER_ACCEPT failed"); } /* Rename worker */ mk_utils_worker_rename("monkey: server"); mk_info("HTTP Server started"); while (1) { remote_fd = mk_socket_accept(server_fd); if (mk_unlikely(remote_fd == -1)) { continue; } #ifdef TRACE MK_TRACE("New connection arrived: FD %i", remote_fd); int i; struct sched_list_node *node; node = sched_list; for (i=0; i < config->workers; i++) { MK_TRACE("Worker Status"); MK_TRACE(" WID %i / conx = %llu", node[i].idx, node[i].accepted_connections - node[i].closed_connections); } #endif /* Assign socket to worker thread */ ret = mk_sched_add_client(remote_fd); if (ret == -1) { close(remote_fd); } } }
static inline struct mk_sched_conn *mk_server_listen_handler(struct mk_sched_worker *sched, void *data) { int ret; int client_fd = -1; struct mk_sched_conn *conn; struct mk_server_listen *listener = data; client_fd = mk_socket_accept(listener->server_fd); if (mk_unlikely(client_fd == -1)) { MK_TRACE("[server] Accept connection failed: %s", strerror(errno)); goto error; } conn = mk_sched_add_connection(client_fd, listener, sched); if (mk_unlikely(!conn)) { goto error; } ret = mk_event_add(sched->loop, client_fd, MK_EVENT_CONNECTION, MK_EVENT_READ, conn); if (mk_unlikely(ret != 0)) { mk_err("[server] Error registering file descriptor: %s", strerror(errno)); goto error; } sched->accepted_connections++; MK_TRACE("[server] New connection arrived: FD %i", client_fd); return conn; error: if (client_fd != -1) { listener->network->network->close(client_fd); } return NULL; }