2 // stream_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2012 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/error.hpp>
21 #include <boost/asio/io_service.hpp>
23 #if defined(BOOST_ASIO_HAS_IOCP)
24 # include <boost/asio/detail/win_iocp_socket_service.hpp>
26 # include <boost/asio/detail/reactive_socket_service.hpp>
29 #include <boost/asio/detail/push_options.hpp>
34 /// Default service implementation for a stream socket.
35 template <typename Protocol>
36 class stream_socket_service
37 #if defined(GENERATING_DOCUMENTATION)
38 : public boost::asio::io_service::service
40 : public boost::asio::detail::service_base<stream_socket_service<Protocol> >
44 #if defined(GENERATING_DOCUMENTATION)
45 /// The unique service identifier.
46 static boost::asio::io_service::id id;
49 /// The protocol type.
50 typedef Protocol protocol_type;
52 /// The endpoint type.
53 typedef typename Protocol::endpoint endpoint_type;
56 // The type of the platform-specific implementation.
57 #if defined(BOOST_ASIO_HAS_IOCP)
58 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
60 typedef detail::reactive_socket_service<Protocol> service_impl_type;
64 /// The type of a stream socket implementation.
65 #if defined(GENERATING_DOCUMENTATION)
66 typedef implementation_defined implementation_type;
68 typedef typename service_impl_type::implementation_type implementation_type;
71 /// (Deprecated: Use native_handle_type.) The native socket type.
72 #if defined(GENERATING_DOCUMENTATION)
73 typedef implementation_defined native_type;
75 typedef typename service_impl_type::native_handle_type native_type;
78 /// The native socket type.
79 #if defined(GENERATING_DOCUMENTATION)
80 typedef implementation_defined native_handle_type;
82 typedef typename service_impl_type::native_handle_type native_handle_type;
85 /// Construct a new stream socket service for the specified io_service.
86 explicit stream_socket_service(boost::asio::io_service& io_service)
87 : boost::asio::detail::service_base<
88 stream_socket_service<Protocol> >(io_service),
89 service_impl_(io_service)
93 /// Construct a new stream socket implementation.
94 void construct(implementation_type& impl)
96 service_impl_.construct(impl);
99 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
100 /// Move-construct a new stream socket implementation.
101 void move_construct(implementation_type& impl,
102 implementation_type& other_impl)
104 service_impl_.move_construct(impl, other_impl);
107 /// Move-assign from another stream socket implementation.
108 void move_assign(implementation_type& impl,
109 stream_socket_service& other_service,
110 implementation_type& other_impl)
112 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
114 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
116 /// Destroy a stream socket implementation.
117 void destroy(implementation_type& impl)
119 service_impl_.destroy(impl);
122 /// Open a stream socket.
123 boost::system::error_code open(implementation_type& impl,
124 const protocol_type& protocol, boost::system::error_code& ec)
126 if (protocol.type() == SOCK_STREAM)
127 service_impl_.open(impl, protocol, ec);
129 ec = boost::asio::error::invalid_argument;
133 /// Assign an existing native socket to a stream socket.
134 boost::system::error_code assign(implementation_type& impl,
135 const protocol_type& protocol, const native_handle_type& native_socket,
136 boost::system::error_code& ec)
138 return service_impl_.assign(impl, protocol, native_socket, ec);
141 /// Determine whether the socket is open.
142 bool is_open(const implementation_type& impl) const
144 return service_impl_.is_open(impl);
147 /// Close a stream socket implementation.
148 boost::system::error_code close(implementation_type& impl,
149 boost::system::error_code& ec)
151 return service_impl_.close(impl, ec);
154 /// (Deprecated: Use native_handle().) Get the native socket implementation.
155 native_type native(implementation_type& impl)
157 return service_impl_.native_handle(impl);
160 /// Get the native socket implementation.
161 native_handle_type native_handle(implementation_type& impl)
163 return service_impl_.native_handle(impl);
166 /// Cancel all asynchronous operations associated with the socket.
167 boost::system::error_code cancel(implementation_type& impl,
168 boost::system::error_code& ec)
170 return service_impl_.cancel(impl, ec);
173 /// Determine whether the socket is at the out-of-band data mark.
174 bool at_mark(const implementation_type& impl,
175 boost::system::error_code& ec) const
177 return service_impl_.at_mark(impl, ec);
180 /// Determine the number of bytes available for reading.
181 std::size_t available(const implementation_type& impl,
182 boost::system::error_code& ec) const
184 return service_impl_.available(impl, ec);
187 /// Bind the stream socket to the specified local endpoint.
188 boost::system::error_code bind(implementation_type& impl,
189 const endpoint_type& endpoint, boost::system::error_code& ec)
191 return service_impl_.bind(impl, endpoint, ec);
194 /// Connect the stream socket to the specified endpoint.
195 boost::system::error_code connect(implementation_type& impl,
196 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
198 return service_impl_.connect(impl, peer_endpoint, ec);
201 /// Start an asynchronous connect.
202 template <typename ConnectHandler>
203 void async_connect(implementation_type& impl,
204 const endpoint_type& peer_endpoint,
205 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
207 service_impl_.async_connect(impl, peer_endpoint,
208 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
211 /// Set a socket option.
212 template <typename SettableSocketOption>
213 boost::system::error_code set_option(implementation_type& impl,
214 const SettableSocketOption& option, boost::system::error_code& ec)
216 return service_impl_.set_option(impl, option, ec);
219 /// Get a socket option.
220 template <typename GettableSocketOption>
221 boost::system::error_code get_option(const implementation_type& impl,
222 GettableSocketOption& option, boost::system::error_code& ec) const
224 return service_impl_.get_option(impl, option, ec);
227 /// Perform an IO control command on the socket.
228 template <typename IoControlCommand>
229 boost::system::error_code io_control(implementation_type& impl,
230 IoControlCommand& command, boost::system::error_code& ec)
232 return service_impl_.io_control(impl, command, ec);
235 /// Gets the non-blocking mode of the socket.
236 bool non_blocking(const implementation_type& impl) const
238 return service_impl_.non_blocking(impl);
241 /// Sets the non-blocking mode of the socket.
242 boost::system::error_code non_blocking(implementation_type& impl,
243 bool mode, boost::system::error_code& ec)
245 return service_impl_.non_blocking(impl, mode, ec);
248 /// Gets the non-blocking mode of the native socket implementation.
249 bool native_non_blocking(const implementation_type& impl) const
251 return service_impl_.native_non_blocking(impl);
254 /// Sets the non-blocking mode of the native socket implementation.
255 boost::system::error_code native_non_blocking(implementation_type& impl,
256 bool mode, boost::system::error_code& ec)
258 return service_impl_.native_non_blocking(impl, mode, ec);
261 /// Get the local endpoint.
262 endpoint_type local_endpoint(const implementation_type& impl,
263 boost::system::error_code& ec) const
265 return service_impl_.local_endpoint(impl, ec);
268 /// Get the remote endpoint.
269 endpoint_type remote_endpoint(const implementation_type& impl,
270 boost::system::error_code& ec) const
272 return service_impl_.remote_endpoint(impl, ec);
275 /// Disable sends or receives on the socket.
276 boost::system::error_code shutdown(implementation_type& impl,
277 socket_base::shutdown_type what, boost::system::error_code& ec)
279 return service_impl_.shutdown(impl, what, ec);
282 /// Send the given data to the peer.
283 template <typename ConstBufferSequence>
284 std::size_t send(implementation_type& impl,
285 const ConstBufferSequence& buffers,
286 socket_base::message_flags flags, boost::system::error_code& ec)
288 return service_impl_.send(impl, buffers, flags, ec);
291 /// Start an asynchronous send.
292 template <typename ConstBufferSequence, typename WriteHandler>
293 void async_send(implementation_type& impl,
294 const ConstBufferSequence& buffers,
295 socket_base::message_flags flags,
296 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
298 service_impl_.async_send(impl, buffers, flags,
299 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
302 /// Receive some data from the peer.
303 template <typename MutableBufferSequence>
304 std::size_t receive(implementation_type& impl,
305 const MutableBufferSequence& buffers,
306 socket_base::message_flags flags, boost::system::error_code& ec)
308 return service_impl_.receive(impl, buffers, flags, ec);
311 /// Start an asynchronous receive.
312 template <typename MutableBufferSequence, typename ReadHandler>
313 void async_receive(implementation_type& impl,
314 const MutableBufferSequence& buffers,
315 socket_base::message_flags flags,
316 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
318 service_impl_.async_receive(impl, buffers, flags,
319 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
323 // Destroy all user-defined handler objects owned by the service.
324 void shutdown_service()
326 service_impl_.shutdown_service();
329 // The platform-specific implementation.
330 service_impl_type service_impl_;
336 #include <boost/asio/detail/pop_options.hpp>
338 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP