HTTP reads and writes return bytes transferred (API Change):

* HTTP read and write operations now return the
  number of bytes transferred to or from the stream.

Actions Required:

* Modify HTTP read and/or write completion handlers to receive
  the extra std::size_t bytes_transferred parameter.
This commit is contained in:
Vinnie Falco
2017-09-03 20:37:28 -07:00
parent cc8246e27e
commit 49d9d47a0b
22 changed files with 460 additions and 230 deletions
@@ -247,7 +247,8 @@ class session : public std::enable_shared_from_this<session>
self_.strand_.wrap(std::bind(
&session::on_write,
self_.shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
};
@@ -305,12 +306,17 @@ public:
strand_.wrap(std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_read(boost::system::error_code ec)
on_read(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This means they closed the connection
if(ec == http::error::end_of_stream)
return do_close();
@@ -323,8 +329,12 @@ public:
}
void
on_write(boost::system::error_code ec)
on_write(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec == http::error::end_of_stream)
{
// This means we should close the connection, usually because
@@ -243,7 +243,8 @@ class session : public std::enable_shared_from_this<session>
self_.strand_.wrap(std::bind(
&session::on_write,
self_.shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
};
@@ -283,12 +284,17 @@ public:
strand_.wrap(std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_read(boost::system::error_code ec)
on_read(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This means they closed the connection
if(ec == http::error::end_of_stream)
return do_close();
@@ -301,8 +307,12 @@ public:
}
void
on_write(boost::system::error_code ec)
on_write(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec == http::error::end_of_stream)
{
// This means we should close the connection, usually because
@@ -172,7 +172,7 @@ private:
socket_,
buffer_,
*parser_,
[this](boost::beast::error_code ec)
[this](boost::beast::error_code ec, std::size_t)
{
if (ec)
accept();
@@ -220,7 +220,7 @@ private:
http::async_write(
socket_,
*string_serializer_,
[this](boost::beast::error_code ec)
[this](boost::beast::error_code ec, std::size_t)
{
socket_.shutdown(tcp::socket::shutdown_send, ec);
string_serializer_.reset();
@@ -276,7 +276,7 @@ private:
http::async_write(
socket_,
*file_serializer_,
[this](boost::beast::error_code ec)
[this](boost::beast::error_code ec, std::size_t)
{
socket_.shutdown(tcp::socket::shutdown_send, ec);
file_serializer_.reset();
+14 -4
View File
@@ -258,7 +258,8 @@ class session
self_.strand_.wrap(std::bind(
&session::on_write,
self_.derived().shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
};
@@ -296,12 +297,17 @@ public:
strand_.wrap(std::bind(
&session::on_read,
derived().shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_read(boost::system::error_code ec)
on_read(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This means they closed the connection
if(ec == http::error::end_of_stream)
return derived().do_eof();
@@ -314,8 +320,12 @@ public:
}
void
on_write(boost::system::error_code ec)
on_write(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec == http::error::end_of_stream)
{
// This means we should close the connection, usually because
@@ -87,8 +87,10 @@ private:
socket_,
buffer_,
request_,
[self](boost::beast::error_code ec)
[self](boost::beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(!ec)
self->process_request();
});
@@ -175,7 +177,7 @@ private:
http::async_write(
socket_,
response_,
[self](boost::beast::error_code ec)
[self](boost::beast::error_code ec, std::size_t)
{
self->socket_.shutdown(tcp::socket::shutdown_send, ec);
self->deadline_.cancel();
@@ -250,7 +250,8 @@ class session
self_.strand_.wrap(std::bind(
&session::loop,
self_.shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
};
@@ -282,13 +283,16 @@ public:
void
run()
{
loop();
loop({}, 0);
}
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
reenter(*this)
{
// Perform the SSL handshake
@@ -297,7 +301,8 @@ public:
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
0)));
if(ec)
return fail(ec, "handshake");
@@ -308,7 +313,8 @@ public:
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
if(ec == http::error::end_of_stream)
{
// The remote host closed the connection
@@ -337,7 +343,8 @@ public:
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
0)));
if(ec)
return fail(ec, "shutdown");
@@ -247,7 +247,8 @@ class session
self_.strand_.wrap(std::bind(
&session::loop,
self_.shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
}
};
@@ -276,13 +277,16 @@ public:
void
run()
{
loop();
loop({}, 0);
}
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
reenter(*this)
{
for(;;)
@@ -292,7 +296,8 @@ public:
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1)));
std::placeholders::_1,
std::placeholders::_2)));
if(ec == http::error::end_of_stream)
{
// The remote host closed the connection