void handle_accept(boost::system::error_code const &ec) { { scoped_mutex_lock stopping_lock(stopping_mutex_); if (stopping) return; // we dont want to add another handler instance, and we // dont want to know about errors for a socket we dont // need anymore } if (ec) { BOOST_NETWORK_MESSAGE("Error accepting connection, reason: " << ec); } #ifdef BOOST_NETWORK_ENABLE_HTTPS socket_options_base::socket_options(new_connection->socket().next_layer()); #else socket_options_base::socket_options(new_connection->socket()); #endif new_connection->start(); new_connection.reset(new connection(service_, handler, *thread_pool, ctx_)); acceptor.async_accept( #ifdef BOOST_NETWORK_ENABLE_HTTPS new_connection->socket().next_layer(), #else new_connection->socket(), #endif boost::bind(&async_server_base<Tag, Handler>::handle_accept, this, boost::asio::placeholders::error)); }
void start_listening() { using boost::asio::ip::tcp; system::error_code error; service_.reset(); // this allows repeated cycles of run -> stop -> // run tcp::resolver resolver(service_); tcp::resolver::query query(address_, port_); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error); if (error) { BOOST_NETWORK_MESSAGE("Error resolving '" << address_ << ':' << port_); return; } tcp::endpoint endpoint = *endpoint_iterator; acceptor.open(endpoint.protocol(), error); if (error) { BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ":" << port_); return; } socket_options_base::acceptor_options(acceptor); acceptor.bind(endpoint, error); if (error) { BOOST_NETWORK_MESSAGE("Error binding socket: " << address_ << ":" << port_); return; } acceptor.listen(asio::socket_base::max_connections, error); if (error) { BOOST_NETWORK_MESSAGE("Error listening on socket: '" << error << "' on " << address_ << ":" << port_); return; } new_connection.reset(new connection(service_, handler, *thread_pool, ctx_)); acceptor.async_accept( #ifdef BOOST_NETWORK_ENABLE_HTTPS new_connection->socket().next_layer(), #else new_connection->socket(), #endif boost::bind(&async_server_base<Tag, Handler>::handle_accept, this, boost::asio::placeholders::error)); listening = true; scoped_mutex_lock stopping_lock(stopping_mutex_); stopping = false; // if we were in the process of stopping, we revoke // that command and continue listening BOOST_NETWORK_MESSAGE("Now listening on socket: '" << address_ << ":" << port_ << "'"); }
void connection_manager::start(connection_ptr c) { connections_.insert(c); boost::system::error_code ec; boost::asio::ip::tcp::endpoint endpoint = c->socket().remote_endpoint(ec); if (ec) { // Prevent the exception to be thrown to run to avoid the server to be locked (still listening but no more connection or stop). // If the exception returns to WebServer to also create a exception loop. _log.Log(LOG_ERROR,"Getting error '%s' while getting remote_endpoint in connection_manager::start", ec.message().c_str()); stop(c); return; } std::string s = endpoint.address().to_string(); if (s.substr(0, 7) == "::ffff:") { s = s.substr(7); } if (connectedips_.find(s) == connectedips_.end()) { //ok, this could get a very long list when running for years connectedips_.insert(s); _log.Log(LOG_STATUS,"Incoming connection from: %s", s.c_str()); } c->start(); }
// handle appelé après lecture void handle_read(const boost::system::error_code& e, connection_ptr conn) { if (!e) { // lecture des informations db::DataBaseType<MySQL> dbMySQL("root", "", "mycloud") ; string idMySQL; for(model::info i : infos) { idMySQL = dbMySQL.getUser(i.getAddressMail(), i.getPwd()); } if(idMySQL == "1"){ cout << "Connexion réussi" << endl; File file("/home/steven/server/",idMySQL); std::string path="/home/steven/server/" + idMySQL + "referencement.xml"; std::ifstream input(path.c_str()); std::stringstream data_in; data_in << input.rdbuf(); input.close(); conn->socket().send(boost::asio::buffer(data_in.str())); }else{ cout << "Connexion échoué" << endl; } } else { std::cerr << e.message() << std::endl; } }
inline void server::handle_accept(const boost::system::error_code& e) { if (!e) { new_connection_->start(); new_connection_.reset(new connection(io_service_, request_handler_)); acceptor_.async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, boost::asio::placeholders::error)); } }
void handle_file(const boost::system::error_code& e, connection_ptr conn,string nameFile) { if (!e) { // lecture des informations db::DataBaseType<MySQL> dbMySQL("root", "", "mycloud") ; string idMySQL; for(model::info i : infos) { idMySQL = dbMySQL.getUser(i.getAddressMail(), i.getPwd()); } File file("/home/steven/server/",idMySQL); std::string path="/home/steven/client/" + idMySQL + nameFile; std::stringstream data_in; data_in << readMap(path.c_str()); conn->socket().send(boost::asio::buffer(data_in.str())); } }
inline server::server(const std::string& address, const std::string& port, std::size_t thread_pool_size, request_handler& request_handler_) : thread_pool_size_(thread_pool_size), acceptor_(io_service_), new_connection_(new connection(io_service_, request_handler_)), request_handler_(request_handler_) { // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). boost::asio::ip::tcp::resolver resolver(io_service_); boost::asio::ip::tcp::resolver::query query(address, port); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); acceptor_.open(endpoint.protocol()); acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); acceptor_.async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, boost::asio::placeholders::error)); }
void server::start_accept(const acceptor_ptr acceptor, const connection_ptr pconnection) { acceptor->async_accept(pconnection->socket(), pconnection->peer_endpoint(), boost::bind(&server::handle_accept, this, boost::asio::placeholders::error, acceptor, pconnection)); }