Skip to content

Commit 791cbb6

Browse files
committed
feat: implement cancellation for read and write socket operations
- Add do_cancel() implementation for read_op using CancelIoEx - Add do_cancel() implementation for write_op using CancelIoEx - Add win_socket_impl reference to read_op and write_op structs - Initialize read and write operations with socket impl in constructor
1 parent 2a6bffa commit 791cbb6

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/detail/win_iocp_sockets.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,35 @@ do_cancel() noexcept
8787
}
8888
}
8989

90+
void
91+
read_op::
92+
do_cancel() noexcept
93+
{
94+
if (impl.is_open())
95+
{
96+
::CancelIoEx(
97+
reinterpret_cast<HANDLE>(impl.native_handle()),
98+
this);
99+
}
100+
}
101+
102+
void
103+
write_op::
104+
do_cancel() noexcept
105+
{
106+
if (impl.is_open())
107+
{
108+
::CancelIoEx(
109+
reinterpret_cast<HANDLE>(impl.native_handle()),
110+
this);
111+
}
112+
}
113+
90114
win_socket_impl::
91115
win_socket_impl(win_iocp_sockets& svc) noexcept
92116
: svc_(svc)
117+
, rd_(*this)
118+
, wr_(*this)
93119
{
94120
}
95121

src/detail/win_iocp_sockets.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ struct read_op : overlapped_op
4949
WSABUF wsabufs[max_buffers];
5050
DWORD wsabuf_count = 0;
5151
DWORD flags = 0;
52+
win_socket_impl& impl;
53+
54+
explicit read_op(win_socket_impl& impl_) noexcept : impl(impl_) {}
5255

5356
bool is_read_operation() const noexcept override { return true; }
57+
void do_cancel() noexcept override;
5458
};
5559

5660
/** Write operation state with buffer descriptors. */
@@ -59,6 +63,11 @@ struct write_op : overlapped_op
5963
static constexpr std::size_t max_buffers = 16;
6064
WSABUF wsabufs[max_buffers];
6165
DWORD wsabuf_count = 0;
66+
win_socket_impl& impl;
67+
68+
explicit write_op(win_socket_impl& impl_) noexcept : impl(impl_) {}
69+
70+
void do_cancel() noexcept override;
6271
};
6372

6473
/** Accept operation state. */

0 commit comments

Comments
 (0)