Skip to content

Commit 667be09

Browse files
committed
net integartion
1 parent ab70029 commit 667be09

27 files changed

+964
-434
lines changed

.github/workflows/cmake.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: CMake
22

3-
on: [push, pull_request]
3+
on:
4+
- push
5+
- pull_request
46

57
env:
68
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)

include/cppcoro/detail/win32.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,18 @@ namespace cppcoro
164164

165165
bool operator!=(handle_t handle) const
166166
{
167-
return m_handle != handle;
168-
}
167+
return m_handle != handle; }
169168

170169
private:
171-
172170
handle_t m_handle;
173-
174171
};
175-
}
176-
}
172+
} // namespace win32
173+
174+
using dword_t = win32::dword_t;
175+
using handle_t = win32::handle_t;
176+
using sock_buf = win32::wsabuf;
177+
using safe_handle = win32::safe_handle;
178+
} // namespace detail
177179
}
178180

179181
#endif

include/cppcoro/detail/win32_overlapped_operation.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace cppcoro
100100
bool await_ready() const noexcept { return false; }
101101

102102
CPPCORO_NOINLINE
103-
bool await_suspend(cppcoro::coroutine_handle<> awaitingCoroutine)
103+
bool await_suspend(coroutine_handle<> awaitingCoroutine)
104104
{
105105
static_assert(std::is_base_of_v<win32_overlapped_operation, OPERATION>);
106106

@@ -127,7 +127,7 @@ namespace cppcoro
127127
operation->m_awaitingCoroutine.resume();
128128
}
129129

130-
cppcoro::coroutine_handle<> m_awaitingCoroutine;
130+
coroutine_handle<> m_awaitingCoroutine;
131131

132132
};
133133

@@ -186,7 +186,7 @@ namespace cppcoro
186186
}
187187

188188
CPPCORO_NOINLINE
189-
bool await_suspend(cppcoro::coroutine_handle<> awaitingCoroutine)
189+
bool await_suspend(coroutine_handle<> awaitingCoroutine)
190190
{
191191
static_assert(std::is_base_of_v<win32_overlapped_operation_cancellable, OPERATION>);
192192

@@ -367,10 +367,17 @@ namespace cppcoro
367367
std::atomic<state> m_state;
368368
cppcoro::cancellation_token m_cancellationToken;
369369
std::optional<cppcoro::cancellation_registration> m_cancellationCallback;
370-
cppcoro::coroutine_handle<> m_awaitingCoroutine;
371-
370+
coroutine_handle<> m_awaitingCoroutine;
372371
};
373-
}
372+
373+
using io_operation_base = win32_overlapped_operation_base;
374+
375+
template<typename OPERATION>
376+
using io_operation = win32_overlapped_operation<OPERATION>;
377+
378+
template<typename OPERATION>
379+
using io_operation_cancellable = win32_overlapped_operation_cancellable<OPERATION>;
380+
} // namespace detail
374381
}
375382

376383
#endif

include/cppcoro/file.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace cppcoro
4747
file_buffering_mode bufferingMode);
4848

4949
detail::safe_handle m_fileHandle;
50-
#ifdef CPPCORO_OS_LINUX
50+
#if CPPCORO_OS_LINUX
5151
io_service *m_ioService;
5252
#endif
5353
};

include/cppcoro/net/socket.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ namespace cppcoro
9292

9393
socket& operator=(socket&& other) noexcept;
9494

95-
#if CPPCORO_OS_WINNT
96-
/// Get the Win32 socket handle assocaited with this socket.
97-
cppcoro::detail::win32::socket_t native_handle() noexcept { return m_handle; }
95+
/// Get the socket handle associated with this socket.
96+
auto native_handle() noexcept { return m_handle; }
9897

98+
#if CPPCORO_OS_WINNT
9999
/// Query whether I/O operations that complete synchronously will skip posting
100100
/// an I/O completion event to the I/O completion port.
101101
///
@@ -246,16 +246,26 @@ namespace cppcoro
246246

247247
friend class socket_accept_operation_impl;
248248
friend class socket_connect_operation_impl;
249+
friend class socket_recv_from_operation_impl;
250+
friend class socket_recv_operation_impl;
251+
friend class socket_send_to_operation_impl;
249252

250253
#if CPPCORO_OS_WINNT
251254
explicit socket(
252255
cppcoro::detail::win32::socket_t handle,
253256
bool skipCompletionOnSuccess) noexcept;
257+
#elif CPPCORO_OS_LINUX
258+
explicit socket(detail::lnx::io_queue& ioQueue,
259+
cppcoro::detail::lnx::fd_t fd) noexcept;
254260
#endif
255261

