00001
00033 #ifndef LL_LLHOST_H
00034 #define LL_LLHOST_H
00035
00036 #include <iostream>
00037 #include <string>
00038
00039 #include "net.h"
00040
00041 const U32 INVALID_PORT = 0;
00042 const U32 INVALID_HOST_IP_ADDRESS = 0x0;
00043
00044 class LLHost {
00045 protected:
00046 U32 mPort;
00047 U32 mIP;
00048 public:
00049
00050 static LLHost invalid;
00051
00052
00053 LLHost()
00054 : mPort(INVALID_PORT),
00055 mIP(INVALID_HOST_IP_ADDRESS)
00056 { }
00057
00058 LLHost( U32 ipv4_addr, U32 port )
00059 : mPort( port )
00060 {
00061 mIP = ipv4_addr;
00062 }
00063
00064 LLHost( const char *ipv4_addr, U32 port )
00065 : mPort( port )
00066 {
00067 mIP = ip_string_to_u32(ipv4_addr);
00068 }
00069
00070 explicit LLHost(const U64 ip_port)
00071 {
00072 U32 ip = (U32)(ip_port >> 32);
00073 U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
00074 mIP = ip;
00075 mPort = port;
00076 }
00077
00078 explicit LLHost(const std::string& ip_and_port);
00079
00080 ~LLHost()
00081 { }
00082
00083
00084 void set( U32 ip, U32 port ) { mIP = ip; mPort = port; }
00085 void set( const char* ipstr, U32 port ) { mIP = ip_string_to_u32(ipstr); mPort = port; }
00086 void setAddress( const char* ipstr ) { mIP = ip_string_to_u32(ipstr); }
00087 void setAddress( U32 ip ) { mIP = ip; }
00088 void setPort( U32 port ) { mPort = port; }
00089 BOOL setHostByName(const char *hname);
00090
00091 LLHost& operator=(const LLHost &rhs);
00092 void invalidate() { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
00093
00094
00095 U32 getAddress() const { return mIP; }
00096 U32 getPort() const { return mPort; }
00097 BOOL isOk() const { return (mIP != INVALID_HOST_IP_ADDRESS) && (mPort != INVALID_PORT); }
00098 size_t hash() const { return (mIP << 16) | (mPort & 0xffff); }
00099 void getString(char* buffer, S32 length) const;
00100 void getIPString(char* buffer, S32 length) const;
00101 std::string getIPString() const;
00102 void getHostName(char *buf, S32 len) const;
00103 std::string getHostName() const;
00104 std::string getIPandPort() const;
00105
00106 friend std::ostream& operator<< (std::ostream& os, const LLHost &hh);
00107
00108
00109
00110
00111
00112 friend bool operator==( const LLHost &lhs, const LLHost &rhs );
00113 friend bool operator!=( const LLHost &lhs, const LLHost &rhs );
00114 friend bool operator<(const LLHost &lhs, const LLHost &rhs);
00115 };
00116
00117
00118
00119 class LLHostHash
00120 {
00121 public:
00122 size_t operator() (const LLHost &hh) const { return hh.hash(); }
00123 };
00124
00125
00126 inline bool operator==( const LLHost &lhs, const LLHost &rhs )
00127 {
00128 return (lhs.mIP == rhs.mIP) && (lhs.mPort == rhs.mPort);
00129 }
00130
00131 inline bool operator!=( const LLHost &lhs, const LLHost &rhs )
00132 {
00133 return (lhs.mIP != rhs.mIP) || (lhs.mPort != rhs.mPort);
00134 }
00135
00136 inline bool operator<(const LLHost &lhs, const LLHost &rhs)
00137 {
00138 if (lhs.mIP < rhs.mIP)
00139 {
00140 return true;
00141 }
00142 if (lhs.mIP > rhs.mIP)
00143 {
00144 return false;
00145 }
00146
00147 if (lhs.mPort < rhs.mPort)
00148 {
00149 return true;
00150 }
00151 else
00152 {
00153 return false;
00154 }
00155 }
00156
00157
00158 #endif // LL_LLHOST_H