问题
Source code: https://think-async.com/Asio/asio-1.18.0/doc/asio/tutorial/tutdaytime7/src.html
tcp_server shows an intention to use socket on the heap, wrapped by a type called tcp_connection.
class tcp_server
{
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(io_context_);
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
asio::placeholders::error));
}
void handle_accept(tcp_connection::pointer new_connection,
const asio::error_code& error)
{
if (!error)
{
new_connection->start();
}
start_accept();
}
...
socket heap objects are managed by enable_shared_from_this
aka shared_ptr
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(asio::io_context& io_context)
{
return pointer(new tcp_connection(io_context));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
asio::async_write(socket_, asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this()));
}
...
While udp server just use member socket.
class udp_server
{
public:
udp_server(asio::io_context& io_context)
: socket_(io_context, udp::endpoint(udp::v4(), 13))
{
start_receive();
}
private:
void start_receive()
{
socket_.async_receive_from(
asio::buffer(recv_buffer_), remote_endpoint_,
boost::bind(&udp_server::handle_receive, this,
asio::placeholders::error));
}
void handle_receive(const asio::error_code& error)
{
if (!error)
{
boost::shared_ptr<std::string> message(
new std::string(make_daytime_string()));
socket_.async_send_to(asio::buffer(*message), remote_endpoint_,
boost::bind(&udp_server::handle_send, this, message));
start_receive();
}
}
void handle_send(boost::shared_ptr<std::string> /*message*/)
{
}
udp::socket socket_;
udp::endpoint remote_endpoint_;
boost::array<char, 1> recv_buffer_;
};
My question is why tcp acceptor and udp socket examples choose different approaches?
回答1:
The shared pointer is there to manage the lifetime of the connection.
In the case of TCP it is more common to have multiple connections which leads to a situation where you will likely have multiple connections instances with unrelated lifetimes.
UDP is connection-less and many times is used for one-shot messages.
In fact you can come up with scenarios where you'd use shared pointers with UDP (e.g. with "logical connections", such as streaming audio over UDP).
Also, conversely you CAN solve the lifetime puzzle differently (regardless of TCP/UDP). For example here I used a std::list
(for reference stability) judicious single-threaded access to it: How to pass a boost asio tcp socket to a thread for sending heartbeat to client or server ¹
¹ I previously compared that to a shared_ptr approach in this answer: How to eliminate crashes when destroying boost::asio entities on fly?, which might interest you as well
回答2:
Only to show different approaches ...
So considering the context, you may use one or another solution.
来源:https://stackoverflow.com/questions/65018766/why-in-asios-example-the-tcp-acceptor-pattern-uses-shared-pointer-model-wrappin