afc9406332324145e898cc4651883e953216ebba
[platform/upstream/boost.git] / boost / asio / stream_socket_service.hpp
1 //
2 // stream_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2012 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 //
10
11 #ifndef BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <cstddef>
20 #include <boost/asio/error.hpp>
21 #include <boost/asio/io_service.hpp>
22
23 #if defined(BOOST_ASIO_HAS_IOCP)
24 # include <boost/asio/detail/win_iocp_socket_service.hpp>
25 #else
26 # include <boost/asio/detail/reactive_socket_service.hpp>
27 #endif
28
29 #include <boost/asio/detail/push_options.hpp>
30
31 namespace boost {
32 namespace asio {
33
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
39 #else
40   : public boost::asio::detail::service_base<stream_socket_service<Protocol> >
41 #endif
42 {
43 public:
44 #if defined(GENERATING_DOCUMENTATION)
45   /// The unique service identifier.
46   static boost::asio::io_service::id id;
47 #endif
48
49   /// The protocol type.
50   typedef Protocol protocol_type;
51
52   /// The endpoint type.
53   typedef typename Protocol::endpoint endpoint_type;
54
55 private:
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;
59 #else
60   typedef detail::reactive_socket_service<Protocol> service_impl_type;
61 #endif
62
63 public:
64   /// The type of a stream socket implementation.
65 #if defined(GENERATING_DOCUMENTATION)
66   typedef implementation_defined implementation_type;
67 #else
68   typedef typename service_impl_type::implementation_type implementation_type;
69 #endif
70
71   /// (Deprecated: Use native_handle_type.) The native socket type.
72 #if defined(GENERATING_DOCUMENTATION)
73   typedef implementation_defined native_type;
74 #else
75   typedef typename service_impl_type::native_handle_type native_type;
76 #endif
77
78   /// The native socket type.
79 #if defined(GENERATING_DOCUMENTATION)
80   typedef implementation_defined native_handle_type;
81 #else
82   typedef typename service_impl_type::native_handle_type native_handle_type;
83 #endif
84
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)
90   {
91   }
92
93   /// Construct a new stream socket implementation.
94   void construct(implementation_type& impl)
95   {
96     service_impl_.construct(impl);
97   }
98
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)
103   {
104     service_impl_.move_construct(impl, other_impl);
105   }
106
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)
111   {
112     service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
113   }
114 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
115
116   /// Destroy a stream socket implementation.
117   void destroy(implementation_type& impl)
118   {
119     service_impl_.destroy(impl);
120   }
121
122   /// Open a stream socket.
123   boost::system::error_code open(implementation_type& impl,
124       const protocol_type& protocol, boost::system::error_code& ec)
125   {
126     if (protocol.type() == SOCK_STREAM)
127       service_impl_.open(impl, protocol, ec);
128     else
129       ec = boost::asio::error::invalid_argument;
130     return ec;
131   }
132
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)
137   {
138     return service_impl_.assign(impl, protocol, native_socket, ec);
139   }
140
141   /// Determine whether the socket is open.
142   bool is_open(const implementation_type& impl) const
143   {
144     return service_impl_.is_open(impl);
145   }
146
147   /// Close a stream socket implementation.
148   boost::system::error_code close(implementation_type& impl,
149       boost::system::error_code& ec)
150   {
151     return service_impl_.close(impl, ec);
152   }
153
154   /// (Deprecated: Use native_handle().) Get the native socket implementation.
155   native_type native(implementation_type& impl)
156   {
157     return service_impl_.native_handle(impl);
158   }
159
160   /// Get the native socket implementation.
161   native_handle_type native_handle(implementation_type& impl)
162   {
163     return service_impl_.native_handle(impl);
164   }
165
166   /// Cancel all asynchronous operations associated with the socket.
167   boost::system::error_code cancel(implementation_type& impl,
168       boost::system::error_code& ec)
169   {
170     return service_impl_.cancel(impl, ec);
171   }
172
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
176   {
177     return service_impl_.at_mark(impl, ec);
178   }
179
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
183   {
184     return service_impl_.available(impl, ec);
185   }
186
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)
190   {
191     return service_impl_.bind(impl, endpoint, ec);
192   }
193
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)
197   {
198     return service_impl_.connect(impl, peer_endpoint, ec);
199   }
200
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)
206   {
207     service_impl_.async_connect(impl, peer_endpoint,
208         BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
209   }
210
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)
215   {
216     return service_impl_.set_option(impl, option, ec);
217   }
218
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
223   {
224     return service_impl_.get_option(impl, option, ec);
225   }
226
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)
231   {
232     return service_impl_.io_control(impl, command, ec);
233   }
234
235   /// Gets the non-blocking mode of the socket.
236   bool non_blocking(const implementation_type& impl) const
237   {
238     return service_impl_.non_blocking(impl);
239   }
240
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)
244   {
245     return service_impl_.non_blocking(impl, mode, ec);
246   }
247
248   /// Gets the non-blocking mode of the native socket implementation.
249   bool native_non_blocking(const implementation_type& impl) const
250   {
251     return service_impl_.native_non_blocking(impl);
252   }
253
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)
257   {
258     return service_impl_.native_non_blocking(impl, mode, ec);
259   }
260
261   /// Get the local endpoint.
262   endpoint_type local_endpoint(const implementation_type& impl,
263       boost::system::error_code& ec) const
264   {
265     return service_impl_.local_endpoint(impl, ec);
266   }
267
268   /// Get the remote endpoint.
269   endpoint_type remote_endpoint(const implementation_type& impl,
270       boost::system::error_code& ec) const
271   {
272     return service_impl_.remote_endpoint(impl, ec);
273   }
274
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)
278   {
279     return service_impl_.shutdown(impl, what, ec);
280   }
281
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)
287   {
288     return service_impl_.send(impl, buffers, flags, ec);
289   }
290
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)
297   {
298     service_impl_.async_send(impl, buffers, flags,
299         BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
300   }
301
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)
307   {
308     return service_impl_.receive(impl, buffers, flags, ec);
309   }
310
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)
317   {
318     service_impl_.async_receive(impl, buffers, flags,
319         BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
320   }
321
322 private:
323   // Destroy all user-defined handler objects owned by the service.
324   void shutdown_service()
325   {
326     service_impl_.shutdown_service();
327   }
328
329   // The platform-specific implementation.
330   service_impl_type service_impl_;
331 };
332
333 } // namespace asio
334 } // namespace boost
335
336 #include <boost/asio/detail/pop_options.hpp>
337
338 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP