Line data Source code
1 : //
2 : // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/beast2
8 : //
9 :
10 : #ifndef BOOST_BEAST2_ENDPOINT_HPP
11 : #define BOOST_BEAST2_ENDPOINT_HPP
12 :
13 : #include <boost/beast2/detail/config.hpp>
14 : #include <boost/url/ipv4_address.hpp>
15 : #include <boost/url/ipv6_address.hpp>
16 : #include <boost/url/host_type.hpp>
17 : #include <iosfwd>
18 :
19 : namespace boost {
20 : namespace beast2 {
21 :
22 : /** Represents a host and port
23 :
24 : This class represents a network endpoint
25 : consisting of a host and port. The host may
26 : be specified as a domain name, an IPv4 address,
27 : or an IPv6 address.
28 : */
29 : class endpoint
30 : {
31 : public:
32 0 : ~endpoint()
33 : {
34 0 : switch(kind_)
35 : {
36 0 : case urls::host_type::ipv4:
37 0 : ipv4_.~ipv4_address();
38 0 : break;
39 0 : case urls::host_type::ipv6:
40 0 : ipv6_.~ipv6_address();
41 0 : break;
42 0 : default:
43 0 : break;
44 : }
45 0 : }
46 :
47 : endpoint() noexcept
48 : : kind_(urls::host_type::none)
49 : {
50 : }
51 :
52 : BOOST_BEAST2_DECL
53 : endpoint(
54 : endpoint const& other) noexcept;
55 :
56 : BOOST_BEAST2_DECL
57 : endpoint(
58 : urls::ipv4_address const& addr,
59 : unsigned short port) noexcept;
60 :
61 : BOOST_BEAST2_DECL
62 : endpoint(
63 : urls::ipv6_address const& addr,
64 : unsigned short port) noexcept;
65 :
66 : BOOST_BEAST2_DECL
67 : endpoint& operator=(endpoint const&) noexcept;
68 :
69 : unsigned short
70 : port() const noexcept
71 : {
72 : return port_;
73 : }
74 :
75 : urls::host_type
76 : kind() const noexcept
77 : {
78 : return kind_;
79 : }
80 :
81 : bool is_ipv4() const noexcept
82 : {
83 : return kind_ == urls::host_type::ipv4;
84 : }
85 :
86 : bool is_ipv6() const noexcept
87 : {
88 : return kind_ == urls::host_type::ipv6;
89 : }
90 :
91 : urls::ipv4_address const&
92 : get_ipv4() const noexcept
93 : {
94 : BOOST_ASSERT(kind_ == urls::host_type::ipv4);
95 : return ipv4_;
96 : }
97 :
98 : urls::ipv6_address const&
99 : get_ipv6() const noexcept
100 : {
101 : BOOST_ASSERT(kind_ == urls::host_type::ipv6);
102 : return ipv6_;
103 : }
104 :
105 : friend
106 : std::ostream&
107 : operator<<(
108 : std::ostream& os,
109 : endpoint const& ep)
110 : {
111 : ep.format(os);
112 : return os;
113 : }
114 :
115 : private:
116 : BOOST_BEAST2_DECL
117 : void format(std::ostream&) const;
118 :
119 : urls::host_type kind_ = urls::host_type::none;
120 : unsigned short port_ = 0;
121 : union
122 : {
123 : urls::ipv4_address ipv4_;
124 : urls::ipv6_address ipv6_;
125 : };
126 : };
127 :
128 : } // beast2
129 : } // boost
130 :
131 : #endif
|