sources/network/ipaddress.h

branch
protocol5
changeset 106
7b156b764d11
parent 88
08ccaf26cffd
child 109
e4966d7e615d
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
32 #include "../main.h" 32 #include "../main.h"
33 33
34 struct sockaddr; 34 struct sockaddr;
35 struct sockaddr_in; 35 struct sockaddr_in;
36 36
37 enum WithPort { IP_WITH_PORT, IP_NO_PORT }; 37 BEGIN_ZFC_NAMESPACE
38 38
39 struct IPAddress 39 struct IPAddress
40 { 40 {
41 enum
42 {
43 LOCALHOST = 0x7f000001
44 };
45
46 enum WithPort
47 {
48 WITH_PORT,
49 NO_PORT
50 };
51
41 class StringParseError : public std::exception 52 class StringParseError : public std::exception
42 { 53 {
43 String m_message; 54 String m_message;
44 55
45 public: 56 public:
46 StringParseError (String message) : 57 StringParseError (String message) :
47 m_message (message) {} 58 m_message (message) {}
48 59
49 inline METHOD 60 const char* what() const throw()
50 what() const throw() -> const char*
51 { 61 {
52 return m_message.chars(); 62 return m_message.chars();
53 } 63 }
54 }; 64 };
55 65
58 68
59 IPAddress(); 69 IPAddress();
60 IPAddress (unsigned long host, unsigned short port); 70 IPAddress (unsigned long host, unsigned short port);
61 IPAddress (const IPAddress& other); 71 IPAddress (const IPAddress& other);
62 72
63 METHOD compare (const IPAddress& other) const -> bool; 73 bool compare (const IPAddress& other) const;
64 METHOD octet (int n) const -> unsigned char; 74 unsigned char octet (int n) const;
65 METHOD set_octet (int n, unsigned char oct) -> void; 75 void set_octet (int n, unsigned char oct);
66 METHOD to_string (WithPort withport = IP_NO_PORT) const -> String; 76 String to_string (WithPort withport = NO_PORT) const;
67 METHOD to_sockaddr_in() const -> sockaddr_in; 77 sockaddr_in to_sockaddr_in() const;
68 METHOD operator< (const IPAddress& other) const -> bool; 78 bool operator< (const IPAddress& other) const;
69 inline METHOD operator== (const IPAddress& other) const -> bool; 79 bool operator== (const IPAddress& other) const { return compare (other); }
70 inline METHOD operator!= (const IPAddress& other) const -> bool; 80 bool operator!= (const IPAddress& other) const { return not compare (other); }
71 inline METHOD operator[] (int n) const -> unsigned char; 81 unsigned char operator[] (int n) const { return octet (n); }
72 82
73 static METHOD from_string (String input) -> IPAddress; 83 static IPAddress from_string (String input);
74 static METHOD resolve (String node) -> IPAddress; 84 static IPAddress resolve (String node);
75 }; 85 };
76 86
77 static const unsigned long localhost = 0x7F000001; 87 END_ZFC_NAMESPACE
78
79 inline METHOD
80 IPAddress::operator[] (int n) const -> unsigned char
81 {
82 return octet (n);
83 }
84
85 inline METHOD
86 IPAddress::operator== (const IPAddress& other) const -> bool
87 {
88 return compare (other);
89 }
90
91 inline METHOD
92 IPAddress::operator!= (const IPAddress& other) const -> bool
93 {
94 return not operator== (other);
95 }

mercurial