2 // raw_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_RAW_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_RAW_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 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
23 #include <boost/asio/async_result.hpp>
24 #include <boost/asio/detail/type_traits.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_context.hpp>
28 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
29 # include <boost/asio/detail/null_socket_service.hpp>
30 #elif defined(BOOST_ASIO_HAS_IOCP)
31 # include <boost/asio/detail/win_iocp_socket_service.hpp>
33 # include <boost/asio/detail/reactive_socket_service.hpp>
36 #include <boost/asio/detail/push_options.hpp>
41 /// Default service implementation for a raw socket.
42 template <typename Protocol>
43 class raw_socket_service
44 #if defined(GENERATING_DOCUMENTATION)
45 : public boost::asio::io_context::service
47 : public boost::asio::detail::service_base<raw_socket_service<Protocol> >
51 #if defined(GENERATING_DOCUMENTATION)
52 /// The unique service identifier.
53 static boost::asio::io_context::id id;
56 /// The protocol type.
57 typedef Protocol protocol_type;
59 /// The endpoint type.
60 typedef typename Protocol::endpoint endpoint_type;
63 // The type of the platform-specific implementation.
64 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
65 typedef detail::null_socket_service<Protocol> service_impl_type;
66 #elif defined(BOOST_ASIO_HAS_IOCP)
67 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
69 typedef detail::reactive_socket_service<Protocol> service_impl_type;
73 /// The type of a raw socket.
74 #if defined(GENERATING_DOCUMENTATION)
75 typedef implementation_defined implementation_type;
77 typedef typename service_impl_type::implementation_type implementation_type;
80 /// The native socket type.
81 #if defined(GENERATING_DOCUMENTATION)
82 typedef implementation_defined native_handle_type;
84 typedef typename service_impl_type::native_handle_type native_handle_type;
87 /// Construct a new raw socket service for the specified io_context.
88 explicit raw_socket_service(boost::asio::io_context& io_context)
89 : boost::asio::detail::service_base<
90 raw_socket_service<Protocol> >(io_context),
91 service_impl_(io_context)
95 /// Construct a new raw socket implementation.
96 void construct(implementation_type& impl)
98 service_impl_.construct(impl);
101 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
102 /// Move-construct a new raw socket implementation.
103 void move_construct(implementation_type& impl,
104 implementation_type& other_impl)
106 service_impl_.move_construct(impl, other_impl);
109 /// Move-assign from another raw socket implementation.
110 void move_assign(implementation_type& impl,
111 raw_socket_service& other_service,
112 implementation_type& other_impl)
114 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
117 // All socket services have access to each other's implementations.
118 template <typename Protocol1> friend class raw_socket_service;
120 /// Move-construct a new raw socket implementation from another protocol
122 template <typename Protocol1>
123 void converting_move_construct(implementation_type& impl,
124 raw_socket_service<Protocol1>& other_service,
125 typename raw_socket_service<
126 Protocol1>::implementation_type& other_impl,
127 typename enable_if<is_convertible<
128 Protocol1, Protocol>::value>::type* = 0)
130 service_impl_.template converting_move_construct<Protocol1>(
131 impl, other_service.service_impl_, other_impl);
133 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
135 /// Destroy a raw socket implementation.
136 void destroy(implementation_type& impl)
138 service_impl_.destroy(impl);
141 // Open a new raw socket implementation.
142 BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
143 const protocol_type& protocol, boost::system::error_code& ec)
145 if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_RAW))
146 service_impl_.open(impl, protocol, ec);
148 ec = boost::asio::error::invalid_argument;
149 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
152 /// Assign an existing native socket to a raw socket.
153 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
154 const protocol_type& protocol, const native_handle_type& native_socket,
155 boost::system::error_code& ec)
157 service_impl_.assign(impl, protocol, native_socket, ec);
158 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
161 /// Determine whether the socket is open.
162 bool is_open(const implementation_type& impl) const
164 return service_impl_.is_open(impl);
167 /// Close a raw socket implementation.
168 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
169 boost::system::error_code& ec)
171 service_impl_.close(impl, ec);
172 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
175 /// Release ownership of the underlying socket.
176 native_handle_type release(implementation_type& impl,
177 boost::system::error_code& ec)
179 return service_impl_.release(impl, ec);
182 /// Get the native socket implementation.
183 native_handle_type native_handle(implementation_type& impl)
185 return service_impl_.native_handle(impl);
188 /// Cancel all asynchronous operations associated with the socket.
189 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
190 boost::system::error_code& ec)
192 service_impl_.cancel(impl, ec);
193 BOOST_ASIO_SYNC_OP_VOID_RETURN(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 raw socket to the specified local endpoint.
211 BOOST_ASIO_SYNC_OP_VOID bind(implementation_type& impl,
212 const endpoint_type& endpoint, boost::system::error_code& ec)
214 service_impl_.bind(impl, endpoint, ec);
215 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
218 /// Connect the raw socket to the specified endpoint.
219 BOOST_ASIO_SYNC_OP_VOID connect(implementation_type& impl,
220 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
222 service_impl_.connect(impl, peer_endpoint, ec);
223 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
226 /// Start an asynchronous connect.
227 template <typename ConnectHandler>
228 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
229 void (boost::system::error_code))
230 async_connect(implementation_type& impl,
231 const endpoint_type& peer_endpoint,
232 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
234 async_completion<ConnectHandler,
235 void (boost::system::error_code)> init(handler);
237 service_impl_.async_connect(impl, peer_endpoint, init.completion_handler);
239 return init.result.get();
242 /// Set a socket option.
243 template <typename SettableSocketOption>
244 BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
245 const SettableSocketOption& option, boost::system::error_code& ec)
247 service_impl_.set_option(impl, option, ec);
248 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
251 /// Get a socket option.
252 template <typename GettableSocketOption>
253 BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
254 GettableSocketOption& option, boost::system::error_code& ec) const
256 service_impl_.get_option(impl, option, ec);
257 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
260 /// Perform an IO control command on the socket.
261 template <typename IoControlCommand>
262 BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl,
263 IoControlCommand& command, boost::system::error_code& ec)
265 service_impl_.io_control(impl, command, ec);
266 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
269 /// Gets the non-blocking mode of the socket.
270 bool non_blocking(const implementation_type& impl) const
272 return service_impl_.non_blocking(impl);
275 /// Sets the non-blocking mode of the socket.
276 BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl,
277 bool mode, boost::system::error_code& ec)
279 service_impl_.non_blocking(impl, mode, ec);
280 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
283 /// Gets the non-blocking mode of the native socket implementation.
284 bool native_non_blocking(const implementation_type& impl) const
286 return service_impl_.native_non_blocking(impl);
289 /// Sets the non-blocking mode of the native socket implementation.
290 BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl,
291 bool mode, boost::system::error_code& ec)
293 service_impl_.native_non_blocking(impl, mode, ec);
294 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
297 /// Get the local endpoint.
298 endpoint_type local_endpoint(const implementation_type& impl,
299 boost::system::error_code& ec) const
301 return service_impl_.local_endpoint(impl, ec);
304 /// Get the remote endpoint.
305 endpoint_type remote_endpoint(const implementation_type& impl,
306 boost::system::error_code& ec) const
308 return service_impl_.remote_endpoint(impl, ec);
311 /// Disable sends or receives on the socket.
312 BOOST_ASIO_SYNC_OP_VOID shutdown(implementation_type& impl,
313 socket_base::shutdown_type what, boost::system::error_code& ec)
315 service_impl_.shutdown(impl, what, ec);
316 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
319 /// Wait for the socket to become ready to read, ready to write, or to have
320 /// pending error conditions.
321 BOOST_ASIO_SYNC_OP_VOID wait(implementation_type& impl,
322 socket_base::wait_type w, boost::system::error_code& ec)
324 service_impl_.wait(impl, w, ec);
325 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
328 /// Asynchronously wait for the socket to become ready to read, ready to
329 /// write, or to have pending error conditions.
330 template <typename WaitHandler>
331 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
332 void (boost::system::error_code))
333 async_wait(implementation_type& impl, socket_base::wait_type w,
334 BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
336 async_completion<WaitHandler,
337 void (boost::system::error_code)> init(handler);
339 service_impl_.async_wait(impl, w, init.completion_handler);
341 return init.result.get();
344 /// Send the given data to the peer.
345 template <typename ConstBufferSequence>
346 std::size_t send(implementation_type& impl,
347 const ConstBufferSequence& buffers,
348 socket_base::message_flags flags, boost::system::error_code& ec)
350 return service_impl_.send(impl, buffers, flags, ec);
353 /// Start an asynchronous send.
354 template <typename ConstBufferSequence, typename WriteHandler>
355 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
356 void (boost::system::error_code, std::size_t))
357 async_send(implementation_type& impl, const ConstBufferSequence& buffers,
358 socket_base::message_flags flags,
359 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
361 async_completion<WriteHandler,
362 void (boost::system::error_code, std::size_t)> init(handler);
364 service_impl_.async_send(impl, buffers, flags, init.completion_handler);
366 return init.result.get();
369 /// Send raw data to the specified endpoint.
370 template <typename ConstBufferSequence>
371 std::size_t send_to(implementation_type& impl,
372 const ConstBufferSequence& buffers, const endpoint_type& destination,
373 socket_base::message_flags flags, boost::system::error_code& ec)
375 return service_impl_.send_to(impl, buffers, destination, flags, ec);
378 /// Start an asynchronous send.
379 template <typename ConstBufferSequence, typename WriteHandler>
380 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
381 void (boost::system::error_code, std::size_t))
382 async_send_to(implementation_type& impl,
383 const ConstBufferSequence& buffers, const endpoint_type& destination,
384 socket_base::message_flags flags,
385 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
387 async_completion<WriteHandler,
388 void (boost::system::error_code, std::size_t)> init(handler);
390 service_impl_.async_send_to(impl, buffers,
391 destination, flags, init.completion_handler);
393 return init.result.get();
396 /// Receive some data from the peer.
397 template <typename MutableBufferSequence>
398 std::size_t receive(implementation_type& impl,
399 const MutableBufferSequence& buffers,
400 socket_base::message_flags flags, boost::system::error_code& ec)
402 return service_impl_.receive(impl, buffers, flags, ec);
405 /// Start an asynchronous receive.
406 template <typename MutableBufferSequence, typename ReadHandler>
407 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
408 void (boost::system::error_code, std::size_t))
409 async_receive(implementation_type& impl,
410 const MutableBufferSequence& buffers,
411 socket_base::message_flags flags,
412 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
414 async_completion<ReadHandler,
415 void (boost::system::error_code, std::size_t)> init(handler);
417 service_impl_.async_receive(impl, buffers, flags, init.completion_handler);
419 return init.result.get();
422 /// Receive raw data with the endpoint of the sender.
423 template <typename MutableBufferSequence>
424 std::size_t receive_from(implementation_type& impl,
425 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
426 socket_base::message_flags flags, boost::system::error_code& ec)
428 return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
432 /// Start an asynchronous receive that will get the endpoint of the sender.
433 template <typename MutableBufferSequence, typename ReadHandler>
434 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
435 void (boost::system::error_code, std::size_t))
436 async_receive_from(implementation_type& impl,
437 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
438 socket_base::message_flags flags,
439 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
441 async_completion<ReadHandler,
442 void (boost::system::error_code, std::size_t)> init(handler);
444 service_impl_.async_receive_from(impl, buffers,
445 sender_endpoint, flags, init.completion_handler);
447 return init.result.get();
451 // Destroy all user-defined handler objects owned by the service.
454 service_impl_.shutdown();
457 // The platform-specific implementation.
458 service_impl_type service_impl_;
464 #include <boost/asio/detail/pop_options.hpp>
466 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
468 #endif // BOOST_ASIO_RAW_SOCKET_SERVICE_HPP