2 // datagram_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
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>
30 # include <boost/asio/detail/reactive_socket_service.hpp>
33 #include <boost/asio/detail/push_options.hpp>
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
44 : public boost::asio::detail::service_base<datagram_socket_service<Protocol> >
48 #if defined(GENERATING_DOCUMENTATION)
49 /// The unique service identifier.
50 static boost::asio::io_service::id id;
53 /// The protocol type.
54 typedef Protocol protocol_type;
56 /// The endpoint type.
57 typedef typename Protocol::endpoint endpoint_type;
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;
66 typedef detail::reactive_socket_service<Protocol> service_impl_type;
70 /// The type of a datagram socket.
71 #if defined(GENERATING_DOCUMENTATION)
72 typedef implementation_defined implementation_type;
74 typedef typename service_impl_type::implementation_type implementation_type;
77 /// (Deprecated: Use native_handle_type.) The native socket type.
78 #if defined(GENERATING_DOCUMENTATION)
79 typedef implementation_defined native_type;
81 typedef typename service_impl_type::native_handle_type native_type;
84 /// The native socket type.
85 #if defined(GENERATING_DOCUMENTATION)
86 typedef implementation_defined native_handle_type;
88 typedef typename service_impl_type::native_handle_type native_handle_type;
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)
99 /// Construct a new datagram socket implementation.
100 void construct(implementation_type& impl)
102 service_impl_.construct(impl);
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)
110 service_impl_.move_construct(impl, other_impl);
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)
118 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
121 // All socket services have access to each other's implementations.
122 template <typename Protocol1> friend class datagram_socket_service;
124 /// Move-construct a new datagram socket implementation from another protocol
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)
134 service_impl_.template converting_move_construct<Protocol1>(
135 impl, other_service.service_impl_, other_impl);
137 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
139 /// Destroy a datagram socket implementation.
140 void destroy(implementation_type& impl)
142 service_impl_.destroy(impl);
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)
149 if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_DGRAM))
150 service_impl_.open(impl, protocol, ec);
152 ec = boost::asio::error::invalid_argument;
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)
161 return service_impl_.assign(impl, protocol, native_socket, ec);
164 /// Determine whether the socket is open.
165 bool is_open(const implementation_type& impl) const
167 return service_impl_.is_open(impl);
170 /// Close a datagram socket implementation.
171 boost::system::error_code close(implementation_type& impl,
172 boost::system::error_code& ec)
174 return service_impl_.close(impl, ec);
177 /// (Deprecated: Use native_handle().) Get the native socket implementation.
178 native_type native(implementation_type& impl)
180 return service_impl_.native_handle(impl);
183 /// Get the native socket implementation.
184 native_handle_type native_handle(implementation_type& impl)
186 return service_impl_.native_handle(impl);
189 /// Cancel all asynchronous operations associated with the socket.
190 boost::system::error_code cancel(implementation_type& impl,
191 boost::system::error_code& ec)
193 return service_impl_.cancel(impl, ec);
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
200 return service_impl_.at_mark(impl, ec);
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
207 return service_impl_.available(impl, ec);
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)
214 return service_impl_.bind(impl, endpoint, ec);
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)
221 return service_impl_.connect(impl, peer_endpoint, ec);
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)
232 detail::async_result_init<
233 ConnectHandler, void (boost::system::error_code)> init(
234 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
236 service_impl_.async_connect(impl, peer_endpoint, init.handler);
238 return init.result.get();
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)
246 return service_impl_.set_option(impl, option, ec);
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
254 return service_impl_.get_option(impl, option, ec);
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)
262 return service_impl_.io_control(impl, command, ec);
265 /// Gets the non-blocking mode of the socket.
266 bool non_blocking(const implementation_type& impl) const
268 return service_impl_.non_blocking(impl);
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)
275 return service_impl_.non_blocking(impl, mode, ec);
278 /// Gets the non-blocking mode of the native socket implementation.
279 bool native_non_blocking(const implementation_type& impl) const
281 return service_impl_.native_non_blocking(impl);
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)
288 return service_impl_.native_non_blocking(impl, mode, ec);
291 /// Get the local endpoint.
292 endpoint_type local_endpoint(const implementation_type& impl,
293 boost::system::error_code& ec) const
295 return service_impl_.local_endpoint(impl, ec);
298 /// Get the remote endpoint.
299 endpoint_type remote_endpoint(const implementation_type& impl,
300 boost::system::error_code& ec) const
302 return service_impl_.remote_endpoint(impl, ec);
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)
309 return service_impl_.shutdown(impl, what, ec);
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)
318 return service_impl_.send(impl, buffers, flags, ec);
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)
329 detail::async_result_init<
330 WriteHandler, void (boost::system::error_code, std::size_t)> init(
331 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
333 service_impl_.async_send(impl, buffers, flags, init.handler);
335 return init.result.get();
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)
344 return service_impl_.send_to(impl, buffers, destination, flags, ec);
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)
356 detail::async_result_init<
357 WriteHandler, void (boost::system::error_code, std::size_t)> init(
358 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
360 service_impl_.async_send_to(impl, buffers,
361 destination, flags, init.handler);
363 return init.result.get();
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)
372 return service_impl_.receive(impl, buffers, flags, ec);
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)
384 detail::async_result_init<
385 ReadHandler, void (boost::system::error_code, std::size_t)> init(
386 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
388 service_impl_.async_receive(impl, buffers, flags, init.handler);
390 return init.result.get();
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)
399 return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
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)
412 detail::async_result_init<
413 ReadHandler, void (boost::system::error_code, std::size_t)> init(
414 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
416 service_impl_.async_receive_from(impl, buffers,
417 sender_endpoint, flags, init.handler);
419 return init.result.get();
423 // Destroy all user-defined handler objects owned by the service.
424 void shutdown_service()
426 service_impl_.shutdown_service();
429 // The platform-specific implementation.
430 service_impl_type service_impl_;
436 #include <boost/asio/detail/pop_options.hpp>
438 #endif // BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP