diff --git a/src/net/socket.cpp b/src/net/socket.cpp index 1c0da390f..9d0492942 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -265,7 +265,7 @@ asio::ssl::socket& socket::get_ssl() NOEXCEPT // ---------------------------------------------------------------------------- // protected -// write framed (ws) or unframed (tcp) fixed size (const buffer size). +// write message in a single frame (ws) or unframed fixed size bytes (tcp). void socket::async_write(const asio::const_buffer& buffer, bool binary, const count_handler& handler) NOEXCEPT { @@ -294,7 +294,7 @@ void socket::async_write(const asio::const_buffer& buffer, bool binary, } } -// read some (up to mutable buffer capacity). +// read ws frame or arbitrary tcp bytes, iterate (up to buffer capacity). void socket::async_read_some(const asio::mutable_buffer& buffer, const count_handler& handler) NOEXCEPT { @@ -320,7 +320,8 @@ void socket::async_read_some(const asio::mutable_buffer& buffer, } } -// read framed/ws (fails if beyond flat buffer capacity). +// read arbitrary tcp bytes, iterate (up to buffer capacity). +// read whole ws message in any number of frames (up to buffer capacity). void socket::async_read(http::flat_buffer& buffer, const count_handler& handler) NOEXCEPT { @@ -335,8 +336,11 @@ void socket::async_read(http::flat_buffer& buffer, } else { - // Use async_read_some() or async_read(mutable_buffer). - handler(error::operation_failed, {}); + constexpr auto size = rpc::writer::default_buffer; + VARIANT_DISPATCH_METHOD(get_tcp(), + async_read_some(buffer.prepare(size), + std::bind(&socket::handle_async, shared_from_this(), + _1, _2, handler, "async_read_some"))); } } catch (const std::exception& e) @@ -346,7 +350,7 @@ void socket::async_read(http::flat_buffer& buffer, } } -// read fixed/p2p (waits until mutable buffer is filled). +// read tcp fixed count of bytes (waits until mutable buffer is filled). void socket::async_read(const asio::mutable_buffer& buffer, const count_handler& handler) NOEXCEPT { diff --git a/src/net/socket_body.cpp b/src/net/socket_body.cpp index 46f50c903..583b6d134 100644 --- a/src/net/socket_body.cpp +++ b/src/net/socket_body.cpp @@ -63,7 +63,6 @@ void socket::do_body_read(boost_code ec, size_t total, const read_state::ptr& in, const count_handler& handler) NOEXCEPT { BC_ASSERT(stranded()); - constexpr auto size = rpc::writer::default_buffer; if (ec) { @@ -73,7 +72,13 @@ void socket::do_body_read(boost_code ec, size_t total, return; } - async_read_some(in->buffer.prepare(size), + // This reader is designed to accept native beast bodies, which require the + // buffer size to be known so that finish() is called only after all data + // is passed. For websockets this reads a full logical message into the + // flat buffer. For tcp the reader will iterate over the buffer using + // async_read_some here, and calls reader.finish() after each. So the body + // reader is usable on tcp only for bodies that allow finish() iteration. + async_read(in->buffer, std::bind(&socket::handle_body_read, shared_from_this(), _1, _2, total, in, handler)); }