Chris@16
|
1 //
|
Chris@16
|
2 // detail/winrt_ssocket_service_base.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_BASE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_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/buffer.hpp>
|
Chris@16
|
23 #include <boost/asio/error.hpp>
|
Chris@16
|
24 #include <boost/asio/io_service.hpp>
|
Chris@16
|
25 #include <boost/asio/socket_base.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
|
Chris@16
|
28 #include <boost/asio/detail/socket_types.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/winrt_async_manager.hpp>
|
Chris@16
|
30 #include <boost/asio/detail/winrt_socket_recv_op.hpp>
|
Chris@16
|
31 #include <boost/asio/detail/winrt_socket_send_op.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace asio {
|
Chris@16
|
37 namespace detail {
|
Chris@16
|
38
|
Chris@16
|
39 class winrt_ssocket_service_base
|
Chris@16
|
40 {
|
Chris@16
|
41 public:
|
Chris@16
|
42 // The native type of a socket.
|
Chris@16
|
43 typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
|
Chris@16
|
44
|
Chris@16
|
45 // The implementation type of the socket.
|
Chris@16
|
46 struct base_implementation_type
|
Chris@16
|
47 {
|
Chris@16
|
48 // Default constructor.
|
Chris@16
|
49 base_implementation_type()
|
Chris@16
|
50 : socket_(nullptr),
|
Chris@16
|
51 next_(0),
|
Chris@16
|
52 prev_(0)
|
Chris@16
|
53 {
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 // The underlying native socket.
|
Chris@16
|
57 native_handle_type socket_;
|
Chris@16
|
58
|
Chris@16
|
59 // Pointers to adjacent socket implementations in linked list.
|
Chris@16
|
60 base_implementation_type* next_;
|
Chris@16
|
61 base_implementation_type* prev_;
|
Chris@16
|
62 };
|
Chris@16
|
63
|
Chris@16
|
64 // Constructor.
|
Chris@16
|
65 BOOST_ASIO_DECL winrt_ssocket_service_base(
|
Chris@16
|
66 boost::asio::io_service& io_service);
|
Chris@16
|
67
|
Chris@16
|
68 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
69 BOOST_ASIO_DECL void shutdown_service();
|
Chris@16
|
70
|
Chris@16
|
71 // Construct a new socket implementation.
|
Chris@16
|
72 BOOST_ASIO_DECL void construct(base_implementation_type&);
|
Chris@16
|
73
|
Chris@16
|
74 // Move-construct a new socket implementation.
|
Chris@16
|
75 BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
|
Chris@16
|
76 base_implementation_type& other_impl);
|
Chris@16
|
77
|
Chris@16
|
78 // Move-assign from another socket implementation.
|
Chris@16
|
79 BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
|
Chris@16
|
80 winrt_ssocket_service_base& other_service,
|
Chris@16
|
81 base_implementation_type& other_impl);
|
Chris@16
|
82
|
Chris@16
|
83 // Destroy a socket implementation.
|
Chris@16
|
84 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
|
Chris@16
|
85
|
Chris@16
|
86 // Determine whether the socket is open.
|
Chris@16
|
87 bool is_open(const base_implementation_type& impl) const
|
Chris@16
|
88 {
|
Chris@16
|
89 return impl.socket_ != nullptr;
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 // Destroy a socket implementation.
|
Chris@16
|
93 BOOST_ASIO_DECL boost::system::error_code close(
|
Chris@16
|
94 base_implementation_type& impl, boost::system::error_code& ec);
|
Chris@16
|
95
|
Chris@16
|
96 // Get the native socket representation.
|
Chris@16
|
97 native_handle_type native_handle(base_implementation_type& impl)
|
Chris@16
|
98 {
|
Chris@16
|
99 return impl.socket_;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 // Cancel all operations associated with the socket.
|
Chris@16
|
103 boost::system::error_code cancel(base_implementation_type&,
|
Chris@16
|
104 boost::system::error_code& ec)
|
Chris@16
|
105 {
|
Chris@16
|
106 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
107 return ec;
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 // Determine whether the socket is at the out-of-band data mark.
|
Chris@16
|
111 bool at_mark(const base_implementation_type&,
|
Chris@16
|
112 boost::system::error_code& ec) const
|
Chris@16
|
113 {
|
Chris@16
|
114 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
115 return false;
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 // Determine the number of bytes available for reading.
|
Chris@16
|
119 std::size_t available(const base_implementation_type&,
|
Chris@16
|
120 boost::system::error_code& ec) const
|
Chris@16
|
121 {
|
Chris@16
|
122 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
123 return 0;
|
Chris@16
|
124 }
|
Chris@16
|
125
|
Chris@16
|
126 // Perform an IO control command on the socket.
|
Chris@16
|
127 template <typename IO_Control_Command>
|
Chris@16
|
128 boost::system::error_code io_control(base_implementation_type&,
|
Chris@16
|
129 IO_Control_Command&, boost::system::error_code& ec)
|
Chris@16
|
130 {
|
Chris@16
|
131 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
132 return ec;
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 // Gets the non-blocking mode of the socket.
|
Chris@16
|
136 bool non_blocking(const base_implementation_type&) const
|
Chris@16
|
137 {
|
Chris@16
|
138 return false;
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 // Sets the non-blocking mode of the socket.
|
Chris@16
|
142 boost::system::error_code non_blocking(base_implementation_type&,
|
Chris@16
|
143 bool, boost::system::error_code& ec)
|
Chris@16
|
144 {
|
Chris@16
|
145 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
146 return ec;
|
Chris@16
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 // Gets the non-blocking mode of the native socket implementation.
|
Chris@16
|
150 bool native_non_blocking(const base_implementation_type&) const
|
Chris@16
|
151 {
|
Chris@16
|
152 return false;
|
Chris@16
|
153 }
|
Chris@16
|
154
|
Chris@16
|
155 // Sets the non-blocking mode of the native socket implementation.
|
Chris@16
|
156 boost::system::error_code native_non_blocking(base_implementation_type&,
|
Chris@16
|
157 bool, boost::system::error_code& ec)
|
Chris@16
|
158 {
|
Chris@16
|
159 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
160 return ec;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 // Disable sends or receives on the socket.
|
Chris@16
|
164 boost::system::error_code shutdown(base_implementation_type&,
|
Chris@16
|
165 socket_base::shutdown_type, boost::system::error_code& ec)
|
Chris@16
|
166 {
|
Chris@16
|
167 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
168 return ec;
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 // Send the given data to the peer.
|
Chris@16
|
172 template <typename ConstBufferSequence>
|
Chris@16
|
173 std::size_t send(base_implementation_type& impl,
|
Chris@16
|
174 const ConstBufferSequence& buffers,
|
Chris@16
|
175 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
176 {
|
Chris@16
|
177 return do_send(impl,
|
Chris@16
|
178 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
179 ConstBufferSequence>::first(buffers), flags, ec);
|
Chris@16
|
180 }
|
Chris@16
|
181
|
Chris@16
|
182 // Wait until data can be sent without blocking.
|
Chris@16
|
183 std::size_t send(base_implementation_type&, const null_buffers&,
|
Chris@16
|
184 socket_base::message_flags, boost::system::error_code& ec)
|
Chris@16
|
185 {
|
Chris@16
|
186 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
187 return 0;
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 // Start an asynchronous send. The data being sent must be valid for the
|
Chris@16
|
191 // lifetime of the asynchronous operation.
|
Chris@16
|
192 template <typename ConstBufferSequence, typename Handler>
|
Chris@16
|
193 void async_send(base_implementation_type& impl,
|
Chris@16
|
194 const ConstBufferSequence& buffers,
|
Chris@16
|
195 socket_base::message_flags flags, Handler& handler)
|
Chris@16
|
196 {
|
Chris@16
|
197 bool is_continuation =
|
Chris@16
|
198 boost_asio_handler_cont_helpers::is_continuation(handler);
|
Chris@16
|
199
|
Chris@16
|
200 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
201 typedef winrt_socket_send_op<ConstBufferSequence, Handler> op;
|
Chris@16
|
202 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
203 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
204 sizeof(op), handler), 0 };
|
Chris@16
|
205 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
206
|
Chris@16
|
207 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
|
Chris@16
|
208
|
Chris@16
|
209 start_send_op(impl,
|
Chris@16
|
210 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
211 ConstBufferSequence>::first(buffers),
|
Chris@16
|
212 flags, p.p, is_continuation);
|
Chris@16
|
213 p.v = p.p = 0;
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 // Start an asynchronous wait until data can be sent without blocking.
|
Chris@16
|
217 template <typename Handler>
|
Chris@16
|
218 void async_send(base_implementation_type&, const null_buffers&,
|
Chris@16
|
219 socket_base::message_flags, Handler& handler)
|
Chris@16
|
220 {
|
Chris@16
|
221 boost::system::error_code ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
222 const std::size_t bytes_transferred = 0;
|
Chris@16
|
223 io_service_.get_io_service().post(
|
Chris@16
|
224 detail::bind_handler(handler, ec, bytes_transferred));
|
Chris@16
|
225 }
|
Chris@16
|
226
|
Chris@16
|
227 // Receive some data from the peer. Returns the number of bytes received.
|
Chris@16
|
228 template <typename MutableBufferSequence>
|
Chris@16
|
229 std::size_t receive(base_implementation_type& impl,
|
Chris@16
|
230 const MutableBufferSequence& buffers,
|
Chris@16
|
231 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
232 {
|
Chris@16
|
233 return do_receive(impl,
|
Chris@16
|
234 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
235 MutableBufferSequence>::first(buffers), flags, ec);
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 // Wait until data can be received without blocking.
|
Chris@16
|
239 std::size_t receive(base_implementation_type&, const null_buffers&,
|
Chris@16
|
240 socket_base::message_flags, boost::system::error_code& ec)
|
Chris@16
|
241 {
|
Chris@16
|
242 ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
243 return 0;
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 // Start an asynchronous receive. The buffer for the data being received
|
Chris@16
|
247 // must be valid for the lifetime of the asynchronous operation.
|
Chris@16
|
248 template <typename MutableBufferSequence, typename Handler>
|
Chris@16
|
249 void async_receive(base_implementation_type& impl,
|
Chris@16
|
250 const MutableBufferSequence& buffers,
|
Chris@16
|
251 socket_base::message_flags flags, Handler& handler)
|
Chris@16
|
252 {
|
Chris@16
|
253 bool is_continuation =
|
Chris@16
|
254 boost_asio_handler_cont_helpers::is_continuation(handler);
|
Chris@16
|
255
|
Chris@16
|
256 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
257 typedef winrt_socket_recv_op<MutableBufferSequence, Handler> op;
|
Chris@16
|
258 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
259 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
260 sizeof(op), handler), 0 };
|
Chris@16
|
261 p.p = new (p.v) op(buffers, handler);
|
Chris@16
|
262
|
Chris@16
|
263 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
|
Chris@16
|
264
|
Chris@16
|
265 start_receive_op(impl,
|
Chris@16
|
266 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
267 MutableBufferSequence>::first(buffers),
|
Chris@16
|
268 flags, p.p, is_continuation);
|
Chris@16
|
269 p.v = p.p = 0;
|
Chris@16
|
270 }
|
Chris@16
|
271
|
Chris@16
|
272 // Wait until data can be received without blocking.
|
Chris@16
|
273 template <typename Handler>
|
Chris@16
|
274 void async_receive(base_implementation_type&, const null_buffers&,
|
Chris@16
|
275 socket_base::message_flags, Handler& handler)
|
Chris@16
|
276 {
|
Chris@16
|
277 boost::system::error_code ec = boost::asio::error::operation_not_supported;
|
Chris@16
|
278 const std::size_t bytes_transferred = 0;
|
Chris@16
|
279 io_service_.get_io_service().post(
|
Chris@16
|
280 detail::bind_handler(handler, ec, bytes_transferred));
|
Chris@16
|
281 }
|
Chris@16
|
282
|
Chris@16
|
283 protected:
|
Chris@16
|
284 // Helper function to obtain endpoints associated with the connection.
|
Chris@16
|
285 BOOST_ASIO_DECL std::size_t do_get_endpoint(
|
Chris@16
|
286 const base_implementation_type& impl, bool local,
|
Chris@16
|
287 void* addr, std::size_t addr_len, boost::system::error_code& ec) const;
|
Chris@16
|
288
|
Chris@16
|
289 // Helper function to set a socket option.
|
Chris@16
|
290 BOOST_ASIO_DECL boost::system::error_code do_set_option(
|
Chris@16
|
291 base_implementation_type& impl,
|
Chris@16
|
292 int level, int optname, const void* optval,
|
Chris@16
|
293 std::size_t optlen, boost::system::error_code& ec);
|
Chris@16
|
294
|
Chris@16
|
295 // Helper function to get a socket option.
|
Chris@16
|
296 BOOST_ASIO_DECL void do_get_option(
|
Chris@16
|
297 const base_implementation_type& impl,
|
Chris@16
|
298 int level, int optname, void* optval,
|
Chris@16
|
299 std::size_t* optlen, boost::system::error_code& ec) const;
|
Chris@16
|
300
|
Chris@16
|
301 // Helper function to perform a synchronous connect.
|
Chris@16
|
302 BOOST_ASIO_DECL boost::system::error_code do_connect(
|
Chris@16
|
303 base_implementation_type& impl,
|
Chris@16
|
304 const void* addr, boost::system::error_code& ec);
|
Chris@16
|
305
|
Chris@16
|
306 // Helper function to start an asynchronous connect.
|
Chris@16
|
307 BOOST_ASIO_DECL void start_connect_op(
|
Chris@16
|
308 base_implementation_type& impl, const void* addr,
|
Chris@16
|
309 winrt_async_op<void>* op, bool is_continuation);
|
Chris@16
|
310
|
Chris@16
|
311 // Helper function to perform a synchronous send.
|
Chris@16
|
312 BOOST_ASIO_DECL std::size_t do_send(
|
Chris@16
|
313 base_implementation_type& impl, const boost::asio::const_buffer& data,
|
Chris@16
|
314 socket_base::message_flags flags, boost::system::error_code& ec);
|
Chris@16
|
315
|
Chris@16
|
316 // Helper function to start an asynchronous send.
|
Chris@16
|
317 BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
|
Chris@16
|
318 const boost::asio::const_buffer& data, socket_base::message_flags flags,
|
Chris@16
|
319 winrt_async_op<unsigned int>* op, bool is_continuation);
|
Chris@16
|
320
|
Chris@16
|
321 // Helper function to perform a synchronous receive.
|
Chris@16
|
322 BOOST_ASIO_DECL std::size_t do_receive(
|
Chris@16
|
323 base_implementation_type& impl, const boost::asio::mutable_buffer& data,
|
Chris@16
|
324 socket_base::message_flags flags, boost::system::error_code& ec);
|
Chris@16
|
325
|
Chris@16
|
326 // Helper function to start an asynchronous receive.
|
Chris@16
|
327 BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
|
Chris@16
|
328 const boost::asio::mutable_buffer& data, socket_base::message_flags flags,
|
Chris@16
|
329 winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
|
Chris@16
|
330 bool is_continuation);
|
Chris@16
|
331
|
Chris@16
|
332 // The io_service implementation used for delivering completions.
|
Chris@16
|
333 io_service_impl& io_service_;
|
Chris@16
|
334
|
Chris@16
|
335 // The manager that keeps track of outstanding operations.
|
Chris@16
|
336 winrt_async_manager& async_manager_;
|
Chris@16
|
337
|
Chris@16
|
338 // Mutex to protect access to the linked list of implementations.
|
Chris@16
|
339 boost::asio::detail::mutex mutex_;
|
Chris@16
|
340
|
Chris@16
|
341 // The head of a linked list of all implementations.
|
Chris@16
|
342 base_implementation_type* impl_list_;
|
Chris@16
|
343 };
|
Chris@16
|
344
|
Chris@16
|
345 } // namespace detail
|
Chris@16
|
346 } // namespace asio
|
Chris@16
|
347 } // namespace boost
|
Chris@16
|
348
|
Chris@16
|
349 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
350
|
Chris@16
|
351 #if defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
352 # include <boost/asio/detail/impl/winrt_ssocket_service_base.ipp>
|
Chris@16
|
353 #endif // defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
354
|
Chris@16
|
355 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
356
|
Chris@16
|
357 #endif // BOOST_ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
|