$search
00001 // 00002 // basic_socket.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_HPP 00012 #define ASIO_BASIC_SOCKET_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/detail/push_options.hpp" 00021 #include <boost/config.hpp> 00022 #include "asio/detail/pop_options.hpp" 00023 00024 #include "asio/basic_io_object.hpp" 00025 #include "asio/error.hpp" 00026 #include "asio/socket_base.hpp" 00027 #include "asio/detail/throw_error.hpp" 00028 00029 namespace asio { 00030 00032 00040 template <typename Protocol, typename SocketService> 00041 class basic_socket 00042 : public basic_io_object<SocketService>, 00043 public socket_base 00044 { 00045 public: 00047 typedef typename SocketService::native_type native_type; 00048 00050 typedef Protocol protocol_type; 00051 00053 typedef typename Protocol::endpoint endpoint_type; 00054 00056 typedef basic_socket<Protocol, SocketService> lowest_layer_type; 00057 00059 00065 explicit basic_socket(asio::io_service& io_service) 00066 : basic_io_object<SocketService>(io_service) 00067 { 00068 } 00069 00071 00081 basic_socket(asio::io_service& io_service, 00082 const protocol_type& protocol) 00083 : basic_io_object<SocketService>(io_service) 00084 { 00085 asio::error_code ec; 00086 this->service.open(this->implementation, protocol, ec); 00087 asio::detail::throw_error(ec); 00088 } 00089 00092 00105 basic_socket(asio::io_service& io_service, 00106 const endpoint_type& endpoint) 00107 : basic_io_object<SocketService>(io_service) 00108 { 00109 asio::error_code ec; 00110 this->service.open(this->implementation, endpoint.protocol(), ec); 00111 asio::detail::throw_error(ec); 00112 this->service.bind(this->implementation, endpoint, ec); 00113 asio::detail::throw_error(ec); 00114 } 00115 00117 00129 basic_socket(asio::io_service& io_service, 00130 const protocol_type& protocol, const native_type& native_socket) 00131 : basic_io_object<SocketService>(io_service) 00132 { 00133 asio::error_code ec; 00134 this->service.assign(this->implementation, protocol, native_socket, ec); 00135 asio::detail::throw_error(ec); 00136 } 00137 00139 00147 lowest_layer_type& lowest_layer() 00148 { 00149 return *this; 00150 } 00151 00153 00166 void open(const protocol_type& protocol = protocol_type()) 00167 { 00168 asio::error_code ec; 00169 this->service.open(this->implementation, protocol, ec); 00170 asio::detail::throw_error(ec); 00171 } 00172 00174 00192 asio::error_code open(const protocol_type& protocol, 00193 asio::error_code& ec) 00194 { 00195 return this->service.open(this->implementation, protocol, ec); 00196 } 00197 00199 /* 00200 * This function opens the socket to hold an existing native socket. 00201 * 00202 * @param protocol An object specifying which protocol is to be used. 00203 * 00204 * @param native_socket A native socket. 00205 * 00206 * @throws asio::system_error Thrown on failure. 00207 */ 00208 void assign(const protocol_type& protocol, const native_type& native_socket) 00209 { 00210 asio::error_code ec; 00211 this->service.assign(this->implementation, protocol, native_socket, ec); 00212 asio::detail::throw_error(ec); 00213 } 00214 00216 /* 00217 * This function opens the socket to hold an existing native socket. 00218 * 00219 * @param protocol An object specifying which protocol is to be used. 00220 * 00221 * @param native_socket A native socket. 00222 * 00223 * @param ec Set to indicate what error occurred, if any. 00224 */ 00225 asio::error_code assign(const protocol_type& protocol, 00226 const native_type& native_socket, asio::error_code& ec) 00227 { 00228 return this->service.assign(this->implementation, 00229 protocol, native_socket, ec); 00230 } 00231 00233 bool is_open() const 00234 { 00235 return this->service.is_open(this->implementation); 00236 } 00237 00239 00249 void close() 00250 { 00251 asio::error_code ec; 00252 this->service.close(this->implementation, ec); 00253 asio::detail::throw_error(ec); 00254 } 00255 00257 00279 asio::error_code close(asio::error_code& ec) 00280 { 00281 return this->service.close(this->implementation, ec); 00282 } 00283 00285 00290 native_type native() 00291 { 00292 return this->service.native(this->implementation); 00293 } 00294 00296 00329 #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \ 00330 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \ 00331 && !defined(ASIO_ENABLE_CANCELIO) 00332 __declspec(deprecated("By default, this function always fails with " 00333 "operation_not_supported when used on Windows XP, Windows Server 2003, " 00334 "or earlier. Consult documentation for details.")) 00335 #endif 00336 void cancel() 00337 { 00338 asio::error_code ec; 00339 this->service.cancel(this->implementation, ec); 00340 asio::detail::throw_error(ec); 00341 } 00342 00344 00377 #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \ 00378 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \ 00379 && !defined(ASIO_ENABLE_CANCELIO) 00380 __declspec(deprecated("By default, this function always fails with " 00381 "operation_not_supported when used on Windows XP, Windows Server 2003, " 00382 "or earlier. Consult documentation for details.")) 00383 #endif 00384 asio::error_code cancel(asio::error_code& ec) 00385 { 00386 return this->service.cancel(this->implementation, ec); 00387 } 00388 00390 00399 bool at_mark() const 00400 { 00401 asio::error_code ec; 00402 bool b = this->service.at_mark(this->implementation, ec); 00403 asio::detail::throw_error(ec); 00404 return b; 00405 } 00406 00408 00417 bool at_mark(asio::error_code& ec) const 00418 { 00419 return this->service.at_mark(this->implementation, ec); 00420 } 00421 00423 00432 std::size_t available() const 00433 { 00434 asio::error_code ec; 00435 std::size_t s = this->service.available(this->implementation, ec); 00436 asio::detail::throw_error(ec); 00437 return s; 00438 } 00439 00441 00450 std::size_t available(asio::error_code& ec) const 00451 { 00452 return this->service.available(this->implementation, ec); 00453 } 00454 00456 00473 void bind(const endpoint_type& endpoint) 00474 { 00475 asio::error_code ec; 00476 this->service.bind(this->implementation, endpoint, ec); 00477 asio::detail::throw_error(ec); 00478 } 00479 00481 00503 asio::error_code bind(const endpoint_type& endpoint, 00504 asio::error_code& ec) 00505 { 00506 return this->service.bind(this->implementation, endpoint, ec); 00507 } 00508 00510 00532 void connect(const endpoint_type& peer_endpoint) 00533 { 00534 asio::error_code ec; 00535 if (!is_open()) 00536 { 00537 this->service.open(this->implementation, peer_endpoint.protocol(), ec); 00538 asio::detail::throw_error(ec); 00539 } 00540 this->service.connect(this->implementation, peer_endpoint, ec); 00541 asio::detail::throw_error(ec); 00542 } 00543 00545 00572 asio::error_code connect(const endpoint_type& peer_endpoint, 00573 asio::error_code& ec) 00574 { 00575 if (!is_open()) 00576 { 00577 if (this->service.open(this->implementation, 00578 peer_endpoint.protocol(), ec)) 00579 { 00580 return ec; 00581 } 00582 } 00583 00584 return this->service.connect(this->implementation, peer_endpoint, ec); 00585 } 00586 00588 00628 template <typename ConnectHandler> 00629 void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler) 00630 { 00631 if (!is_open()) 00632 { 00633 asio::error_code ec; 00634 if (this->service.open(this->implementation, 00635 peer_endpoint.protocol(), ec)) 00636 { 00637 this->get_io_service().post( 00638 asio::detail::bind_handler(handler, ec)); 00639 return; 00640 } 00641 } 00642 00643 this->service.async_connect(this->implementation, peer_endpoint, handler); 00644 } 00645 00647 00680 template <typename SettableSocketOption> 00681 void set_option(const SettableSocketOption& option) 00682 { 00683 asio::error_code ec; 00684 this->service.set_option(this->implementation, option, ec); 00685 asio::detail::throw_error(ec); 00686 } 00687 00689 00727 template <typename SettableSocketOption> 00728 asio::error_code set_option(const SettableSocketOption& option, 00729 asio::error_code& ec) 00730 { 00731 return this->service.set_option(this->implementation, option, ec); 00732 } 00733 00735 00769 template <typename GettableSocketOption> 00770 void get_option(GettableSocketOption& option) const 00771 { 00772 asio::error_code ec; 00773 this->service.get_option(this->implementation, option, ec); 00774 asio::detail::throw_error(ec); 00775 } 00776 00778 00817 template <typename GettableSocketOption> 00818 asio::error_code get_option(GettableSocketOption& option, 00819 asio::error_code& ec) const 00820 { 00821 return this->service.get_option(this->implementation, option, ec); 00822 } 00823 00825 00846 template <typename IoControlCommand> 00847 void io_control(IoControlCommand& command) 00848 { 00849 asio::error_code ec; 00850 this->service.io_control(this->implementation, command, ec); 00851 asio::detail::throw_error(ec); 00852 } 00853 00855 00881 template <typename IoControlCommand> 00882 asio::error_code io_control(IoControlCommand& command, 00883 asio::error_code& ec) 00884 { 00885 return this->service.io_control(this->implementation, command, ec); 00886 } 00887 00889 00903 endpoint_type local_endpoint() const 00904 { 00905 asio::error_code ec; 00906 endpoint_type ep = this->service.local_endpoint(this->implementation, ec); 00907 asio::detail::throw_error(ec); 00908 return ep; 00909 } 00910 00912 00932 endpoint_type local_endpoint(asio::error_code& ec) const 00933 { 00934 return this->service.local_endpoint(this->implementation, ec); 00935 } 00936 00938 00952 endpoint_type remote_endpoint() const 00953 { 00954 asio::error_code ec; 00955 endpoint_type ep = this->service.remote_endpoint(this->implementation, ec); 00956 asio::detail::throw_error(ec); 00957 return ep; 00958 } 00959 00961 00981 endpoint_type remote_endpoint(asio::error_code& ec) const 00982 { 00983 return this->service.remote_endpoint(this->implementation, ec); 00984 } 00985 00987 01003 void shutdown(shutdown_type what) 01004 { 01005 asio::error_code ec; 01006 this->service.shutdown(this->implementation, what, ec); 01007 asio::detail::throw_error(ec); 01008 } 01009 01011 01032 asio::error_code shutdown(shutdown_type what, 01033 asio::error_code& ec) 01034 { 01035 return this->service.shutdown(this->implementation, what, ec); 01036 } 01037 01038 protected: 01040 ~basic_socket() 01041 { 01042 } 01043 }; 01044 01045 } // namespace asio 01046 01047 #include "asio/detail/pop_options.hpp" 01048 01049 #endif // ASIO_BASIC_SOCKET_HPP