Skip to content

Commit 2a6bffa

Browse files
committed
feat: add CancelIoEx support for accept operations on Windows
- Add virtual do_cancel() hook to overlapped_op base class - Implement accept_op::do_cancel() calling CancelIoEx on listen socket - Wire canceller to invoke do_cancel() after request_cancel()
1 parent 066183c commit 2a6bffa

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/detail/win_iocp_sockets.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ operator()()
7575
d(h).resume();
7676
}
7777

78+
void
79+
accept_op::
80+
do_cancel() noexcept
81+
{
82+
if (listen_socket != INVALID_SOCKET)
83+
{
84+
::CancelIoEx(
85+
reinterpret_cast<HANDLE>(listen_socket),
86+
this);
87+
}
88+
}
89+
7890
win_socket_impl::
7991
win_socket_impl(win_iocp_sockets& svc) noexcept
8092
: svc_(svc)

src/detail/win_iocp_sockets.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ struct accept_op : overlapped_op
7373

7474
/** Resume the coroutine after accept completes. */
7575
void operator()() override;
76+
77+
/** Cancel the pending accept via CancelIoEx. */
78+
void do_cancel() noexcept override;
7679
};
7780

7881
//------------------------------------------------------------------------------

src/detail/win_overlapped_op.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ struct overlapped_op
3636
struct canceller
3737
{
3838
overlapped_op* op;
39-
void operator()() const noexcept { op->request_cancel(); }
39+
void operator()() const noexcept
40+
{
41+
op->request_cancel();
42+
op->do_cancel();
43+
}
4044
};
4145

4246
capy::any_coro h;
@@ -108,6 +112,11 @@ struct overlapped_op
108112
cancelled.store(true, std::memory_order_release);
109113
}
110114

115+
/** Hook for derived classes to perform actual I/O cancellation. */
116+
virtual void do_cancel() noexcept
117+
{
118+
}
119+
111120
void start(std::stop_token token)
112121
{
113122
cancelled.store(false, std::memory_order_release);

0 commit comments

Comments
 (0)