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