Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 048c13c

Browse filesBrowse files
committed
handle write in io_context
1 parent 1989d4b commit 048c13c
Copy full SHA for 048c13c

File tree

1 file changed

+15
-10
lines changed
Filter options

1 file changed

+15
-10
lines changed

‎src/sdk/network/tcp/stcp_connection.cc

Copy file name to clipboardExpand all lines: src/sdk/network/tcp/stcp_connection.cc
+15-10Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void StcpConnection::start_read(shared_ptr_<Connection> &&connection) {
3131
}
3232
_socket->async_read_some(
3333
boost::asio::buffer(&buffer[partial_size], buffer.size() - partial_size),
34-
[this, connection = std::move(connection), partial_size](
34+
[ this, connection = std::move(connection), partial_size ](
3535
const boost::system::error_code &err, size_t transferred) mutable {
3636
read_loop_(std::move(connection), partial_size, err, transferred);
3737
});
@@ -60,16 +60,21 @@ void StcpConnection::WriteBuffer::add(const Message &message, int32_t rid,
6060
size += message.size();
6161
}
6262
void StcpConnection::WriteBuffer::write(WriteHandler &&callback) {
63-
std::lock_guard<std::mutex> write_lock(connection.mutex);
64-
boost::asio::async_write(
65-
*connection._socket,
66-
boost::asio::buffer(connection._write_buffer.data(), size),
67-
[callback = std::move(callback)](const boost::system::error_code &error,
68-
size_t bytes_transferred) {
69-
DSA_REF_GUARD;
63+
connection.socket().get_io_context().post([
64+
size = size, conn_ptr = connection.share_this<StcpConnection>(),
65+
callback = std::move(callback)
66+
]() {
67+
std::lock_guard<std::mutex> write_lock(conn_ptr->mutex);
68+
boost::asio::async_write(
69+
*conn_ptr->_socket,
70+
boost::asio::buffer(conn_ptr->_write_buffer.data(), size),
71+
[callback = std::move(callback)](const boost::system::error_code &error,
72+
size_t bytes_transferred) {
73+
DSA_REF_GUARD;
7074

71-
callback(error);
72-
});
75+
callback(error);
76+
});
77+
});
7378
}
7479

7580
ssl_socket::lowest_layer_type &StcpConnection::socket() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.