Chris@16
|
1 //
|
Chris@16
|
2 // basic_socket_acceptor.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_BASIC_SOCKET_ACCEPTOR_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_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 <boost/asio/basic_io_object.hpp>
|
Chris@16
|
20 #include <boost/asio/basic_socket.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
24 #include <boost/asio/error.hpp>
|
Chris@16
|
25 #include <boost/asio/socket_acceptor_service.hpp>
|
Chris@16
|
26 #include <boost/asio/socket_base.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace asio {
|
Chris@16
|
32
|
Chris@16
|
33 /// Provides the ability to accept new connections.
|
Chris@16
|
34 /**
|
Chris@16
|
35 * The basic_socket_acceptor class template is used for accepting new socket
|
Chris@16
|
36 * connections.
|
Chris@16
|
37 *
|
Chris@16
|
38 * @par Thread Safety
|
Chris@16
|
39 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
40 * @e Shared @e objects: Unsafe.
|
Chris@16
|
41 *
|
Chris@16
|
42 * @par Example
|
Chris@16
|
43 * Opening a socket acceptor with the SO_REUSEADDR option enabled:
|
Chris@16
|
44 * @code
|
Chris@16
|
45 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
46 * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
|
Chris@16
|
47 * acceptor.open(endpoint.protocol());
|
Chris@16
|
48 * acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
Chris@16
|
49 * acceptor.bind(endpoint);
|
Chris@16
|
50 * acceptor.listen();
|
Chris@16
|
51 * @endcode
|
Chris@16
|
52 */
|
Chris@16
|
53 template <typename Protocol,
|
Chris@16
|
54 typename SocketAcceptorService = socket_acceptor_service<Protocol> >
|
Chris@16
|
55 class basic_socket_acceptor
|
Chris@16
|
56 : public basic_io_object<SocketAcceptorService>,
|
Chris@16
|
57 public socket_base
|
Chris@16
|
58 {
|
Chris@16
|
59 public:
|
Chris@16
|
60 /// (Deprecated: Use native_handle_type.) The native representation of an
|
Chris@16
|
61 /// acceptor.
|
Chris@16
|
62 typedef typename SocketAcceptorService::native_handle_type native_type;
|
Chris@16
|
63
|
Chris@16
|
64 /// The native representation of an acceptor.
|
Chris@16
|
65 typedef typename SocketAcceptorService::native_handle_type native_handle_type;
|
Chris@16
|
66
|
Chris@16
|
67 /// The protocol type.
|
Chris@16
|
68 typedef Protocol protocol_type;
|
Chris@16
|
69
|
Chris@16
|
70 /// The endpoint type.
|
Chris@16
|
71 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
72
|
Chris@16
|
73 /// Construct an acceptor without opening it.
|
Chris@16
|
74 /**
|
Chris@16
|
75 * This constructor creates an acceptor without opening it to listen for new
|
Chris@16
|
76 * connections. The open() function must be called before the acceptor can
|
Chris@16
|
77 * accept new socket connections.
|
Chris@16
|
78 *
|
Chris@16
|
79 * @param io_service The io_service object that the acceptor will use to
|
Chris@16
|
80 * dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
81 * acceptor.
|
Chris@16
|
82 */
|
Chris@16
|
83 explicit basic_socket_acceptor(boost::asio::io_service& io_service)
|
Chris@16
|
84 : basic_io_object<SocketAcceptorService>(io_service)
|
Chris@16
|
85 {
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 /// Construct an open acceptor.
|
Chris@16
|
89 /**
|
Chris@16
|
90 * This constructor creates an acceptor and automatically opens it.
|
Chris@16
|
91 *
|
Chris@16
|
92 * @param io_service The io_service object that the acceptor will use to
|
Chris@16
|
93 * dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
94 * acceptor.
|
Chris@16
|
95 *
|
Chris@16
|
96 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
97 *
|
Chris@16
|
98 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
99 */
|
Chris@16
|
100 basic_socket_acceptor(boost::asio::io_service& io_service,
|
Chris@16
|
101 const protocol_type& protocol)
|
Chris@16
|
102 : basic_io_object<SocketAcceptorService>(io_service)
|
Chris@16
|
103 {
|
Chris@16
|
104 boost::system::error_code ec;
|
Chris@16
|
105 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
106 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 /// Construct an acceptor opened on the given endpoint.
|
Chris@16
|
110 /**
|
Chris@16
|
111 * This constructor creates an acceptor and automatically opens it to listen
|
Chris@16
|
112 * for new connections on the specified endpoint.
|
Chris@16
|
113 *
|
Chris@16
|
114 * @param io_service The io_service object that the acceptor will use to
|
Chris@16
|
115 * dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
116 * acceptor.
|
Chris@16
|
117 *
|
Chris@16
|
118 * @param endpoint An endpoint on the local machine on which the acceptor
|
Chris@16
|
119 * will listen for new connections.
|
Chris@16
|
120 *
|
Chris@16
|
121 * @param reuse_addr Whether the constructor should set the socket option
|
Chris@16
|
122 * socket_base::reuse_address.
|
Chris@16
|
123 *
|
Chris@16
|
124 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
125 *
|
Chris@16
|
126 * @note This constructor is equivalent to the following code:
|
Chris@16
|
127 * @code
|
Chris@16
|
128 * basic_socket_acceptor<Protocol> acceptor(io_service);
|
Chris@16
|
129 * acceptor.open(endpoint.protocol());
|
Chris@16
|
130 * if (reuse_addr)
|
Chris@16
|
131 * acceptor.set_option(socket_base::reuse_address(true));
|
Chris@16
|
132 * acceptor.bind(endpoint);
|
Chris@16
|
133 * acceptor.listen(listen_backlog);
|
Chris@16
|
134 * @endcode
|
Chris@16
|
135 */
|
Chris@16
|
136 basic_socket_acceptor(boost::asio::io_service& io_service,
|
Chris@16
|
137 const endpoint_type& endpoint, bool reuse_addr = true)
|
Chris@16
|
138 : basic_io_object<SocketAcceptorService>(io_service)
|
Chris@16
|
139 {
|
Chris@16
|
140 boost::system::error_code ec;
|
Chris@16
|
141 const protocol_type protocol = endpoint.protocol();
|
Chris@16
|
142 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
143 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
144 if (reuse_addr)
|
Chris@16
|
145 {
|
Chris@16
|
146 this->get_service().set_option(this->get_implementation(),
|
Chris@16
|
147 socket_base::reuse_address(true), ec);
|
Chris@16
|
148 boost::asio::detail::throw_error(ec, "set_option");
|
Chris@16
|
149 }
|
Chris@16
|
150 this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
151 boost::asio::detail::throw_error(ec, "bind");
|
Chris@16
|
152 this->get_service().listen(this->get_implementation(),
|
Chris@16
|
153 socket_base::max_connections, ec);
|
Chris@16
|
154 boost::asio::detail::throw_error(ec, "listen");
|
Chris@16
|
155 }
|
Chris@16
|
156
|
Chris@16
|
157 /// Construct a basic_socket_acceptor on an existing native acceptor.
|
Chris@16
|
158 /**
|
Chris@16
|
159 * This constructor creates an acceptor object to hold an existing native
|
Chris@16
|
160 * acceptor.
|
Chris@16
|
161 *
|
Chris@16
|
162 * @param io_service The io_service object that the acceptor will use to
|
Chris@16
|
163 * dispatch handlers for any asynchronous operations performed on the
|
Chris@16
|
164 * acceptor.
|
Chris@16
|
165 *
|
Chris@16
|
166 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
167 *
|
Chris@16
|
168 * @param native_acceptor A native acceptor.
|
Chris@16
|
169 *
|
Chris@16
|
170 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
171 */
|
Chris@16
|
172 basic_socket_acceptor(boost::asio::io_service& io_service,
|
Chris@16
|
173 const protocol_type& protocol, const native_handle_type& native_acceptor)
|
Chris@16
|
174 : basic_io_object<SocketAcceptorService>(io_service)
|
Chris@16
|
175 {
|
Chris@16
|
176 boost::system::error_code ec;
|
Chris@16
|
177 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
178 protocol, native_acceptor, ec);
|
Chris@16
|
179 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
180 }
|
Chris@16
|
181
|
Chris@16
|
182 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
183 /// Move-construct a basic_socket_acceptor from another.
|
Chris@16
|
184 /**
|
Chris@16
|
185 * This constructor moves an acceptor from one object to another.
|
Chris@16
|
186 *
|
Chris@16
|
187 * @param other The other basic_socket_acceptor object from which the move
|
Chris@16
|
188 * will occur.
|
Chris@16
|
189 *
|
Chris@16
|
190 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
191 * constructed using the @c basic_socket_acceptor(io_service&) constructor.
|
Chris@16
|
192 */
|
Chris@16
|
193 basic_socket_acceptor(basic_socket_acceptor&& other)
|
Chris@16
|
194 : basic_io_object<SocketAcceptorService>(
|
Chris@16
|
195 BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other))
|
Chris@16
|
196 {
|
Chris@16
|
197 }
|
Chris@16
|
198
|
Chris@16
|
199 /// Move-assign a basic_socket_acceptor from another.
|
Chris@16
|
200 /**
|
Chris@16
|
201 * This assignment operator moves an acceptor from one object to another.
|
Chris@16
|
202 *
|
Chris@16
|
203 * @param other The other basic_socket_acceptor object from which the move
|
Chris@16
|
204 * will occur.
|
Chris@16
|
205 *
|
Chris@16
|
206 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
207 * constructed using the @c basic_socket_acceptor(io_service&) constructor.
|
Chris@16
|
208 */
|
Chris@16
|
209 basic_socket_acceptor& operator=(basic_socket_acceptor&& other)
|
Chris@16
|
210 {
|
Chris@16
|
211 basic_io_object<SocketAcceptorService>::operator=(
|
Chris@16
|
212 BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other));
|
Chris@16
|
213 return *this;
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 // All socket acceptors have access to each other's implementations.
|
Chris@16
|
217 template <typename Protocol1, typename SocketAcceptorService1>
|
Chris@16
|
218 friend class basic_socket_acceptor;
|
Chris@16
|
219
|
Chris@16
|
220 /// Move-construct a basic_socket_acceptor from an acceptor of another
|
Chris@16
|
221 /// protocol type.
|
Chris@16
|
222 /**
|
Chris@16
|
223 * This constructor moves an acceptor from one object to another.
|
Chris@16
|
224 *
|
Chris@16
|
225 * @param other The other basic_socket_acceptor object from which the move
|
Chris@16
|
226 * will occur.
|
Chris@16
|
227 *
|
Chris@16
|
228 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
229 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
230 */
|
Chris@16
|
231 template <typename Protocol1, typename SocketAcceptorService1>
|
Chris@16
|
232 basic_socket_acceptor(
|
Chris@16
|
233 basic_socket_acceptor<Protocol1, SocketAcceptorService1>&& other,
|
Chris@16
|
234 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
|
Chris@16
|
235 : basic_io_object<SocketAcceptorService>(other.get_io_service())
|
Chris@16
|
236 {
|
Chris@16
|
237 this->get_service().template converting_move_construct<Protocol1>(
|
Chris@16
|
238 this->get_implementation(), other.get_implementation());
|
Chris@16
|
239 }
|
Chris@16
|
240
|
Chris@16
|
241 /// Move-assign a basic_socket_acceptor from an acceptor of another protocol
|
Chris@16
|
242 /// type.
|
Chris@16
|
243 /**
|
Chris@16
|
244 * This assignment operator moves an acceptor from one object to another.
|
Chris@16
|
245 *
|
Chris@16
|
246 * @param other The other basic_socket_acceptor object from which the move
|
Chris@16
|
247 * will occur.
|
Chris@16
|
248 *
|
Chris@16
|
249 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
250 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
251 */
|
Chris@16
|
252 template <typename Protocol1, typename SocketAcceptorService1>
|
Chris@16
|
253 typename enable_if<is_convertible<Protocol1, Protocol>::value,
|
Chris@16
|
254 basic_socket_acceptor>::type& operator=(
|
Chris@16
|
255 basic_socket_acceptor<Protocol1, SocketAcceptorService1>&& other)
|
Chris@16
|
256 {
|
Chris@16
|
257 basic_socket_acceptor tmp(BOOST_ASIO_MOVE_CAST2(basic_socket_acceptor<
|
Chris@16
|
258 Protocol1, SocketAcceptorService1>)(other));
|
Chris@16
|
259 basic_io_object<SocketAcceptorService>::operator=(
|
Chris@16
|
260 BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(tmp));
|
Chris@16
|
261 return *this;
|
Chris@16
|
262 }
|
Chris@16
|
263 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
264
|
Chris@16
|
265 /// Open the acceptor using the specified protocol.
|
Chris@16
|
266 /**
|
Chris@16
|
267 * This function opens the socket acceptor so that it will use the specified
|
Chris@16
|
268 * protocol.
|
Chris@16
|
269 *
|
Chris@16
|
270 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
271 *
|
Chris@16
|
272 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
273 *
|
Chris@16
|
274 * @par Example
|
Chris@16
|
275 * @code
|
Chris@16
|
276 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
277 * acceptor.open(boost::asio::ip::tcp::v4());
|
Chris@16
|
278 * @endcode
|
Chris@16
|
279 */
|
Chris@16
|
280 void open(const protocol_type& protocol = protocol_type())
|
Chris@16
|
281 {
|
Chris@16
|
282 boost::system::error_code ec;
|
Chris@16
|
283 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
284 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 /// Open the acceptor using the specified protocol.
|
Chris@16
|
288 /**
|
Chris@16
|
289 * This function opens the socket acceptor so that it will use the specified
|
Chris@16
|
290 * protocol.
|
Chris@16
|
291 *
|
Chris@16
|
292 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
293 *
|
Chris@16
|
294 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
295 *
|
Chris@16
|
296 * @par Example
|
Chris@16
|
297 * @code
|
Chris@16
|
298 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
299 * boost::system::error_code ec;
|
Chris@16
|
300 * acceptor.open(boost::asio::ip::tcp::v4(), ec);
|
Chris@16
|
301 * if (ec)
|
Chris@16
|
302 * {
|
Chris@16
|
303 * // An error occurred.
|
Chris@16
|
304 * }
|
Chris@16
|
305 * @endcode
|
Chris@16
|
306 */
|
Chris@16
|
307 boost::system::error_code open(const protocol_type& protocol,
|
Chris@16
|
308 boost::system::error_code& ec)
|
Chris@16
|
309 {
|
Chris@16
|
310 return this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313 /// Assigns an existing native acceptor to the acceptor.
|
Chris@16
|
314 /*
|
Chris@16
|
315 * This function opens the acceptor to hold an existing native acceptor.
|
Chris@16
|
316 *
|
Chris@16
|
317 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
318 *
|
Chris@16
|
319 * @param native_acceptor A native acceptor.
|
Chris@16
|
320 *
|
Chris@16
|
321 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
322 */
|
Chris@16
|
323 void assign(const protocol_type& protocol,
|
Chris@16
|
324 const native_handle_type& native_acceptor)
|
Chris@16
|
325 {
|
Chris@16
|
326 boost::system::error_code ec;
|
Chris@16
|
327 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
328 protocol, native_acceptor, ec);
|
Chris@16
|
329 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 /// Assigns an existing native acceptor to the acceptor.
|
Chris@16
|
333 /*
|
Chris@16
|
334 * This function opens the acceptor to hold an existing native acceptor.
|
Chris@16
|
335 *
|
Chris@16
|
336 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
337 *
|
Chris@16
|
338 * @param native_acceptor A native acceptor.
|
Chris@16
|
339 *
|
Chris@16
|
340 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
341 */
|
Chris@16
|
342 boost::system::error_code assign(const protocol_type& protocol,
|
Chris@16
|
343 const native_handle_type& native_acceptor, boost::system::error_code& ec)
|
Chris@16
|
344 {
|
Chris@16
|
345 return this->get_service().assign(this->get_implementation(),
|
Chris@16
|
346 protocol, native_acceptor, ec);
|
Chris@16
|
347 }
|
Chris@16
|
348
|
Chris@16
|
349 /// Determine whether the acceptor is open.
|
Chris@16
|
350 bool is_open() const
|
Chris@16
|
351 {
|
Chris@16
|
352 return this->get_service().is_open(this->get_implementation());
|
Chris@16
|
353 }
|
Chris@16
|
354
|
Chris@16
|
355 /// Bind the acceptor to the given local endpoint.
|
Chris@16
|
356 /**
|
Chris@16
|
357 * This function binds the socket acceptor to the specified endpoint on the
|
Chris@16
|
358 * local machine.
|
Chris@16
|
359 *
|
Chris@16
|
360 * @param endpoint An endpoint on the local machine to which the socket
|
Chris@16
|
361 * acceptor will be bound.
|
Chris@16
|
362 *
|
Chris@16
|
363 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
364 *
|
Chris@16
|
365 * @par Example
|
Chris@16
|
366 * @code
|
Chris@16
|
367 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
368 * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345);
|
Chris@16
|
369 * acceptor.open(endpoint.protocol());
|
Chris@16
|
370 * acceptor.bind(endpoint);
|
Chris@16
|
371 * @endcode
|
Chris@16
|
372 */
|
Chris@16
|
373 void bind(const endpoint_type& endpoint)
|
Chris@16
|
374 {
|
Chris@16
|
375 boost::system::error_code ec;
|
Chris@16
|
376 this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
377 boost::asio::detail::throw_error(ec, "bind");
|
Chris@16
|
378 }
|
Chris@16
|
379
|
Chris@16
|
380 /// Bind the acceptor to the given local endpoint.
|
Chris@16
|
381 /**
|
Chris@16
|
382 * This function binds the socket acceptor to the specified endpoint on the
|
Chris@16
|
383 * local machine.
|
Chris@16
|
384 *
|
Chris@16
|
385 * @param endpoint An endpoint on the local machine to which the socket
|
Chris@16
|
386 * acceptor will be bound.
|
Chris@16
|
387 *
|
Chris@16
|
388 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
389 *
|
Chris@16
|
390 * @par Example
|
Chris@16
|
391 * @code
|
Chris@16
|
392 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
393 * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345);
|
Chris@16
|
394 * acceptor.open(endpoint.protocol());
|
Chris@16
|
395 * boost::system::error_code ec;
|
Chris@16
|
396 * acceptor.bind(endpoint, ec);
|
Chris@16
|
397 * if (ec)
|
Chris@16
|
398 * {
|
Chris@16
|
399 * // An error occurred.
|
Chris@16
|
400 * }
|
Chris@16
|
401 * @endcode
|
Chris@16
|
402 */
|
Chris@16
|
403 boost::system::error_code bind(const endpoint_type& endpoint,
|
Chris@16
|
404 boost::system::error_code& ec)
|
Chris@16
|
405 {
|
Chris@16
|
406 return this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
407 }
|
Chris@16
|
408
|
Chris@16
|
409 /// Place the acceptor into the state where it will listen for new
|
Chris@16
|
410 /// connections.
|
Chris@16
|
411 /**
|
Chris@16
|
412 * This function puts the socket acceptor into the state where it may accept
|
Chris@16
|
413 * new connections.
|
Chris@16
|
414 *
|
Chris@16
|
415 * @param backlog The maximum length of the queue of pending connections.
|
Chris@16
|
416 *
|
Chris@16
|
417 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
418 */
|
Chris@16
|
419 void listen(int backlog = socket_base::max_connections)
|
Chris@16
|
420 {
|
Chris@16
|
421 boost::system::error_code ec;
|
Chris@16
|
422 this->get_service().listen(this->get_implementation(), backlog, ec);
|
Chris@16
|
423 boost::asio::detail::throw_error(ec, "listen");
|
Chris@16
|
424 }
|
Chris@16
|
425
|
Chris@16
|
426 /// Place the acceptor into the state where it will listen for new
|
Chris@16
|
427 /// connections.
|
Chris@16
|
428 /**
|
Chris@16
|
429 * This function puts the socket acceptor into the state where it may accept
|
Chris@16
|
430 * new connections.
|
Chris@16
|
431 *
|
Chris@16
|
432 * @param backlog The maximum length of the queue of pending connections.
|
Chris@16
|
433 *
|
Chris@16
|
434 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
435 *
|
Chris@16
|
436 * @par Example
|
Chris@16
|
437 * @code
|
Chris@16
|
438 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
439 * ...
|
Chris@16
|
440 * boost::system::error_code ec;
|
Chris@16
|
441 * acceptor.listen(boost::asio::socket_base::max_connections, ec);
|
Chris@16
|
442 * if (ec)
|
Chris@16
|
443 * {
|
Chris@16
|
444 * // An error occurred.
|
Chris@16
|
445 * }
|
Chris@16
|
446 * @endcode
|
Chris@16
|
447 */
|
Chris@16
|
448 boost::system::error_code listen(int backlog, boost::system::error_code& ec)
|
Chris@16
|
449 {
|
Chris@16
|
450 return this->get_service().listen(this->get_implementation(), backlog, ec);
|
Chris@16
|
451 }
|
Chris@16
|
452
|
Chris@16
|
453 /// Close the acceptor.
|
Chris@16
|
454 /**
|
Chris@16
|
455 * This function is used to close the acceptor. Any asynchronous accept
|
Chris@16
|
456 * operations will be cancelled immediately.
|
Chris@16
|
457 *
|
Chris@16
|
458 * A subsequent call to open() is required before the acceptor can again be
|
Chris@16
|
459 * used to again perform socket accept operations.
|
Chris@16
|
460 *
|
Chris@16
|
461 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
462 */
|
Chris@16
|
463 void close()
|
Chris@16
|
464 {
|
Chris@16
|
465 boost::system::error_code ec;
|
Chris@16
|
466 this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
467 boost::asio::detail::throw_error(ec, "close");
|
Chris@16
|
468 }
|
Chris@16
|
469
|
Chris@16
|
470 /// Close the acceptor.
|
Chris@16
|
471 /**
|
Chris@16
|
472 * This function is used to close the acceptor. Any asynchronous accept
|
Chris@16
|
473 * operations will be cancelled immediately.
|
Chris@16
|
474 *
|
Chris@16
|
475 * A subsequent call to open() is required before the acceptor can again be
|
Chris@16
|
476 * used to again perform socket accept operations.
|
Chris@16
|
477 *
|
Chris@16
|
478 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
479 *
|
Chris@16
|
480 * @par Example
|
Chris@16
|
481 * @code
|
Chris@16
|
482 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
483 * ...
|
Chris@16
|
484 * boost::system::error_code ec;
|
Chris@16
|
485 * acceptor.close(ec);
|
Chris@16
|
486 * if (ec)
|
Chris@16
|
487 * {
|
Chris@16
|
488 * // An error occurred.
|
Chris@16
|
489 * }
|
Chris@16
|
490 * @endcode
|
Chris@16
|
491 */
|
Chris@16
|
492 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
493 {
|
Chris@16
|
494 return this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
495 }
|
Chris@16
|
496
|
Chris@16
|
497 /// (Deprecated: Use native_handle().) Get the native acceptor representation.
|
Chris@16
|
498 /**
|
Chris@16
|
499 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
500 * acceptor. This is intended to allow access to native acceptor functionality
|
Chris@16
|
501 * that is not otherwise provided.
|
Chris@16
|
502 */
|
Chris@16
|
503 native_type native()
|
Chris@16
|
504 {
|
Chris@16
|
505 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
506 }
|
Chris@16
|
507
|
Chris@16
|
508 /// Get the native acceptor representation.
|
Chris@16
|
509 /**
|
Chris@16
|
510 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
511 * acceptor. This is intended to allow access to native acceptor functionality
|
Chris@16
|
512 * that is not otherwise provided.
|
Chris@16
|
513 */
|
Chris@16
|
514 native_handle_type native_handle()
|
Chris@16
|
515 {
|
Chris@16
|
516 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
517 }
|
Chris@16
|
518
|
Chris@16
|
519 /// Cancel all asynchronous operations associated with the acceptor.
|
Chris@16
|
520 /**
|
Chris@16
|
521 * This function causes all outstanding asynchronous connect, send and receive
|
Chris@16
|
522 * operations to finish immediately, and the handlers for cancelled operations
|
Chris@16
|
523 * will be passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
524 *
|
Chris@16
|
525 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
526 */
|
Chris@16
|
527 void cancel()
|
Chris@16
|
528 {
|
Chris@16
|
529 boost::system::error_code ec;
|
Chris@16
|
530 this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
531 boost::asio::detail::throw_error(ec, "cancel");
|
Chris@16
|
532 }
|
Chris@16
|
533
|
Chris@16
|
534 /// Cancel all asynchronous operations associated with the acceptor.
|
Chris@16
|
535 /**
|
Chris@16
|
536 * This function causes all outstanding asynchronous connect, send and receive
|
Chris@16
|
537 * operations to finish immediately, and the handlers for cancelled operations
|
Chris@16
|
538 * will be passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
539 *
|
Chris@16
|
540 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
541 */
|
Chris@16
|
542 boost::system::error_code cancel(boost::system::error_code& ec)
|
Chris@16
|
543 {
|
Chris@16
|
544 return this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
545 }
|
Chris@16
|
546
|
Chris@16
|
547 /// Set an option on the acceptor.
|
Chris@16
|
548 /**
|
Chris@16
|
549 * This function is used to set an option on the acceptor.
|
Chris@16
|
550 *
|
Chris@16
|
551 * @param option The new option value to be set on the acceptor.
|
Chris@16
|
552 *
|
Chris@16
|
553 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
554 *
|
Chris@16
|
555 * @sa SettableSocketOption @n
|
Chris@16
|
556 * boost::asio::socket_base::reuse_address
|
Chris@16
|
557 * boost::asio::socket_base::enable_connection_aborted
|
Chris@16
|
558 *
|
Chris@16
|
559 * @par Example
|
Chris@16
|
560 * Setting the SOL_SOCKET/SO_REUSEADDR option:
|
Chris@16
|
561 * @code
|
Chris@16
|
562 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
563 * ...
|
Chris@16
|
564 * boost::asio::ip::tcp::acceptor::reuse_address option(true);
|
Chris@16
|
565 * acceptor.set_option(option);
|
Chris@16
|
566 * @endcode
|
Chris@16
|
567 */
|
Chris@16
|
568 template <typename SettableSocketOption>
|
Chris@16
|
569 void set_option(const SettableSocketOption& option)
|
Chris@16
|
570 {
|
Chris@16
|
571 boost::system::error_code ec;
|
Chris@16
|
572 this->get_service().set_option(this->get_implementation(), option, ec);
|
Chris@16
|
573 boost::asio::detail::throw_error(ec, "set_option");
|
Chris@16
|
574 }
|
Chris@16
|
575
|
Chris@16
|
576 /// Set an option on the acceptor.
|
Chris@16
|
577 /**
|
Chris@16
|
578 * This function is used to set an option on the acceptor.
|
Chris@16
|
579 *
|
Chris@16
|
580 * @param option The new option value to be set on the acceptor.
|
Chris@16
|
581 *
|
Chris@16
|
582 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
583 *
|
Chris@16
|
584 * @sa SettableSocketOption @n
|
Chris@16
|
585 * boost::asio::socket_base::reuse_address
|
Chris@16
|
586 * boost::asio::socket_base::enable_connection_aborted
|
Chris@16
|
587 *
|
Chris@16
|
588 * @par Example
|
Chris@16
|
589 * Setting the SOL_SOCKET/SO_REUSEADDR option:
|
Chris@16
|
590 * @code
|
Chris@16
|
591 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
592 * ...
|
Chris@16
|
593 * boost::asio::ip::tcp::acceptor::reuse_address option(true);
|
Chris@16
|
594 * boost::system::error_code ec;
|
Chris@16
|
595 * acceptor.set_option(option, ec);
|
Chris@16
|
596 * if (ec)
|
Chris@16
|
597 * {
|
Chris@16
|
598 * // An error occurred.
|
Chris@16
|
599 * }
|
Chris@16
|
600 * @endcode
|
Chris@16
|
601 */
|
Chris@16
|
602 template <typename SettableSocketOption>
|
Chris@16
|
603 boost::system::error_code set_option(const SettableSocketOption& option,
|
Chris@16
|
604 boost::system::error_code& ec)
|
Chris@16
|
605 {
|
Chris@16
|
606 return this->get_service().set_option(
|
Chris@16
|
607 this->get_implementation(), option, ec);
|
Chris@16
|
608 }
|
Chris@16
|
609
|
Chris@16
|
610 /// Get an option from the acceptor.
|
Chris@16
|
611 /**
|
Chris@16
|
612 * This function is used to get the current value of an option on the
|
Chris@16
|
613 * acceptor.
|
Chris@16
|
614 *
|
Chris@16
|
615 * @param option The option value to be obtained from the acceptor.
|
Chris@16
|
616 *
|
Chris@16
|
617 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
618 *
|
Chris@16
|
619 * @sa GettableSocketOption @n
|
Chris@16
|
620 * boost::asio::socket_base::reuse_address
|
Chris@16
|
621 *
|
Chris@16
|
622 * @par Example
|
Chris@16
|
623 * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
|
Chris@16
|
624 * @code
|
Chris@16
|
625 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
626 * ...
|
Chris@16
|
627 * boost::asio::ip::tcp::acceptor::reuse_address option;
|
Chris@16
|
628 * acceptor.get_option(option);
|
Chris@16
|
629 * bool is_set = option.get();
|
Chris@16
|
630 * @endcode
|
Chris@16
|
631 */
|
Chris@16
|
632 template <typename GettableSocketOption>
|
Chris@16
|
633 void get_option(GettableSocketOption& option)
|
Chris@16
|
634 {
|
Chris@16
|
635 boost::system::error_code ec;
|
Chris@16
|
636 this->get_service().get_option(this->get_implementation(), option, ec);
|
Chris@16
|
637 boost::asio::detail::throw_error(ec, "get_option");
|
Chris@16
|
638 }
|
Chris@16
|
639
|
Chris@16
|
640 /// Get an option from the acceptor.
|
Chris@16
|
641 /**
|
Chris@16
|
642 * This function is used to get the current value of an option on the
|
Chris@16
|
643 * acceptor.
|
Chris@16
|
644 *
|
Chris@16
|
645 * @param option The option value to be obtained from the acceptor.
|
Chris@16
|
646 *
|
Chris@16
|
647 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
648 *
|
Chris@16
|
649 * @sa GettableSocketOption @n
|
Chris@16
|
650 * boost::asio::socket_base::reuse_address
|
Chris@16
|
651 *
|
Chris@16
|
652 * @par Example
|
Chris@16
|
653 * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
|
Chris@16
|
654 * @code
|
Chris@16
|
655 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
656 * ...
|
Chris@16
|
657 * boost::asio::ip::tcp::acceptor::reuse_address option;
|
Chris@16
|
658 * boost::system::error_code ec;
|
Chris@16
|
659 * acceptor.get_option(option, ec);
|
Chris@16
|
660 * if (ec)
|
Chris@16
|
661 * {
|
Chris@16
|
662 * // An error occurred.
|
Chris@16
|
663 * }
|
Chris@16
|
664 * bool is_set = option.get();
|
Chris@16
|
665 * @endcode
|
Chris@16
|
666 */
|
Chris@16
|
667 template <typename GettableSocketOption>
|
Chris@16
|
668 boost::system::error_code get_option(GettableSocketOption& option,
|
Chris@16
|
669 boost::system::error_code& ec)
|
Chris@16
|
670 {
|
Chris@16
|
671 return this->get_service().get_option(
|
Chris@16
|
672 this->get_implementation(), option, ec);
|
Chris@16
|
673 }
|
Chris@16
|
674
|
Chris@16
|
675 /// Perform an IO control command on the acceptor.
|
Chris@16
|
676 /**
|
Chris@16
|
677 * This function is used to execute an IO control command on the acceptor.
|
Chris@16
|
678 *
|
Chris@16
|
679 * @param command The IO control command to be performed on the acceptor.
|
Chris@16
|
680 *
|
Chris@16
|
681 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
682 *
|
Chris@16
|
683 * @sa IoControlCommand @n
|
Chris@16
|
684 * boost::asio::socket_base::non_blocking_io
|
Chris@16
|
685 *
|
Chris@16
|
686 * @par Example
|
Chris@16
|
687 * Getting the number of bytes ready to read:
|
Chris@16
|
688 * @code
|
Chris@16
|
689 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
690 * ...
|
Chris@16
|
691 * boost::asio::ip::tcp::acceptor::non_blocking_io command(true);
|
Chris@16
|
692 * socket.io_control(command);
|
Chris@16
|
693 * @endcode
|
Chris@16
|
694 */
|
Chris@16
|
695 template <typename IoControlCommand>
|
Chris@16
|
696 void io_control(IoControlCommand& command)
|
Chris@16
|
697 {
|
Chris@16
|
698 boost::system::error_code ec;
|
Chris@16
|
699 this->get_service().io_control(this->get_implementation(), command, ec);
|
Chris@16
|
700 boost::asio::detail::throw_error(ec, "io_control");
|
Chris@16
|
701 }
|
Chris@16
|
702
|
Chris@16
|
703 /// Perform an IO control command on the acceptor.
|
Chris@16
|
704 /**
|
Chris@16
|
705 * This function is used to execute an IO control command on the acceptor.
|
Chris@16
|
706 *
|
Chris@16
|
707 * @param command The IO control command to be performed on the acceptor.
|
Chris@16
|
708 *
|
Chris@16
|
709 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
710 *
|
Chris@16
|
711 * @sa IoControlCommand @n
|
Chris@16
|
712 * boost::asio::socket_base::non_blocking_io
|
Chris@16
|
713 *
|
Chris@16
|
714 * @par Example
|
Chris@16
|
715 * Getting the number of bytes ready to read:
|
Chris@16
|
716 * @code
|
Chris@16
|
717 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
718 * ...
|
Chris@16
|
719 * boost::asio::ip::tcp::acceptor::non_blocking_io command(true);
|
Chris@16
|
720 * boost::system::error_code ec;
|
Chris@16
|
721 * socket.io_control(command, ec);
|
Chris@16
|
722 * if (ec)
|
Chris@16
|
723 * {
|
Chris@16
|
724 * // An error occurred.
|
Chris@16
|
725 * }
|
Chris@16
|
726 * @endcode
|
Chris@16
|
727 */
|
Chris@16
|
728 template <typename IoControlCommand>
|
Chris@16
|
729 boost::system::error_code io_control(IoControlCommand& command,
|
Chris@16
|
730 boost::system::error_code& ec)
|
Chris@16
|
731 {
|
Chris@16
|
732 return this->get_service().io_control(
|
Chris@16
|
733 this->get_implementation(), command, ec);
|
Chris@16
|
734 }
|
Chris@16
|
735
|
Chris@16
|
736 /// Gets the non-blocking mode of the acceptor.
|
Chris@16
|
737 /**
|
Chris@16
|
738 * @returns @c true if the acceptor's synchronous operations will fail with
|
Chris@16
|
739 * boost::asio::error::would_block if they are unable to perform the requested
|
Chris@16
|
740 * operation immediately. If @c false, synchronous operations will block
|
Chris@16
|
741 * until complete.
|
Chris@16
|
742 *
|
Chris@16
|
743 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
744 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
745 * boost::asio::error::would_block.
|
Chris@16
|
746 */
|
Chris@16
|
747 bool non_blocking() const
|
Chris@16
|
748 {
|
Chris@16
|
749 return this->get_service().non_blocking(this->get_implementation());
|
Chris@16
|
750 }
|
Chris@16
|
751
|
Chris@16
|
752 /// Sets the non-blocking mode of the acceptor.
|
Chris@16
|
753 /**
|
Chris@16
|
754 * @param mode If @c true, the acceptor's synchronous operations will fail
|
Chris@16
|
755 * with boost::asio::error::would_block if they are unable to perform the
|
Chris@16
|
756 * requested operation immediately. If @c false, synchronous operations will
|
Chris@16
|
757 * block until complete.
|
Chris@16
|
758 *
|
Chris@16
|
759 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
760 *
|
Chris@16
|
761 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
762 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
763 * boost::asio::error::would_block.
|
Chris@16
|
764 */
|
Chris@16
|
765 void non_blocking(bool mode)
|
Chris@16
|
766 {
|
Chris@16
|
767 boost::system::error_code ec;
|
Chris@16
|
768 this->get_service().non_blocking(this->get_implementation(), mode, ec);
|
Chris@16
|
769 boost::asio::detail::throw_error(ec, "non_blocking");
|
Chris@16
|
770 }
|
Chris@16
|
771
|
Chris@16
|
772 /// Sets the non-blocking mode of the acceptor.
|
Chris@16
|
773 /**
|
Chris@16
|
774 * @param mode If @c true, the acceptor's synchronous operations will fail
|
Chris@16
|
775 * with boost::asio::error::would_block if they are unable to perform the
|
Chris@16
|
776 * requested operation immediately. If @c false, synchronous operations will
|
Chris@16
|
777 * block until complete.
|
Chris@16
|
778 *
|
Chris@16
|
779 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
780 *
|
Chris@16
|
781 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
782 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
783 * boost::asio::error::would_block.
|
Chris@16
|
784 */
|
Chris@16
|
785 boost::system::error_code non_blocking(
|
Chris@16
|
786 bool mode, boost::system::error_code& ec)
|
Chris@16
|
787 {
|
Chris@16
|
788 return this->get_service().non_blocking(
|
Chris@16
|
789 this->get_implementation(), mode, ec);
|
Chris@16
|
790 }
|
Chris@16
|
791
|
Chris@16
|
792 /// Gets the non-blocking mode of the native acceptor implementation.
|
Chris@16
|
793 /**
|
Chris@16
|
794 * This function is used to retrieve the non-blocking mode of the underlying
|
Chris@16
|
795 * native acceptor. This mode has no effect on the behaviour of the acceptor
|
Chris@16
|
796 * object's synchronous operations.
|
Chris@16
|
797 *
|
Chris@16
|
798 * @returns @c true if the underlying acceptor is in non-blocking mode and
|
Chris@16
|
799 * direct system calls may fail with boost::asio::error::would_block (or the
|
Chris@16
|
800 * equivalent system error).
|
Chris@16
|
801 *
|
Chris@16
|
802 * @note The current non-blocking mode is cached by the acceptor object.
|
Chris@16
|
803 * Consequently, the return value may be incorrect if the non-blocking mode
|
Chris@16
|
804 * was set directly on the native acceptor.
|
Chris@16
|
805 */
|
Chris@16
|
806 bool native_non_blocking() const
|
Chris@16
|
807 {
|
Chris@16
|
808 return this->get_service().native_non_blocking(this->get_implementation());
|
Chris@16
|
809 }
|
Chris@16
|
810
|
Chris@16
|
811 /// Sets the non-blocking mode of the native acceptor implementation.
|
Chris@16
|
812 /**
|
Chris@16
|
813 * This function is used to modify the non-blocking mode of the underlying
|
Chris@16
|
814 * native acceptor. It has no effect on the behaviour of the acceptor object's
|
Chris@16
|
815 * synchronous operations.
|
Chris@16
|
816 *
|
Chris@16
|
817 * @param mode If @c true, the underlying acceptor is put into non-blocking
|
Chris@16
|
818 * mode and direct system calls may fail with boost::asio::error::would_block
|
Chris@16
|
819 * (or the equivalent system error).
|
Chris@16
|
820 *
|
Chris@16
|
821 * @throws boost::system::system_error Thrown on failure. If the @c mode is
|
Chris@16
|
822 * @c false, but the current value of @c non_blocking() is @c true, this
|
Chris@16
|
823 * function fails with boost::asio::error::invalid_argument, as the
|
Chris@16
|
824 * combination does not make sense.
|
Chris@16
|
825 */
|
Chris@16
|
826 void native_non_blocking(bool mode)
|
Chris@16
|
827 {
|
Chris@16
|
828 boost::system::error_code ec;
|
Chris@16
|
829 this->get_service().native_non_blocking(
|
Chris@16
|
830 this->get_implementation(), mode, ec);
|
Chris@16
|
831 boost::asio::detail::throw_error(ec, "native_non_blocking");
|
Chris@16
|
832 }
|
Chris@16
|
833
|
Chris@16
|
834 /// Sets the non-blocking mode of the native acceptor implementation.
|
Chris@16
|
835 /**
|
Chris@16
|
836 * This function is used to modify the non-blocking mode of the underlying
|
Chris@16
|
837 * native acceptor. It has no effect on the behaviour of the acceptor object's
|
Chris@16
|
838 * synchronous operations.
|
Chris@16
|
839 *
|
Chris@16
|
840 * @param mode If @c true, the underlying acceptor is put into non-blocking
|
Chris@16
|
841 * mode and direct system calls may fail with boost::asio::error::would_block
|
Chris@16
|
842 * (or the equivalent system error).
|
Chris@16
|
843 *
|
Chris@16
|
844 * @param ec Set to indicate what error occurred, if any. If the @c mode is
|
Chris@16
|
845 * @c false, but the current value of @c non_blocking() is @c true, this
|
Chris@16
|
846 * function fails with boost::asio::error::invalid_argument, as the
|
Chris@16
|
847 * combination does not make sense.
|
Chris@16
|
848 */
|
Chris@16
|
849 boost::system::error_code native_non_blocking(
|
Chris@16
|
850 bool mode, boost::system::error_code& ec)
|
Chris@16
|
851 {
|
Chris@16
|
852 return this->get_service().native_non_blocking(
|
Chris@16
|
853 this->get_implementation(), mode, ec);
|
Chris@16
|
854 }
|
Chris@16
|
855
|
Chris@16
|
856 /// Get the local endpoint of the acceptor.
|
Chris@16
|
857 /**
|
Chris@16
|
858 * This function is used to obtain the locally bound endpoint of the acceptor.
|
Chris@16
|
859 *
|
Chris@16
|
860 * @returns An object that represents the local endpoint of the acceptor.
|
Chris@16
|
861 *
|
Chris@16
|
862 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
863 *
|
Chris@16
|
864 * @par Example
|
Chris@16
|
865 * @code
|
Chris@16
|
866 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
867 * ...
|
Chris@16
|
868 * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
|
Chris@16
|
869 * @endcode
|
Chris@16
|
870 */
|
Chris@16
|
871 endpoint_type local_endpoint() const
|
Chris@16
|
872 {
|
Chris@16
|
873 boost::system::error_code ec;
|
Chris@16
|
874 endpoint_type ep = this->get_service().local_endpoint(
|
Chris@16
|
875 this->get_implementation(), ec);
|
Chris@16
|
876 boost::asio::detail::throw_error(ec, "local_endpoint");
|
Chris@16
|
877 return ep;
|
Chris@16
|
878 }
|
Chris@16
|
879
|
Chris@16
|
880 /// Get the local endpoint of the acceptor.
|
Chris@16
|
881 /**
|
Chris@16
|
882 * This function is used to obtain the locally bound endpoint of the acceptor.
|
Chris@16
|
883 *
|
Chris@16
|
884 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
885 *
|
Chris@16
|
886 * @returns An object that represents the local endpoint of the acceptor.
|
Chris@16
|
887 * Returns a default-constructed endpoint object if an error occurred and the
|
Chris@16
|
888 * error handler did not throw an exception.
|
Chris@16
|
889 *
|
Chris@16
|
890 * @par Example
|
Chris@16
|
891 * @code
|
Chris@16
|
892 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
893 * ...
|
Chris@16
|
894 * boost::system::error_code ec;
|
Chris@16
|
895 * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
|
Chris@16
|
896 * if (ec)
|
Chris@16
|
897 * {
|
Chris@16
|
898 * // An error occurred.
|
Chris@16
|
899 * }
|
Chris@16
|
900 * @endcode
|
Chris@16
|
901 */
|
Chris@16
|
902 endpoint_type local_endpoint(boost::system::error_code& ec) const
|
Chris@16
|
903 {
|
Chris@16
|
904 return this->get_service().local_endpoint(this->get_implementation(), ec);
|
Chris@16
|
905 }
|
Chris@16
|
906
|
Chris@16
|
907 /// Accept a new connection.
|
Chris@16
|
908 /**
|
Chris@16
|
909 * This function is used to accept a new connection from a peer into the
|
Chris@16
|
910 * given socket. The function call will block until a new connection has been
|
Chris@16
|
911 * accepted successfully or an error occurs.
|
Chris@16
|
912 *
|
Chris@16
|
913 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
914 *
|
Chris@16
|
915 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
916 *
|
Chris@16
|
917 * @par Example
|
Chris@16
|
918 * @code
|
Chris@16
|
919 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
920 * ...
|
Chris@16
|
921 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
922 * acceptor.accept(socket);
|
Chris@16
|
923 * @endcode
|
Chris@16
|
924 */
|
Chris@16
|
925 template <typename Protocol1, typename SocketService>
|
Chris@16
|
926 void accept(basic_socket<Protocol1, SocketService>& peer,
|
Chris@16
|
927 typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
|
Chris@16
|
928 {
|
Chris@16
|
929 boost::system::error_code ec;
|
Chris@16
|
930 this->get_service().accept(this->get_implementation(),
|
Chris@16
|
931 peer, static_cast<endpoint_type*>(0), ec);
|
Chris@16
|
932 boost::asio::detail::throw_error(ec, "accept");
|
Chris@16
|
933 }
|
Chris@16
|
934
|
Chris@16
|
935 /// Accept a new connection.
|
Chris@16
|
936 /**
|
Chris@16
|
937 * This function is used to accept a new connection from a peer into the
|
Chris@16
|
938 * given socket. The function call will block until a new connection has been
|
Chris@16
|
939 * accepted successfully or an error occurs.
|
Chris@16
|
940 *
|
Chris@16
|
941 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
942 *
|
Chris@16
|
943 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
944 *
|
Chris@16
|
945 * @par Example
|
Chris@16
|
946 * @code
|
Chris@16
|
947 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
948 * ...
|
Chris@16
|
949 * boost::asio::ip::tcp::soocket socket(io_service);
|
Chris@16
|
950 * boost::system::error_code ec;
|
Chris@16
|
951 * acceptor.accept(socket, ec);
|
Chris@16
|
952 * if (ec)
|
Chris@16
|
953 * {
|
Chris@16
|
954 * // An error occurred.
|
Chris@16
|
955 * }
|
Chris@16
|
956 * @endcode
|
Chris@16
|
957 */
|
Chris@16
|
958 template <typename Protocol1, typename SocketService>
|
Chris@16
|
959 boost::system::error_code accept(
|
Chris@16
|
960 basic_socket<Protocol1, SocketService>& peer,
|
Chris@16
|
961 boost::system::error_code& ec,
|
Chris@16
|
962 typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
|
Chris@16
|
963 {
|
Chris@16
|
964 return this->get_service().accept(this->get_implementation(),
|
Chris@16
|
965 peer, static_cast<endpoint_type*>(0), ec);
|
Chris@16
|
966 }
|
Chris@16
|
967
|
Chris@16
|
968 /// Start an asynchronous accept.
|
Chris@16
|
969 /**
|
Chris@16
|
970 * This function is used to asynchronously accept a new connection into a
|
Chris@16
|
971 * socket. The function call always returns immediately.
|
Chris@16
|
972 *
|
Chris@16
|
973 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
974 * Ownership of the peer object is retained by the caller, which must
|
Chris@16
|
975 * guarantee that it is valid until the handler is called.
|
Chris@16
|
976 *
|
Chris@16
|
977 * @param handler The handler to be called when the accept operation
|
Chris@16
|
978 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
979 * signature of the handler must be:
|
Chris@16
|
980 * @code void handler(
|
Chris@16
|
981 * const boost::system::error_code& error // Result of operation.
|
Chris@16
|
982 * ); @endcode
|
Chris@16
|
983 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
984 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
985 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
986 * boost::asio::io_service::post().
|
Chris@16
|
987 *
|
Chris@16
|
988 * @par Example
|
Chris@16
|
989 * @code
|
Chris@16
|
990 * void accept_handler(const boost::system::error_code& error)
|
Chris@16
|
991 * {
|
Chris@16
|
992 * if (!error)
|
Chris@16
|
993 * {
|
Chris@16
|
994 * // Accept succeeded.
|
Chris@16
|
995 * }
|
Chris@16
|
996 * }
|
Chris@16
|
997 *
|
Chris@16
|
998 * ...
|
Chris@16
|
999 *
|
Chris@16
|
1000 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
1001 * ...
|
Chris@16
|
1002 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1003 * acceptor.async_accept(socket, accept_handler);
|
Chris@16
|
1004 * @endcode
|
Chris@16
|
1005 */
|
Chris@16
|
1006 template <typename Protocol1, typename SocketService, typename AcceptHandler>
|
Chris@16
|
1007 BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
|
Chris@16
|
1008 void (boost::system::error_code))
|
Chris@16
|
1009 async_accept(basic_socket<Protocol1, SocketService>& peer,
|
Chris@16
|
1010 BOOST_ASIO_MOVE_ARG(AcceptHandler) handler,
|
Chris@16
|
1011 typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
|
Chris@16
|
1012 {
|
Chris@16
|
1013 // If you get an error on the following line it means that your handler does
|
Chris@16
|
1014 // not meet the documented type requirements for a AcceptHandler.
|
Chris@16
|
1015 BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
|
Chris@16
|
1016
|
Chris@16
|
1017 return this->get_service().async_accept(this->get_implementation(),
|
Chris@16
|
1018 peer, static_cast<endpoint_type*>(0),
|
Chris@16
|
1019 BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
|
Chris@16
|
1020 }
|
Chris@16
|
1021
|
Chris@16
|
1022 /// Accept a new connection and obtain the endpoint of the peer
|
Chris@16
|
1023 /**
|
Chris@16
|
1024 * This function is used to accept a new connection from a peer into the
|
Chris@16
|
1025 * given socket, and additionally provide the endpoint of the remote peer.
|
Chris@16
|
1026 * The function call will block until a new connection has been accepted
|
Chris@16
|
1027 * successfully or an error occurs.
|
Chris@16
|
1028 *
|
Chris@16
|
1029 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
1030 *
|
Chris@16
|
1031 * @param peer_endpoint An endpoint object which will receive the endpoint of
|
Chris@16
|
1032 * the remote peer.
|
Chris@16
|
1033 *
|
Chris@16
|
1034 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
1035 *
|
Chris@16
|
1036 * @par Example
|
Chris@16
|
1037 * @code
|
Chris@16
|
1038 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
1039 * ...
|
Chris@16
|
1040 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1041 * boost::asio::ip::tcp::endpoint endpoint;
|
Chris@16
|
1042 * acceptor.accept(socket, endpoint);
|
Chris@16
|
1043 * @endcode
|
Chris@16
|
1044 */
|
Chris@16
|
1045 template <typename SocketService>
|
Chris@16
|
1046 void accept(basic_socket<protocol_type, SocketService>& peer,
|
Chris@16
|
1047 endpoint_type& peer_endpoint)
|
Chris@16
|
1048 {
|
Chris@16
|
1049 boost::system::error_code ec;
|
Chris@16
|
1050 this->get_service().accept(this->get_implementation(),
|
Chris@16
|
1051 peer, &peer_endpoint, ec);
|
Chris@16
|
1052 boost::asio::detail::throw_error(ec, "accept");
|
Chris@16
|
1053 }
|
Chris@16
|
1054
|
Chris@16
|
1055 /// Accept a new connection and obtain the endpoint of the peer
|
Chris@16
|
1056 /**
|
Chris@16
|
1057 * This function is used to accept a new connection from a peer into the
|
Chris@16
|
1058 * given socket, and additionally provide the endpoint of the remote peer.
|
Chris@16
|
1059 * The function call will block until a new connection has been accepted
|
Chris@16
|
1060 * successfully or an error occurs.
|
Chris@16
|
1061 *
|
Chris@16
|
1062 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
1063 *
|
Chris@16
|
1064 * @param peer_endpoint An endpoint object which will receive the endpoint of
|
Chris@16
|
1065 * the remote peer.
|
Chris@16
|
1066 *
|
Chris@16
|
1067 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
1068 *
|
Chris@16
|
1069 * @par Example
|
Chris@16
|
1070 * @code
|
Chris@16
|
1071 * boost::asio::ip::tcp::acceptor acceptor(io_service);
|
Chris@16
|
1072 * ...
|
Chris@16
|
1073 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1074 * boost::asio::ip::tcp::endpoint endpoint;
|
Chris@16
|
1075 * boost::system::error_code ec;
|
Chris@16
|
1076 * acceptor.accept(socket, endpoint, ec);
|
Chris@16
|
1077 * if (ec)
|
Chris@16
|
1078 * {
|
Chris@16
|
1079 * // An error occurred.
|
Chris@16
|
1080 * }
|
Chris@16
|
1081 * @endcode
|
Chris@16
|
1082 */
|
Chris@16
|
1083 template <typename SocketService>
|
Chris@16
|
1084 boost::system::error_code accept(
|
Chris@16
|
1085 basic_socket<protocol_type, SocketService>& peer,
|
Chris@16
|
1086 endpoint_type& peer_endpoint, boost::system::error_code& ec)
|
Chris@16
|
1087 {
|
Chris@16
|
1088 return this->get_service().accept(
|
Chris@16
|
1089 this->get_implementation(), peer, &peer_endpoint, ec);
|
Chris@16
|
1090 }
|
Chris@16
|
1091
|
Chris@16
|
1092 /// Start an asynchronous accept.
|
Chris@16
|
1093 /**
|
Chris@16
|
1094 * This function is used to asynchronously accept a new connection into a
|
Chris@16
|
1095 * socket, and additionally obtain the endpoint of the remote peer. The
|
Chris@16
|
1096 * function call always returns immediately.
|
Chris@16
|
1097 *
|
Chris@16
|
1098 * @param peer The socket into which the new connection will be accepted.
|
Chris@16
|
1099 * Ownership of the peer object is retained by the caller, which must
|
Chris@16
|
1100 * guarantee that it is valid until the handler is called.
|
Chris@16
|
1101 *
|
Chris@16
|
1102 * @param peer_endpoint An endpoint object into which the endpoint of the
|
Chris@16
|
1103 * remote peer will be written. Ownership of the peer_endpoint object is
|
Chris@16
|
1104 * retained by the caller, which must guarantee that it is valid until the
|
Chris@16
|
1105 * handler is called.
|
Chris@16
|
1106 *
|
Chris@16
|
1107 * @param handler The handler to be called when the accept operation
|
Chris@16
|
1108 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
1109 * signature of the handler must be:
|
Chris@16
|
1110 * @code void handler(
|
Chris@16
|
1111 * const boost::system::error_code& error // Result of operation.
|
Chris@16
|
1112 * ); @endcode
|
Chris@16
|
1113 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
1114 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
1115 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
1116 * boost::asio::io_service::post().
|
Chris@16
|
1117 */
|
Chris@16
|
1118 template <typename SocketService, typename AcceptHandler>
|
Chris@16
|
1119 BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
|
Chris@16
|
1120 void (boost::system::error_code))
|
Chris@16
|
1121 async_accept(basic_socket<protocol_type, SocketService>& peer,
|
Chris@16
|
1122 endpoint_type& peer_endpoint, BOOST_ASIO_MOVE_ARG(AcceptHandler) handler)
|
Chris@16
|
1123 {
|
Chris@16
|
1124 // If you get an error on the following line it means that your handler does
|
Chris@16
|
1125 // not meet the documented type requirements for a AcceptHandler.
|
Chris@16
|
1126 BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
|
Chris@16
|
1127
|
Chris@16
|
1128 return this->get_service().async_accept(this->get_implementation(), peer,
|
Chris@16
|
1129 &peer_endpoint, BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
|
Chris@16
|
1130 }
|
Chris@16
|
1131 };
|
Chris@16
|
1132
|
Chris@16
|
1133 } // namespace asio
|
Chris@16
|
1134 } // namespace boost
|
Chris@16
|
1135
|
Chris@16
|
1136 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
1137
|
Chris@16
|
1138 #endif // BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_HPP
|