Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / datagram_socket_service.hpp
1 //
2 // datagram_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_DATAGRAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DATAGRAM_SOCKET_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 #include <cstddef>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/error.hpp>
23 #include <boost/asio/io_service.hpp>
24
25 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
26 # include <boost/asio/detail/null_socket_service.hpp>
27 #elif defined(BOOST_ASIO_HAS_IOCP)
28 # include <boost/asio/detail/win_iocp_socket_service.hpp>
29 #else
30 # include <boost/asio/detail/reactive_socket_service.hpp>
31 #endif
32
33 #include <boost/asio/detail/push_options.hpp>
34
35 namespace boost {
36 namespace asio {
37
38 /// Default service implementation for a datagram socket.
39 template <typename Protocol>
40 class datagram_socket_service
41 #if defined(GENERATING_DOCUMENTATION)
42   : public boost::asio::io_service::service
43 #else
44   : public boost::asio::detail::service_base<datagram_socket_service<Protocol> >
45 #endif
46 {
47 public:
48 #if defined(GENERATING_DOCUMENTATION)
49   /// The unique service identifier.
50   static boost::asio::io_service::id id;
51 #endif
52
53   /// The protocol type.
54   typedef Protocol protocol_type;
55
56   /// The endpoint type.
57   typedef typename Protocol::endpoint endpoint_type;
58
59 private:
60   // The type of the platform-specific implementation.
61 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
62   typedef detail::null_socket_service<Protocol> service_impl_type;
63 #elif defined(BOOST_ASIO_HAS_IOCP)
64   typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
65 #else
66   typedef detail::reactive_socket_service<Protocol> service_impl_type;
67 #endif
68
69 public:
70   /// The type of a datagram socket.
71 #if defined(GENERATING_DOCUMENTATION)
72   typedef implementation_defined implementation_type;
73 #else
74   typedef typename service_impl_type::implementation_type implementation_type;
75 #endif
76
77   /// (Deprecated: Use native_handle_type.) The native socket type.
78 #if defined(GENERATING_DOCUMENTATION)
79   typedef implementation_defined native_type;
80 #else
81   typedef typename service_impl_type::native_handle_type native_type;
82 #endif
83
84   /// The native socket type.
85 #if defined(GENERATING_DOCUMENTATION)
86   typedef implementation_defined native_handle_type;
87 #else
88   typedef typename service_impl_type::native_handle_type native_handle_type;
89 #endif
90
91   /// Construct a new datagram socket service for the specified io_service.
92   explicit datagram_socket_service(boost::asio::io_service& io_service)
93     : boost::asio::detail::service_base<
94         datagram_socket_service<Protocol> >(io_service),
95       service_impl_(io_service)
96   {
97   }
98
99   /// Construct a new datagram socket implementation.
100   void construct(implementation_type& impl)
101   {
102     service_impl_.construct(impl);
103   }
104
105 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
106   /// Move-construct a new datagram socket implementation.
107   void move_construct(implementation_type& impl,
108       implementation_type& other_impl)
109   {
110     service_impl_.move_construct(impl, other_impl);
111   }
112
113   /// Move-assign from another datagram socket implementation.
114   void move_assign(implementation_type& impl,
115       datagram_socket_service& other_service,
116       implementation_type& other_impl)
117   {
118     service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
119   }
120
121   // All socket services have access to each other's implementations.
122   template <typename Protocol1> friend class datagram_socket_service;
123
124   /// Move-construct a new datagram socket implementation from another protocol
125   /// type.
126   template <typename Protocol1>
127   void converting_move_construct(implementation_type& impl,
128       datagram_socket_service<Protocol1>& other_service,
129       typename datagram_socket_service<
130         Protocol1>::implementation_type& other_impl,
131       typename enable_if<is_convertible<
132         Protocol1, Protocol>::value>::type* = 0)
133   {
134     service_impl_.template converting_move_construct<Protocol1>(
135         impl, other_service.service_impl_, other_impl);
136   }
137 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
138
139   /// Destroy a datagram socket implementation.
140   void destroy(implementation_type& impl)
141   {
142     service_impl_.destroy(impl);
143   }
144
145   // Open a new datagram socket implementation.
146   boost::system::error_code open(implementation_type& impl,
147       const protocol_type& protocol, boost::system::error_code& ec)
148   {
149     if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_DGRAM))
150       service_impl_.open(impl, protocol, ec);
151     else
152       ec = boost::asio::error::invalid_argument;
153     return ec;
154   }
155
156   /// Assign an existing native socket to a datagram socket.
157   boost::system::error_code assign(implementation_type& impl,
158       const protocol_type& protocol, const native_handle_type& native_socket,
159       boost::system::error_code& ec)
160   {
161     return service_impl_.assign(impl, protocol, native_socket, ec);
162   }
163
164   /// Determine whether the socket is open.
165   bool is_open(const implementation_type& impl) const
166   {
167     return service_impl_.is_open(impl);
168   }
169
170   /// Close a datagram socket implementation.
171   boost::system::error_code close(implementation_type& impl,
172       boost::system::error_code& ec)
173   {
174     return service_impl_.close(impl, ec);
175   }
176
177   /// (Deprecated: Use native_handle().) Get the native socket implementation.
178   native_type native(implementation_type& impl)
179   {
180     return service_impl_.native_handle(impl);
181   }
182
183   /// Get the native socket implementation.
184   native_handle_type native_handle(implementation_type& impl)
185   {
186     return service_impl_.native_handle(impl);
187   }
188
189   /// Cancel all asynchronous operations associated with the socket.
190   boost::system::error_code cancel(implementation_type& impl,
191       boost::system::error_code& ec)
192   {
193     return service_impl_.cancel(impl, ec);
194   }
195
196   /// Determine whether the socket is at the out-of-band data mark.
197   bool at_mark(const implementation_type& impl,
198       boost::system::error_code& ec) const
199   {
200     return service_impl_.at_mark(impl, ec);
201   }
202
203   /// Determine the number of bytes available for reading.
204   std::size_t available(const implementation_type& impl,
205       boost::system::error_code& ec) const
206   {
207     return service_impl_.available(impl, ec);
208   }
209
210   // Bind the datagram socket to the specified local endpoint.
211   boost::system::error_code bind(implementation_type& impl,
212       const endpoint_type& endpoint, boost::system::error_code& ec)
213   {
214     return service_impl_.bind(impl, endpoint, ec);
215   }
216
217   /// Connect the datagram socket to the specified endpoint.
218   boost::system::error_code connect(implementation_type& impl,
219       const endpoint_type& peer_endpoint, boost::system::error_code& ec)
220   {
221     return service_impl_.connect(impl, peer_endpoint, ec);
222   }
223
224   /// Start an asynchronous connect.
225   template <typename ConnectHandler>
226   BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
227       void (boost::system::error_code))
228   async_connect(implementation_type& impl,
229       const endpoint_type& peer_endpoint,
230       BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
231   {
232     detail::async_result_init<
233       ConnectHandler, void (boost::system::error_code)> init(
234         BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
235
236     service_impl_.async_connect(impl, peer_endpoint, init.handler);
237
238     return init.result.get();
239   }
240
241   /// Set a socket option.
242   template <typename SettableSocketOption>
243   boost::system::error_code set_option(implementation_type& impl,
244       const SettableSocketOption& option, boost::system::error_code& ec)
245   {
246     return service_impl_.set_option(impl, option, ec);
247   }
248
249   /// Get a socket option.
250   template <typename GettableSocketOption>
251   boost::system::error_code get_option(const implementation_type& impl,
252       GettableSocketOption& option, boost::system::error_code& ec) const
253   {
254     return service_impl_.get_option(impl, option, ec);
255   }
256
257   /// Perform an IO control command on the socket.
258   template <typename IoControlCommand>
259   boost::system::error_code io_control(implementation_type& impl,
260       IoControlCommand& command, boost::system::error_code& ec)
261   {
262     return service_impl_.io_control(impl, command, ec);
263   }
264
265   /// Gets the non-blocking mode of the socket.
266   bool non_blocking(const implementation_type& impl) const
267   {
268     return service_impl_.non_blocking(impl);
269   }
270
271   /// Sets the non-blocking mode of the socket.
272   boost::system::error_code non_blocking(implementation_type& impl,
273       bool mode, boost::system::error_code& ec)
274   {
275     return service_impl_.non_blocking(impl, mode, ec);
276   }
277
278   /// Gets the non-blocking mode of the native socket implementation.
279   bool native_non_blocking(const implementation_type& impl) const
280   {
281     return service_impl_.native_non_blocking(impl);
282   }
283
284   /// Sets the non-blocking mode of the native socket implementation.
285   boost::system::error_code native_non_blocking(implementation_type& impl,
286       bool mode, boost::system::error_code& ec)
287   {
288     return service_impl_.native_non_blocking(impl, mode, ec);
289   }
290
291   /// Get the local endpoint.
292   endpoint_type local_endpoint(const implementation_type& impl,
293       boost::system::error_code& ec) const
294   {
295     return service_impl_.local_endpoint(impl, ec);
296   }
297
298   /// Get the remote endpoint.
299   endpoint_type remote_endpoint(const implementation_type& impl,
300       boost::system::error_code& ec) const
301   {
302     return service_impl_.remote_endpoint(impl, ec);
303   }
304
305   /// Disable sends or receives on the socket.
306   boost::system::error_code shutdown(implementation_type& impl,
307       socket_base::shutdown_type what, boost::system::error_code& ec)
308   {
309     return service_impl_.shutdown(impl, what, ec);
310   }
311
312   /// Send the given data to the peer.
313   template <typename ConstBufferSequence>
314   std::size_t send(implementation_type& impl,
315       const ConstBufferSequence& buffers,
316       socket_base::message_flags flags, boost::system::error_code& ec)
317   {
318     return service_impl_.send(impl, buffers, flags, ec);
319   }
320
321   /// Start an asynchronous send.
322   template <typename ConstBufferSequence, typename WriteHandler>
323   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
324       void (boost::system::error_code, std::size_t))
325   async_send(implementation_type& impl, const ConstBufferSequence& buffers,
326       socket_base::message_flags flags,
327       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
328   {
329     detail::async_result_init<
330       WriteHandler, void (boost::system::error_code, std::size_t)> init(
331         BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
332
333     service_impl_.async_send(impl, buffers, flags, init.handler);
334
335     return init.result.get();
336   }
337
338   /// Send a datagram to the specified endpoint.
339   template <typename ConstBufferSequence>
340   std::size_t send_to(implementation_type& impl,
341       const ConstBufferSequence& buffers, const endpoint_type& destination,
342       socket_base::message_flags flags, boost::system::error_code& ec)
343   {
344     return service_impl_.send_to(impl, buffers, destination, flags, ec);
345   }
346
347   /// Start an asynchronous send.
348   template <typename ConstBufferSequence, typename WriteHandler>
349   BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
350       void (boost::system::error_code, std::size_t))
351   async_send_to(implementation_type& impl,
352       const ConstBufferSequence& buffers, const endpoint_type& destination,
353       socket_base::message_flags flags,
354       BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
355   {
356     detail::async_result_init<
357       WriteHandler, void (boost::system::error_code, std::size_t)> init(
358         BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
359
360     service_impl_.async_send_to(impl, buffers,
361         destination, flags, init.handler);
362
363     return init.result.get();
364   }
365
366   /// Receive some data from the peer.
367   template <typename MutableBufferSequence>
368   std::size_t receive(implementation_type& impl,
369       const MutableBufferSequence& buffers,
370       socket_base::message_flags flags, boost::system::error_code& ec)
371   {
372     return service_impl_.receive(impl, buffers, flags, ec);
373   }
374
375   /// Start an asynchronous receive.
376   template <typename MutableBufferSequence, typename ReadHandler>
377   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
378       void (boost::system::error_code, std::size_t))
379   async_receive(implementation_type& impl,
380       const MutableBufferSequence& buffers,
381       socket_base::message_flags flags,
382       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
383   {
384     detail::async_result_init<
385       ReadHandler, void (boost::system::error_code, std::size_t)> init(
386         BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
387
388     service_impl_.async_receive(impl, buffers, flags, init.handler);
389
390     return init.result.get();
391   }
392
393   /// Receive a datagram with the endpoint of the sender.
394   template <typename MutableBufferSequence>
395   std::size_t receive_from(implementation_type& impl,
396       const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
397       socket_base::message_flags flags, boost::system::error_code& ec)
398   {
399     return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
400         ec);
401   }
402
403   /// Start an asynchronous receive that will get the endpoint of the sender.
404   template <typename MutableBufferSequence, typename ReadHandler>
405   BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
406       void (boost::system::error_code, std::size_t))
407   async_receive_from(implementation_type& impl,
408       const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
409       socket_base::message_flags flags,
410       BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
411   {
412     detail::async_result_init<
413       ReadHandler, void (boost::system::error_code, std::size_t)> init(
414         BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
415
416     service_impl_.async_receive_from(impl, buffers,
417         sender_endpoint, flags, init.handler);
418
419     return init.result.get();
420   }
421
422 private:
423   // Destroy all user-defined handler objects owned by the service.
424   void shutdown_service()
425   {
426     service_impl_.shutdown_service();
427   }
428
429   // The platform-specific implementation.
430   service_impl_type service_impl_;
431 };
432
433 } // namespace asio
434 } // namespace boost
435
436 #include <boost/asio/detail/pop_options.hpp>
437
438 #endif // BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP