mn_basic_endpoint.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef _MINLIB_BASIC_ENDPOINT_HPP_
19 #define _MINLIB_BASIC_ENDPOINT_HPP_
20 
21 #include "../mn_config.hpp"
22 #include "../mn_def.hpp"
23 
24 
25 #include "mn_basic_ip_address.hpp"
26 #include "mn_basic_ip4_address.hpp"
27 #include "mn_basic_ip6_address.hpp"
28 #include "mn_net_types.hpp"
29 
30 
31 namespace mn {
32  namespace net {
44  public:
49  virtual basic_endpoint* get_copy() = 0;
50  };
51 
58  template <class TIPCLASS, address_family TAF>
60  public:
61  using ip_type = TIPCLASS;
63 
67  basic_ip_endpoint(const ip_type& ip, const uint16_t& port) noexcept
68  : m_ipAdress(ip), m_iPort(port) { }
69 
73  basic_ip_endpoint(const self_type& pOther) noexcept
74  : m_ipAdress(pOther.m_ipAdress), m_iPort(pOther.m_iPort) { }
75 
80  uint16_t get_port() { return m_iPort; }
85  ip_type get_host() { return m_ipAdress; }
90  address_family get_famile() { return TAF; }
94  void set_port(const uint16_t& port) { if(port < 9999) m_iPort = port; }
99  void set_host(const ip_type& ip) { m_ipAdress = ip; }
104  bool is_multicast() { return m_ipAdress.is_multicast(); }
109  bool is_loopback() { return m_ipAdress.is_loopback(); }
110 
111 
112 
116  self_type& operator = (const self_type& pOther) {
117  if (this == &pOther) return *this;
118  m_ipAdress = pOther.m_ipAdress; m_iPort = pOther.m_iPort; return *this;
119  }
124  bool operator == (const self_type& pOther) {
125  if(m_iPort != pOther.m_iPort) return false;
126  return m_ipAdress == pOther.m_ipAdress;
127  }
132  bool operator != (const self_type& pOther) {
133  return !(*this == pOther);
134  }
138  bool operator < (const self_type& pOther) const {
139  if(m_iPort >= pOther.m_iPort) return false;
140  return m_ipAdress < pOther.m_ipAdress;
141  }
142 
143  virtual void swap(self_type& rhs) noexcept {
144  mn::swap<uint16_t>(m_iPort, rhs.m_iPort);
145  m_ipAdress.swap(rhs.m_ipAdress);
146  }
147  protected:
149  uint16_t m_iPort;
150  };
151  }
152 }
153 
154 #endif // _MINLIB_BASIC_ENDPOINT_HPP_
This abstract basic class represents an endpoint/socket address.
Definition: mn_basic_endpoint.hpp:43
virtual basic_endpoint * get_copy()=0
Get a copy from this.
This template basic class represents an ip endpoint/socket address.
Definition: mn_basic_endpoint.hpp:59
address_family get_famile()
Get the address family of the address.
Definition: mn_basic_endpoint.hpp:90
basic_ip_endpoint(const self_type &pOther) noexcept
Construct a basic_ip_endpoint by copying another one.
Definition: mn_basic_endpoint.hpp:73
ip_type m_ipAdress
Definition: mn_basic_endpoint.hpp:148
void set_port(const uint16_t &port)
Set a port number, between 0-9999.
Definition: mn_basic_endpoint.hpp:94
uint16_t m_iPort
Definition: mn_basic_endpoint.hpp:149
basic_ip_endpoint(const ip_type &ip, const uint16_t &port) noexcept
Construct a basic_ip_endpoint by giving ip address and port.
Definition: mn_basic_endpoint.hpp:67
bool operator==(const self_type &pOther)
Is the right basic_endpoint equel with this.
Definition: mn_basic_endpoint.hpp:124
uint16_t get_port()
Get the port number.
Definition: mn_basic_endpoint.hpp:80
bool operator!=(const self_type &pOther)
Is the right basic_endpoint not equel with this.
Definition: mn_basic_endpoint.hpp:132
bool is_multicast()
Is the host address a multicast address.
Definition: mn_basic_endpoint.hpp:104
self_type & operator=(const self_type &pOther)
Assigns another basic_ip_endpoint.
Definition: mn_basic_endpoint.hpp:116
virtual void swap(self_type &rhs) noexcept
Definition: mn_basic_endpoint.hpp:143
void set_host(const ip_type &ip)
Set a new the host IP address.
Definition: mn_basic_endpoint.hpp:99
bool operator<(const self_type &pOther) const
smaler as .. - odering
Definition: mn_basic_endpoint.hpp:138
ip_type get_host()
Get the host IP address.
Definition: mn_basic_endpoint.hpp:85
TIPCLASS ip_type
Definition: mn_basic_endpoint.hpp:61
bool is_loopback()
Is the host address a loopaddress.
Definition: mn_basic_endpoint.hpp:109
address_family
Defines socket address types for creating.
Definition: mn_net_types.hpp:41
@ ip
options for IPv4 level
Definition: mn_allocator_typetraits.hpp:25