fix doc example typo
[boost.git] / boost / asio / datagram_socket_service.hpp
blob8cc6617392a05a5929d1a9fd9ecd712f231f1e40
1 //
2 // datagram_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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 //
11 #ifndef BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <cstddef>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_service.hpp>
27 #include <boost/asio/detail/epoll_reactor.hpp>
28 #include <boost/asio/detail/kqueue_reactor.hpp>
29 #include <boost/asio/detail/select_reactor.hpp>
30 #include <boost/asio/detail/service_base.hpp>
31 #include <boost/asio/detail/reactive_socket_service.hpp>
32 #include <boost/asio/detail/win_iocp_socket_service.hpp>
34 namespace boost {
35 namespace asio {
37 /// Default service implementation for a datagram socket.
38 template <typename Protocol>
39 class datagram_socket_service
40 #if defined(GENERATING_DOCUMENTATION)
41 : public boost::asio::io_service::service
42 #else
43 : public boost::asio::detail::service_base<datagram_socket_service<Protocol> >
44 #endif
46 public:
47 #if defined(GENERATING_DOCUMENTATION)
48 /// The unique service identifier.
49 static boost::asio::io_service::id id;
50 #endif
52 /// The protocol type.
53 typedef Protocol protocol_type;
55 /// The endpoint type.
56 typedef typename Protocol::endpoint endpoint_type;
58 private:
59 // The type of the platform-specific implementation.
60 #if defined(BOOST_ASIO_HAS_IOCP)
61 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
62 #elif defined(BOOST_ASIO_HAS_EPOLL)
63 typedef detail::reactive_socket_service<
64 Protocol, detail::epoll_reactor<false> > service_impl_type;
65 #elif defined(BOOST_ASIO_HAS_KQUEUE)
66 typedef detail::reactive_socket_service<
67 Protocol, detail::kqueue_reactor<false> > service_impl_type;
68 #elif defined(BOOST_ASIO_HAS_DEV_POLL)
69 typedef detail::reactive_socket_service<
70 Protocol, detail::dev_poll_reactor<false> > service_impl_type;
71 #else
72 typedef detail::reactive_socket_service<
73 Protocol, detail::select_reactor<false> > service_impl_type;
74 #endif
76 public:
77 /// The type of a datagram socket.
78 #if defined(GENERATING_DOCUMENTATION)
79 typedef implementation_defined implementation_type;
80 #else
81 typedef typename service_impl_type::implementation_type implementation_type;
82 #endif
84 /// The native socket type.
85 #if defined(GENERATING_DOCUMENTATION)
86 typedef implementation_defined native_type;
87 #else
88 typedef typename service_impl_type::native_type native_type;
89 #endif
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_(boost::asio::use_service<service_impl_type>(io_service))
99 /// Destroy all user-defined handler objects owned by the service.
100 void shutdown_service()
104 /// Construct a new datagram socket implementation.
105 void construct(implementation_type& impl)
107 service_impl_.construct(impl);
110 /// Destroy a datagram socket implementation.
111 void destroy(implementation_type& impl)
113 service_impl_.destroy(impl);
116 // Open a new datagram socket implementation.
117 boost::system::error_code open(implementation_type& impl,
118 const protocol_type& protocol, boost::system::error_code& ec)
120 if (protocol.type() == SOCK_DGRAM)
121 service_impl_.open(impl, protocol, ec);
122 else
123 ec = boost::asio::error::invalid_argument;
124 return ec;
127 /// Assign an existing native socket to a datagram socket.
128 boost::system::error_code assign(implementation_type& impl,
129 const protocol_type& protocol, const native_type& native_socket,
130 boost::system::error_code& ec)
132 return service_impl_.assign(impl, protocol, native_socket, ec);
135 /// Determine whether the socket is open.
136 bool is_open(const implementation_type& impl) const
138 return service_impl_.is_open(impl);
141 /// Close a datagram socket implementation.
142 boost::system::error_code close(implementation_type& impl,
143 boost::system::error_code& ec)
145 return service_impl_.close(impl, ec);
148 /// Get the native socket implementation.
149 native_type native(implementation_type& impl)
151 return service_impl_.native(impl);
154 /// Cancel all asynchronous operations associated with the socket.
155 boost::system::error_code cancel(implementation_type& impl,
156 boost::system::error_code& ec)
158 return service_impl_.cancel(impl, ec);
161 /// Determine whether the socket is at the out-of-band data mark.
162 bool at_mark(const implementation_type& impl,
163 boost::system::error_code& ec) const
165 return service_impl_.at_mark(impl, ec);
168 /// Determine the number of bytes available for reading.
169 std::size_t available(const implementation_type& impl,
170 boost::system::error_code& ec) const
172 return service_impl_.available(impl, ec);
175 // Bind the datagram socket to the specified local endpoint.
176 boost::system::error_code bind(implementation_type& impl,
177 const endpoint_type& endpoint, boost::system::error_code& ec)
179 return service_impl_.bind(impl, endpoint, ec);
182 /// Connect the datagram socket to the specified endpoint.
183 boost::system::error_code connect(implementation_type& impl,
184 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
186 return service_impl_.connect(impl, peer_endpoint, ec);
189 /// Start an asynchronous connect.
190 template <typename ConnectHandler>
191 void async_connect(implementation_type& impl,
192 const endpoint_type& peer_endpoint, ConnectHandler handler)
194 service_impl_.async_connect(impl, peer_endpoint, handler);
197 /// Set a socket option.
198 template <typename SettableSocketOption>
199 boost::system::error_code set_option(implementation_type& impl,
200 const SettableSocketOption& option, boost::system::error_code& ec)
202 return service_impl_.set_option(impl, option, ec);
205 /// Get a socket option.
206 template <typename GettableSocketOption>
207 boost::system::error_code get_option(const implementation_type& impl,
208 GettableSocketOption& option, boost::system::error_code& ec) const
210 return service_impl_.get_option(impl, option, ec);
213 /// Perform an IO control command on the socket.
214 template <typename IoControlCommand>
215 boost::system::error_code io_control(implementation_type& impl,
216 IoControlCommand& command, boost::system::error_code& ec)
218 return service_impl_.io_control(impl, command, ec);
221 /// Get the local endpoint.
222 endpoint_type local_endpoint(const implementation_type& impl,
223 boost::system::error_code& ec) const
225 return service_impl_.local_endpoint(impl, ec);
228 /// Get the remote endpoint.
229 endpoint_type remote_endpoint(const implementation_type& impl,
230 boost::system::error_code& ec) const
232 return service_impl_.remote_endpoint(impl, ec);
235 /// Disable sends or receives on the socket.
236 boost::system::error_code shutdown(implementation_type& impl,
237 socket_base::shutdown_type what, boost::system::error_code& ec)
239 return service_impl_.shutdown(impl, what, ec);
242 /// Send the given data to the peer.
243 template <typename ConstBufferSequence>
244 std::size_t send(implementation_type& impl,
245 const ConstBufferSequence& buffers,
246 socket_base::message_flags flags, boost::system::error_code& ec)
248 return service_impl_.send(impl, buffers, flags, ec);
251 /// Start an asynchronous send.
252 template <typename ConstBufferSequence, typename WriteHandler>
253 void async_send(implementation_type& impl, const ConstBufferSequence& buffers,
254 socket_base::message_flags flags, WriteHandler handler)
256 service_impl_.async_send(impl, buffers, flags, handler);
259 /// Send a datagram to the specified endpoint.
260 template <typename ConstBufferSequence>
261 std::size_t send_to(implementation_type& impl,
262 const ConstBufferSequence& buffers, const endpoint_type& destination,
263 socket_base::message_flags flags, boost::system::error_code& ec)
265 return service_impl_.send_to(impl, buffers, destination, flags, ec);
268 /// Start an asynchronous send.
269 template <typename ConstBufferSequence, typename WriteHandler>
270 void async_send_to(implementation_type& impl,
271 const ConstBufferSequence& buffers, const endpoint_type& destination,
272 socket_base::message_flags flags, WriteHandler handler)
274 service_impl_.async_send_to(impl, buffers, destination, flags, handler);
277 /// Receive some data from the peer.
278 template <typename MutableBufferSequence>
279 std::size_t receive(implementation_type& impl,
280 const MutableBufferSequence& buffers,
281 socket_base::message_flags flags, boost::system::error_code& ec)
283 return service_impl_.receive(impl, buffers, flags, ec);
286 /// Start an asynchronous receive.
287 template <typename MutableBufferSequence, typename ReadHandler>
288 void async_receive(implementation_type& impl,
289 const MutableBufferSequence& buffers,
290 socket_base::message_flags flags, ReadHandler handler)
292 service_impl_.async_receive(impl, buffers, flags, handler);
295 /// Receive a datagram with the endpoint of the sender.
296 template <typename MutableBufferSequence>
297 std::size_t receive_from(implementation_type& impl,
298 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
299 socket_base::message_flags flags, boost::system::error_code& ec)
301 return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
302 ec);
305 /// Start an asynchronous receive that will get the endpoint of the sender.
306 template <typename MutableBufferSequence, typename ReadHandler>
307 void async_receive_from(implementation_type& impl,
308 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
309 socket_base::message_flags flags, ReadHandler handler)
311 service_impl_.async_receive_from(impl, buffers, sender_endpoint, flags,
312 handler);
315 private:
316 // The service that provides the platform-specific implementation.
317 service_impl_type& service_impl_;
320 } // namespace asio
321 } // namespace boost
323 #include <boost/asio/detail/pop_options.hpp>
325 #endif // BOOST_ASIO_DATAGRAM_SOCKET_SERVICE_HPP