2
// basic_socket_acceptor.hpp
3
// ~~~~~~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11
#ifndef ASIO_BASIC_SOCKET_ACCEPTOR_HPP
12
#define ASIO_BASIC_SOCKET_ACCEPTOR_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/basic_io_object.hpp"
21
#include "asio/basic_socket.hpp"
22
#include "asio/error.hpp"
23
#include "asio/socket_acceptor_service.hpp"
24
#include "asio/socket_base.hpp"
25
#include "asio/detail/throw_error.hpp"
29
/// Provides the ability to accept new connections.
31
* The basic_socket_acceptor class template is used for accepting new socket
35
* @e Distinct @e objects: Safe.@n
36
* @e Shared @e objects: Unsafe.
39
* Opening a socket acceptor with the SO_REUSEADDR option enabled:
41
* asio::ip::tcp::acceptor acceptor(io_service);
42
* asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
43
* acceptor.open(endpoint.protocol());
44
* acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
45
* acceptor.bind(endpoint);
49
template <typename Protocol,
50
typename SocketAcceptorService = socket_acceptor_service<Protocol> >
51
class basic_socket_acceptor
52
: public basic_io_object<SocketAcceptorService>,
56
/// The native representation of an acceptor.
57
typedef typename SocketAcceptorService::native_type native_type;
59
/// The protocol type.
60
typedef Protocol protocol_type;
62
/// The endpoint type.
63
typedef typename Protocol::endpoint endpoint_type;
65
/// Construct an acceptor without opening it.
67
* This constructor creates an acceptor without opening it to listen for new
68
* connections. The open() function must be called before the acceptor can
69
* accept new socket connections.
71
* @param io_service The io_service object that the acceptor will use to
72
* dispatch handlers for any asynchronous operations performed on the
75
explicit basic_socket_acceptor(asio::io_service& io_service)
76
: basic_io_object<SocketAcceptorService>(io_service)
80
/// Construct an open acceptor.
82
* This constructor creates an acceptor and automatically opens it.
84
* @param io_service The io_service object that the acceptor will use to
85
* dispatch handlers for any asynchronous operations performed on the
88
* @param protocol An object specifying protocol parameters to be used.
90
* @throws asio::system_error Thrown on failure.
92
basic_socket_acceptor(asio::io_service& io_service,
93
const protocol_type& protocol)
94
: basic_io_object<SocketAcceptorService>(io_service)
97
this->service.open(this->implementation, protocol, ec);
98
asio::detail::throw_error(ec);
101
/// Construct an acceptor opened on the given endpoint.
103
* This constructor creates an acceptor and automatically opens it to listen
104
* for new connections on the specified endpoint.
106
* @param io_service The io_service object that the acceptor will use to
107
* dispatch handlers for any asynchronous operations performed on the
110
* @param endpoint An endpoint on the local machine on which the acceptor
111
* will listen for new connections.
113
* @param reuse_addr Whether the constructor should set the socket option
114
* socket_base::reuse_address.
116
* @throws asio::system_error Thrown on failure.
118
* @note This constructor is equivalent to the following code:
120
* basic_socket_acceptor<Protocol> acceptor(io_service);
121
* acceptor.open(endpoint.protocol());
123
* acceptor.set_option(socket_base::reuse_address(true));
124
* acceptor.bind(endpoint);
125
* acceptor.listen(listen_backlog);
128
basic_socket_acceptor(asio::io_service& io_service,
129
const endpoint_type& endpoint, bool reuse_addr = true)
130
: basic_io_object<SocketAcceptorService>(io_service)
133
this->service.open(this->implementation, endpoint.protocol(), ec);
134
asio::detail::throw_error(ec);
137
this->service.set_option(this->implementation,
138
socket_base::reuse_address(true), ec);
139
asio::detail::throw_error(ec);
141
this->service.bind(this->implementation, endpoint, ec);
142
asio::detail::throw_error(ec);
143
this->service.listen(this->implementation,
144
socket_base::max_connections, ec);
145
asio::detail::throw_error(ec);
148
/// Construct a basic_socket_acceptor on an existing native acceptor.
150
* This constructor creates an acceptor object to hold an existing native
153
* @param io_service The io_service object that the acceptor will use to
154
* dispatch handlers for any asynchronous operations performed on the
157
* @param protocol An object specifying protocol parameters to be used.
159
* @param native_acceptor A native acceptor.
161
* @throws asio::system_error Thrown on failure.
163
basic_socket_acceptor(asio::io_service& io_service,
164
const protocol_type& protocol, const native_type& native_acceptor)
165
: basic_io_object<SocketAcceptorService>(io_service)
168
this->service.assign(this->implementation, protocol, native_acceptor, ec);
169
asio::detail::throw_error(ec);
172
/// Open the acceptor using the specified protocol.
174
* This function opens the socket acceptor so that it will use the specified
177
* @param protocol An object specifying which protocol is to be used.
179
* @throws asio::system_error Thrown on failure.
183
* asio::ip::tcp::acceptor acceptor(io_service);
184
* acceptor.open(asio::ip::tcp::v4());
187
void open(const protocol_type& protocol = protocol_type())
190
this->service.open(this->implementation, protocol, ec);
191
asio::detail::throw_error(ec);
194
/// Open the acceptor using the specified protocol.
196
* This function opens the socket acceptor so that it will use the specified
199
* @param protocol An object specifying which protocol is to be used.
201
* @param ec Set to indicate what error occurred, if any.
205
* asio::ip::tcp::acceptor acceptor(io_service);
206
* asio::error_code ec;
207
* acceptor.open(asio::ip::tcp::v4(), ec);
210
* // An error occurred.
214
asio::error_code open(const protocol_type& protocol,
215
asio::error_code& ec)
217
return this->service.open(this->implementation, protocol, ec);
220
/// Assigns an existing native acceptor to the acceptor.
222
* This function opens the acceptor to hold an existing native acceptor.
224
* @param protocol An object specifying which protocol is to be used.
226
* @param native_acceptor A native acceptor.
228
* @throws asio::system_error Thrown on failure.
230
void assign(const protocol_type& protocol, const native_type& native_acceptor)
233
this->service.assign(this->implementation, protocol, native_acceptor, ec);
234
asio::detail::throw_error(ec);
237
/// Assigns an existing native acceptor to the acceptor.
239
* This function opens the acceptor to hold an existing native acceptor.
241
* @param protocol An object specifying which protocol is to be used.
243
* @param native_acceptor A native acceptor.
245
* @param ec Set to indicate what error occurred, if any.
247
asio::error_code assign(const protocol_type& protocol,
248
const native_type& native_acceptor, asio::error_code& ec)
250
return this->service.assign(this->implementation,
251
protocol, native_acceptor, ec);
254
/// Determine whether the acceptor is open.
257
return this->service.is_open(this->implementation);
260
/// Bind the acceptor to the given local endpoint.
262
* This function binds the socket acceptor to the specified endpoint on the
265
* @param endpoint An endpoint on the local machine to which the socket
266
* acceptor will be bound.
268
* @throws asio::system_error Thrown on failure.
272
* asio::ip::tcp::acceptor acceptor(io_service);
273
* acceptor.open(asio::ip::tcp::v4());
274
* acceptor.bind(asio::ip::tcp::endpoint(12345));
277
void bind(const endpoint_type& endpoint)
280
this->service.bind(this->implementation, endpoint, ec);
281
asio::detail::throw_error(ec);
284
/// Bind the acceptor to the given local endpoint.
286
* This function binds the socket acceptor to the specified endpoint on the
289
* @param endpoint An endpoint on the local machine to which the socket
290
* acceptor will be bound.
292
* @param ec Set to indicate what error occurred, if any.
296
* asio::ip::tcp::acceptor acceptor(io_service);
297
* acceptor.open(asio::ip::tcp::v4());
298
* asio::error_code ec;
299
* acceptor.bind(asio::ip::tcp::endpoint(12345), ec);
302
* // An error occurred.
306
asio::error_code bind(const endpoint_type& endpoint,
307
asio::error_code& ec)
309
return this->service.bind(this->implementation, endpoint, ec);
312
/// Place the acceptor into the state where it will listen for new
315
* This function puts the socket acceptor into the state where it may accept
318
* @param backlog The maximum length of the queue of pending connections.
320
* @throws asio::system_error Thrown on failure.
322
void listen(int backlog = socket_base::max_connections)
325
this->service.listen(this->implementation, backlog, ec);
326
asio::detail::throw_error(ec);
329
/// Place the acceptor into the state where it will listen for new
332
* This function puts the socket acceptor into the state where it may accept
335
* @param backlog The maximum length of the queue of pending connections.
337
* @param ec Set to indicate what error occurred, if any.
341
* asio::ip::tcp::acceptor acceptor(io_service);
343
* asio::error_code ec;
344
* acceptor.listen(asio::socket_base::max_connections, ec);
347
* // An error occurred.
351
asio::error_code listen(int backlog, asio::error_code& ec)
353
return this->service.listen(this->implementation, backlog, ec);
356
/// Close the acceptor.
358
* This function is used to close the acceptor. Any asynchronous accept
359
* operations will be cancelled immediately.
361
* A subsequent call to open() is required before the acceptor can again be
362
* used to again perform socket accept operations.
364
* @throws asio::system_error Thrown on failure.
369
this->service.close(this->implementation, ec);
370
asio::detail::throw_error(ec);
373
/// Close the acceptor.
375
* This function is used to close the acceptor. Any asynchronous accept
376
* operations will be cancelled immediately.
378
* A subsequent call to open() is required before the acceptor can again be
379
* used to again perform socket accept operations.
381
* @param ec Set to indicate what error occurred, if any.
385
* asio::ip::tcp::acceptor acceptor(io_service);
387
* asio::error_code ec;
388
* acceptor.close(ec);
391
* // An error occurred.
395
asio::error_code close(asio::error_code& ec)
397
return this->service.close(this->implementation, ec);
400
/// Get the native acceptor representation.
402
* This function may be used to obtain the underlying representation of the
403
* acceptor. This is intended to allow access to native acceptor functionality
404
* that is not otherwise provided.
408
return this->service.native(this->implementation);
411
/// Cancel all asynchronous operations associated with the acceptor.
413
* This function causes all outstanding asynchronous connect, send and receive
414
* operations to finish immediately, and the handlers for cancelled operations
415
* will be passed the asio::error::operation_aborted error.
417
* @throws asio::system_error Thrown on failure.
422
this->service.cancel(this->implementation, ec);
423
asio::detail::throw_error(ec);
426
/// Cancel all asynchronous operations associated with the acceptor.
428
* This function causes all outstanding asynchronous connect, send and receive
429
* operations to finish immediately, and the handlers for cancelled operations
430
* will be passed the asio::error::operation_aborted error.
432
* @param ec Set to indicate what error occurred, if any.
434
asio::error_code cancel(asio::error_code& ec)
436
return this->service.cancel(this->implementation, ec);
439
/// Set an option on the acceptor.
441
* This function is used to set an option on the acceptor.
443
* @param option The new option value to be set on the acceptor.
445
* @throws asio::system_error Thrown on failure.
447
* @sa SettableSocketOption @n
448
* asio::socket_base::reuse_address
449
* asio::socket_base::enable_connection_aborted
452
* Setting the SOL_SOCKET/SO_REUSEADDR option:
454
* asio::ip::tcp::acceptor acceptor(io_service);
456
* asio::ip::tcp::acceptor::reuse_address option(true);
457
* acceptor.set_option(option);
460
template <typename SettableSocketOption>
461
void set_option(const SettableSocketOption& option)
464
this->service.set_option(this->implementation, option, ec);
465
asio::detail::throw_error(ec);
468
/// Set an option on the acceptor.
470
* This function is used to set an option on the acceptor.
472
* @param option The new option value to be set on the acceptor.
474
* @param ec Set to indicate what error occurred, if any.
476
* @sa SettableSocketOption @n
477
* asio::socket_base::reuse_address
478
* asio::socket_base::enable_connection_aborted
481
* Setting the SOL_SOCKET/SO_REUSEADDR option:
483
* asio::ip::tcp::acceptor acceptor(io_service);
485
* asio::ip::tcp::acceptor::reuse_address option(true);
486
* asio::error_code ec;
487
* acceptor.set_option(option, ec);
490
* // An error occurred.
494
template <typename SettableSocketOption>
495
asio::error_code set_option(const SettableSocketOption& option,
496
asio::error_code& ec)
498
return this->service.set_option(this->implementation, option, ec);
501
/// Get an option from the acceptor.
503
* This function is used to get the current value of an option on the
506
* @param option The option value to be obtained from the acceptor.
508
* @throws asio::system_error Thrown on failure.
510
* @sa GettableSocketOption @n
511
* asio::socket_base::reuse_address
514
* Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
516
* asio::ip::tcp::acceptor acceptor(io_service);
518
* asio::ip::tcp::acceptor::reuse_address option;
519
* acceptor.get_option(option);
520
* bool is_set = option.get();
523
template <typename GettableSocketOption>
524
void get_option(GettableSocketOption& option)
527
this->service.get_option(this->implementation, option, ec);
528
asio::detail::throw_error(ec);
531
/// Get an option from the acceptor.
533
* This function is used to get the current value of an option on the
536
* @param option The option value to be obtained from the acceptor.
538
* @param ec Set to indicate what error occurred, if any.
540
* @sa GettableSocketOption @n
541
* asio::socket_base::reuse_address
544
* Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
546
* asio::ip::tcp::acceptor acceptor(io_service);
548
* asio::ip::tcp::acceptor::reuse_address option;
549
* asio::error_code ec;
550
* acceptor.get_option(option, ec);
553
* // An error occurred.
555
* bool is_set = option.get();
558
template <typename GettableSocketOption>
559
asio::error_code get_option(GettableSocketOption& option,
560
asio::error_code& ec)
562
return this->service.get_option(this->implementation, option, ec);
565
/// Get the local endpoint of the acceptor.
567
* This function is used to obtain the locally bound endpoint of the acceptor.
569
* @returns An object that represents the local endpoint of the acceptor.
571
* @throws asio::system_error Thrown on failure.
575
* asio::ip::tcp::acceptor acceptor(io_service);
577
* asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
580
endpoint_type local_endpoint() const
583
endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
584
asio::detail::throw_error(ec);
588
/// Get the local endpoint of the acceptor.
590
* This function is used to obtain the locally bound endpoint of the acceptor.
592
* @param ec Set to indicate what error occurred, if any.
594
* @returns An object that represents the local endpoint of the acceptor.
595
* Returns a default-constructed endpoint object if an error occurred and the
596
* error handler did not throw an exception.
600
* asio::ip::tcp::acceptor acceptor(io_service);
602
* asio::error_code ec;
603
* asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
606
* // An error occurred.
610
endpoint_type local_endpoint(asio::error_code& ec) const
612
return this->service.local_endpoint(this->implementation, ec);
615
/// Accept a new connection.
617
* This function is used to accept a new connection from a peer into the
618
* given socket. The function call will block until a new connection has been
619
* accepted successfully or an error occurs.
621
* @param peer The socket into which the new connection will be accepted.
623
* @throws asio::system_error Thrown on failure.
627
* asio::ip::tcp::acceptor acceptor(io_service);
629
* asio::ip::tcp::socket socket(io_service);
630
* acceptor.accept(socket);
633
template <typename SocketService>
634
void accept(basic_socket<protocol_type, SocketService>& peer)
637
this->service.accept(this->implementation, peer, 0, ec);
638
asio::detail::throw_error(ec);
641
/// Accept a new connection.
643
* This function is used to accept a new connection from a peer into the
644
* given socket. The function call will block until a new connection has been
645
* accepted successfully or an error occurs.
647
* @param peer The socket into which the new connection will be accepted.
649
* @param ec Set to indicate what error occurred, if any.
653
* asio::ip::tcp::acceptor acceptor(io_service);
655
* asio::ip::tcp::soocket socket(io_service);
656
* asio::error_code ec;
657
* acceptor.accept(socket, ec);
660
* // An error occurred.
664
template <typename SocketService>
665
asio::error_code accept(
666
basic_socket<protocol_type, SocketService>& peer,
667
asio::error_code& ec)
669
return this->service.accept(this->implementation, peer, 0, ec);
672
/// Start an asynchronous accept.
674
* This function is used to asynchronously accept a new connection into a
675
* socket. The function call always returns immediately.
677
* @param peer The socket into which the new connection will be accepted.
678
* Ownership of the peer object is retained by the caller, which must
679
* guarantee that it is valid until the handler is called.
681
* @param handler The handler to be called when the accept operation
682
* completes. Copies will be made of the handler as required. The function
683
* signature of the handler must be:
684
* @code void handler(
685
* const asio::error_code& error // Result of operation.
687
* Regardless of whether the asynchronous operation completes immediately or
688
* not, the handler will not be invoked from within this function. Invocation
689
* of the handler will be performed in a manner equivalent to using
690
* asio::io_service::post().
694
* void accept_handler(const asio::error_code& error)
698
* // Accept succeeded.
704
* asio::ip::tcp::acceptor acceptor(io_service);
706
* asio::ip::tcp::socket socket(io_service);
707
* acceptor.async_accept(socket, accept_handler);
710
template <typename SocketService, typename AcceptHandler>
711
void async_accept(basic_socket<protocol_type, SocketService>& peer,
712
AcceptHandler handler)
714
this->service.async_accept(this->implementation, peer, 0, handler);
717
/// Accept a new connection and obtain the endpoint of the peer
719
* This function is used to accept a new connection from a peer into the
720
* given socket, and additionally provide the endpoint of the remote peer.
721
* The function call will block until a new connection has been accepted
722
* successfully or an error occurs.
724
* @param peer The socket into which the new connection will be accepted.
726
* @param peer_endpoint An endpoint object which will receive the endpoint of
729
* @throws asio::system_error Thrown on failure.
733
* asio::ip::tcp::acceptor acceptor(io_service);
735
* asio::ip::tcp::socket socket(io_service);
736
* asio::ip::tcp::endpoint endpoint;
737
* acceptor.accept(socket, endpoint);
740
template <typename SocketService>
741
void accept(basic_socket<protocol_type, SocketService>& peer,
742
endpoint_type& peer_endpoint)
745
this->service.accept(this->implementation, peer, &peer_endpoint, ec);
746
asio::detail::throw_error(ec);
749
/// Accept a new connection and obtain the endpoint of the peer
751
* This function is used to accept a new connection from a peer into the
752
* given socket, and additionally provide the endpoint of the remote peer.
753
* The function call will block until a new connection has been accepted
754
* successfully or an error occurs.
756
* @param peer The socket into which the new connection will be accepted.
758
* @param peer_endpoint An endpoint object which will receive the endpoint of
761
* @param ec Set to indicate what error occurred, if any.
765
* asio::ip::tcp::acceptor acceptor(io_service);
767
* asio::ip::tcp::socket socket(io_service);
768
* asio::ip::tcp::endpoint endpoint;
769
* asio::error_code ec;
770
* acceptor.accept(socket, endpoint, ec);
773
* // An error occurred.
777
template <typename SocketService>
778
asio::error_code accept(
779
basic_socket<protocol_type, SocketService>& peer,
780
endpoint_type& peer_endpoint, asio::error_code& ec)
782
return this->service.accept(this->implementation, peer, &peer_endpoint, ec);
785
/// Start an asynchronous accept.
787
* This function is used to asynchronously accept a new connection into a
788
* socket, and additionally obtain the endpoint of the remote peer. The
789
* function call always returns immediately.
791
* @param peer The socket into which the new connection will be accepted.
792
* Ownership of the peer object is retained by the caller, which must
793
* guarantee that it is valid until the handler is called.
795
* @param peer_endpoint An endpoint object into which the endpoint of the
796
* remote peer will be written. Ownership of the peer_endpoint object is
797
* retained by the caller, which must guarantee that it is valid until the
800
* @param handler The handler to be called when the accept operation
801
* completes. Copies will be made of the handler as required. The function
802
* signature of the handler must be:
803
* @code void handler(
804
* const asio::error_code& error // Result of operation.
806
* Regardless of whether the asynchronous operation completes immediately or
807
* not, the handler will not be invoked from within this function. Invocation
808
* of the handler will be performed in a manner equivalent to using
809
* asio::io_service::post().
811
template <typename SocketService, typename AcceptHandler>
812
void async_accept(basic_socket<protocol_type, SocketService>& peer,
813
endpoint_type& peer_endpoint, AcceptHandler handler)
815
this->service.async_accept(this->implementation,
816
peer, &peer_endpoint, handler);
822
#include "asio/detail/pop_options.hpp"
824
#endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP