/** * The listener thread main function. The listener listens for * connections from clients. */ static void *listener_main(void *arg_p) { struct http_server_t *self_p = arg_p; struct http_server_listener_t *listener_p; struct http_server_connection_t *connection_p; struct inet_addr_t addr; thrd_set_name(self_p->listener_p->thrd.name_p); listener_p = self_p->listener_p; if (socket_open_tcp(&listener_p->socket) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to open socket.")); return (NULL); } if (inet_aton(listener_p->address_p, &addr.ip) != 0) { return (NULL); } addr.port = listener_p->port; if (socket_bind(&listener_p->socket, &addr) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to bind socket.")); return (NULL); } if (socket_listen(&listener_p->socket, 3) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to listen on socket.")); return (NULL); } /* Wait for clients to connect. */ while (1) { /* Allocate a connection. */ connection_p = allocate_connection(self_p); /* Wait for a client to connect. */ socket_accept(&listener_p->socket, &connection_p->socket, &addr); handle_accept(self_p, connection_p); } return (NULL); }
static void handle_accepting_connection(int server_fd) { sockaddr_in clientaddr; socklen_t clientlen = sizeof(clientaddr); int client_fd = accept(server_fd, (sockaddr*)&clientaddr, &clientlen); check_errors("accept", client_fd); char client_address[NI_MAXHOST], client_port[NI_MAXSERV]; int error_code = getnameinfo ((sockaddr*)&clientaddr, clientlen, client_address, sizeof client_address, client_port, sizeof client_port, NI_NUMERICHOST | NI_NUMERICSERV); make_socket_non_blocking(client_fd); connection_data *connection = allocate_connection(client_fd); connections++; if (global_accept_handler != NULL) global_accept_handler(error_code, connection, client_address, client_port); }
static void new_handle_accepting_connection(int server_fd, struct epoll_event &client_event, struct epoll_event &server_event) { sockaddr_in clientaddr; socklen_t clientlen = sizeof(clientaddr); int client_fd = accept(server_fd, (sockaddr*)&clientaddr, &clientlen); assert(client_fd > 0 || (client_fd == -1 && errno == EAGAIN)); if (client_fd > 0) { char client_address[NI_MAXHOST], client_port[NI_MAXSERV]; int error_code = getnameinfo ((sockaddr*)&clientaddr, clientlen, client_address, sizeof client_address, client_port, sizeof client_port, NI_NUMERICHOST | NI_NUMERICSERV); make_socket_non_blocking(client_fd); connection_data *connection = allocate_connection(client_fd); connections++; // epoll_event event; // event.events = EPOLLIN | EPOLLET; // event.data.ptr = connection; // int return_code = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event); // assert(return_code >= 0); int return_code = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, server_fd, &server_event); assert(return_code >= 0); if (global_accept_handler != NULL) global_accept_handler(error_code, connection, client_address, client_port); } }
boost::shared_ptr<cql::cql_connection_t> cql::cql_session_impl_t::connect( boost::shared_ptr<cql::cql_query_plan_t> query_plan, cql_stream_t* stream, std::list<cql_endpoint_t>* tried_hosts) { assert(stream != NULL); assert(tried_hosts != NULL); while (boost::shared_ptr<cql_host_t> host = query_plan->next_host_to_query()) { if (!host->is_considerably_up()) { continue; } cql_endpoint_t host_address = host->endpoint(); tried_hosts->push_back(host_address); cql_connections_collection_t* connections = add_to_connection_pool(host_address); boost::shared_ptr<cql_connection_t> conn; conn = try_find_free_stream(host, connections, stream); if (conn) { // Connection must know the pointer to the containing session. // Otherwise it would be unable to propagate the result if // employed for, e.g. USE query. conn->set_session_ptr(this->shared_from_this()); return conn; } conn = _trashcan->recycle(host_address); if (conn && !conn->is_healthy()) { free_connection(conn); conn.reset(); } if (!conn) { try { if (!(conn = allocate_connection(host))) { continue; } } catch (cql_connection_allocation_error& e) { // This error is interpreted as host being dead. host->set_down(); continue; } catch (cql_too_many_connections_per_host_exception& e) { // This happens; let's try with another host. continue; } host->bring_up(); } if (conn) { *stream = conn->acquire_stream(); (*connections)[conn->id()] = conn; conn->set_session_ptr(this->shared_from_this()); } return conn; } throw cql_no_host_available_exception("no host is available according to load balancing policy."); }