2 // detail/winrt_ssocket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_service.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/addressof.hpp>
27 #include <boost/asio/detail/buffer_sequence_adapter.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_service& io_service);
68 // Destroy all user-defined handler objects owned by the service.
69 BOOST_ASIO_DECL void shutdown_service();
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 // Get the native socket representation.
97 native_handle_type native_handle(base_implementation_type& impl)
102 // Cancel all operations associated with the socket.
103 boost::system::error_code cancel(base_implementation_type&,
104 boost::system::error_code& ec)
106 ec = boost::asio::error::operation_not_supported;
110 // Determine whether the socket is at the out-of-band data mark.
111 bool at_mark(const base_implementation_type&,
112 boost::system::error_code& ec) const
114 ec = boost::asio::error::operation_not_supported;
118 // Determine the number of bytes available for reading.
119 std::size_t available(const base_implementation_type&,
120 boost::system::error_code& ec) const
122 ec = boost::asio::error::operation_not_supported;
126 // Perform an IO control command on the socket.
127 template <typename IO_Control_Command>
128 boost::system::error_code io_control(base_implementation_type&,
129 IO_Control_Command&, boost::system::error_code& ec)
131 ec = boost::asio::error::operation_not_supported;
135 // Gets the non-blocking mode of the socket.
136 bool non_blocking(const base_implementation_type&) const
141 // Sets the non-blocking mode of the socket.
142 boost::system::error_code non_blocking(base_implementation_type&,
143 bool, boost::system::error_code& ec)
145 ec = boost::asio::error::operation_not_supported;
149 // Gets the non-blocking mode of the native socket implementation.
150 bool native_non_blocking(const base_implementation_type&) const
155 // Sets the non-blocking mode of the native socket implementation.
156 boost::system::error_code native_non_blocking(base_implementation_type&,
157 bool, boost::system::error_code& ec)
159 ec = boost::asio::error::operation_not_supported;
163 // Disable sends or receives on the socket.
164 boost::system::error_code shutdown(base_implementation_type&,
165 socket_base::shutdown_type, boost::system::error_code& ec)
167 ec = boost::asio::error::operation_not_supported;
171 // Send the given data to the peer.
172 template <typename ConstBufferSequence>
173 std::size_t send(base_implementation_type& impl,
174 const ConstBufferSequence& buffers,
175 socket_base::message_flags flags, boost::system::error_code& ec)
178 buffer_sequence_adapter<boost::asio::const_buffer,
179 ConstBufferSequence>::first(buffers), flags, ec);
182 // Wait until data can be sent without blocking.
183 std::size_t send(base_implementation_type&, const null_buffers&,
184 socket_base::message_flags, boost::system::error_code& ec)
186 ec = boost::asio::error::operation_not_supported;
190 // Start an asynchronous send. The data being sent must be valid for the
191 // lifetime of the asynchronous operation.
192 template <typename ConstBufferSequence, typename Handler>
193 void async_send(base_implementation_type& impl,
194 const ConstBufferSequence& buffers,
195 socket_base::message_flags flags, Handler& handler)
197 bool is_continuation =
198 boost_asio_handler_cont_helpers::is_continuation(handler);
200 // Allocate and construct an operation to wrap the handler.
201 typedef winrt_socket_send_op<ConstBufferSequence, Handler> op;
202 typename op::ptr p = { boost::asio::detail::addressof(handler),
203 boost_asio_handler_alloc_helpers::allocate(
204 sizeof(op), handler), 0 };
205 p.p = new (p.v) op(buffers, handler);
207 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
210 buffer_sequence_adapter<boost::asio::const_buffer,
211 ConstBufferSequence>::first(buffers),
212 flags, p.p, is_continuation);
216 // Start an asynchronous wait until data can be sent without blocking.
217 template <typename Handler>
218 void async_send(base_implementation_type&, const null_buffers&,
219 socket_base::message_flags, Handler& handler)
221 boost::system::error_code ec = boost::asio::error::operation_not_supported;
222 const std::size_t bytes_transferred = 0;
223 io_service_.get_io_service().post(
224 detail::bind_handler(handler, ec, bytes_transferred));
227 // Receive some data from the peer. Returns the number of bytes received.
228 template <typename MutableBufferSequence>
229 std::size_t receive(base_implementation_type& impl,
230 const MutableBufferSequence& buffers,
231 socket_base::message_flags flags, boost::system::error_code& ec)
233 return do_receive(impl,
234 buffer_sequence_adapter<boost::asio::mutable_buffer,
235 MutableBufferSequence>::first(buffers), flags, ec);
238 // Wait until data can be received without blocking.
239 std::size_t receive(base_implementation_type&, const null_buffers&,
240 socket_base::message_flags, boost::system::error_code& ec)
242 ec = boost::asio::error::operation_not_supported;
246 // Start an asynchronous receive. The buffer for the data being received
247 // must be valid for the lifetime of the asynchronous operation.
248 template <typename MutableBufferSequence, typename Handler>
249 void async_receive(base_implementation_type& impl,
250 const MutableBufferSequence& buffers,
251 socket_base::message_flags flags, Handler& handler)
253 bool is_continuation =
254 boost_asio_handler_cont_helpers::is_continuation(handler);
256 // Allocate and construct an operation to wrap the handler.
257 typedef winrt_socket_recv_op<MutableBufferSequence, Handler> op;
258 typename op::ptr p = { boost::asio::detail::addressof(handler),
259 boost_asio_handler_alloc_helpers::allocate(
260 sizeof(op), handler), 0 };
261 p.p = new (p.v) op(buffers, handler);
263 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
265 start_receive_op(impl,
266 buffer_sequence_adapter<boost::asio::mutable_buffer,
267 MutableBufferSequence>::first(buffers),
268 flags, p.p, is_continuation);
272 // Wait until data can be received without blocking.
273 template <typename Handler>
274 void async_receive(base_implementation_type&, const null_buffers&,
275 socket_base::message_flags, Handler& handler)
277 boost::system::error_code ec = boost::asio::error::operation_not_supported;
278 const std::size_t bytes_transferred = 0;
279 io_service_.get_io_service().post(
280 detail::bind_handler(handler, ec, bytes_transferred));
284 // Helper function to obtain endpoints associated with the connection.
285 BOOST_ASIO_DECL std::size_t do_get_endpoint(
286 const base_implementation_type& impl, bool local,
287 void* addr, std::size_t addr_len, boost::system::error_code& ec) const;
289 // Helper function to set a socket option.
290 BOOST_ASIO_DECL boost::system::error_code do_set_option(
291 base_implementation_type& impl,
292 int level, int optname, const void* optval,
293 std::size_t optlen, boost::system::error_code& ec);
295 // Helper function to get a socket option.
296 BOOST_ASIO_DECL void do_get_option(
297 const base_implementation_type& impl,
298 int level, int optname, void* optval,
299 std::size_t* optlen, boost::system::error_code& ec) const;
301 // Helper function to perform a synchronous connect.
302 BOOST_ASIO_DECL boost::system::error_code do_connect(
303 base_implementation_type& impl,
304 const void* addr, boost::system::error_code& ec);
306 // Helper function to start an asynchronous connect.
307 BOOST_ASIO_DECL void start_connect_op(
308 base_implementation_type& impl, const void* addr,
309 winrt_async_op<void>* op, bool is_continuation);
311 // Helper function to perform a synchronous send.
312 BOOST_ASIO_DECL std::size_t do_send(
313 base_implementation_type& impl, const boost::asio::const_buffer& data,
314 socket_base::message_flags flags, boost::system::error_code& ec);
316 // Helper function to start an asynchronous send.
317 BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
318 const boost::asio::const_buffer& data, socket_base::message_flags flags,
319 winrt_async_op<unsigned int>* op, bool is_continuation);
321 // Helper function to perform a synchronous receive.
322 BOOST_ASIO_DECL std::size_t do_receive(
323 base_implementation_type& impl, const boost::asio::mutable_buffer& data,
324 socket_base::message_flags flags, boost::system::error_code& ec);
326 // Helper function to start an asynchronous receive.
327 BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
328 const boost::asio::mutable_buffer& data, socket_base::message_flags flags,
329 winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
330 bool is_continuation);
332 // The io_service implementation used for delivering completions.
333 io_service_impl& io_service_;
335 // The manager that keeps track of outstanding operations.
336 winrt_async_manager& async_manager_;
338 // Mutex to protect access to the linked list of implementations.
339 boost::asio::detail::mutex mutex_;
341 // The head of a linked list of all implementations.
342 base_implementation_type* impl_list_;
345 } // namespace detail
349 #include <boost/asio/detail/pop_options.hpp>
351 #if defined(BOOST_ASIO_HEADER_ONLY)
352 # include <boost/asio/detail/impl/winrt_ssocket_service_base.ipp>
353 #endif // defined(BOOST_ASIO_HEADER_ONLY)
355 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
357 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP