00001
00002
00003
00004
00005
00006
00007
00008
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/basic_io_object.hpp"
00021 #include "asio/error.hpp"
00022 #include "asio/socket_base.hpp"
00023 #include "asio/detail/throw_error.hpp"
00024
00025 namespace asio {
00026
00028
00036 template <typename Protocol, typename SocketService>
00037 class basic_socket
00038 : public basic_io_object<SocketService>,
00039 public socket_base
00040 {
00041 public:
00043 typedef typename SocketService::native_type native_type;
00044
00046 typedef Protocol protocol_type;
00047
00049 typedef typename Protocol::endpoint endpoint_type;
00050
00052 typedef basic_socket<Protocol, SocketService> lowest_layer_type;
00053
00055
00061 explicit basic_socket(asio::io_service& io_service)
00062 : basic_io_object<SocketService>(io_service)
00063 {
00064 }
00065
00067
00077 basic_socket(asio::io_service& io_service,
00078 const protocol_type& protocol)
00079 : basic_io_object<SocketService>(io_service)
00080 {
00081 asio::error_code ec;
00082 this->service.open(this->implementation, protocol, ec);
00083 asio::detail::throw_error(ec);
00084 }
00085
00088
00101 basic_socket(asio::io_service& io_service,
00102 const endpoint_type& endpoint)
00103 : basic_io_object<SocketService>(io_service)
00104 {
00105 asio::error_code ec;
00106 this->service.open(this->implementation, endpoint.protocol(), ec);
00107 asio::detail::throw_error(ec);
00108 this->service.bind(this->implementation, endpoint, ec);
00109 asio::detail::throw_error(ec);
00110 }
00111
00113
00125 basic_socket(asio::io_service& io_service,
00126 const protocol_type& protocol, const native_type& native_socket)
00127 : basic_io_object<SocketService>(io_service)
00128 {
00129 asio::error_code ec;
00130 this->service.assign(this->implementation, protocol, native_socket, ec);
00131 asio::detail::throw_error(ec);
00132 }
00133
00135
00143 lowest_layer_type& lowest_layer()
00144 {
00145 return *this;
00146 }
00147
00149
00162 void open(const protocol_type& protocol = protocol_type())
00163 {
00164 asio::error_code ec;
00165 this->service.open(this->implementation, protocol, ec);
00166 asio::detail::throw_error(ec);
00167 }
00168
00170
00188 asio::error_code open(const protocol_type& protocol,
00189 asio::error_code& ec)
00190 {
00191 return this->service.open(this->implementation, protocol, ec);
00192 }
00193
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 void assign(const protocol_type& protocol, const native_type& native_socket)
00205 {
00206 asio::error_code ec;
00207 this->service.assign(this->implementation, protocol, native_socket, ec);
00208 asio::detail::throw_error(ec);
00209 }
00210
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 asio::error_code assign(const protocol_type& protocol,
00222 const native_type& native_socket, asio::error_code& ec)
00223 {
00224 return this->service.assign(this->implementation,
00225 protocol, native_socket, ec);
00226 }
00227
00229 bool is_open() const
00230 {
00231 return this->service.is_open(this->implementation);
00232 }
00233
00235
00242 void close()
00243 {
00244 asio::error_code ec;
00245 this->service.close(this->implementation, ec);
00246 asio::detail::throw_error(ec);
00247 }
00248
00250
00269 asio::error_code close(asio::error_code& ec)
00270 {
00271 return this->service.close(this->implementation, ec);
00272 }
00273
00275
00280 native_type native()
00281 {
00282 return this->service.native(this->implementation);
00283 }
00284
00286
00293 void cancel()
00294 {
00295 asio::error_code ec;
00296 this->service.cancel(this->implementation, ec);
00297 asio::detail::throw_error(ec);
00298 }
00299
00301
00308 asio::error_code cancel(asio::error_code& ec)
00309 {
00310 return this->service.cancel(this->implementation, ec);
00311 }
00312
00314
00323 bool at_mark() const
00324 {
00325 asio::error_code ec;
00326 bool b = this->service.at_mark(this->implementation, ec);
00327 asio::detail::throw_error(ec);
00328 return b;
00329 }
00330
00332
00341 bool at_mark(asio::error_code& ec) const
00342 {
00343 return this->service.at_mark(this->implementation, ec);
00344 }
00345
00347
00356 std::size_t available() const
00357 {
00358 asio::error_code ec;
00359 std::size_t s = this->service.available(this->implementation, ec);
00360 asio::detail::throw_error(ec);
00361 return s;
00362 }
00363
00365
00374 std::size_t available(asio::error_code& ec) const
00375 {
00376 return this->service.available(this->implementation, ec);
00377 }
00378
00380
00397 void bind(const endpoint_type& endpoint)
00398 {
00399 asio::error_code ec;
00400 this->service.bind(this->implementation, endpoint, ec);
00401 asio::detail::throw_error(ec);
00402 }
00403
00405
00427 asio::error_code bind(const endpoint_type& endpoint,
00428 asio::error_code& ec)
00429 {
00430 return this->service.bind(this->implementation, endpoint, ec);
00431 }
00432
00434
00456 void connect(const endpoint_type& peer_endpoint)
00457 {
00458 asio::error_code ec;
00459 if (!is_open())
00460 {
00461 this->service.open(this->implementation, peer_endpoint.protocol(), ec);
00462 asio::detail::throw_error(ec);
00463 }
00464 this->service.connect(this->implementation, peer_endpoint, ec);
00465 asio::detail::throw_error(ec);
00466 }
00467
00469
00496 asio::error_code connect(const endpoint_type& peer_endpoint,
00497 asio::error_code& ec)
00498 {
00499 if (!is_open())
00500 {
00501 if (this->service.open(this->implementation,
00502 peer_endpoint.protocol(), ec))
00503 {
00504 return ec;
00505 }
00506 }
00507
00508 return this->service.connect(this->implementation, peer_endpoint, ec);
00509 }
00510
00512
00552 template <typename ConnectHandler>
00553 void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
00554 {
00555 if (!is_open())
00556 {
00557 asio::error_code ec;
00558 if (this->service.open(this->implementation,
00559 peer_endpoint.protocol(), ec))
00560 {
00561 this->io_service().post(asio::detail::bind_handler(handler, ec));
00562 return;
00563 }
00564 }
00565
00566 this->service.async_connect(this->implementation, peer_endpoint, handler);
00567 }
00568
00570
00603 template <typename SettableSocketOption>
00604 void set_option(const SettableSocketOption& option)
00605 {
00606 asio::error_code ec;
00607 this->service.set_option(this->implementation, option, ec);
00608 asio::detail::throw_error(ec);
00609 }
00610
00612
00650 template <typename SettableSocketOption>
00651 asio::error_code set_option(const SettableSocketOption& option,
00652 asio::error_code& ec)
00653 {
00654 return this->service.set_option(this->implementation, option, ec);
00655 }
00656
00658
00692 template <typename GettableSocketOption>
00693 void get_option(GettableSocketOption& option) const
00694 {
00695 asio::error_code ec;
00696 this->service.get_option(this->implementation, option, ec);
00697 asio::detail::throw_error(ec);
00698 }
00699
00701
00740 template <typename GettableSocketOption>
00741 asio::error_code get_option(GettableSocketOption& option,
00742 asio::error_code& ec) const
00743 {
00744 return this->service.get_option(this->implementation, option, ec);
00745 }
00746
00748
00769 template <typename IoControlCommand>
00770 void io_control(IoControlCommand& command)
00771 {
00772 asio::error_code ec;
00773 this->service.io_control(this->implementation, command, ec);
00774 asio::detail::throw_error(ec);
00775 }
00776
00778
00804 template <typename IoControlCommand>
00805 asio::error_code io_control(IoControlCommand& command,
00806 asio::error_code& ec)
00807 {
00808 return this->service.io_control(this->implementation, command, ec);
00809 }
00810
00812
00826 endpoint_type local_endpoint() const
00827 {
00828 asio::error_code ec;
00829 endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
00830 asio::detail::throw_error(ec);
00831 return ep;
00832 }
00833
00835
00855 endpoint_type local_endpoint(asio::error_code& ec) const
00856 {
00857 return this->service.local_endpoint(this->implementation, ec);
00858 }
00859
00861
00875 endpoint_type remote_endpoint() const
00876 {
00877 asio::error_code ec;
00878 endpoint_type ep = this->service.remote_endpoint(this->implementation, ec);
00879 asio::detail::throw_error(ec);
00880 return ep;
00881 }
00882
00884
00904 endpoint_type remote_endpoint(asio::error_code& ec) const
00905 {
00906 return this->service.remote_endpoint(this->implementation, ec);
00907 }
00908
00910
00926 void shutdown(shutdown_type what)
00927 {
00928 asio::error_code ec;
00929 this->service.shutdown(this->implementation, what, ec);
00930 asio::detail::throw_error(ec);
00931 }
00932
00934
00955 asio::error_code shutdown(shutdown_type what,
00956 asio::error_code& ec)
00957 {
00958 return this->service.shutdown(this->implementation, what, ec);
00959 }
00960
00961 protected:
00963 ~basic_socket()
00964 {
00965 }
00966 };
00967
00968 }
00969
00970 #include "asio/detail/pop_options.hpp"
00971
00972 #endif // ASIO_BASIC_SOCKET_HPP