$search
00001 // 00002 // basic_socket_acceptor.hpp 00003 // ~~~~~~~~~~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_BASIC_SOCKET_ACCEPTOR_HPP 00012 #define ASIO_BASIC_SOCKET_ACCEPTOR_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/basic_io_object.hpp" 00021 #include "asio/basic_socket.hpp" 00022 #include "asio/error.hpp" 00023 #include "asio/socket_acceptor_service.hpp" 00024 #include "asio/socket_base.hpp" 00025 #include "asio/detail/throw_error.hpp" 00026 00027 namespace asio { 00028 00030 00049 template <typename Protocol, 00050 typename SocketAcceptorService = socket_acceptor_service<Protocol> > 00051 class basic_socket_acceptor 00052 : public basic_io_object<SocketAcceptorService>, 00053 public socket_base 00054 { 00055 public: 00057 typedef typename SocketAcceptorService::native_type native_type; 00058 00060 typedef Protocol protocol_type; 00061 00063 typedef typename Protocol::endpoint endpoint_type; 00064 00066 00075 explicit basic_socket_acceptor(asio::io_service& io_service) 00076 : basic_io_object<SocketAcceptorService>(io_service) 00077 { 00078 } 00079 00081 00092 basic_socket_acceptor(asio::io_service& io_service, 00093 const protocol_type& protocol) 00094 : basic_io_object<SocketAcceptorService>(io_service) 00095 { 00096 asio::error_code ec; 00097 this->service.open(this->implementation, protocol, ec); 00098 asio::detail::throw_error(ec); 00099 } 00100 00102 00128 basic_socket_acceptor(asio::io_service& io_service, 00129 const endpoint_type& endpoint, bool reuse_addr = true) 00130 : basic_io_object<SocketAcceptorService>(io_service) 00131 { 00132 asio::error_code ec; 00133 this->service.open(this->implementation, endpoint.protocol(), ec); 00134 asio::detail::throw_error(ec); 00135 if (reuse_addr) 00136 { 00137 this->service.set_option(this->implementation, 00138 socket_base::reuse_address(true), ec); 00139 asio::detail::throw_error(ec); 00140 } 00141 this->service.bind(this->implementation, endpoint, ec); 00142 asio::detail::throw_error(ec); 00143 this->service.listen(this->implementation, 00144 socket_base::max_connections, ec); 00145 asio::detail::throw_error(ec); 00146 } 00147 00149 00163 basic_socket_acceptor(asio::io_service& io_service, 00164 const protocol_type& protocol, const native_type& native_acceptor) 00165 : basic_io_object<SocketAcceptorService>(io_service) 00166 { 00167 asio::error_code ec; 00168 this->service.assign(this->implementation, protocol, native_acceptor, ec); 00169 asio::detail::throw_error(ec); 00170 } 00171 00173 00187 void open(const protocol_type& protocol = protocol_type()) 00188 { 00189 asio::error_code ec; 00190 this->service.open(this->implementation, protocol, ec); 00191 asio::detail::throw_error(ec); 00192 } 00193 00195 00214 asio::error_code open(const protocol_type& protocol, 00215 asio::error_code& ec) 00216 { 00217 return this->service.open(this->implementation, protocol, ec); 00218 } 00219 00221 /* 00222 * This function opens the acceptor to hold an existing native acceptor. 00223 * 00224 * @param protocol An object specifying which protocol is to be used. 00225 * 00226 * @param native_acceptor A native acceptor. 00227 * 00228 * @throws asio::system_error Thrown on failure. 00229 */ 00230 void assign(const protocol_type& protocol, const native_type& native_acceptor) 00231 { 00232 asio::error_code ec; 00233 this->service.assign(this->implementation, protocol, native_acceptor, ec); 00234 asio::detail::throw_error(ec); 00235 } 00236 00238 /* 00239 * This function opens the acceptor to hold an existing native acceptor. 00240 * 00241 * @param protocol An object specifying which protocol is to be used. 00242 * 00243 * @param native_acceptor A native acceptor. 00244 * 00245 * @param ec Set to indicate what error occurred, if any. 00246 */ 00247 asio::error_code assign(const protocol_type& protocol, 00248 const native_type& native_acceptor, asio::error_code& ec) 00249 { 00250 return this->service.assign(this->implementation, 00251 protocol, native_acceptor, ec); 00252 } 00253 00255 bool is_open() const 00256 { 00257 return this->service.is_open(this->implementation); 00258 } 00259 00261 00277 void bind(const endpoint_type& endpoint) 00278 { 00279 asio::error_code ec; 00280 this->service.bind(this->implementation, endpoint, ec); 00281 asio::detail::throw_error(ec); 00282 } 00283 00285 00306 asio::error_code bind(const endpoint_type& endpoint, 00307 asio::error_code& ec) 00308 { 00309 return this->service.bind(this->implementation, endpoint, ec); 00310 } 00311 00314 00322 void listen(int backlog = socket_base::max_connections) 00323 { 00324 asio::error_code ec; 00325 this->service.listen(this->implementation, backlog, ec); 00326 asio::detail::throw_error(ec); 00327 } 00328 00331 00351 asio::error_code listen(int backlog, asio::error_code& ec) 00352 { 00353 return this->service.listen(this->implementation, backlog, ec); 00354 } 00355 00357 00366 void close() 00367 { 00368 asio::error_code ec; 00369 this->service.close(this->implementation, ec); 00370 asio::detail::throw_error(ec); 00371 } 00372 00374 00395 asio::error_code close(asio::error_code& ec) 00396 { 00397 return this->service.close(this->implementation, ec); 00398 } 00399 00401 00406 native_type native() 00407 { 00408 return this->service.native(this->implementation); 00409 } 00410 00412 00419 void cancel() 00420 { 00421 asio::error_code ec; 00422 this->service.cancel(this->implementation, ec); 00423 asio::detail::throw_error(ec); 00424 } 00425 00427 00434 asio::error_code cancel(asio::error_code& ec) 00435 { 00436 return this->service.cancel(this->implementation, ec); 00437 } 00438 00440 00460 template <typename SettableSocketOption> 00461 void set_option(const SettableSocketOption& option) 00462 { 00463 asio::error_code ec; 00464 this->service.set_option(this->implementation, option, ec); 00465 asio::detail::throw_error(ec); 00466 } 00467 00469 00494 template <typename SettableSocketOption> 00495 asio::error_code set_option(const SettableSocketOption& option, 00496 asio::error_code& ec) 00497 { 00498 return this->service.set_option(this->implementation, option, ec); 00499 } 00500 00502 00523 template <typename GettableSocketOption> 00524 void get_option(GettableSocketOption& option) 00525 { 00526 asio::error_code ec; 00527 this->service.get_option(this->implementation, option, ec); 00528 asio::detail::throw_error(ec); 00529 } 00530 00532 00558 template <typename GettableSocketOption> 00559 asio::error_code get_option(GettableSocketOption& option, 00560 asio::error_code& ec) 00561 { 00562 return this->service.get_option(this->implementation, option, ec); 00563 } 00564 00566 00580 endpoint_type local_endpoint() const 00581 { 00582 asio::error_code ec; 00583 endpoint_type ep = this->service.local_endpoint(this->implementation, ec); 00584 asio::detail::throw_error(ec); 00585 return ep; 00586 } 00587 00589 00610 endpoint_type local_endpoint(asio::error_code& ec) const 00611 { 00612 return this->service.local_endpoint(this->implementation, ec); 00613 } 00614 00616 00633 template <typename SocketService> 00634 void accept(basic_socket<protocol_type, SocketService>& peer) 00635 { 00636 asio::error_code ec; 00637 this->service.accept(this->implementation, peer, 0, ec); 00638 asio::detail::throw_error(ec); 00639 } 00640 00642 00664 template <typename SocketService> 00665 asio::error_code accept( 00666 basic_socket<protocol_type, SocketService>& peer, 00667 asio::error_code& ec) 00668 { 00669 return this->service.accept(this->implementation, peer, 0, ec); 00670 } 00671 00673 00710 template <typename SocketService, typename AcceptHandler> 00711 void async_accept(basic_socket<protocol_type, SocketService>& peer, 00712 AcceptHandler handler) 00713 { 00714 this->service.async_accept(this->implementation, peer, 0, handler); 00715 } 00716 00718 00740 template <typename SocketService> 00741 void accept(basic_socket<protocol_type, SocketService>& peer, 00742 endpoint_type& peer_endpoint) 00743 { 00744 asio::error_code ec; 00745 this->service.accept(this->implementation, peer, &peer_endpoint, ec); 00746 asio::detail::throw_error(ec); 00747 } 00748 00750 00777 template <typename SocketService> 00778 asio::error_code accept( 00779 basic_socket<protocol_type, SocketService>& peer, 00780 endpoint_type& peer_endpoint, asio::error_code& ec) 00781 { 00782 return this->service.accept(this->implementation, peer, &peer_endpoint, ec); 00783 } 00784 00786 00811 template <typename SocketService, typename AcceptHandler> 00812 void async_accept(basic_socket<protocol_type, SocketService>& peer, 00813 endpoint_type& peer_endpoint, AcceptHandler handler) 00814 { 00815 this->service.async_accept(this->implementation, 00816 peer, &peer_endpoint, handler); 00817 } 00818 }; 00819 00820 } // namespace asio 00821 00822 #include "asio/detail/pop_options.hpp" 00823 00824 #endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP