2 // detail/winrt_ssocket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
22 #include <boost/asio/buffer.hpp>
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/execution_context.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
27 #include <boost/asio/detail/memory.hpp>
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>
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)
39 #include <boost/asio/detail/push_options.hpp>
45 class winrt_ssocket_service_base
48 // The native type of a socket.
49 typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
51 // The implementation type of the socket.
52 struct base_implementation_type
54 // Default constructor.
55 base_implementation_type()
62 // The underlying native socket.
63 native_handle_type socket_;
65 // Pointers to adjacent socket implementations in linked list.
66 base_implementation_type* next_;
67 base_implementation_type* prev_;
71 BOOST_ASIO_DECL winrt_ssocket_service_base(execution_context& context);
73 // Destroy all user-defined handler objects owned by the service.
74 BOOST_ASIO_DECL void base_shutdown();
76 // Construct a new socket implementation.
77 BOOST_ASIO_DECL void construct(base_implementation_type&);
79 // Move-construct a new socket implementation.
80 BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
81 base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT;
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);
88 // Destroy a socket implementation.
89 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
91 // Determine whether the socket is open.
92 bool is_open(const base_implementation_type& impl) const
94 return impl.socket_ != nullptr;
97 // Destroy a socket implementation.
98 BOOST_ASIO_DECL boost::system::error_code close(
99 base_implementation_type& impl, boost::system::error_code& ec);
101 // Release ownership of the socket.
102 BOOST_ASIO_DECL native_handle_type release(
103 base_implementation_type& impl, boost::system::error_code& ec);
105 // Get the native socket representation.
106 native_handle_type native_handle(base_implementation_type& impl)
111 // Cancel all operations associated with the socket.
112 boost::system::error_code cancel(base_implementation_type&,
113 boost::system::error_code& ec)
115 ec = boost::asio::error::operation_not_supported;
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
123 ec = boost::asio::error::operation_not_supported;
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
131 ec = boost::asio::error::operation_not_supported;
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)
140 ec = boost::asio::error::operation_not_supported;
144 // Gets the non-blocking mode of the socket.
145 bool non_blocking(const base_implementation_type&) const
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)
154 ec = boost::asio::error::operation_not_supported;
158 // Gets the non-blocking mode of the native socket implementation.
159 bool native_non_blocking(const base_implementation_type&) const
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)
168 ec = boost::asio::error::operation_not_supported;
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)
179 buffer_sequence_adapter<boost::asio::const_buffer,
180 ConstBufferSequence>::first(buffers), flags, ec);
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)
187 ec = boost::asio::error::operation_not_supported;
191 // Start an asynchronous send. The data being sent must be valid for the
192 // lifetime of the asynchronous operation.
193 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
194 void async_send(base_implementation_type& impl,
195 const ConstBufferSequence& buffers, socket_base::message_flags flags,
196 Handler& handler, const IoExecutor& io_ex)
198 bool is_continuation =
199 boost_asio_handler_cont_helpers::is_continuation(handler);
201 // Allocate and construct an operation to wrap the handler.
202 typedef winrt_socket_send_op<ConstBufferSequence, Handler, IoExecutor> op;
203 typename op::ptr p = { boost::asio::detail::addressof(handler),
204 op::ptr::allocate(handler), 0 };
205 p.p = new (p.v) op(buffers, handler, io_ex);
207 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
208 *p.p, "socket", &impl, 0, "async_send"));
211 buffer_sequence_adapter<boost::asio::const_buffer,
212 ConstBufferSequence>::first(buffers),
213 flags, p.p, is_continuation);
217 // Start an asynchronous wait until data can be sent without blocking.
218 template <typename Handler, typename IoExecutor>
219 void async_send(base_implementation_type&, const null_buffers&,
220 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
222 boost::system::error_code ec = boost::asio::error::operation_not_supported;
223 const std::size_t bytes_transferred = 0;
224 boost::asio::post(io_ex,
225 detail::bind_handler(handler, ec, bytes_transferred));
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)
234 return do_receive(impl,
235 buffer_sequence_adapter<boost::asio::mutable_buffer,
236 MutableBufferSequence>::first(buffers), flags, ec);
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)
243 ec = boost::asio::error::operation_not_supported;
247 // Start an asynchronous receive. The buffer for the data being received
248 // must be valid for the lifetime of the asynchronous operation.
249 template <typename MutableBufferSequence,
250 typename Handler, typename IoExecutor>
251 void async_receive(base_implementation_type& impl,
252 const MutableBufferSequence& buffers, socket_base::message_flags flags,
253 Handler& handler, const IoExecutor& io_ex)
255 bool is_continuation =
256 boost_asio_handler_cont_helpers::is_continuation(handler);
258 // Allocate and construct an operation to wrap the handler.
259 typedef winrt_socket_recv_op<MutableBufferSequence, Handler, IoExecutor> op;
260 typename op::ptr p = { boost::asio::detail::addressof(handler),
261 op::ptr::allocate(handler), 0 };
262 p.p = new (p.v) op(buffers, handler, io_ex);
264 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
265 *p.p, "socket", &impl, 0, "async_receive"));
267 start_receive_op(impl,
268 buffer_sequence_adapter<boost::asio::mutable_buffer,
269 MutableBufferSequence>::first(buffers),
270 flags, p.p, is_continuation);
274 // Wait until data can be received without blocking.
275 template <typename Handler, typename IoExecutor>
276 void async_receive(base_implementation_type&, const null_buffers&,
277 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
279 boost::system::error_code ec = boost::asio::error::operation_not_supported;
280 const std::size_t bytes_transferred = 0;
281 boost::asio::post(io_ex,
282 detail::bind_handler(handler, ec, bytes_transferred));
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;
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);
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;
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);
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);
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);
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);
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);
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);
334 // The scheduler implementation used for delivering completions.
335 #if defined(BOOST_ASIO_HAS_IOCP)
336 typedef class win_iocp_io_context scheduler_impl;
338 typedef class scheduler scheduler_impl;
340 scheduler_impl& scheduler_;
342 // The manager that keeps track of outstanding operations.
343 winrt_async_manager& async_manager_;
345 // Mutex to protect access to the linked list of implementations.
346 boost::asio::detail::mutex mutex_;
348 // The head of a linked list of all implementations.
349 base_implementation_type* impl_list_;
352 } // namespace detail
356 #include <boost/asio/detail/pop_options.hpp>
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)
362 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
364 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP