1 // 2 // detail/winrt_ssocket_service_base.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 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> 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> 32 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 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. base_implementation_typeboost::asio::detail::winrt_ssocket_service_base::base_implementation_type55 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. 71 BOOST_ASIO_DECL winrt_ssocket_service_base(execution_context& context); 72 73 // Destroy all user-defined handler objects owned by the service. 74 BOOST_ASIO_DECL void base_shutdown(); 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, 81 base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT; 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. is_open(const base_implementation_type & impl) const92 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 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 105 // Get the native socket representation. native_handle(base_implementation_type & impl)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. cancel(base_implementation_type &,boost::system::error_code & ec)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. at_mark(const base_implementation_type &,boost::system::error_code & ec) const120 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. available(const base_implementation_type &,boost::system::error_code & ec) const128 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> io_control(base_implementation_type &,IO_Control_Command &,boost::system::error_code & ec)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. non_blocking(const base_implementation_type &) const145 bool non_blocking(const base_implementation_type&) const 146 { 147 return false; 148 } 149 150 // Sets the non-blocking mode of the socket. non_blocking(base_implementation_type &,bool,boost::system::error_code & ec)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. native_non_blocking(const base_implementation_type &) const159 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. native_non_blocking(base_implementation_type &,bool,boost::system::error_code & ec)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 172 // Send the given data to the peer. 173 template <typename ConstBufferSequence> send(base_implementation_type & impl,const ConstBufferSequence & buffers,socket_base::message_flags flags,boost::system::error_code & ec)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. send(base_implementation_type &,const null_buffers &,socket_base::message_flags,boost::system::error_code & ec)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. 193 template <typename ConstBufferSequence, typename Handler, typename IoExecutor> async_send(base_implementation_type & impl,const ConstBufferSequence & buffers,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)194 void async_send(base_implementation_type& impl, 195 const ConstBufferSequence& buffers, socket_base::message_flags flags, 196 Handler& handler, const IoExecutor& io_ex) 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. 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); 206 207 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), 208 *p.p, "socket", &impl, 0, "async_send")); 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. 218 template <typename Handler, typename IoExecutor> async_send(base_implementation_type &,const null_buffers &,socket_base::message_flags,Handler & handler,const IoExecutor & io_ex)219 void async_send(base_implementation_type&, const null_buffers&, 220 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) 221 { 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)); 226 } 227 228 // Receive some data from the peer. Returns the number of bytes received. 229 template <typename MutableBufferSequence> receive(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags flags,boost::system::error_code & ec)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. receive(base_implementation_type &,const null_buffers &,socket_base::message_flags,boost::system::error_code & ec)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. 249 template <typename MutableBufferSequence, 250 typename Handler, typename IoExecutor> async_receive(base_implementation_type & impl,const MutableBufferSequence & buffers,socket_base::message_flags flags,Handler & handler,const IoExecutor & io_ex)251 void async_receive(base_implementation_type& impl, 252 const MutableBufferSequence& buffers, socket_base::message_flags flags, 253 Handler& handler, const IoExecutor& io_ex) 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. 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); 263 264 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), 265 *p.p, "socket", &impl, 0, "async_receive")); 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. 275 template <typename Handler, typename IoExecutor> async_receive(base_implementation_type &,const null_buffers &,socket_base::message_flags,Handler & handler,const IoExecutor & io_ex)276 void async_receive(base_implementation_type&, const null_buffers&, 277 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) 278 { 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)); 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 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_; 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 365