2 // stream_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_STREAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_STREAM_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/winrt_ssocket_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 stream socket.
42 template <typename Protocol>
43 class stream_socket_service
44 #if defined(GENERATING_DOCUMENTATION)
45 : public boost::asio::io_context::service
47 : public boost::asio::detail::service_base<stream_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::winrt_ssocket_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 stream socket implementation.
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 stream socket service for the specified io_context.
88 explicit stream_socket_service(boost::asio::io_context& io_context)
89 : boost::asio::detail::service_base<
90 stream_socket_service<Protocol> >(io_context),
91 service_impl_(io_context)
95 /// Construct a new stream 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 stream 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 stream socket implementation.
110 void move_assign(implementation_type& impl,
111 stream_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 stream_socket_service;
120 /// Move-construct a new stream socket implementation from another protocol
122 template <typename Protocol1>
123 void converting_move_construct(implementation_type& impl,
124 stream_socket_service<Protocol1>& other_service,
125 typename stream_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 stream socket implementation.
136 void destroy(implementation_type& impl)
138 service_impl_.destroy(impl);
141 /// Open a stream socket.
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_STREAM))
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 stream 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 stream 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 stream 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 stream 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,
358 const ConstBufferSequence& buffers,
359 socket_base::message_flags flags,
360 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
362 async_completion<WriteHandler,
363 void (boost::system::error_code, std::size_t)> init(handler);
365 service_impl_.async_send(impl, buffers, flags, init.completion_handler);
367 return init.result.get();
370 /// Receive some data from the peer.
371 template <typename MutableBufferSequence>
372 std::size_t receive(implementation_type& impl,
373 const MutableBufferSequence& buffers,
374 socket_base::message_flags flags, boost::system::error_code& ec)
376 return service_impl_.receive(impl, buffers, flags, ec);
379 /// Start an asynchronous receive.
380 template <typename MutableBufferSequence, typename ReadHandler>
381 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
382 void (boost::system::error_code, std::size_t))
383 async_receive(implementation_type& impl,
384 const MutableBufferSequence& buffers,
385 socket_base::message_flags flags,
386 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
388 async_completion<ReadHandler,
389 void (boost::system::error_code, std::size_t)> init(handler);
391 service_impl_.async_receive(impl, buffers, flags, init.completion_handler);
393 return init.result.get();
397 // Destroy all user-defined handler objects owned by the service.
400 service_impl_.shutdown();
403 // The platform-specific implementation.
404 service_impl_type service_impl_;
410 #include <boost/asio/detail/pop_options.hpp>
412 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
414 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP