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