Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / asio / detail / winrt_ssocket_service.hpp
1 //
2 // detail/winrt_ssocket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_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/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>
28
29 #include <boost/asio/detail/push_options.hpp>
30
31 namespace boost {
32 namespace asio {
33 namespace detail {
34
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
39 {
40 public:
41   // The protocol type.
42   typedef Protocol protocol_type;
43
44   // The endpoint type.
45   typedef typename Protocol::endpoint endpoint_type;
46
47   // The native type of a socket.
48   typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
49
50   // The implementation type of the socket.
51   struct implementation_type : base_implementation_type
52   {
53     // Default constructor.
54     implementation_type()
55       : base_implementation_type(),
56         protocol_(endpoint_type().protocol())
57     {
58     }
59
60     // The protocol associated with the socket.
61     protocol_type protocol_;
62   };
63
64   // Constructor.
65   winrt_ssocket_service(execution_context& context)
66     : execution_context_service_base<winrt_ssocket_service<Protocol> >(context),
67       winrt_ssocket_service_base(context)
68   {
69   }
70
71   // Destroy all user-defined handler objects owned by the service.
72   void shutdown()
73   {
74     this->base_shutdown();
75   }
76
77   // Move-construct a new socket implementation.
78   void move_construct(implementation_type& impl,
79       implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
80   {
81     this->base_move_construct(impl, other_impl);
82
83     impl.protocol_ = other_impl.protocol_;
84     other_impl.protocol_ = endpoint_type().protocol();
85   }
86
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)
91   {
92     this->base_move_assign(impl, other_service, other_impl);
93
94     impl.protocol_ = other_impl.protocol_;
95     other_impl.protocol_ = endpoint_type().protocol();
96   }
97
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)
104   {
105     this->base_move_construct(impl, other_impl);
106
107     impl.protocol_ = protocol_type(other_impl.protocol_);
108     other_impl.protocol_ = typename Protocol1::endpoint().protocol();
109   }
110
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)
114   {
115     if (is_open(impl))
116     {
117       ec = boost::asio::error::already_open;
118       return ec;
119     }
120
121     try
122     {
123       impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
124       impl.protocol_ = protocol;
125       ec = boost::system::error_code();
126     }
127     catch (Platform::Exception^ e)
128     {
129       ec = boost::system::error_code(e->HResult,
130             boost::system::system_category());
131     }
132
133     return ec;
134   }
135
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)
140   {
141     if (is_open(impl))
142     {
143       ec = boost::asio::error::already_open;
144       return ec;
145     }
146
147     impl.socket_ = native_socket;
148     impl.protocol_ = protocol;
149     ec = boost::system::error_code();
150
151     return ec;
152   }
153
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)
157   {
158     ec = boost::asio::error::operation_not_supported;
159     return ec;
160   }
161
162   // Get the local endpoint.
163   endpoint_type local_endpoint(const implementation_type& impl,
164       boost::system::error_code& ec) const
165   {
166     endpoint_type endpoint;
167     endpoint.resize(do_get_endpoint(impl, true,
168           endpoint.data(), endpoint.size(), ec));
169     return endpoint;
170   }
171
172   // Get the remote endpoint.
173   endpoint_type remote_endpoint(const implementation_type& impl,
174       boost::system::error_code& ec) const
175   {
176     endpoint_type endpoint;
177     endpoint.resize(do_get_endpoint(impl, false,
178           endpoint.data(), endpoint.size(), ec));
179     return endpoint;
180   }
181
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)
185   {
186     ec = boost::asio::error::operation_not_supported;
187     return ec;
188   }
189
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)
194   {
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);
198   }
199
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
204   {
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);
209     if (!ec)
210       option.resize(impl.protocol_, size);
211     return ec;
212   }
213
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)
217   {
218     return do_connect(impl, peer_endpoint.data(), ec);
219   }
220
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)
226   {
227     bool is_continuation =
228       boost_asio_handler_cont_helpers::is_continuation(handler);
229
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);
235
236     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
237           *p.p, "socket", &impl, 0, "async_connect"));
238
239     start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
240     p.v = p.p = 0;
241   }
242 };
243
244 } // namespace detail
245 } // namespace asio
246 } // namespace boost
247
248 #include <boost/asio/detail/pop_options.hpp>
249
250 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
251
252 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP