]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/winrt_ssocket_service_base.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP | |
12 | #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
21 | ||
22 | #include <boost/asio/buffer.hpp> | |
23 | #include <boost/asio/error.hpp> | |
92f5a8d4 | 24 | #include <boost/asio/execution_context.hpp> |
7c673cae | 25 | #include <boost/asio/socket_base.hpp> |
7c673cae | 26 | #include <boost/asio/detail/buffer_sequence_adapter.hpp> |
b32b8144 | 27 | #include <boost/asio/detail/memory.hpp> |
7c673cae FG |
28 | #include <boost/asio/detail/socket_types.hpp> |
29 | #include <boost/asio/detail/winrt_async_manager.hpp> | |
30 | #include <boost/asio/detail/winrt_socket_recv_op.hpp> | |
31 | #include <boost/asio/detail/winrt_socket_send_op.hpp> | |
32 | ||
92f5a8d4 TL |
33 | #if defined(BOOST_ASIO_HAS_IOCP) |
34 | # include <boost/asio/detail/win_iocp_io_context.hpp> | |
35 | #else // defined(BOOST_ASIO_HAS_IOCP) | |
36 | # include <boost/asio/detail/scheduler.hpp> | |
37 | #endif // defined(BOOST_ASIO_HAS_IOCP) | |
38 | ||
7c673cae FG |
39 | #include <boost/asio/detail/push_options.hpp> |
40 | ||
41 | namespace boost { | |
42 | namespace asio { | |
43 | namespace detail { | |
44 | ||
45 | class winrt_ssocket_service_base | |
46 | { | |
47 | public: | |
48 | // The native type of a socket. | |
49 | typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type; | |
50 | ||
51 | // The implementation type of the socket. | |
52 | struct base_implementation_type | |
53 | { | |
54 | // Default constructor. | |
55 | base_implementation_type() | |
56 | : socket_(nullptr), | |
57 | next_(0), | |
58 | prev_(0) | |
59 | { | |
60 | } | |
61 | ||
62 | // The underlying native socket. | |
63 | native_handle_type socket_; | |
64 | ||
65 | // Pointers to adjacent socket implementations in linked list. | |
66 | base_implementation_type* next_; | |
67 | base_implementation_type* prev_; | |
68 | }; | |
69 | ||
70 | // Constructor. | |
92f5a8d4 | 71 | BOOST_ASIO_DECL winrt_ssocket_service_base(execution_context& context); |
7c673cae FG |
72 | |
73 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 74 | BOOST_ASIO_DECL void base_shutdown(); |
7c673cae FG |
75 | |
76 | // Construct a new socket implementation. | |
77 | BOOST_ASIO_DECL void construct(base_implementation_type&); | |
78 | ||
79 | // Move-construct a new socket implementation. | |
80 | BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl, | |
92f5a8d4 | 81 | base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT; |
7c673cae FG |
82 | |
83 | // Move-assign from another socket implementation. | |
84 | BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl, | |
85 | winrt_ssocket_service_base& other_service, | |
86 | base_implementation_type& other_impl); | |
87 | ||
88 | // Destroy a socket implementation. | |
89 | BOOST_ASIO_DECL void destroy(base_implementation_type& impl); | |
90 | ||
91 | // Determine whether the socket is open. | |
92 | bool is_open(const base_implementation_type& impl) const | |
93 | { | |
94 | return impl.socket_ != nullptr; | |
95 | } | |
96 | ||
97 | // Destroy a socket implementation. | |
98 | BOOST_ASIO_DECL boost::system::error_code close( | |
99 | base_implementation_type& impl, boost::system::error_code& ec); | |
100 | ||
b32b8144 FG |
101 | // Release ownership of the socket. |
102 | BOOST_ASIO_DECL native_handle_type release( | |
103 | base_implementation_type& impl, boost::system::error_code& ec); | |
104 | ||
7c673cae FG |
105 | // Get the native socket representation. |
106 | native_handle_type native_handle(base_implementation_type& impl) | |
107 | { | |
108 | return impl.socket_; | |
109 | } | |
110 | ||
111 | // Cancel all operations associated with the socket. | |
112 | boost::system::error_code cancel(base_implementation_type&, | |
113 | boost::system::error_code& ec) | |
114 | { | |
115 | ec = boost::asio::error::operation_not_supported; | |
116 | return ec; | |
117 | } | |
118 | ||
119 | // Determine whether the socket is at the out-of-band data mark. | |
120 | bool at_mark(const base_implementation_type&, | |
121 | boost::system::error_code& ec) const | |
122 | { | |
123 | ec = boost::asio::error::operation_not_supported; | |
124 | return false; | |
125 | } | |
126 | ||
127 | // Determine the number of bytes available for reading. | |
128 | std::size_t available(const base_implementation_type&, | |
129 | boost::system::error_code& ec) const | |
130 | { | |
131 | ec = boost::asio::error::operation_not_supported; | |
132 | return 0; | |
133 | } | |
134 | ||
135 | // Perform an IO control command on the socket. | |
136 | template <typename IO_Control_Command> | |
137 | boost::system::error_code io_control(base_implementation_type&, | |
138 | IO_Control_Command&, boost::system::error_code& ec) | |
139 | { | |
140 | ec = boost::asio::error::operation_not_supported; | |
141 | return ec; | |
142 | } | |
143 | ||
144 | // Gets the non-blocking mode of the socket. | |
145 | bool non_blocking(const base_implementation_type&) const | |
146 | { | |
147 | return false; | |
148 | } | |
149 | ||
150 | // Sets the non-blocking mode of the socket. | |
151 | boost::system::error_code non_blocking(base_implementation_type&, | |
152 | bool, boost::system::error_code& ec) | |
153 | { | |
154 | ec = boost::asio::error::operation_not_supported; | |
155 | return ec; | |
156 | } | |
157 | ||
158 | // Gets the non-blocking mode of the native socket implementation. | |
159 | bool native_non_blocking(const base_implementation_type&) const | |
160 | { | |
161 | return false; | |
162 | } | |
163 | ||
164 | // Sets the non-blocking mode of the native socket implementation. | |
165 | boost::system::error_code native_non_blocking(base_implementation_type&, | |
166 | bool, boost::system::error_code& ec) | |
167 | { | |
168 | ec = boost::asio::error::operation_not_supported; | |
169 | return ec; | |
170 | } | |
171 | ||
7c673cae FG |
172 | // Send the given data to the peer. |
173 | template <typename ConstBufferSequence> | |
174 | std::size_t send(base_implementation_type& impl, | |
175 | const ConstBufferSequence& buffers, | |
176 | socket_base::message_flags flags, boost::system::error_code& ec) | |
177 | { | |
178 | return do_send(impl, | |
179 | buffer_sequence_adapter<boost::asio::const_buffer, | |
180 | ConstBufferSequence>::first(buffers), flags, ec); | |
181 | } | |
182 | ||
183 | // Wait until data can be sent without blocking. | |
184 | std::size_t send(base_implementation_type&, const null_buffers&, | |
185 | socket_base::message_flags, boost::system::error_code& ec) | |
186 | { | |
187 | ec = boost::asio::error::operation_not_supported; | |
188 | return 0; | |
189 | } | |
190 | ||
191 | // Start an asynchronous send. The data being sent must be valid for the | |
192 | // lifetime of the asynchronous operation. | |
92f5a8d4 | 193 | template <typename ConstBufferSequence, typename Handler, typename IoExecutor> |
7c673cae | 194 | void async_send(base_implementation_type& impl, |
92f5a8d4 TL |
195 | const ConstBufferSequence& buffers, socket_base::message_flags flags, |
196 | Handler& handler, const IoExecutor& io_ex) | |
7c673cae FG |
197 | { |
198 | bool is_continuation = | |
199 | boost_asio_handler_cont_helpers::is_continuation(handler); | |
200 | ||
201 | // Allocate and construct an operation to wrap the handler. | |
92f5a8d4 | 202 | typedef winrt_socket_send_op<ConstBufferSequence, Handler, IoExecutor> op; |
7c673cae | 203 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 | 204 | op::ptr::allocate(handler), 0 }; |
92f5a8d4 | 205 | p.p = new (p.v) op(buffers, handler, io_ex); |
7c673cae | 206 | |
92f5a8d4 | 207 | BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), |
b32b8144 | 208 | *p.p, "socket", &impl, 0, "async_send")); |
7c673cae FG |
209 | |
210 | start_send_op(impl, | |
211 | buffer_sequence_adapter<boost::asio::const_buffer, | |
212 | ConstBufferSequence>::first(buffers), | |
213 | flags, p.p, is_continuation); | |
214 | p.v = p.p = 0; | |
215 | } | |
216 | ||
217 | // Start an asynchronous wait until data can be sent without blocking. | |
92f5a8d4 | 218 | template <typename Handler, typename IoExecutor> |
7c673cae | 219 | void async_send(base_implementation_type&, const null_buffers&, |
92f5a8d4 | 220 | socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) |
7c673cae FG |
221 | { |
222 | boost::system::error_code ec = boost::asio::error::operation_not_supported; | |
223 | const std::size_t bytes_transferred = 0; | |
92f5a8d4 | 224 | boost::asio::post(io_ex, |
7c673cae FG |
225 | detail::bind_handler(handler, ec, bytes_transferred)); |
226 | } | |
227 | ||
228 | // Receive some data from the peer. Returns the number of bytes received. | |
229 | template <typename MutableBufferSequence> | |
230 | std::size_t receive(base_implementation_type& impl, | |
231 | const MutableBufferSequence& buffers, | |
232 | socket_base::message_flags flags, boost::system::error_code& ec) | |
233 | { | |
234 | return do_receive(impl, | |
235 | buffer_sequence_adapter<boost::asio::mutable_buffer, | |
236 | MutableBufferSequence>::first(buffers), flags, ec); | |
237 | } | |
238 | ||
239 | // Wait until data can be received without blocking. | |
240 | std::size_t receive(base_implementation_type&, const null_buffers&, | |
241 | socket_base::message_flags, boost::system::error_code& ec) | |
242 | { | |
243 | ec = boost::asio::error::operation_not_supported; | |
244 | return 0; | |
245 | } | |
246 | ||
247 | // Start an asynchronous receive. The buffer for the data being received | |
248 | // must be valid for the lifetime of the asynchronous operation. | |
92f5a8d4 TL |
249 | template <typename MutableBufferSequence, |
250 | typename Handler, typename IoExecutor> | |
7c673cae | 251 | void async_receive(base_implementation_type& impl, |
92f5a8d4 TL |
252 | const MutableBufferSequence& buffers, socket_base::message_flags flags, |
253 | Handler& handler, const IoExecutor& io_ex) | |
7c673cae FG |
254 | { |
255 | bool is_continuation = | |
256 | boost_asio_handler_cont_helpers::is_continuation(handler); | |
257 | ||
258 | // Allocate and construct an operation to wrap the handler. | |
92f5a8d4 | 259 | typedef winrt_socket_recv_op<MutableBufferSequence, Handler, IoExecutor> op; |
7c673cae | 260 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 | 261 | op::ptr::allocate(handler), 0 }; |
92f5a8d4 | 262 | p.p = new (p.v) op(buffers, handler, io_ex); |
7c673cae | 263 | |
92f5a8d4 | 264 | BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), |
b32b8144 | 265 | *p.p, "socket", &impl, 0, "async_receive")); |
7c673cae FG |
266 | |
267 | start_receive_op(impl, | |
268 | buffer_sequence_adapter<boost::asio::mutable_buffer, | |
269 | MutableBufferSequence>::first(buffers), | |
270 | flags, p.p, is_continuation); | |
271 | p.v = p.p = 0; | |
272 | } | |
273 | ||
274 | // Wait until data can be received without blocking. | |
92f5a8d4 | 275 | template <typename Handler, typename IoExecutor> |
7c673cae | 276 | void async_receive(base_implementation_type&, const null_buffers&, |
92f5a8d4 | 277 | socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) |
7c673cae FG |
278 | { |
279 | boost::system::error_code ec = boost::asio::error::operation_not_supported; | |
280 | const std::size_t bytes_transferred = 0; | |
92f5a8d4 | 281 | boost::asio::post(io_ex, |
7c673cae FG |
282 | detail::bind_handler(handler, ec, bytes_transferred)); |
283 | } | |
284 | ||
285 | protected: | |
286 | // Helper function to obtain endpoints associated with the connection. | |
287 | BOOST_ASIO_DECL std::size_t do_get_endpoint( | |
288 | const base_implementation_type& impl, bool local, | |
289 | void* addr, std::size_t addr_len, boost::system::error_code& ec) const; | |
290 | ||
291 | // Helper function to set a socket option. | |
292 | BOOST_ASIO_DECL boost::system::error_code do_set_option( | |
293 | base_implementation_type& impl, | |
294 | int level, int optname, const void* optval, | |
295 | std::size_t optlen, boost::system::error_code& ec); | |
296 | ||
297 | // Helper function to get a socket option. | |
298 | BOOST_ASIO_DECL void do_get_option( | |
299 | const base_implementation_type& impl, | |
300 | int level, int optname, void* optval, | |
301 | std::size_t* optlen, boost::system::error_code& ec) const; | |
302 | ||
303 | // Helper function to perform a synchronous connect. | |
304 | BOOST_ASIO_DECL boost::system::error_code do_connect( | |
305 | base_implementation_type& impl, | |
306 | const void* addr, boost::system::error_code& ec); | |
307 | ||
308 | // Helper function to start an asynchronous connect. | |
309 | BOOST_ASIO_DECL void start_connect_op( | |
310 | base_implementation_type& impl, const void* addr, | |
311 | winrt_async_op<void>* op, bool is_continuation); | |
312 | ||
313 | // Helper function to perform a synchronous send. | |
314 | BOOST_ASIO_DECL std::size_t do_send( | |
315 | base_implementation_type& impl, const boost::asio::const_buffer& data, | |
316 | socket_base::message_flags flags, boost::system::error_code& ec); | |
317 | ||
318 | // Helper function to start an asynchronous send. | |
319 | BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl, | |
320 | const boost::asio::const_buffer& data, socket_base::message_flags flags, | |
321 | winrt_async_op<unsigned int>* op, bool is_continuation); | |
322 | ||
323 | // Helper function to perform a synchronous receive. | |
324 | BOOST_ASIO_DECL std::size_t do_receive( | |
325 | base_implementation_type& impl, const boost::asio::mutable_buffer& data, | |
326 | socket_base::message_flags flags, boost::system::error_code& ec); | |
327 | ||
328 | // Helper function to start an asynchronous receive. | |
329 | BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl, | |
330 | const boost::asio::mutable_buffer& data, socket_base::message_flags flags, | |
331 | winrt_async_op<Windows::Storage::Streams::IBuffer^>* op, | |
332 | bool is_continuation); | |
333 | ||
92f5a8d4 TL |
334 | // The scheduler implementation used for delivering completions. |
335 | #if defined(BOOST_ASIO_HAS_IOCP) | |
336 | typedef class win_iocp_io_context scheduler_impl; | |
337 | #else | |
338 | typedef class scheduler scheduler_impl; | |
339 | #endif | |
340 | scheduler_impl& scheduler_; | |
7c673cae FG |
341 | |
342 | // The manager that keeps track of outstanding operations. | |
343 | winrt_async_manager& async_manager_; | |
344 | ||
345 | // Mutex to protect access to the linked list of implementations. | |
346 | boost::asio::detail::mutex mutex_; | |
347 | ||
348 | // The head of a linked list of all implementations. | |
349 | base_implementation_type* impl_list_; | |
350 | }; | |
351 | ||
352 | } // namespace detail | |
353 | } // namespace asio | |
354 | } // namespace boost | |
355 | ||
356 | #include <boost/asio/detail/pop_options.hpp> | |
357 | ||
358 | #if defined(BOOST_ASIO_HEADER_ONLY) | |
359 | # include <boost/asio/detail/impl/winrt_ssocket_service_base.ipp> | |
360 | #endif // defined(BOOST_ASIO_HEADER_ONLY) | |
361 | ||
362 | #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
363 | ||
364 | #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP |