Skip to content

Commit 172e90a

Browse files
committed
test stream write cancellation support and beast2::write tests
1 parent 40f4977 commit 172e90a

File tree

6 files changed

+615
-166
lines changed

6 files changed

+615
-166
lines changed

include/boost/beast2/impl/write.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class write_some_op
5454

5555
BOOST_ASIO_CORO_REENTER(*this)
5656
{
57+
self.reset_cancellation_state(
58+
asio::enable_total_cancellation());
59+
5760
rv = sr_.prepare();
5861
if(! rv)
5962
{
@@ -117,8 +120,17 @@ class write_op
117120
{
118121
BOOST_ASIO_CORO_REENTER(*this)
119122
{
123+
self.reset_cancellation_state(asio::enable_total_cancellation());
124+
120125
do
121126
{
127+
if(!!self.cancelled())
128+
{
129+
ec = asio::error::operation_aborted;
130+
131+
break; // goto upcall
132+
}
133+
122134
BOOST_ASIO_CORO_YIELD
123135
{
124136
BOOST_ASIO_HANDLER_LOCATION((
@@ -128,12 +140,13 @@ class write_op
128140
dest_, sr_, std::move(self));
129141
}
130142
n_ += bytes_transferred;
143+
131144
if(ec.failed())
132-
break;
145+
break; // goto upcall
133146
}
134147
while(! sr_.is_done());
135148

136-
// upcall
149+
// upcall:
137150
self.complete(ec, n_ );
138151
}
139152
}

include/boost/beast2/test/detail/stream_state.hpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class stream_service
6666

6767
//------------------------------------------------------------------------------
6868

69-
struct stream_read_op_base
69+
struct stream_op_base
7070
{
71-
virtual ~stream_read_op_base() = default;
71+
virtual ~stream_op_base() = default;
7272
virtual void operator()(system::error_code ec) = 0;
7373
};
7474

@@ -89,8 +89,9 @@ struct stream_state
8989
std::mutex m;
9090
std::string storage;
9191
buffers::string_buffer b;
92-
std::condition_variable cv;
93-
std::unique_ptr<stream_read_op_base> op;
92+
//std::condition_variable cv;
93+
std::unique_ptr<stream_op_base> rop;
94+
std::unique_ptr<stream_op_base> wop;
9495
stream_status code = stream_status::ok;
9596
fail_count* fc = nullptr;
9697
std::size_t nread = 0;
@@ -133,14 +134,17 @@ void
133134
stream_service::
134135
shutdown()
135136
{
136-
std::vector<std::unique_ptr<detail::stream_read_op_base>> v;
137-
std::lock_guard<std::mutex> g1(sp_->m_);
138-
v.reserve(sp_->v_.size());
139-
for(auto p : sp_->v_)
137+
std::vector<std::unique_ptr<detail::stream_op_base>> v;
140138
{
141-
std::lock_guard<std::mutex> g2(p->m);
142-
v.emplace_back(std::move(p->op));
143-
p->code = detail::stream_status::eof;
139+
std::lock_guard<std::mutex> g1(sp_->m_);
140+
v.reserve(2 * sp_->v_.size());
141+
for(auto p : sp_->v_)
142+
{
143+
std::lock_guard<std::mutex> g2(p->m);
144+
v.emplace_back(std::move(p->rop));
145+
v.emplace_back(std::move(p->wop));
146+
p->code = detail::stream_status::eof;
147+
}
144148
}
145149
}
146150

@@ -200,8 +204,11 @@ stream_state::
200204
~stream_state()
201205
{
202206
// cancel outstanding read
203-
if(op != nullptr)
204-
(*op)(asio::error::operation_aborted);
207+
if(rop != nullptr)
208+
(*rop)(asio::error::operation_aborted);
209+
// cancel outstanding write
210+
if(wop != nullptr)
211+
(*wop)(asio::error::operation_aborted);
205212
}
206213

207214
inline
@@ -223,16 +230,13 @@ void
223230
stream_state::
224231
notify_read()
225232
{
226-
if(op)
233+
if(rop)
227234
{
228-
auto op_ = std::move(op);
235+
auto op_ = std::move(rop);
229236
op_->operator()(system::error_code{});
230237
}
231-
else
232-
{
233-
cv.notify_all();
234-
}
235238
}
239+
236240
} // detail
237241
} // test
238242
} // beast2

0 commit comments

Comments
 (0)