gpt4 book ai didi

c++ - boost asio http服务器,如何停止?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:59 28 4
gpt4 key购买 nike

我正在使用 boost 附带的示例 http 服务器之一(在 doc/html/boost_asio/example/cpp03/http/server,或者在 http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.http_server )。

该示例打开一个接受器套接字并进行监听,将请求剥离到请求对象。服务器还有一个 boost asio signal_set,它注册信号处理程序并在收到 SIGINT 或 SIGTERM 时取消来自 io_service 的所有 asio 请求。按 CTRL-C 并收到信号,io_service 调用 handle_stop() 函数,然后调用 .close()所有 asio::ip::tcp::sockets。整个服务器正常关闭,io_service.run() 调用退出,程序结束。

我希望能够在一个线程中启动这个 http 服务器,然后以编程方式取消它,而不是使用信号。这样做的公认方式是什么?我删除了 signal_set 和信号处理程序,然后在另一个线程上启动了服务器。它可以很好地处理来自该新线程的 http 请求。我如何从另一个线程停止它?从不同的线程调用 asio::ip::tcp::socket::close() 是否安全?文档不清楚,只是这样做感觉非常不符合 asio 风格。事实上,当我尝试这样做时,只要我还没有接受任何 http 请求,它就可以正常工作。如果我只处理了一个 http 请求,进程就会在 boost 中崩溃:

>   test_ssl_server_sa.exe!boost::detail::sp_counted_base::add_ref_lock()  Line 81 + 0x3 bytes  C++
test_ssl_server_sa.exe!boost::detail::shared_count::shared_count(const boost::detail::weak_count & r={...}) Line 578 + 0x12 bytes C++
test_ssl_server_sa.exe!boost::shared_ptr<http::server::connection>::shared_ptr<http::server::connection><http::server::connection>(const boost::weak_ptr<http::server::connection> & r={...}) Line 405 + 0x3f bytes C++
test_ssl_server_sa.exe!boost::enable_shared_from_this<http::server::connection>::shared_from_this() Line 49 + 0xc bytes C++
test_ssl_server_sa.exe!http::server::connection::handle_handshake(const boost::system::error_code & error={...}) Line 83 + 0x11 bytes C++
test_ssl_server_sa.exe!boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>::operator()(http::server::connection * p=0x004b8fe8, const boost::system::error_code & a1={...}) Line 165 + 0x18 bytes C++
test_ssl_server_sa.exe!boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> >::operator()<boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list1<boost::system::error_code const &> >(boost::_bi::type<void> __formal={...}, boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &> & f={...}, boost::_bi::list1<boost::system::error_code const &> & a={...}, boost::_bi::type<void> __formal={...}) Line 314 C++
test_ssl_server_sa.exe!boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > >::operator()<boost::system::error_code>(const boost::system::error_code & a1={...}) Line 48 C++
test_ssl_server_sa.exe!boost::asio::ssl::detail::handshake_op::call_handler<boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >(boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > & handler={...}, const boost::system::error_code & ec={...}, const unsigned int & __formal=0) Line 55 C++
test_ssl_server_sa.exe!boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >::operator()(boost::system::error_code ec={...}, unsigned int bytes_transferred=0, int start=0) Line 276 C++
test_ssl_server_sa.exe!boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int>::operator()() Line 129 C++
test_ssl_server_sa.exe!boost::asio::asio_handler_invoke<boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int> >(boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int> & function={...}, ...) Line 70 C++
test_ssl_server_sa.exe!boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int>,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >(boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int> & function={...}, boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > & context={...}) Line 37 + 0x16 bytes C++
test_ssl_server_sa.exe!boost::asio::ssl::detail::asio_handler_invoke<boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int>,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >(boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int> & function={...}, boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > > * this_handler=0x00fefb70) Line 319 + 0x10 bytes C++
test_ssl_server_sa.exe!boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int>,boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > > >(boost::asio::detail::binder2<boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > >,boost::system::error_code,unsigned int> & function={...}, boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > > & context={...}) Line 37 + 0x16 bytes C++
test_ssl_server_sa.exe!boost::asio::detail::reactive_socket_recv_op<boost::asio::mutable_buffers_1,boost::asio::ssl::detail::io_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::ssl::detail::handshake_op,boost::_bi::bind_t<void,boost::_mfi::mf1<void,http::server::connection,boost::system::error_code const &>,boost::_bi::list2<boost::_bi::value<http::server::connection *>,boost::arg<1> > > > >::do_complete(boost::asio::detail::task_io_service * owner=0x00c1aca0, boost::asio::detail::task_io_service_operation * base=0x00c1f948, const boost::system::error_code & __formal={...}, const boost::system::error_code & __formal={...}) Line 110 + 0xd bytes C++
test_ssl_server_sa.exe!boost::asio::detail::task_io_service_operation::complete(boost::asio::detail::task_io_service & owner={...}, const boost::system::error_code & ec={...}, unsigned int bytes_transferred=0) Line 38 + 0x1a bytes C++
test_ssl_server_sa.exe!boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::win_mutex> & lock={...}, boost::asio::detail::task_io_service_thread_info & this_thread={...}, const boost::system::error_code & ec={...}) Line 386 C++
test_ssl_server_sa.exe!boost::asio::detail::task_io_service::run(boost::system::error_code & ec={...}) Line 153 + 0x1e bytes C++

我并不是说服务器可能没有不同的同步问题 - 特别是,看起来服务器在 boost::shared_ptr 上对连接对象进行多次访问时崩溃了。但我更感兴趣的是,从另一个线程关闭 asio io_service.run() 调用的正确方法是什么。当我想象我希望它如何工作时,我想象一个等待控制变量的 io_service 请求。如果我想关闭服务器,我会从另一个线程向控制变量发出信号,io_service 会在 io_service 线程上调用我的回调。从那里,它可以 .close() 任何套接字或以其他方式取消任何其他正在进行的请求。

最佳答案

您可以将 boost::asio::io_service::post 与来自试图停止 http 服务器的线程的 lambda 一起使用。 lambda 将在 http 服务器线程中执行。它可以在所有套接字上安全地调用 socket::close 。这将优雅地停止服务。

关于c++ - boost asio http服务器,如何停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com