256262
#if CPPCORO_OS_WINNT
257263
cppcoro::detail::win32::socket_t m_handle;
258264
bool m_skipCompletionOnSuccess;
265+
#elif CPPCORO_OS_LINUX
266+
cppcoro::detail::lnx::fd_t m_handle = -1;
267+
cppcoro::detail::lnx::io_queue& m_ioQueue;
268+
int m_recvFlags = 0;
259269
#endif
260270

261271
ip_endpoint m_localEndPoint;

include/cppcoro/net/socket_accept_operation.hpp

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
#if CPPCORO_OS_WINNT
1313
# include <cppcoro/detail/win32.hpp>
1414
# include <cppcoro/detail/win32_overlapped_operation.hpp>
15+
#elif CPPCORO_OS_LINUX
16+
# include <cppcoro/detail/linux_io_operation.hpp>
17+
#endif
1518

16-
# include <atomic>
17-
# include <optional>
19+
#include <cppcoro/coroutine.hpp>
20+
#include <atomic>
21+
#include <optional>
1822

1923
namespace cppcoro
2024
{
@@ -33,41 +37,53 @@ namespace cppcoro
3337
, m_acceptingSocket(acceptingSocket)
3438
{}
3539

36-
bool try_start(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
37-
void cancel(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
38-
void get_result(cppcoro::detail::win32_overlapped_operation_base& operation);
40+
bool try_start(cppcoro::detail::io_operation_base& operation) noexcept;
41+
void cancel(cppcoro::detail::io_operation_base& operation) noexcept;
42+
void get_result(cppcoro::detail::io_operation_base& operation);
3943

4044
private:
4145

4246
#if CPPCORO_COMPILER_MSVC
4347
# pragma warning(push)
44-
# pragma warning(disable : 4324) // Structure padded due to alignment
48+
# pragma warning(disable : 4324) // Structure padded due to alignment
4549
#endif
4650

4751
socket& m_listeningSocket;
4852
socket& m_acceptingSocket;
4953
alignas(8) std::uint8_t m_addressBuffer[88];
5054

55+
#if CPPCORO_OS_LINUX
56+
socklen_t m_addressBufferLength = sizeof(m_addressBuffer);
57+
#endif
58+
5159
#if CPPCORO_COMPILER_MSVC
52-
# pragma warning(pop)
60+
#pragma warning(pop)
5361
#endif
5462

5563
};
5664

5765
class socket_accept_operation
58-
: public cppcoro::detail::win32_overlapped_operation<socket_accept_operation>
66+
: public cppcoro::detail::io_operation<socket_accept_operation>
5967
{
6068
public:
6169

6270
socket_accept_operation(
71+
#if CPPCORO_OS_LINUX
72+
detail::lnx::io_queue& ioQueue,
73+
#endif
6374
socket& listeningSocket,
6475
socket& acceptingSocket) noexcept
65-
: m_impl(listeningSocket, acceptingSocket)
76+
: cppcoro::detail::io_operation<socket_accept_operation>{
77+
#if CPPCORO_OS_LINUX
78+
ioQueue,
79+
#endif
80+
}
81+
, m_impl(listeningSocket, acceptingSocket)
6682
{}
6783

6884
private:
6985

70-
friend class cppcoro::detail::win32_overlapped_operation<socket_accept_operation>;
86+
friend cppcoro::detail::io_operation<socket_accept_operation>;
7187

7288
bool try_start() noexcept { return m_impl.try_start(*this); }
7389
void get_result() { m_impl.get_result(*this); }
@@ -77,21 +93,28 @@ namespace cppcoro
7793
};
7894

7995
class socket_accept_operation_cancellable
80-
: public cppcoro::detail::win32_overlapped_operation_cancellable<socket_accept_operation_cancellable>
96+
: public cppcoro::detail::io_operation_cancellable<socket_accept_operation_cancellable>
8197
{
8298
public:
8399

84100
socket_accept_operation_cancellable(
101+
#if CPPCORO_OS_LINUX
102+
detail::lnx::io_queue& ioQueue,
103+
#endif
85104
socket& listeningSocket,
86105
socket& acceptingSocket,
87106
cancellation_token&& ct) noexcept
88-
: cppcoro::detail::win32_overlapped_operation_cancellable<socket_accept_operation_cancellable>(std::move(ct))
107+
: cppcoro::detail::io_operation_cancellable<socket_accept_operation_cancellable>(
108+
#if CPPCORO_OS_LINUX
109+
ioQueue,
110+
#endif
111+
std::move(ct))
89112
, m_impl(listeningSocket, acceptingSocket)
90113
{}
91114

92115
private:
93116

94-
friend class cppcoro::detail::win32_overlapped_operation_cancellable<socket_accept_operation_cancellable>;
117+
friend cppcoro::detail::io_operation_cancellable<socket_accept_operation_cancellable>;
95118

96119
bool try_start() noexcept { return m_impl.try_start(*this); }
97120
void cancel() noexcept { m_impl.cancel(*this); }
@@ -100,9 +123,7 @@ namespace cppcoro
100123
socket_accept_operation_impl m_impl;
101124

102125
};
103-
}
104-
}
105-
106-
#endif // CPPCORO_OS_WINNT
126+
} // namespace net
127+
} // namespace cppcoro
107128

108129
#endif

include/cppcoro/net/socket_connect_operation.hpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#if CPPCORO_OS_WINNT
1313
# include <cppcoro/detail/win32.hpp>
1414
# include <cppcoro/detail/win32_overlapped_operation.hpp>
15+
#elif CPPCORO_OS_LINUX
16+
# include <cppcoro/detail/linux_io_operation.hpp>
17+
#endif
1518

1619
namespace cppcoro
1720
{
@@ -30,31 +33,38 @@ namespace cppcoro
3033
, m_remoteEndPoint(remoteEndPoint)
3134
{}
3235

33-
bool try_start(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
34-
void cancel(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
35-
void get_result(cppcoro::detail::win32_overlapped_operation_base& operation);
36+
bool try_start(cppcoro::detail::io_operation_base& operation) noexcept;
37+
void cancel(cppcoro::detail::io_operation_base& operation) noexcept;
38+
void get_result(cppcoro::detail::io_operation_base& operation);
3639

3740
private:
3841

3942
socket& m_socket;
4043
ip_endpoint m_remoteEndPoint;
41-
4244
};
4345

4446
class socket_connect_operation
45-
: public cppcoro::detail::win32_overlapped_operation<socket_connect_operation>
47+
: public cppcoro::detail::io_operation<socket_connect_operation>
4648
{
4749
public:
4850

4951
socket_connect_operation(
52+
#if CPPCORO_OS_LINUX
53+
detail::lnx::io_queue &ioQueue,
54+
#endif
5055
socket& socket,
5156
const ip_endpoint& remoteEndPoint) noexcept
52-
: m_impl(socket, remoteEndPoint)
57+
: cppcoro::detail::io_operation<socket_connect_operation> {
58+
#if CPPCORO_OS_LINUX
59+
ioQueue,
60+
#endif
61+
}
62+
, m_impl(socket, remoteEndPoint)
5363
{}
5464

5565
private:
5666

57-
friend class cppcoro::detail::win32_overlapped_operation<socket_connect_operation>;
67+
friend cppcoro::detail::io_operation<socket_connect_operation>;
5868

5969
bool try_start() noexcept { return m_impl.try_start(*this); }
6070
decltype(auto) get_result() { return m_impl.get_result(*this); }
@@ -64,21 +74,28 @@ namespace cppcoro
6474
};
6575

6676
class socket_connect_operation_cancellable
67-
: public cppcoro::detail::win32_overlapped_operation_cancellable<socket_connect_operation_cancellable>
77+
: public cppcoro::detail::io_operation_cancellable<socket_connect_operation_cancellable>
6878
{
6979
public:
7080

7181
socket_connect_operation_cancellable(
82+
#if CPPCORO_OS_LINUX
83+
detail::lnx::io_queue &ioQueue,
84+
#endif
7285
socket& socket,
7386
const ip_endpoint& remoteEndPoint,
7487
cancellation_token&& ct) noexcept
75-
: cppcoro::detail::win32_overlapped_operation_cancellable<socket_connect_operation_cancellable>(std::move(ct))
88+
: cppcoro::detail::io_operation_cancellable<socket_connect_operation_cancellable> {
89+
#if CPPCORO_OS_LINUX
90+
ioQueue,
91+
#endif
92+
std::move(ct)}
7693
, m_impl(socket, remoteEndPoint)
7794
{}
7895

7996
private:
8097

81-
friend class cppcoro::detail::win32_overlapped_operation_cancellable<socket_connect_operation_cancellable>;
98+
friend cppcoro::detail::io_operation_cancellable<socket_connect_operation_cancellable>;
8299

83100
bool try_start() noexcept { return m_impl.try_start(*this); }
84101
void cancel() noexcept { m_impl.cancel(*this); }
@@ -90,6 +107,4 @@ namespace cppcoro
90107
}
91108
}
92109

93-
#endif // CPPCORO_OS_WINNT
94-
95110
#endif

0 commit comments

Comments
 (0)