Chris@16
|
1 //
|
Chris@16
|
2 // stream_socket_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <cstddef>
|
Chris@16
|
20 #include <boost/asio/async_result.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
22 #include <boost/asio/error.hpp>
|
Chris@16
|
23 #include <boost/asio/io_service.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
26 # include <boost/asio/detail/winrt_ssocket_service.hpp>
|
Chris@16
|
27 #elif defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
28 # include <boost/asio/detail/win_iocp_socket_service.hpp>
|
Chris@16
|
29 #else
|
Chris@16
|
30 # include <boost/asio/detail/reactive_socket_service.hpp>
|
Chris@16
|
31 #endif
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace asio {
|
Chris@16
|
37
|
Chris@16
|
38 /// Default service implementation for a stream socket.
|
Chris@16
|
39 template <typename Protocol>
|
Chris@16
|
40 class stream_socket_service
|
Chris@16
|
41 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
42 : public boost::asio::io_service::service
|
Chris@16
|
43 #else
|
Chris@16
|
44 : public boost::asio::detail::service_base<stream_socket_service<Protocol> >
|
Chris@16
|
45 #endif
|
Chris@16
|
46 {
|
Chris@16
|
47 public:
|
Chris@16
|
48 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
49 /// The unique service identifier.
|
Chris@16
|
50 static boost::asio::io_service::id id;
|
Chris@16
|
51 #endif
|
Chris@16
|
52
|
Chris@16
|
53 /// The protocol type.
|
Chris@16
|
54 typedef Protocol protocol_type;
|
Chris@16
|
55
|
Chris@16
|
56 /// The endpoint type.
|
Chris@16
|
57 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
58
|
Chris@16
|
59 private:
|
Chris@16
|
60 // The type of the platform-specific implementation.
|
Chris@16
|
61 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
62 typedef detail::winrt_ssocket_service<Protocol> service_impl_type;
|
Chris@16
|
63 #elif defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
64 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
|
Chris@16
|
65 #else
|
Chris@16
|
66 typedef detail::reactive_socket_service<Protocol> service_impl_type;
|
Chris@16
|
67 #endif
|
Chris@16
|
68
|
Chris@16
|
69 public:
|
Chris@16
|
70 /// The type of a stream socket implementation.
|
Chris@16
|
71 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
72 typedef implementation_defined implementation_type;
|
Chris@16
|
73 #else
|
Chris@16
|
74 typedef typename service_impl_type::implementation_type implementation_type;
|
Chris@16
|
75 #endif
|
Chris@16
|
76
|
Chris@16
|
77 /// (Deprecated: Use native_handle_type.) The native socket type.
|
Chris@16
|
78 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
79 typedef implementation_defined native_type;
|
Chris@16
|
80 #else
|
Chris@16
|
81 typedef typename service_impl_type::native_handle_type native_type;
|
Chris@16
|
82 #endif
|
Chris@16
|
83
|
Chris@16
|
84 /// The native socket type.
|
Chris@16
|
85 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
86 typedef implementation_defined native_handle_type;
|
Chris@16
|
87 #else
|
Chris@16
|
88 typedef typename service_impl_type::native_handle_type native_handle_type;
|
Chris@16
|
89 #endif
|
Chris@16
|
90
|
Chris@16
|
91 /// Construct a new stream socket service for the specified io_service.
|
Chris@16
|
92 explicit stream_socket_service(boost::asio::io_service& io_service)
|
Chris@16
|
93 : boost::asio::detail::service_base<
|
Chris@16
|
94 stream_socket_service<Protocol> >(io_service),
|
Chris@16
|
95 service_impl_(io_service)
|
Chris@16
|
96 {
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 /// Construct a new stream socket implementation.
|
Chris@16
|
100 void construct(implementation_type& impl)
|
Chris@16
|
101 {
|
Chris@16
|
102 service_impl_.construct(impl);
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
106 /// Move-construct a new stream socket implementation.
|
Chris@16
|
107 void move_construct(implementation_type& impl,
|
Chris@16
|
108 implementation_type& other_impl)
|
Chris@16
|
109 {
|
Chris@16
|
110 service_impl_.move_construct(impl, other_impl);
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 /// Move-assign from another stream socket implementation.
|
Chris@16
|
114 void move_assign(implementation_type& impl,
|
Chris@16
|
115 stream_socket_service& other_service,
|
Chris@16
|
116 implementation_type& other_impl)
|
Chris@16
|
117 {
|
Chris@16
|
118 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121 /// Move-construct a new stream socket implementation from another protocol
|
Chris@16
|
122 /// type.
|
Chris@16
|
123 template <typename Protocol1>
|
Chris@16
|
124 void converting_move_construct(implementation_type& impl,
|
Chris@16
|
125 typename stream_socket_service<
|
Chris@16
|
126 Protocol1>::implementation_type& other_impl,
|
Chris@16
|
127 typename enable_if<is_convertible<
|
Chris@16
|
128 Protocol1, Protocol>::value>::type* = 0)
|
Chris@16
|
129 {
|
Chris@16
|
130 service_impl_.template converting_move_construct<Protocol1>(
|
Chris@16
|
131 impl, other_impl);
|
Chris@16
|
132 }
|
Chris@16
|
133 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
134
|
Chris@16
|
135 /// Destroy a stream socket implementation.
|
Chris@16
|
136 void destroy(implementation_type& impl)
|
Chris@16
|
137 {
|
Chris@16
|
138 service_impl_.destroy(impl);
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 /// Open a stream socket.
|
Chris@16
|
142 boost::system::error_code open(implementation_type& impl,
|
Chris@16
|
143 const protocol_type& protocol, boost::system::error_code& ec)
|
Chris@16
|
144 {
|
Chris@16
|
145 if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_STREAM))
|
Chris@16
|
146 service_impl_.open(impl, protocol, ec);
|
Chris@16
|
147 else
|
Chris@16
|
148 ec = boost::asio::error::invalid_argument;
|
Chris@16
|
149 return ec;
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 /// Assign an existing native socket to a stream socket.
|
Chris@16
|
153 boost::system::error_code assign(implementation_type& impl,
|
Chris@16
|
154 const protocol_type& protocol, const native_handle_type& native_socket,
|
Chris@16
|
155 boost::system::error_code& ec)
|
Chris@16
|
156 {
|
Chris@16
|
157 return service_impl_.assign(impl, protocol, native_socket, ec);
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 /// Determine whether the socket is open.
|
Chris@16
|
161 bool is_open(const implementation_type& impl) const
|
Chris@16
|
162 {
|
Chris@16
|
163 return service_impl_.is_open(impl);
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 /// Close a stream socket implementation.
|
Chris@16
|
167 boost::system::error_code close(implementation_type& impl,
|
Chris@16
|
168 boost::system::error_code& ec)
|
Chris@16
|
169 {
|
Chris@16
|
170 return service_impl_.close(impl, ec);
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 /// (Deprecated: Use native_handle().) Get the native socket implementation.
|
Chris@16
|
174 native_type native(implementation_type& impl)
|
Chris@16
|
175 {
|
Chris@16
|
176 return service_impl_.native_handle(impl);
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 /// Get the native socket implementation.
|
Chris@16
|
180 native_handle_type native_handle(implementation_type& impl)
|
Chris@16
|
181 {
|
Chris@16
|
182 return service_impl_.native_handle(impl);
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 /// Cancel all asynchronous operations associated with the socket.
|
Chris@16
|
186 boost::system::error_code cancel(implementation_type& impl,
|
Chris@16
|
187 boost::system::error_code& ec)
|
Chris@16
|
188 {
|
Chris@16
|
189 return service_impl_.cancel(impl, ec);
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 /// Determine whether the socket is at the out-of-band data mark.
|
Chris@16
|
193 bool at_mark(const implementation_type& impl,
|
Chris@16
|
194 boost::system::error_code& ec) const
|
Chris@16
|
195 {
|
Chris@16
|
196 return service_impl_.at_mark(impl, ec);
|
Chris@16
|
197 }
|
Chris@16
|
198
|
Chris@16
|
199 /// Determine the number of bytes available for reading.
|
Chris@16
|
200 std::size_t available(const implementation_type& impl,
|
Chris@16
|
201 boost::system::error_code& ec) const
|
Chris@16
|
202 {
|
Chris@16
|
203 return service_impl_.available(impl, ec);
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 /// Bind the stream socket to the specified local endpoint.
|
Chris@16
|
207 boost::system::error_code bind(implementation_type& impl,
|
Chris@16
|
208 const endpoint_type& endpoint, boost::system::error_code& ec)
|
Chris@16
|
209 {
|
Chris@16
|
210 return service_impl_.bind(impl, endpoint, ec);
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 /// Connect the stream socket to the specified endpoint.
|
Chris@16
|
214 boost::system::error_code connect(implementation_type& impl,
|
Chris@16
|
215 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
|
Chris@16
|
216 {
|
Chris@16
|
217 return service_impl_.connect(impl, peer_endpoint, ec);
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 /// Start an asynchronous connect.
|
Chris@16
|
221 template <typename ConnectHandler>
|
Chris@16
|
222 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
|
Chris@16
|
223 void (boost::system::error_code))
|
Chris@16
|
224 async_connect(implementation_type& impl,
|
Chris@16
|
225 const endpoint_type& peer_endpoint,
|
Chris@16
|
226 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
|
Chris@16
|
227 {
|
Chris@16
|
228 detail::async_result_init<
|
Chris@16
|
229 ConnectHandler, void (boost::system::error_code)> init(
|
Chris@16
|
230 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
|
Chris@16
|
231
|
Chris@16
|
232 service_impl_.async_connect(impl, peer_endpoint, init.handler);
|
Chris@16
|
233
|
Chris@16
|
234 return init.result.get();
|
Chris@16
|
235 }
|
Chris@16
|
236
|
Chris@16
|
237 /// Set a socket option.
|
Chris@16
|
238 template <typename SettableSocketOption>
|
Chris@16
|
239 boost::system::error_code set_option(implementation_type& impl,
|
Chris@16
|
240 const SettableSocketOption& option, boost::system::error_code& ec)
|
Chris@16
|
241 {
|
Chris@16
|
242 return service_impl_.set_option(impl, option, ec);
|
Chris@16
|
243 }
|
Chris@16
|
244
|
Chris@16
|
245 /// Get a socket option.
|
Chris@16
|
246 template <typename GettableSocketOption>
|
Chris@16
|
247 boost::system::error_code get_option(const implementation_type& impl,
|
Chris@16
|
248 GettableSocketOption& option, boost::system::error_code& ec) const
|
Chris@16
|
249 {
|
Chris@16
|
250 return service_impl_.get_option(impl, option, ec);
|
Chris@16
|
251 }
|
Chris@16
|
252
|
Chris@16
|
253 /// Perform an IO control command on the socket.
|
Chris@16
|
254 template <typename IoControlCommand>
|
Chris@16
|
255 boost::system::error_code io_control(implementation_type& impl,
|
Chris@16
|
256 IoControlCommand& command, boost::system::error_code& ec)
|
Chris@16
|
257 {
|
Chris@16
|
258 return service_impl_.io_control(impl, command, ec);
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 /// Gets the non-blocking mode of the socket.
|
Chris@16
|
262 bool non_blocking(const implementation_type& impl) const
|
Chris@16
|
263 {
|
Chris@16
|
264 return service_impl_.non_blocking(impl);
|
Chris@16
|
265 }
|
Chris@16
|
266
|
Chris@16
|
267 /// Sets the non-blocking mode of the socket.
|
Chris@16
|
268 boost::system::error_code non_blocking(implementation_type& impl,
|
Chris@16
|
269 bool mode, boost::system::error_code& ec)
|
Chris@16
|
270 {
|
Chris@16
|
271 return service_impl_.non_blocking(impl, mode, ec);
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 /// Gets the non-blocking mode of the native socket implementation.
|
Chris@16
|
275 bool native_non_blocking(const implementation_type& impl) const
|
Chris@16
|
276 {
|
Chris@16
|
277 return service_impl_.native_non_blocking(impl);
|
Chris@16
|
278 }
|
Chris@16
|
279
|
Chris@16
|
280 /// Sets the non-blocking mode of the native socket implementation.
|
Chris@16
|
281 boost::system::error_code native_non_blocking(implementation_type& impl,
|
Chris@16
|
282 bool mode, boost::system::error_code& ec)
|
Chris@16
|
283 {
|
Chris@16
|
284 return service_impl_.native_non_blocking(impl, mode, ec);
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 /// Get the local endpoint.
|
Chris@16
|
288 endpoint_type local_endpoint(const implementation_type& impl,
|
Chris@16
|
289 boost::system::error_code& ec) const
|
Chris@16
|
290 {
|
Chris@16
|
291 return service_impl_.local_endpoint(impl, ec);
|
Chris@16
|
292 }
|
Chris@16
|
293
|
Chris@16
|
294 /// Get the remote endpoint.
|
Chris@16
|
295 endpoint_type remote_endpoint(const implementation_type& impl,
|
Chris@16
|
296 boost::system::error_code& ec) const
|
Chris@16
|
297 {
|
Chris@16
|
298 return service_impl_.remote_endpoint(impl, ec);
|
Chris@16
|
299 }
|
Chris@16
|
300
|
Chris@16
|
301 /// Disable sends or receives on the socket.
|
Chris@16
|
302 boost::system::error_code shutdown(implementation_type& impl,
|
Chris@16
|
303 socket_base::shutdown_type what, boost::system::error_code& ec)
|
Chris@16
|
304 {
|
Chris@16
|
305 return service_impl_.shutdown(impl, what, ec);
|
Chris@16
|
306 }
|
Chris@16
|
307
|
Chris@16
|
308 /// Send the given data to the peer.
|
Chris@16
|
309 template <typename ConstBufferSequence>
|
Chris@16
|
310 std::size_t send(implementation_type& impl,
|
Chris@16
|
311 const ConstBufferSequence& buffers,
|
Chris@16
|
312 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
313 {
|
Chris@16
|
314 return service_impl_.send(impl, buffers, flags, ec);
|
Chris@16
|
315 }
|
Chris@16
|
316
|
Chris@16
|
317 /// Start an asynchronous send.
|
Chris@16
|
318 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
319 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
320 void (boost::system::error_code, std::size_t))
|
Chris@16
|
321 async_send(implementation_type& impl,
|
Chris@16
|
322 const ConstBufferSequence& buffers,
|
Chris@16
|
323 socket_base::message_flags flags,
|
Chris@16
|
324 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
325 {
|
Chris@16
|
326 detail::async_result_init<
|
Chris@16
|
327 WriteHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
328 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
329
|
Chris@16
|
330 service_impl_.async_send(impl, buffers, flags, init.handler);
|
Chris@16
|
331
|
Chris@16
|
332 return init.result.get();
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 /// Receive some data from the peer.
|
Chris@16
|
336 template <typename MutableBufferSequence>
|
Chris@16
|
337 std::size_t receive(implementation_type& impl,
|
Chris@16
|
338 const MutableBufferSequence& buffers,
|
Chris@16
|
339 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
340 {
|
Chris@16
|
341 return service_impl_.receive(impl, buffers, flags, ec);
|
Chris@16
|
342 }
|
Chris@16
|
343
|
Chris@16
|
344 /// Start an asynchronous receive.
|
Chris@16
|
345 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
346 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
347 void (boost::system::error_code, std::size_t))
|
Chris@16
|
348 async_receive(implementation_type& impl,
|
Chris@16
|
349 const MutableBufferSequence& buffers,
|
Chris@16
|
350 socket_base::message_flags flags,
|
Chris@16
|
351 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
352 {
|
Chris@16
|
353 detail::async_result_init<
|
Chris@16
|
354 ReadHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
355 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
356
|
Chris@16
|
357 service_impl_.async_receive(impl, buffers, flags, init.handler);
|
Chris@16
|
358
|
Chris@16
|
359 return init.result.get();
|
Chris@16
|
360 }
|
Chris@16
|
361
|
Chris@16
|
362 private:
|
Chris@16
|
363 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
364 void shutdown_service()
|
Chris@16
|
365 {
|
Chris@16
|
366 service_impl_.shutdown_service();
|
Chris@16
|
367 }
|
Chris@16
|
368
|
Chris@16
|
369 // The platform-specific implementation.
|
Chris@16
|
370 service_impl_type service_impl_;
|
Chris@16
|
371 };
|
Chris@16
|
372
|
Chris@16
|
373 } // namespace asio
|
Chris@16
|
374 } // namespace boost
|
Chris@16
|
375
|
Chris@16
|
376 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
377
|
Chris@16
|
378 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|