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