Browse Source

Fix lifetime of 'frame' buffer in tcp server/client.

pull/177/head
Sergey Semushin 7 years ago committed by Anders Jenbo
parent
commit
c474692329
  1. 10
      SourceX/dvlnet/tcp_client.cpp
  2. 12
      SourceX/dvlnet/tcp_server.cpp

10
SourceX/dvlnet/tcp_client.cpp

@ -97,10 +97,12 @@ void tcp_client::handle_send(const asio::error_code& error, size_t bytes_sent)
void tcp_client::send(packet& pkt)
{
auto frame = frame_queue::make_frame(pkt.data());
asio::async_write(sock, asio::buffer(frame),
std::bind(&tcp_client::handle_send, this,
std::placeholders::_1, std::placeholders::_2));
auto frame = std::make_shared<buffer_t>(frame_queue::make_frame(pkt.data()));
auto buf = asio::buffer(*frame);
asio::async_write(sock, buf, [this, frame = std::move(frame)]
(const asio::error_code &error, size_t bytes_sent) {
handle_send(error, bytes_sent);
});
}
} // namespace net

12
SourceX/dvlnet/tcp_server.cpp

@ -135,11 +135,13 @@ void tcp_server::send_packet(packet& pkt)
void tcp_server::start_send(scc con, packet& pkt)
{
auto frame = frame_queue::make_frame(pkt.data());
asio::async_write(con->socket, asio::buffer(frame),
std::bind(&tcp_server::handle_send, this, con,
std::placeholders::_1, std::placeholders::_2));
auto frame = std::make_shared<buffer_t>(frame_queue::make_frame(pkt.data()));
auto buf = asio::buffer(*frame);
asio::async_write(con->socket, buf,
[this, con, frame = std::move(frame)]
(const asio::error_code &ec, size_t bytes_sent) {
handle_send(con, ec, bytes_sent);
});
}
void tcp_server::handle_send(scc con, const asio::error_code& ec,

Loading…
Cancel
Save