git-svn-id: svn://db.shs.com.ru/pip@543 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
4
main.cpp
4
main.cpp
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
PIEthernet::Address PIEthernet::Address::resolve(const PIString & host_port) {
|
||||||
}
|
PIString host; int port(0);
|
||||||
PIString ret = PIStringAscii("0.0.0.0");
|
splitIPPort(host_port, &host, &port);
|
||||||
if (!port.isEmpty()) ret += PIStringAscii(":") + port;
|
return resolve(host, port);
|
||||||
//#ifdef WINDOWS
|
}
|
||||||
hostent * he = gethostbyname(ip.dataAscii());
|
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
@@ -288,7 +286,7 @@ bool PIEthernet::init() {
|
|||||||
else
|
else
|
||||||
sock_s = sock;
|
sock_s = sock;
|
||||||
if (sock == -1 || sock_s == -1) {
|
if (sock == -1 || sock_s == -1) {
|
||||||
piCoutObj << "Can`t create socket, " << ethErrorString();
|
piCoutObj << "Can`t create socket," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR);
|
if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR);
|
||||||
@@ -377,7 +375,7 @@ bool PIEthernet::openDevice() {
|
|||||||
tries++;
|
tries++;
|
||||||
}
|
}
|
||||||
if (tries == 2) {
|
if (tries == 2) {
|
||||||
piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString();
|
piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
opened_ = true;
|
opened_ = true;
|
||||||
@@ -496,7 +494,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
|||||||
//piCout << "join group" << group << "ip" << ip_ << "with index" << mreq.imr_ifindex << "socket" << sock;
|
//piCout << "join group" << group << "ip" << ip_ << "with index" << mreq.imr_ifindex << "socket" << sock;
|
||||||
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||||
if (ethSetsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
|
if (ethSetsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
|
||||||
piCoutObj << "Can`t join multicast group " << group << ", " << ethErrorString();
|
piCoutObj << "Can`t join multicast group" << group << "," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (params[PIEthernet::MulticastLoop]) ethSetsockoptInt(sock, IPPROTO_IP, IP_MULTICAST_LOOP);
|
if (params[PIEthernet::MulticastLoop]) ethSetsockoptInt(sock, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||||
@@ -536,7 +534,7 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) {
|
|||||||
else mreq.imr_address.s_addr = addr_r.ip();
|
else mreq.imr_address.s_addr = addr_r.ip();
|
||||||
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||||
if (ethSetsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) {
|
if (ethSetsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) {
|
||||||
piCoutObj << "Can`t leave multicast group " << group << ", " << ethErrorString();
|
piCoutObj << "Can`t leave multicast group" << group << "," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
mcast_groups.removeAll(group);
|
mcast_groups.removeAll(group);
|
||||||
@@ -597,11 +595,11 @@ bool PIEthernet::listen(bool threaded) {
|
|||||||
tries++;
|
tries++;
|
||||||
}
|
}
|
||||||
if (tries == 2) {
|
if (tries == 2) {
|
||||||
piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString();
|
piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (::listen(sock, 64) == -1) {
|
if (::listen(sock, 64) == -1) {
|
||||||
piCoutObj << "Can`t listen on "<< addr_r << ", " << ethErrorString();
|
piCoutObj << "Can`t listen on"<< addr_r << "," << ethErrorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
opened_ = server_bounded = true;
|
opened_ = server_bounded = true;
|
||||||
@@ -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;
|
||||||
@@ -655,7 +653,7 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
|
|||||||
//piCoutObj << "connect to " << ip_ << ":" << port_ << connected_;
|
//piCoutObj << "connect to " << ip_ << ":" << port_ << connected_;
|
||||||
//qDebug() << "connect to " << ip_.data() << ":" << port_ << connected_;
|
//qDebug() << "connect to " << ip_.data() << ":" << port_ << connected_;
|
||||||
if (!connected_)
|
if (!connected_)
|
||||||
piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString();
|
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
||||||
opened_ = connected_;
|
opened_ = connected_;
|
||||||
if (connected_) {
|
if (connected_) {
|
||||||
connecting_ = false;
|
connecting_ = false;
|
||||||
@@ -769,7 +767,7 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
|
|||||||
//piCoutObj << "connect to " << ip << ":" << port_;
|
//piCoutObj << "connect to " << ip << ":" << port_;
|
||||||
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
|
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
|
||||||
if (!connected_)
|
if (!connected_)
|
||||||
piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString();
|
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
||||||
opened_ = connected_;
|
opened_ = connected_;
|
||||||
if (connected_) {
|
if (connected_) {
|
||||||
connecting_ = false;
|
connecting_ = false;
|
||||||
@@ -823,7 +821,7 @@ void PIEthernet::server_func(void * eth) {
|
|||||||
piMSleep(10);
|
piMSleep(10);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ce->debug()) piCout << "[PIEthernet] Can`t accept new connection, " << ethErrorString();
|
if (ce->debug()) piCout << "[PIEthernet] Can`t accept new connection," << ethErrorString();
|
||||||
piMSleep(10);
|
piMSleep(10);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -950,7 +948,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
|||||||
pAdapter = pAdapter->Next;
|
pAdapter = pAdapter->Next;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
piCout << "[PIEthernet] GetAdaptersInfo failed with error: " << ret;
|
piCout << "[PIEthernet] GetAdaptersInfo failed with error:" << ret;
|
||||||
if (pAdapterInfo)
|
if (pAdapterInfo)
|
||||||
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -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:
|
||||||
@@ -227,6 +260,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_;}
|
||||||
|
|||||||
Reference in New Issue
Block a user