git-svn-id: svn://db.shs.com.ru/pip@543 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-09-06 07:27:24 +00:00
parent 6f54f501cd
commit e69f1e5ba2
3 changed files with 74 additions and 37 deletions

View File

@@ -54,8 +54,8 @@ int main(int argc, char *argv[]) {
PIString s2; PIString s2;
s2 = PIString::fromUTF8(ba); s2 = PIString::fromUTF8(ba);
piCout << s2;*/ piCout << s2;*/
PIEthernet::Address a("127.1", -1); piCout << PIEthernet::Address::resolve("www.ya.ru:22");
piCout << a << PICoutManipulators::Hex << a.ip(); piCout << PIEthernet::Address::resolve("www.ya.ru", 22);
return 0; return 0;
} }

View File

@@ -162,34 +162,32 @@ void PIEthernet::Address::clear() {
} }
PIString PIEthernet::Address::resolve(const PIString & host) { bool PIEthernet::Address::isNull() const {
PIString ip(host), port; return (ip_ == 0) && (port_ == 0);
int i = host.find(':');
if (i >= 0) {
ip = host.left(i);
port = host.right(host.length() - i - 1);
} }
PIString ret = PIStringAscii("0.0.0.0");
if (!port.isEmpty()) ret += PIStringAscii(":") + port;
//#ifdef WINDOWS PIEthernet::Address PIEthernet::Address::resolve(const PIString & host_port) {
hostent * he = gethostbyname(ip.dataAscii()); PIString host; int port(0);
splitIPPort(host_port, &host, &port);
return resolve(host, port);
}
PIEthernet::Address PIEthernet::Address::resolve(const PIString & host, ushort port) {
Address ret(0, port);
hostent * he = gethostbyname(host.dataAscii());
if (!he) if (!he)
return ret; return ret;
struct in_addr addr; if (he->h_addr_list[0])
if (he->h_addr_list[0]) { ret.setIP(*((uint*)(he->h_addr_list[0])));
addr.s_addr = *((uint*)(he->h_addr_list[0]));
ret = inet_ntoa(addr);
if (!port.isEmpty()) ret += PIStringAscii(":") + port;
}
//#else
//#endif
return ret; return ret;
} }
void PIEthernet::Address::splitIPPort(const PIString & ipp, PIString * _ip, int * _port) { void PIEthernet::Address::splitIPPort(const PIString & ipp, PIString * _ip, int * _port) {
//piCout << "parse" << ipp; //piCout << "parse" << ipp;
int sp = ipp.find(":"); int sp = ipp.findLast(":");
if (_ip != 0) *_ip = (sp >= 0 ? ipp.left(sp) : ipp); if (_ip != 0) *_ip = (sp >= 0 ? ipp.left(sp) : ipp);
if (_port != 0 && sp >= 0) *_port = ipp.right(ipp.length() - ipp.find(":") - 1).toInt(); if (_port != 0 && sp >= 0) *_port = ipp.right(ipp.length() - ipp.find(":") - 1).toInt();
} }
@@ -641,8 +639,8 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
init(); init();
qDebug() << "init() in read thread";*/ qDebug() << "init() in read thread";*/
#endif #endif
memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_));
addr_r.set(path()); addr_r.set(path());
memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_));
PRIVATE->addr_.sin_port = htons(addr_r.port()); PRIVATE->addr_.sin_port = htons(addr_r.port());
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip(); PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
PRIVATE->addr_.sin_family = AF_INET; PRIVATE->addr_.sin_family = AF_INET;

View File

@@ -62,30 +62,63 @@ public:
}; };
//! \brief IPv4 network address, ip and port //! \brief IPv4 network address, IP and port
class Address { class Address {
friend class PIEthernet; friend class PIEthernet;
friend inline PIByteArray & operator <<(PIByteArray & s, const PIEthernet::Address & v); friend inline PIByteArray & operator <<(PIByteArray & s, const PIEthernet::Address & v);
friend inline PIByteArray & operator >>(PIByteArray & s, PIEthernet::Address & v); friend inline PIByteArray & operator >>(PIByteArray & s, PIEthernet::Address & v);
public: public:
//! Contructs %Address with binary representation of ip and port
//! Contructs %Address with binary representation of IP and port
Address(uint ip = 0, ushort port = 0); Address(uint ip = 0, ushort port = 0);
//! Contructs %Address with string representation "i.i.i.i:p"
Address(const PIString & ip_port); Address(const PIString & ip_port);
//! Contructs %Address with IP string representation "i.i.i.i" and port
Address(const PIString & ip, ushort port); Address(const PIString & ip, ushort port);
//! Returns binary IP
uint ip() const {return ip_;} uint ip() const {return ip_;}
//! Returns port
ushort port() const {return port_;} ushort port() const {return port_;}
//! Returns string IP
PIString ipString() const; PIString ipString() const;
//! Returns string representation of IP and port "i.i.i.i:p"
PIString toString() const; PIString toString() const;
//! Set address IP
void setIP(uint ip); void setIP(uint ip);
//! Set address IP
void setIP(const PIString & ip); void setIP(const PIString & ip);
//! Set address port
void setPort(ushort port); void setPort(ushort port);
//! Set address IP and port, "i.i.i.i:p"
void set(const PIString & ip_port); void set(const PIString & ip_port);
//! Set address IP and port, "i.i.i.i"
void set(const PIString & ip, ushort port); void set(const PIString & ip, ushort port);
//! Set address binary IP and port
void set(uint ip, ushort port); void set(uint ip, ushort port);
//! Set IP and port to 0
void clear(); void clear();
//! Resolve hostname "host" and return it IPv4 address or "0.0.0.0" on error //! Returns if IP and port is 0
static PIString resolve(const PIString & host); bool isNull() const;
//! Resolve hostname "host:port" and return it address or null address on error
static Address resolve(const PIString & host_port);
//! Resolve hostname "host" with port "port" and return it address or null address on error
static Address resolve(const PIString & host, ushort port);
static void splitIPPort(const PIString & ipp, PIString * ip, int * port); static void splitIPPort(const PIString & ipp, PIString * ip, int * port);
private: private:
@@ -228,6 +261,9 @@ public:
//! Connect to TCP server with address "ip_port". Use only for TCP_Client //! Connect to TCP server with address "ip_port". Use only for TCP_Client
bool connect(const PIString & ip_port) {setPath(ip_port); return connect();} bool connect(const PIString & ip_port) {setPath(ip_port); return connect();}
//! Connect to TCP server with address "addr". Use only for TCP_Client
bool connect(const Address & addr) {setPath(addr.toString()); return connect();}
//! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client //! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client
bool isConnected() const {return connected_;} bool isConnected() const {return connected_;}
@@ -244,6 +280,9 @@ public:
//! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server //! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server
bool listen(const PIString & ip_port, bool threaded = false) {setReadAddress(ip_port); return listen(threaded);} bool listen(const PIString & ip_port, bool threaded = false) {setReadAddress(ip_port); return listen(threaded);}
//! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server
bool listen(const Address & addr, bool threaded = false) {setReadAddress(addr); return listen(threaded);}
PIEthernet * client(int index) {return clients_[index];} PIEthernet * client(int index) {return clients_[index];}
int clientsCount() const {return clients_.size_s();} int clientsCount() const {return clients_.size_s();}
PIVector<PIEthernet * > clients() const {return clients_;} PIVector<PIEthernet * > clients() const {return clients_;}