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

@@ -162,34 +162,32 @@ void PIEthernet::Address::clear() {
}
PIString PIEthernet::Address::resolve(const PIString & host) {
PIString ip(host), port;
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
hostent * he = gethostbyname(ip.dataAscii());
bool PIEthernet::Address::isNull() const {
return (ip_ == 0) && (port_ == 0);
}
PIEthernet::Address PIEthernet::Address::resolve(const PIString & host_port) {
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)
return ret;
struct in_addr addr;
if (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
if (he->h_addr_list[0])
ret.setIP(*((uint*)(he->h_addr_list[0])));
return ret;
}
void PIEthernet::Address::splitIPPort(const PIString & ipp, PIString * _ip, int * _port) {
//piCout << "parse" << ipp;
int sp = ipp.find(":");
int sp = ipp.findLast(":");
if (_ip != 0) *_ip = (sp >= 0 ? ipp.left(sp) : ipp);
if (_port != 0 && sp >= 0) *_port = ipp.right(ipp.length() - ipp.find(":") - 1).toInt();
}
@@ -288,7 +286,7 @@ bool PIEthernet::init() {
else
sock_s = sock;
if (sock == -1 || sock_s == -1) {
piCoutObj << "Can`t create socket, " << ethErrorString();
piCoutObj << "Can`t create socket," << ethErrorString();
return false;
}
if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR);
@@ -377,7 +375,7 @@ bool PIEthernet::openDevice() {
tries++;
}
if (tries == 2) {
piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString();
piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString();
return false;
}
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;
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
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;
}
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();
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
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;
}
mcast_groups.removeAll(group);
@@ -597,11 +595,11 @@ bool PIEthernet::listen(bool threaded) {
tries++;
}
if (tries == 2) {
piCoutObj << "Can`t bind to " << addr_r << ", " << ethErrorString();
piCoutObj << "Can`t bind to" << addr_r << "," << ethErrorString();
return false;
}
if (::listen(sock, 64) == -1) {
piCoutObj << "Can`t listen on "<< addr_r << ", " << ethErrorString();
piCoutObj << "Can`t listen on"<< addr_r << "," << ethErrorString();
return false;
}
opened_ = server_bounded = true;
@@ -641,8 +639,8 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
init();
qDebug() << "init() in read thread";*/
#endif
memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_));
addr_r.set(path());
memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_));
PRIVATE->addr_.sin_port = htons(addr_r.port());
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
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_;
//qDebug() << "connect to " << ip_.data() << ":" << port_ << connected_;
if (!connected_)
piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString();
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
opened_ = connected_;
if (connected_) {
connecting_ = false;
@@ -769,7 +767,7 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
//piCoutObj << "connect to " << ip << ":" << port_;
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
if (!connected_)
piCoutObj << "Can`t connect to " << addr_r << ", " << ethErrorString();
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
opened_ = connected_;
if (connected_) {
connecting_ = false;
@@ -823,7 +821,7 @@ void PIEthernet::server_func(void * eth) {
piMSleep(10);
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);
return;
}
@@ -950,7 +948,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
pAdapter = pAdapter->Next;
}
} else
piCout << "[PIEthernet] GetAdaptersInfo failed with error: " << ret;
piCout << "[PIEthernet] GetAdaptersInfo failed with error:" << ret;
if (pAdapterInfo)
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
#else