2 // detail/winrt_ssocket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_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/error.hpp>
23 #include <boost/asio/execution_context.hpp>
24 #include <boost/asio/detail/memory.hpp>
25 #include <boost/asio/detail/winrt_socket_connect_op.hpp>
26 #include <boost/asio/detail/winrt_ssocket_service_base.hpp>
27 #include <boost/asio/detail/winrt_utils.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 template <typename Protocol>
36 class winrt_ssocket_service :
37 public execution_context_service_base<winrt_ssocket_service<Protocol> >,
38 public winrt_ssocket_service_base
42 typedef Protocol protocol_type;
45 typedef typename Protocol::endpoint endpoint_type;
47 // The native type of a socket.
48 typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
50 // The implementation type of the socket.
51 struct implementation_type : base_implementation_type
53 // Default constructor.
55 : base_implementation_type(),
56 protocol_(endpoint_type().protocol())
60 // The protocol associated with the socket.
61 protocol_type protocol_;
65 winrt_ssocket_service(execution_context& context)
66 : execution_context_service_base<winrt_ssocket_service<Protocol> >(context),
67 winrt_ssocket_service_base(context)
71 // Destroy all user-defined handler objects owned by the service.
74 this->base_shutdown();
77 // Move-construct a new socket implementation.
78 void move_construct(implementation_type& impl,
79 implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
81 this->base_move_construct(impl, other_impl);
83 impl.protocol_ = other_impl.protocol_;
84 other_impl.protocol_ = endpoint_type().protocol();
87 // Move-assign from another socket implementation.
88 void move_assign(implementation_type& impl,
89 winrt_ssocket_service& other_service,
90 implementation_type& other_impl)
92 this->base_move_assign(impl, other_service, other_impl);
94 impl.protocol_ = other_impl.protocol_;
95 other_impl.protocol_ = endpoint_type().protocol();
98 // Move-construct a new socket implementation from another protocol type.
99 template <typename Protocol1>
100 void converting_move_construct(implementation_type& impl,
101 winrt_ssocket_service<Protocol1>&,
102 typename winrt_ssocket_service<
103 Protocol1>::implementation_type& other_impl)
105 this->base_move_construct(impl, other_impl);
107 impl.protocol_ = protocol_type(other_impl.protocol_);
108 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
111 // Open a new socket implementation.
112 boost::system::error_code open(implementation_type& impl,
113 const protocol_type& protocol, boost::system::error_code& ec)
117 ec = boost::asio::error::already_open;
123 impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
124 impl.protocol_ = protocol;
125 ec = boost::system::error_code();
127 catch (Platform::Exception^ e)
129 ec = boost::system::error_code(e->HResult,
130 boost::system::system_category());
136 // Assign a native socket to a socket implementation.
137 boost::system::error_code assign(implementation_type& impl,
138 const protocol_type& protocol, const native_handle_type& native_socket,
139 boost::system::error_code& ec)
143 ec = boost::asio::error::already_open;
147 impl.socket_ = native_socket;
148 impl.protocol_ = protocol;
149 ec = boost::system::error_code();
154 // Bind the socket to the specified local endpoint.
155 boost::system::error_code bind(implementation_type&,
156 const endpoint_type&, boost::system::error_code& ec)
158 ec = boost::asio::error::operation_not_supported;
162 // Get the local endpoint.
163 endpoint_type local_endpoint(const implementation_type& impl,
164 boost::system::error_code& ec) const
166 endpoint_type endpoint;
167 endpoint.resize(do_get_endpoint(impl, true,
168 endpoint.data(), endpoint.size(), ec));
172 // Get the remote endpoint.
173 endpoint_type remote_endpoint(const implementation_type& impl,
174 boost::system::error_code& ec) const
176 endpoint_type endpoint;
177 endpoint.resize(do_get_endpoint(impl, false,
178 endpoint.data(), endpoint.size(), ec));
182 // Disable sends or receives on the socket.
183 boost::system::error_code shutdown(implementation_type&,
184 socket_base::shutdown_type, boost::system::error_code& ec)
186 ec = boost::asio::error::operation_not_supported;
190 // Set a socket option.
191 template <typename Option>
192 boost::system::error_code set_option(implementation_type& impl,
193 const Option& option, boost::system::error_code& ec)
195 return do_set_option(impl, option.level(impl.protocol_),
196 option.name(impl.protocol_), option.data(impl.protocol_),
197 option.size(impl.protocol_), ec);
200 // Get a socket option.
201 template <typename Option>
202 boost::system::error_code get_option(const implementation_type& impl,
203 Option& option, boost::system::error_code& ec) const
205 std::size_t size = option.size(impl.protocol_);
206 do_get_option(impl, option.level(impl.protocol_),
207 option.name(impl.protocol_),
208 option.data(impl.protocol_), &size, ec);
210 option.resize(impl.protocol_, size);
214 // Connect the socket to the specified endpoint.
215 boost::system::error_code connect(implementation_type& impl,
216 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
218 return do_connect(impl, peer_endpoint.data(), ec);
221 // Start an asynchronous connect.
222 template <typename Handler, typename IoExecutor>
223 void async_connect(implementation_type& impl,
224 const endpoint_type& peer_endpoint,
225 Handler& handler, const IoExecutor& io_ex)
227 bool is_continuation =
228 boost_asio_handler_cont_helpers::is_continuation(handler);
230 // Allocate and construct an operation to wrap the handler.
231 typedef winrt_socket_connect_op<Handler, IoExecutor> op;
232 typename op::ptr p = { boost::asio::detail::addressof(handler),
233 op::ptr::allocate(handler), 0 };
234 p.p = new (p.v) op(handler, io_ex);
236 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
237 *p.p, "socket", &impl, 0, "async_connect"));
239 start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
244 } // namespace detail
248 #include <boost/asio/detail/pop_options.hpp>
250 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
252 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP