Chris@16: // Chris@16: // ip/basic_endpoint.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_IP_BASIC_ENDPOINT_HPP Chris@16: #define BOOST_ASIO_IP_BASIC_ENDPOINT_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: # include Chris@16: #endif // !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace ip { Chris@16: Chris@16: /// Describes an endpoint for a version-independent IP socket. Chris@16: /** Chris@16: * The boost::asio::ip::basic_endpoint class template describes an endpoint that Chris@16: * may be associated with a particular socket. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: * Chris@16: * @par Concepts: Chris@16: * Endpoint. Chris@16: */ Chris@16: template Chris@16: class basic_endpoint Chris@16: { Chris@16: public: Chris@16: /// The protocol type associated with the endpoint. Chris@16: typedef InternetProtocol protocol_type; Chris@16: Chris@16: /// The type of the endpoint structure. This type is dependent on the Chris@16: /// underlying implementation of the socket layer. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined data_type; Chris@16: #else Chris@16: typedef boost::asio::detail::socket_addr_type data_type; Chris@16: #endif Chris@16: Chris@16: /// Default constructor. Chris@16: basic_endpoint() Chris@16: : impl_() Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct an endpoint using a port number, specified in the host's byte Chris@16: /// order. The IP address will be the any address (i.e. INADDR_ANY or Chris@16: /// in6addr_any). This constructor would typically be used for accepting new Chris@16: /// connections. Chris@16: /** Chris@16: * @par Examples Chris@16: * To initialise an IPv4 TCP endpoint for port 1234, use: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234); Chris@16: * @endcode Chris@16: * Chris@16: * To specify an IPv6 UDP endpoint for port 9876, use: Chris@16: * @code Chris@16: * boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876); Chris@16: * @endcode Chris@16: */ Chris@16: basic_endpoint(const InternetProtocol& internet_protocol, Chris@16: unsigned short port_num) Chris@16: : impl_(internet_protocol.family(), port_num) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct an endpoint using a port number and an IP address. This Chris@16: /// constructor may be used for accepting connections on a specific interface Chris@16: /// or for making a connection to a remote endpoint. Chris@16: basic_endpoint(const boost::asio::ip::address& addr, unsigned short port_num) Chris@16: : impl_(addr, port_num) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Copy constructor. Chris@16: basic_endpoint(const basic_endpoint& other) Chris@16: : impl_(other.impl_) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: /// Move constructor. Chris@16: basic_endpoint(basic_endpoint&& other) Chris@16: : impl_(other.impl_) Chris@16: { Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: /// Assign from another endpoint. Chris@16: basic_endpoint& operator=(const basic_endpoint& other) Chris@16: { Chris@16: impl_ = other.impl_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: /// Move-assign from another endpoint. Chris@16: basic_endpoint& operator=(basic_endpoint&& other) Chris@16: { Chris@16: impl_ = other.impl_; Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: /// The protocol associated with the endpoint. Chris@16: protocol_type protocol() const Chris@16: { Chris@16: if (impl_.is_v4()) Chris@16: return InternetProtocol::v4(); Chris@16: return InternetProtocol::v6(); Chris@16: } Chris@16: Chris@16: /// Get the underlying endpoint in the native type. Chris@16: data_type* data() Chris@16: { Chris@16: return impl_.data(); Chris@16: } Chris@16: Chris@16: /// Get the underlying endpoint in the native type. Chris@16: const data_type* data() const Chris@16: { Chris@16: return impl_.data(); Chris@16: } Chris@16: Chris@16: /// Get the underlying size of the endpoint in the native type. Chris@16: std::size_t size() const Chris@16: { Chris@16: return impl_.size(); Chris@16: } Chris@16: Chris@16: /// Set the underlying size of the endpoint in the native type. Chris@16: void resize(std::size_t new_size) Chris@16: { Chris@16: impl_.resize(new_size); Chris@16: } Chris@16: Chris@16: /// Get the capacity of the endpoint in the native type. Chris@16: std::size_t capacity() const Chris@16: { Chris@16: return impl_.capacity(); Chris@16: } Chris@16: Chris@16: /// Get the port associated with the endpoint. The port number is always in Chris@16: /// the host's byte order. Chris@16: unsigned short port() const Chris@16: { Chris@16: return impl_.port(); Chris@16: } Chris@16: Chris@16: /// Set the port associated with the endpoint. The port number is always in Chris@16: /// the host's byte order. Chris@16: void port(unsigned short port_num) Chris@16: { Chris@16: impl_.port(port_num); Chris@16: } Chris@16: Chris@16: /// Get the IP address associated with the endpoint. Chris@16: boost::asio::ip::address address() const Chris@16: { Chris@16: return impl_.address(); Chris@16: } Chris@16: Chris@16: /// Set the IP address associated with the endpoint. Chris@16: void address(const boost::asio::ip::address& addr) Chris@16: { Chris@16: impl_.address(addr); Chris@16: } Chris@16: Chris@16: /// Compare two endpoints for equality. Chris@16: friend bool operator==(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return e1.impl_ == e2.impl_; Chris@16: } Chris@16: Chris@16: /// Compare two endpoints for inequality. Chris@16: friend bool operator!=(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return !(e1 == e2); Chris@16: } Chris@16: Chris@16: /// Compare endpoints for ordering. Chris@16: friend bool operator<(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return e1.impl_ < e2.impl_; Chris@16: } Chris@16: Chris@16: /// Compare endpoints for ordering. Chris@16: friend bool operator>(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return e2.impl_ < e1.impl_; Chris@16: } Chris@16: Chris@16: /// Compare endpoints for ordering. Chris@16: friend bool operator<=(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return !(e2 < e1); Chris@16: } Chris@16: Chris@16: /// Compare endpoints for ordering. Chris@16: friend bool operator>=(const basic_endpoint& e1, Chris@16: const basic_endpoint& e2) Chris@16: { Chris@16: return !(e1 < e2); Chris@16: } Chris@16: Chris@16: private: Chris@16: // The underlying IP endpoint. Chris@16: boost::asio::ip::detail::endpoint impl_; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: /// Output an endpoint as a string. Chris@16: /** Chris@16: * Used to output a human-readable string for a specified endpoint. Chris@16: * Chris@16: * @param os The output stream to which the string will be written. Chris@16: * Chris@16: * @param endpoint The endpoint to be written. Chris@16: * Chris@16: * @return The output stream. Chris@16: * Chris@16: * @relates boost::asio::ip::basic_endpoint Chris@16: */ Chris@16: template Chris@16: std::basic_ostream& operator<<( Chris@16: std::basic_ostream& os, Chris@16: const basic_endpoint& endpoint); Chris@16: Chris@16: #endif // !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: } // namespace ip Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP