Chris@16
|
1 //
|
Chris@16
|
2 // detail/winrt_ssocket_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/asio/error.hpp>
|
Chris@16
|
23 #include <boost/asio/io_service.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/winrt_socket_connect_op.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/winrt_ssocket_service_base.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/winrt_utils.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace asio {
|
Chris@16
|
33 namespace detail {
|
Chris@16
|
34
|
Chris@16
|
35 template <typename Protocol>
|
Chris@16
|
36 class winrt_ssocket_service :
|
Chris@16
|
37 public winrt_ssocket_service_base
|
Chris@16
|
38 {
|
Chris@16
|
39 public:
|
Chris@16
|
40 // The protocol type.
|
Chris@16
|
41 typedef Protocol protocol_type;
|
Chris@16
|
42
|
Chris@16
|
43 // The endpoint type.
|
Chris@16
|
44 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
45
|
Chris@16
|
46 // The native type of a socket.
|
Chris@16
|
47 typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
|
Chris@16
|
48
|
Chris@16
|
49 // The implementation type of the socket.
|
Chris@16
|
50 struct implementation_type : base_implementation_type
|
Chris@16
|
51 {
|
Chris@16
|
52 // Default constructor.
|
Chris@16
|
53 implementation_type()
|
Chris@16
|
54 : base_implementation_type(),
|
Chris@16
|
55 protocol_(endpoint_type().protocol())
|
Chris@16
|
56 {
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 // The protocol associated with the socket.
|
Chris@16
|
60 protocol_type protocol_;
|
Chris@16
|
61 };
|
Chris@16
|
62
|
Chris@16
|
63 // Constructor.
|
Chris@16
|
64 winrt_ssocket_service(boost::asio::io_service& io_service)
|
Chris@16
|
65 : winrt_ssocket_service_base(io_service)
|
Chris@16
|
66 {
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 // Move-construct a new socket implementation.
|
Chris@16
|
70 void move_construct(implementation_type& impl,
|
Chris@16
|
71 implementation_type& other_impl)
|
Chris@16
|
72 {
|
Chris@16
|
73 this->base_move_construct(impl, other_impl);
|
Chris@16
|
74
|
Chris@16
|
75 impl.protocol_ = other_impl.protocol_;
|
Chris@16
|
76 other_impl.protocol_ = endpoint_type().protocol();
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 // Move-assign from another socket implementation.
|
Chris@16
|
80 void move_assign(implementation_type& impl,
|
Chris@16
|
81 winrt_ssocket_service& other_service,
|
Chris@16
|
82 implementation_type& other_impl)
|
Chris@16
|
83 {
|
Chris@16
|
84 this->base_move_assign(impl, other_service, other_impl);
|
Chris@16
|
85
|
Chris@16
|
86 impl.protocol_ = other_impl.protocol_;
|
Chris@16
|
87 other_impl.protocol_ = endpoint_type().protocol();
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 // Move-construct a new socket implementation from another protocol type.
|
Chris@16
|
91 template <typename Protocol1>
|
Chris@16
|
92 void converting_move_construct(implementation_type& impl,
|
Chris@16
|
93 typename winrt_ssocket_service<
|
Chris@16
|
94 Protocol1>::implementation_type& other_impl)
|
Chris@16
|
95 {
|
Chris@16
|
96 this->base_move_construct(impl, other_impl);
|
Chris@16
|
97
|
Chris@16
|
98 impl.protocol_ = protocol_type(other_impl.protocol_);
|
Chris@16
|
99 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 // Open a new socket implementation.
|
Chris@16
|
103 boost::system::error_code open(implementation_type& impl,
|
Chris@16
|
104 const protocol_type& protocol, boost::system::error_code& ec)
|
Chris@16
|
105 {
|
Chris@16
|
106 if (is_open(impl))
|
Chris@16
|
107 {
|
Chris@16
|
108 ec = boost::asio::error::already_open;
|
Chris@16
|
109 return ec;
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 try
|
Chris@16
|
113 {
|
Chris@16
|
114 impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket;
|
Chris@16
|
115 impl.protocol_ = protocol;
|
Chris@16
|
116 ec = boost::system::error_code();
|
Chris@16
|
117 }
|
Chris@16
|
118 catch (Platform::Exception^ e)
|
Chris@16
|
119 {
|
Chris@16
|
120 ec = boost::system::error_code(e->HResult,
|
Chris@16
|
121 boost::system::system_category());
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 return ec;
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 // Assign a native socket to a socket implementation.
|
Chris@16
|
128 boost::system::error_code assign(implementation_type& impl,
|
Chris@16
|
129 const protocol_type& protocol, const native_handle_type& native_socket,
|
Chris@16
|
130 boost::system::error_code& ec)
|
Chris@16
|
131 {
|
Chris@16
|
132 if (is_open(impl))
|
Chris@16
|
133 {
|
Chris@16
|
134 ec = boost::asio::error::already_open;
|
Chris@16
|
135 return ec;
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 impl.socket_ = native_socket;
|
Chris@16
|
139 impl.protocol_ = protocol;
|
Chris@16
|
140 ec = boost::system::error_code();
|
Chris@16
|
141
|
Chris@16
|
142 return ec;
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 // Bind the socket to the specified local endpoint.
|
Chris@16
|
146 boost::system::error_code bind(implementation_type&,
|
Chris@16
|
147 const endpoint_type&, boost::system::error_code& ec)
|
Chris@16
|
148 {
|
Chris@16
|
149 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
150 return ec;
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 // Get the local endpoint.
|
Chris@16
|
154 endpoint_type local_endpoint(const implementation_type& impl,
|
Chris@16
|
155 boost::system::error_code& ec) const
|
Chris@16
|
156 {
|
Chris@16
|
157 endpoint_type endpoint;
|
Chris@16
|
158 endpoint.resize(do_get_endpoint(impl, true,
|
Chris@16
|
159 endpoint.data(), endpoint.size(), ec));
|
Chris@16
|
160 return endpoint;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 // Get the remote endpoint.
|
Chris@16
|
164 endpoint_type remote_endpoint(const implementation_type& impl,
|
Chris@16
|
165 boost::system::error_code& ec) const
|
Chris@16
|
166 {
|
Chris@16
|
167 endpoint_type endpoint;
|
Chris@16
|
168 endpoint.resize(do_get_endpoint(impl, false,
|
Chris@16
|
169 endpoint.data(), endpoint.size(), ec));
|
Chris@16
|
170 return endpoint;
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 // Set a socket option.
|
Chris@16
|
174 template <typename Option>
|
Chris@16
|
175 boost::system::error_code set_option(implementation_type& impl,
|
Chris@16
|
176 const Option& option, boost::system::error_code& ec)
|
Chris@16
|
177 {
|
Chris@16
|
178 return do_set_option(impl, option.level(impl.protocol_),
|
Chris@16
|
179 option.name(impl.protocol_), option.data(impl.protocol_),
|
Chris@16
|
180 option.size(impl.protocol_), ec);
|
Chris@16
|
181 }
|
Chris@16
|
182
|
Chris@16
|
183 // Get a socket option.
|
Chris@16
|
184 template <typename Option>
|
Chris@16
|
185 boost::system::error_code get_option(const implementation_type& impl,
|
Chris@16
|
186 Option& option, boost::system::error_code& ec) const
|
Chris@16
|
187 {
|
Chris@16
|
188 std::size_t size = option.size(impl.protocol_);
|
Chris@16
|
189 do_get_option(impl, option.level(impl.protocol_),
|
Chris@16
|
190 option.name(impl.protocol_),
|
Chris@16
|
191 option.data(impl.protocol_), &size, ec);
|
Chris@16
|
192 if (!ec)
|
Chris@16
|
193 option.resize(impl.protocol_, size);
|
Chris@16
|
194 return ec;
|
Chris@16
|
195 }
|
Chris@16
|
196
|
Chris@16
|
197 // Connect the socket to the specified endpoint.
|
Chris@16
|
198 boost::system::error_code connect(implementation_type& impl,
|
Chris@16
|
199 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
|
Chris@16
|
200 {
|
Chris@16
|
201 return do_connect(impl, peer_endpoint.data(), ec);
|
Chris@16
|
202 }
|
Chris@16
|
203
|
Chris@16
|
204 // Start an asynchronous connect.
|
Chris@16
|
205 template <typename Handler>
|
Chris@16
|
206 void async_connect(implementation_type& impl,
|
Chris@16
|
207 const endpoint_type& peer_endpoint, Handler& handler)
|
Chris@16
|
208 {
|
Chris@16
|
209 bool is_continuation =
|
Chris@16
|
210 boost_asio_handler_cont_helpers::is_continuation(handler);
|
Chris@16
|
211
|
Chris@16
|
212 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
213 typedef winrt_socket_connect_op<Handler> op;
|
Chris@16
|
214 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
215 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
216 sizeof(op), handler), 0 };
|
Chris@16
|
217 p.p = new (p.v) op(handler);
|
Chris@16
|
218
|
Chris@16
|
219 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));
|
Chris@16
|
220
|
Chris@16
|
221 start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation);
|
Chris@16
|
222 p.v = p.p = 0;
|
Chris@16
|
223 }
|
Chris@16
|
224 };
|
Chris@16
|
225
|
Chris@16
|
226 } // namespace detail
|
Chris@16
|
227 } // namespace asio
|
Chris@16
|
228 } // namespace boost
|
Chris@16
|
229
|
Chris@16
|
230 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
231
|
Chris@16
|
232 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
233
|
Chris@16
|
234 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
|