2 // detail/winrt_ssocket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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/io_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 #include <boost/asio/detail/push_options.hpp>
39 class winrt_ssocket_service_base
42 // The native type of a socket.
43 typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
45 // The implementation type of the socket.
46 struct base_implementation_type
48 // Default constructor.
49 base_implementation_type()
56 // The underlying native socket.
57 native_handle_type socket_;
59 // Pointers to adjacent socket implementations in linked list.
60 base_implementation_type* next_;
61 base_implementation_type* prev_;
65 BOOST_ASIO_DECL winrt_ssocket_service_base(
66 boost::asio::io_context& io_context);
68 // Destroy all user-defined handler objects owned by the service.
69 BOOST_ASIO_DECL void base_shutdown();
71 // Construct a new socket implementation.
72 BOOST_ASIO_DECL void construct(base_implementation_type&);
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);
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);
83 // Destroy a socket implementation.
84 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
86 // Determine whether the socket is open.
87 bool is_open(const base_implementation_type& impl) const
89 return impl.socket_ != nullptr;
92 // Destroy a socket implementation.
93 BOOST_ASIO_DECL boost::system::error_code close(
94 base_implementation_type& impl, boost::system::error_code& ec);
96 // Release ownership of the socket.
97 BOOST_ASIO_DECL native_handle_type release(
98 base_implementation_type& impl, boost::system::error_code& ec);
100 // Get the native socket representation.
101 native_handle_type native_handle(base_implementation_type& impl)
106 // Cancel all operations associated with the socket.
107 boost::system::error_code cancel(base_implementation_type&,
108 boost::system::error_code& ec)
110 ec = boost::asio::error::operation_not_supported;
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
118 ec = boost::asio::error::operation_not_supported;
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
126 ec = boost::asio::error::operation_not_supported;
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)
135 ec = boost::asio::error::operation_not_supported;
139 // Gets the non-blocking mode of the socket.
140 bool non_blocking(const base_implementation_type&) const
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)
149 ec = boost::asio::error::operation_not_supported;
153 // Gets the non-blocking mode of the native socket implementation.
154 bool native_non_blocking(const base_implementation_type&) const
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)
163 ec = boost::asio::error::operation_not_supported;
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)
171 ec = boost::asio::error::operation_not_supported;
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)
182 buffer_sequence_adapter<boost::asio::const_buffer,
183 ConstBufferSequence>::first(buffers), flags, ec);
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)
190 ec = boost::asio::error::operation_not_supported;
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)
201 bool is_continuation =
202 boost_asio_handler_cont_helpers::is_continuation(handler);
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),
207 op::ptr::allocate(handler), 0 };
208 p.p = new (p.v) op(buffers, handler);
210 BOOST_ASIO_HANDLER_CREATION((io_context_.context(),
211 *p.p, "socket", &impl, 0, "async_send"));
214 buffer_sequence_adapter<boost::asio::const_buffer,
215 ConstBufferSequence>::first(buffers),
216 flags, p.p, is_continuation);
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)
225 boost::system::error_code ec = boost::asio::error::operation_not_supported;
226 const std::size_t bytes_transferred = 0;
227 io_context_.get_io_context().post(
228 detail::bind_handler(handler, ec, bytes_transferred));
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)
237 return do_receive(impl,
238 buffer_sequence_adapter<boost::asio::mutable_buffer,
239 MutableBufferSequence>::first(buffers), flags, ec);
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)
246 ec = boost::asio::error::operation_not_supported;
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)
257 bool is_continuation =
258 boost_asio_handler_cont_helpers::is_continuation(handler);
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),
263 op::ptr::allocate(handler), 0 };
264 p.p = new (p.v) op(buffers, handler);
266 BOOST_ASIO_HANDLER_CREATION((io_context_.context(),
267 *p.p, "socket", &impl, 0, "async_receive"));
269 start_receive_op(impl,
270 buffer_sequence_adapter<boost::asio::mutable_buffer,
271 MutableBufferSequence>::first(buffers),
272 flags, p.p, is_continuation);
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)
281 boost::system::error_code ec = boost::asio::error::operation_not_supported;
282 const std::size_t bytes_transferred = 0;
283 io_context_.get_io_context().post(
284 detail::bind_handler(handler, ec, bytes_transferred));
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;
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);
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;
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);
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);
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);
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);
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);
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);
336 // The io_context implementation used for delivering completions.
337 io_context_impl& io_context_;
339 // The manager that keeps track of outstanding operations.
340 winrt_async_manager& async_manager_;
342 // Mutex to protect access to the linked list of implementations.
343 boost::asio::detail::mutex mutex_;
345 // The head of a linked list of all implementations.
346 base_implementation_type* impl_list_;
349 } // namespace detail
353 #include <boost/asio/detail/pop_options.hpp>
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)
359 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
361 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP