TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NetAddress.h
Go to the documentation of this file.
1 
7 #ifndef G3D_NetAddress_h
8 #define G3D_NetAddress_h
9 
10 #include "G3D/platform.h"
11 #include "G3D/Table.h"
12 #include "G3D/netheaders.h"
13 
14 #include "G3D/g3dmath.h"
15 
16 namespace G3D {
17 
18 class NetAddress {
19 private:
20  friend class NetworkDevice;
21  friend class LightweightConduit;
22  friend class ReliableConduit;
23 
25  void init(uint32 host, uint16 port);
26  void init(const std::string& hostname, uint16 port);
27  NetAddress(const SOCKADDR_IN& a);
28  NetAddress(const struct in_addr& addr, uint16 port = 0);
29 
31 
32 public:
33 
34  enum {
38  // Must match ENET_HOST_ANY
40  };
41 
47  explicit NetAddress(uint32 host, uint16 port = 0);
48 
52  NetAddress(const std::string& hostname, uint16 port);
53 
57  explicit NetAddress(const std::string& hostnameAndPort);
58 
73 
74  NetAddress();
75 
76  static void localHostAddresses(Array<NetAddress>& array);
77 
78  void serialize(class BinaryOutput& b) const;
79  void deserialize(class BinaryInput& b);
80 
82  bool ok() const;
83 
86  inline uint32 ip() const {
87  return ntohl(addr.sin_addr.s_addr);
88  //return ntohl(addr.sin_addr.S_un.S_addr);
89  }
90 
91  inline uint16 port() const {
92  return ntohs(addr.sin_port);
93  }
94 
95  std::string ipString() const;
96  std::string toString() const;
97 
101  std::string hostname() const;
102 
104  static std::string localHostname();
105 };
106 
107 std::ostream& operator<<(std::ostream& os, const NetAddress&);
108 
109 } // namespace G3D
110 
111 template <> struct HashTrait<G3D::NetAddress> {
112  static size_t hashCode(const G3D::NetAddress& key) {
113  return static_cast<size_t>(key.ip() + (static_cast<G3D::uint32>(key.port()) << 16));
114  }
115 };
116 
117 namespace G3D {
118 
123 inline bool operator==(const NetAddress& a, const NetAddress& b) {
124  return (a.ip() == b.ip()) && (a.port() == b.port());
125 }
126 
127 
128 inline bool operator!=(const NetAddress& a, const NetAddress& b) {
129  return !(a == b);
130 }
131 
132 } // namespace G3D
133 
134 #endif
SOCKADDR_IN addr
Definition: NetAddress.h:30
bool ok() const
Returns true if this is not an illegal address.
Definition: NetAddress.cpp:175
Definition: BinaryInput.h:69
Definition: NetAddress.h:39
bool operator!=(const NetAddress &a, const NetAddress &b)
Definition: NetAddress.h:128
Definition: HashTrait.h:105
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
NetAddress()
Definition: NetAddress.cpp:46
Definition: NetAddress.h:18
static std::string localHostname()
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
static void localHostAddresses(Array< NetAddress > &array)
Definition: NetAddress.cpp:135
bool operator==(const NetAddress &a, const NetAddress &b)
Definition: NetAddress.h:123
uint16_t uint16
Definition: g3dmath.h:166
#define SOCKADDR_IN
Definition: netheaders.h:17
uint32 ip() const
Returns a value in host format (i.e., don't worry about endian issues)
Definition: NetAddress.h:86
static size_t hashCode(const G3D::NetAddress &key)
Definition: NetAddress.h:112
Abstraction of network (socket) functionality.
Definition: NetworkDevice.h:608
std::string ipString() const
Definition: NetAddress.cpp:180
uint16 port() const
Definition: NetAddress.h:91
std::string hostname() const
void serialize(class BinaryOutput &b) const
Definition: NetAddress.cpp:158
static NetAddress broadcastAddress(uint16 port)
Creates a UDP broadcast address for use with a G3D::LightweightConduit.
Definition: NetAddress.cpp:108
std::ostream & operator<<(std::ostream &os, const NetAddress &)
Definition: NetworkDevice.cpp:26
Definition: BinaryOutput.h:52
std::string toString() const
Definition: NetAddress.cpp:185
Definition: NetworkDevice.h:407
uint32_t uint32
Definition: g3dmath.h:168
Definition: NetworkDevice.h:168
void deserialize(class BinaryInput &b)
Definition: NetAddress.cpp:164