20.10.2013 - Modified PIObject - virtual debugName() for macro piCoutObj, improved timer measurements and timers on Windows
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "piethernet.h"
|
||||
#include "piconfig.h"
|
||||
|
||||
|
||||
PIEthernet::PIEthernet(void * data, ReadRetFunc slot): PIIODevice("", ReadWrite) {
|
||||
@@ -102,7 +103,7 @@ bool PIEthernet::init() {
|
||||
sock = socket(AF_INET, st, pr);
|
||||
#endif
|
||||
if (sock == -1) {
|
||||
piCoutObj << "[PIEthernet] Can`t create socket, " << ethErrorString();
|
||||
piCoutObj << "Can`t create socket, " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
#ifndef WINDOWS
|
||||
@@ -146,7 +147,7 @@ bool PIEthernet::openDevice() {
|
||||
tries++;
|
||||
}
|
||||
if (tries == 10) {
|
||||
piCoutObj << "[PIEthernet] Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
opened_ = true;
|
||||
@@ -188,7 +189,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
if (sock == -1) init();
|
||||
if (sock == -1) return false;
|
||||
if (type_ != UDP) {
|
||||
piCoutObj << "[PIEthernet] Only UDP sockets can join multicast groups";
|
||||
piCoutObj << "Only UDP sockets can join multicast groups";
|
||||
return false;
|
||||
}
|
||||
if (!opened_) {
|
||||
@@ -206,14 +207,14 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
//setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char * )&so, sizeof(so));
|
||||
SOCKET ret = WSAJoinLeaf(sock, (sockaddr *)&addr_, sizeof(addr_), NULL, NULL, NULL, NULL, JL_BOTH);
|
||||
if (ret == INVALID_SOCKET) {
|
||||
piCoutObj << "[PIEthernet] Can`t join multicast group " << group << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t join multicast group " << group << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
if (ret != sock) leafs.insert(group, ret);
|
||||
#else
|
||||
# ifndef QNX
|
||||
if (!params[Broadcast])
|
||||
piCoutObj << "[PIEthernet] Warning: \"Broadcast\" parameter not set, \"joinMulticastGroup(\"" << group << "\")\" may be useless!";
|
||||
piCoutObj << "Warning: \"Broadcast\" parameter not set, \"joinMulticastGroup(\"" << group << "\")\" may be useless!";
|
||||
parseAddress(path_, &ip_, &port_);
|
||||
struct ip_mreqn mreq;
|
||||
memset(&mreq, 0, sizeof(mreq));
|
||||
@@ -230,7 +231,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &so, sizeof(so));
|
||||
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq));
|
||||
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
|
||||
piCoutObj << "[PIEthernet] Can`t join multicast group " << group << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t join multicast group " << group << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
# endif
|
||||
@@ -243,7 +244,7 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) {
|
||||
if (sock == -1) init();
|
||||
if (sock == -1) return false;
|
||||
if (type_ != UDP) {
|
||||
piCoutObj << "[PIEthernet] Only UDP sockets can leave multicast groups";
|
||||
piCoutObj << "Only UDP sockets can leave multicast groups";
|
||||
return false;
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
@@ -262,7 +263,7 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) {
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(group.data());
|
||||
mreq.imr_ifindex = 0;
|
||||
if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) {
|
||||
piCoutObj << "[PIEthernet] Can`t leave multicast group " << group << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t leave multicast group " << group << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
@@ -281,10 +282,10 @@ bool PIEthernet::connect() {
|
||||
#ifdef QNX
|
||||
addr_.sin_len = sizeof(addr_);
|
||||
#endif
|
||||
//piCoutObj << "[PIEthernet] connect to " << ip << ":" << port_;
|
||||
//piCoutObj << "connect to " << ip << ":" << port_;
|
||||
connected_ = (::connect(sock, (sockaddr * )&addr_, sizeof(addr_)) == 0);
|
||||
if (!connected_)
|
||||
piCoutObj << "[PIEthernet] Can`t connect to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t connect to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
opened_ = connected_;
|
||||
if (connected_) connected();
|
||||
return connected_;
|
||||
@@ -308,14 +309,14 @@ bool PIEthernet::listen() {
|
||||
tries++;
|
||||
}
|
||||
if (tries == 10) {
|
||||
piCoutObj << "[PIEthernet] Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
if (::listen(sock, 64) == -1) {
|
||||
piCoutObj << "[PIEthernet] Can`t listen on "<< ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
piCoutObj << "Can`t listen on "<< ip_ << ":" << port_ << ", " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
//piCoutObj << "[PIEthernet] listen on " << ip_ << ":" << port_;
|
||||
//piCoutObj << "listen on " << ip_ << ":" << port_;
|
||||
server_thread_.start(server_func);
|
||||
return true;
|
||||
}
|
||||
@@ -328,13 +329,13 @@ int PIEthernet::read(void * read_to, int max_size) {
|
||||
int rs = 0, s = 0;
|
||||
sockaddr_in client_addr;
|
||||
socklen_t slen = sizeof(client_addr);
|
||||
//piCoutObj << "[PIEthernet] read from " << ip_ << ":" << port_ << endl;
|
||||
//piCoutObj << "read from " << ip_ << ":" << port_ << endl;
|
||||
switch (type_) {
|
||||
case TCP_SingleTCP:
|
||||
::listen(sock, 64);
|
||||
s = accept(sock, (sockaddr * )&client_addr, &slen);
|
||||
if (s == -1) {
|
||||
//piCoutObj << "[PIEthernet] Can`t accept new connection, " << ethErrorString();
|
||||
//piCoutObj << "Can`t accept new connection, " << ethErrorString();
|
||||
msleep(1);
|
||||
return -1;
|
||||
}
|
||||
@@ -368,10 +369,10 @@ int PIEthernet::read(void * read_to, int max_size) {
|
||||
int PIEthernet::write(const void * data, int max_size) {
|
||||
if (sock == -1) init();
|
||||
if (sock == -1 || !isWriteable()) {
|
||||
//piCoutObj << "[PIEthernet] Can`t send to uninitialized socket";
|
||||
//piCoutObj << "Can`t send to uninitialized socket";
|
||||
return -1;
|
||||
}
|
||||
//piCoutObj << "[PIEthernet] sending to " << ip_s << ":" << port_s << " " << max_size << " bytes";
|
||||
//piCoutObj << "sending to " << ip_s << ":" << port_s << " " << max_size << " bytes";
|
||||
int ret = 0;
|
||||
switch (type_) {
|
||||
case TCP_SingleTCP:
|
||||
@@ -384,7 +385,7 @@ int PIEthernet::write(const void * data, int max_size) {
|
||||
#endif
|
||||
//piCoutObj << "connect SingleTCP" << ip_s << ":" << port_s << "...";
|
||||
if (::connect(sock, (sockaddr * )&addr_, sizeof(addr_)) != 0) {
|
||||
//piCoutObj << "[PIEthernet] Can`t connect to " << ip_s << ":" << port_s << ", " << ethErrorString();
|
||||
//piCoutObj << "Can`t connect to " << ip_s << ":" << port_s << ", " << ethErrorString();
|
||||
msleep(1);
|
||||
return -1;
|
||||
}
|
||||
@@ -435,6 +436,17 @@ void PIEthernet::server_func(void * eth) {
|
||||
}
|
||||
|
||||
|
||||
bool PIEthernet::configureDevice(const void * e_main, const void * e_parent) {
|
||||
PIConfig::Entry * em = (PIConfig::Entry * )e_main;
|
||||
PIConfig::Entry * ep = (PIConfig::Entry * )e_parent;
|
||||
setReadIP(readDeviceSetting<PIString>("ip", readIP(), em, ep));
|
||||
setReadPort(readDeviceSetting<int>("port", readPort(), em, ep));
|
||||
setParameter(PIEthernet::Broadcast, readDeviceSetting<bool>("broadcast", isParameterSet(PIEthernet::Broadcast), em, ep));
|
||||
setParameter(PIEthernet::ReuseAddress, readDeviceSetting<bool>("reuseAddress", isParameterSet(PIEthernet::ReuseAddress), em, ep));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
#ifdef WINDOWS
|
||||
PIEthernet::InterfaceList il;
|
||||
@@ -544,12 +556,25 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
ci.mac = macFromBytes(PIByteArray(nic.permanent_address, 6));
|
||||
}
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
PIString req = PIString(ifconfigPath) + " " + ci.name + " | grep ether";
|
||||
FILE * fp = popen(req.data(), "r");
|
||||
if (fp != 0) {
|
||||
char in[256];
|
||||
if (fgets(in, 256, fp) != 0) {
|
||||
req = PIString(in).trim();
|
||||
ci.mac = req.cutLeft(req.find(" ") + 1).trim().toUpperCase();
|
||||
}
|
||||
pclose(fp);
|
||||
}
|
||||
# else
|
||||
if (s != -1) {
|
||||
struct ifreq ir;
|
||||
strcpy(ir.ifr_name, ret->ifa_name);
|
||||
if (ioctl(s, SIOCGIFHWADDR, &ir) == 0)
|
||||
ci.mac = macFromBytes(PIByteArray(ir.ifr_hwaddr.sa_data, 6));
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
ci.flags = 0;
|
||||
if (ret->ifa_flags & IFF_UP) ci.flags |= PIEthernet::ifActive;
|
||||
|
||||
Reference in New Issue
Block a user