static void ev_handler(struct mg_connection *con, int ev, void *ev_data) { struct http_message *msg = (struct http_message *)ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: if (mg_vcmp(&msg->uri, "/play") == 0) { handle_play(con, msg); } else if (mg_vcmp(&msg->uri, "/pause") == 0) { if (playback_pause) { playback_pause = 0; } else { playback_pause = 1; } mg_printf(con, "HTTP/1.1 200 OK\r\n\r\n%s"); } else if (mg_vcmp(&msg->uri, "/stop") == 0) { handle_stop(con, msg); } else if (mg_vcmp(&msg->uri, "/init") == 0) { if (tpod_mg_str_cmp(&msg->method, &msg_http_method_get)) { /* char query_string[msg->query_string.len + 1]; */ /* strncpy(query_string, msg->query_string.p, msg->query_string.len); */ char *jsnstr_podcasts_obj = load_episodes(); mg_printf(con, "HTTP/1.1 200 OK\r\nContent-Type: " "application/json\r\nContent-Length: %d\r\n\r\n%s", (int)strlen(jsnstr_podcasts_obj), jsnstr_podcasts_obj); } } else { mg_serve_http(con, msg, s_http_server_opts); } break; default: break; } }
/** * Handle a request and send a response * * @param cls unused * @param other the sender * @param message the message * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error */ static int core_receive_handler (void *cls, const struct GNUNET_PeerIdentity *other, const struct GNUNET_MessageHeader *message) { if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader)) { GNUNET_break (0); return GNUNET_SYSERR; } switch (ntohs (message->type)) { case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST: handle_request (other, message); break; case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE: handle_response (other, message); break; case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START: handle_start (other, message); break; case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK: handle_start_ack (other, message); break; case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP: handle_stop (other, message); break; default: break; } return GNUNET_OK; }
int timer_stop (evHandle *handle) { if (!handle || !_is_active(handle)) return 0; evTimer *timer = handle->ev; heap_remove((struct heap*) &handle->loop->timer_heap, (struct heap_node*) &timer->heap_node, timer_less_than); handle_stop(handle); return 0; }
//-------------------------------------------------------------------------------- void Server::stop() { LogStream log(__PRETTY_FUNCTION__); handle_stop(); log << manip::info_normal << "--------------------------------------------------------------------------------" << '\n' << "Stopping Process : " << Config::instance()->getAppId() << '\n' << "Instance : " << Config::instance()->getAppInstance() << '\n' << "Server address : " << Config::instance()->get<std::string>("kcc-server.address") << '\n' << "Server port : " << Config::instance()->get<std::string>("kcc-server.port") << '\n' << "--------------------------------------------------------------------------------" << manip::endl; }
void handle_close (evHandle *h){ if ((h->flags & HANDLE_CLOSING) != 0) return; if (h->type == EV_IO){ io_close(h); } else if (h->type == EV_TIMER){ timer_close(h); } else { handle_stop(h); h->flags |= HANDLE_CLOSING; QUEUE_REMOVE(&h->queue); QUEUE_INSERT_TAIL(&h->loop->closing_queue, &h->queue); //closing un initaited handle //assert(0 && "CLOSING UNKNOWN HANDLE\n"); } }
/// Ask the server to stop using asynchronous command void server_base::stop() { if (is_running) { // Post a call to the stop function so that server_base::stop() is safe to call from any thread. io_service_.post(boost::bind(&server_base::handle_stop, this)); } else { // if io_service is not running then the post call will not be performed handle_stop(); } // Wait for acceptor and connections to stop int timeout = 15; // force stop after 15 seconds time_t start = mytime(NULL); while(true) { if (!is_running && is_stop_complete) { break; } if ((mytime(NULL) - start) > timeout) { // timeout occurred break; } sleep_milliseconds(500); } }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server ASIO::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the optional handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // set the connection timeout (10 seconds) http_server.set_timeout(10000); // set the connection buffer sizes http_server.set_rx_buffer_size(16384); http_server.tcp_server()->set_receive_buffer_size(16384); http_server.tcp_server()->set_send_buffer_size(16384); // start accepting http connections on the port ASIO_ERROR_CODE error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register termination notifications ASIO::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (ASIO_ERROR_CODE const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // run the io_service to start communications io_service.run(); std::cout << "io_service.run complete, shutdown successful" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }
facade::~facade() { #if 0 handle_stop(); #endif }
facade_extension::~facade_extension() { handle_stop(); }
client<NetSpeedThreads, TimerSpeedThreads>::~client() { handle_stop(); }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server boost::asio::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // start accepting http connections on the given port boost::system::error_code error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register for termination notifications boost::asio::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (boost::system::error_code const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // Determine the number of concurrent threads supported size_t no_of_threads(std::thread::hardware_concurrency()); std::cout << "No of threads: " << no_of_threads << std::endl; if (no_of_threads > 0) { // Create a thread pool for the threads and run the asio io_service // in each of the threads. std::vector<std::shared_ptr<std::thread> > threads; for (std::size_t i = 0; i < no_of_threads; ++i) { std::shared_ptr<std::thread> thread(std::make_shared<std::thread> ([&io_service](){ io_service.run(); })); threads.push_back(thread); } // Wait for all threads in the pool to exit. for (std::size_t i(0); i < threads.size(); ++i) threads[i]->join(); } else io_service.run(); std::cout << "io_service.run, all work has finished" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }