2 // stream_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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 #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/winrt_ssocket_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 stream socket.
39 template <typename Protocol>
40 class stream_socket_service
41 #if defined(GENERATING_DOCUMENTATION)
42 : public boost::asio::io_service::service
44 : public boost::asio::detail::service_base<stream_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::winrt_ssocket_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 stream socket implementation.
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 stream socket service for the specified io_service.
92 explicit stream_socket_service(boost::asio::io_service& io_service)
93 : boost::asio::detail::service_base<
94 stream_socket_service<Protocol> >(io_service),
95 service_impl_(io_service)
99 /// Construct a new stream 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 stream 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 stream socket implementation.
114 void move_assign(implementation_type& impl,
115 stream_socket_service& other_service,
116 implementation_type& other_impl)
118 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
121 /// Move-construct a new stream socket implementation from another protocol
123 template <typename Protocol1>
124 void converting_move_construct(implementation_type& impl,
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>(
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::system::error_code 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;
152 /// Assign an existing native socket to a stream socket.
153 boost::system::error_code assign(implementation_type& impl,
154 const protocol_type& protocol, const native_handle_type& native_socket,
155 boost::system::error_code& ec)
157 return service_impl_.assign(impl, protocol, native_socket, ec);
160 /// Determine whether the socket is open.
161 bool is_open(const implementation_type& impl) const
163 return service_impl_.is_open(impl);
166 /// Close a stream socket implementation.
167 boost::system::error_code close(implementation_type& impl,
168 boost::system::error_code& ec)
170 return service_impl_.close(impl, ec);
173 /// (Deprecated: Use native_handle().) Get the native socket implementation.
174 native_type native(implementation_type& impl)
176 return service_impl_.native_handle(impl);
179 /// Get the native socket implementation.
180 native_handle_type native_handle(implementation_type& impl)
182 return service_impl_.native_handle(impl);
185 /// Cancel all asynchronous operations associated with the socket.
186 boost::system::error_code cancel(implementation_type& impl,
187 boost::system::error_code& ec)
189 return service_impl_.cancel(impl, ec);
192 /// Determine whether the socket is at the out-of-band data mark.
193 bool at_mark(const implementation_type& impl,
194 boost::system::error_code& ec) const
196 return service_impl_.at_mark(impl, ec);
199 /// Determine the number of bytes available for reading.
200 std::size_t available(const implementation_type& impl,
201 boost::system::error_code& ec) const
203 return service_impl_.available(impl, ec);
206 /// Bind the stream socket to the specified local endpoint.
207 boost::system::error_code bind(implementation_type& impl,
208 const endpoint_type& endpoint, boost::system::error_code& ec)
210 return service_impl_.bind(impl, endpoint, ec);
213 /// Connect the stream socket to the specified endpoint.
214 boost::system::error_code connect(implementation_type& impl,
215 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
217 return service_impl_.connect(impl, peer_endpoint, ec);
220 /// Start an asynchronous connect.
221 template <typename ConnectHandler>
222 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
223 void (boost::system::error_code))
224 async_connect(implementation_type& impl,
225 const endpoint_type& peer_endpoint,
226 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
228 detail::async_result_init<
229 ConnectHandler, void (boost::system::error_code)> init(
230 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
232 service_impl_.async_connect(impl, peer_endpoint, init.handler);
234 return init.result.get();
237 /// Set a socket option.
238 template <typename SettableSocketOption>
239 boost::system::error_code set_option(implementation_type& impl,
240 const SettableSocketOption& option, boost::system::error_code& ec)
242 return service_impl_.set_option(impl, option, ec);
245 /// Get a socket option.
246 template <typename GettableSocketOption>
247 boost::system::error_code get_option(const implementation_type& impl,
248 GettableSocketOption& option, boost::system::error_code& ec) const
250 return service_impl_.get_option(impl, option, ec);
253 /// Perform an IO control command on the socket.
254 template <typename IoControlCommand>
255 boost::system::error_code io_control(implementation_type& impl,
256 IoControlCommand& command, boost::system::error_code& ec)
258 return service_impl_.io_control(impl, command, ec);
261 /// Gets the non-blocking mode of the socket.
262 bool non_blocking(const implementation_type& impl) const
264 return service_impl_.non_blocking(impl);
267 /// Sets the non-blocking mode of the socket.
268 boost::system::error_code non_blocking(implementation_type& impl,
269 bool mode, boost::system::error_code& ec)
271 return service_impl_.non_blocking(impl, mode, ec);
274 /// Gets the non-blocking mode of the native socket implementation.
275 bool native_non_blocking(const implementation_type& impl) const
277 return service_impl_.native_non_blocking(impl);
280 /// Sets the non-blocking mode of the native socket implementation.
281 boost::system::error_code native_non_blocking(implementation_type& impl,
282 bool mode, boost::system::error_code& ec)
284 return service_impl_.native_non_blocking(impl, mode, ec);
287 /// Get the local endpoint.
288 endpoint_type local_endpoint(const implementation_type& impl,
289 boost::system::error_code& ec) const
291 return service_impl_.local_endpoint(impl, ec);
294 /// Get the remote endpoint.
295 endpoint_type remote_endpoint(const implementation_type& impl,
296 boost::system::error_code& ec) const
298 return service_impl_.remote_endpoint(impl, ec);
301 /// Disable sends or receives on the socket.
302 boost::system::error_code shutdown(implementation_type& impl,
303 socket_base::shutdown_type what, boost::system::error_code& ec)
305 return service_impl_.shutdown(impl, what, ec);
308 /// Send the given data to the peer.
309 template <typename ConstBufferSequence>
310 std::size_t send(implementation_type& impl,
311 const ConstBufferSequence& buffers,
312 socket_base::message_flags flags, boost::system::error_code& ec)
314 return service_impl_.send(impl, buffers, flags, ec);
317 /// Start an asynchronous send.
318 template <typename ConstBufferSequence, typename WriteHandler>
319 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
320 void (boost::system::error_code, std::size_t))
321 async_send(implementation_type& impl,
322 const ConstBufferSequence& buffers,
323 socket_base::message_flags flags,
324 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
326 detail::async_result_init<
327 WriteHandler, void (boost::system::error_code, std::size_t)> init(
328 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
330 service_impl_.async_send(impl, buffers, flags, init.handler);
332 return init.result.get();
335 /// Receive some data from the peer.
336 template <typename MutableBufferSequence>
337 std::size_t receive(implementation_type& impl,
338 const MutableBufferSequence& buffers,
339 socket_base::message_flags flags, boost::system::error_code& ec)
341 return service_impl_.receive(impl, buffers, flags, ec);
344 /// Start an asynchronous receive.
345 template <typename MutableBufferSequence, typename ReadHandler>
346 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
347 void (boost::system::error_code, std::size_t))
348 async_receive(implementation_type& impl,
349 const MutableBufferSequence& buffers,
350 socket_base::message_flags flags,
351 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
353 detail::async_result_init<
354 ReadHandler, void (boost::system::error_code, std::size_t)> init(
355 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
357 service_impl_.async_receive(impl, buffers, flags, init.handler);
359 return init.result.get();
363 // Destroy all user-defined handler objects owned by the service.
364 void shutdown_service()
366 service_impl_.shutdown_service();
369 // The platform-specific implementation.
370 service_impl_type service_impl_;
376 #include <boost/asio/detail/pop_options.hpp>
378 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP