forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_send_operation.hpp
More file actions
174 lines (128 loc) · 4.18 KB
/
Copy pathsocket_send_operation.hpp
File metadata and controls
174 lines (128 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_NET_SOCKET_SEND_OPERATION_HPP_INCLUDED
#define CPPCORO_NET_SOCKET_SEND_OPERATION_HPP_INCLUDED
#include <cppcoro/config.hpp>
#include <cppcoro/cancellation_token.hpp>
#include <cstdint>
#if CPPCORO_OS_WINNT
# include <cppcoro/detail/win32.hpp>
# include <cppcoro/detail/win32_overlapped_operation.hpp>
#elif CPPCORO_OS_LINUX
# include <cppcoro/detail/linux.hpp>
# include <cppcoro/detail/linux_async_operation.hpp>
#endif
namespace cppcoro::net
{
#if CPPCORO_OS_WINNT
class socket;
class socket_send_operation_impl
{
public:
socket_send_operation_impl(
socket& s,
const void* buffer,
std::size_t byteCount) noexcept
: m_socket(s)
, m_buffer(const_cast<void*>(buffer), byteCount)
{}
bool try_start(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
void cancel(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
private:
socket& m_socket;
cppcoro::detail::win32::wsabuf m_buffer;
};
class socket_send_operation
: public cppcoro::detail::win32_overlapped_operation<socket_send_operation>
{
public:
socket_send_operation(
socket& s,
const void* buffer,
std::size_t byteCount) noexcept
: m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::win32_overlapped_operation<socket_send_operation>;
bool try_start() noexcept { return m_impl.try_start(*this); }
socket_send_operation_impl m_impl;
};
class socket_send_operation_cancellable
: public cppcoro::detail::win32_overlapped_operation_cancellable<socket_send_operation_cancellable>
{
public:
socket_send_operation_cancellable(
socket& s,
const void* buffer,
std::size_t byteCount,
cancellation_token&& ct) noexcept
: cppcoro::detail::win32_overlapped_operation_cancellable<socket_send_operation_cancellable>(std::move(ct))
, m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::win32_overlapped_operation_cancellable<socket_send_operation_cancellable>;
bool try_start() noexcept { return m_impl.try_start(*this); }
void cancel() noexcept { return m_impl.cancel(*this); }
socket_send_operation_impl m_impl;
};
#elif CPPCORO_OS_LINUX
class socket;
class socket_send_operation_impl
{
public:
socket_send_operation_impl(
socket& s,
const void* buffer,
std::size_t byteCount) noexcept
: m_socket(s)
, m_buffer(buffer)
, m_byteCount(byteCount)
{}
bool try_start(cppcoro::detail::linux_async_operation_base& operation) noexcept;
void cancel(cppcoro::detail::linux_async_operation_base& operation) noexcept;
private:
socket& m_socket;
const void* m_buffer;
std::size_t m_byteCount;
};
class socket_send_operation
: public cppcoro::detail::linux_async_operation<socket_send_operation>
{
public:
socket_send_operation(
socket& s,
const void* buffer,
std::size_t byteCount,
cppcoro::detail::linux::message_queue* mq) noexcept
: cppcoro::detail::linux_async_operation<socket_send_operation>(mq)
, m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::linux_async_operation<socket_send_operation>;
bool try_start() noexcept { return m_impl.try_start(*this); }
socket_send_operation_impl m_impl;
};
class socket_send_operation_cancellable
: public cppcoro::detail::linux_async_operation_cancellable<socket_send_operation_cancellable>
{
public:
socket_send_operation_cancellable(
socket& s,
const void* buffer,
std::size_t byteCount,
cppcoro::detail::linux::message_queue* mq,
cancellation_token&& ct) noexcept
: cppcoro::detail::linux_async_operation_cancellable<socket_send_operation_cancellable>(mq, std::move(ct))
, m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::linux_async_operation_cancellable<socket_send_operation_cancellable>;
bool try_start() noexcept { return m_impl.try_start(*this); }
void cancel() noexcept { return m_impl.cancel(*this); }
socket_send_operation_impl m_impl;
};
#endif
}
#endif