comms: auto-format

This commit is contained in:
Peter Keresztes Schmidt 2021-04-06 19:19:45 +02:00
parent 3011fc3e18
commit 7dee86b104
2 changed files with 528 additions and 587 deletions

View File

@ -36,7 +36,7 @@
int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) { int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
va_list arg_ptr; va_list arg_ptr;
struct iovec iov[iovcnt]; iovec iov[iovcnt];
va_start(arg_ptr, iovcnt); va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) { for (int i = 0; i < iovcnt; i++) {
@ -46,14 +46,15 @@ int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
va_end(arg_ptr); va_end(arg_ptr);
int nBytes = ::readv(mRd, iov, iovcnt); int nBytes = ::readv(mRd, iov, iovcnt);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno)); Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno));
}
return nBytes; return nBytes;
} }
int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) { int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
va_list arg_ptr; va_list arg_ptr;
struct iovec iov[iovcnt]; iovec iov[iovcnt];
va_start(arg_ptr, iovcnt); va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) { for (int i = 0; i < iovcnt; i++) {
@ -63,8 +64,9 @@ int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
va_end(arg_ptr); va_end(arg_ptr);
ssize_t nBytes = ::writev(mWd, iov, iovcnt); ssize_t nBytes = ::writev(mWd, iov, iovcnt);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno)); Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno));
}
return nBytes; return nBytes;
} }
@ -78,9 +80,13 @@ bool ZM::Pipe::open() {
} }
bool ZM::Pipe::close() { bool ZM::Pipe::close() {
if ( mFd[0] > -1 ) ::close( mFd[0] ); if (mFd[0] > -1) {
::close(mFd[0]);
}
mFd[0] = -1; mFd[0] = -1;
if ( mFd[1] > -1 ) ::close( mFd[1] ); if (mFd[1] > -1) {
::close(mFd[1]);
}
mFd[1] = -1; mFd[1] = -1;
return true; return true;
} }
@ -106,45 +112,42 @@ bool ZM::Pipe::setBlocking(bool blocking) {
return true; return true;
} }
ZM::SockAddr::SockAddr(const struct sockaddr *addr) : mAddr(addr) { ZM::SockAddr *ZM::SockAddr::newSockAddr(const sockaddr &addr, socklen_t len) {
if ((addr.sa_family == AF_INET) && (len == SockAddrInet::addrSize())) {
return new SockAddrInet((const sockaddr_in *) &addr);
} else if ((addr.sa_family == AF_UNIX) && (len == SockAddrUnix::addrSize())) {
return new SockAddrUnix((const sockaddr_un *) &addr);
} }
ZM::SockAddr *ZM::SockAddr::newSockAddr(const struct sockaddr &addr, socklen_t len) {
if ( (addr.sa_family == AF_INET) && (len == SockAddrInet::addrSize()) ) {
return new SockAddrInet((const struct sockaddr_in *)&addr);
} else if ( (addr.sa_family == AF_UNIX) && (len == SockAddrUnix::addrSize()) ) {
return new SockAddrUnix((const struct sockaddr_un *)&addr);
}
Error("Unable to create new SockAddr from addr family %d with size %d", addr.sa_family, len); Error("Unable to create new SockAddr from addr family %d with size %d", addr.sa_family, len);
return nullptr; return nullptr;
} }
ZM::SockAddr *ZM::SockAddr::newSockAddr(const SockAddr *addr) { ZM::SockAddr *ZM::SockAddr::newSockAddr(const SockAddr *addr) {
if ( !addr ) if (!addr) {
return nullptr; return nullptr;
}
if (addr->getDomain() == AF_INET) { if (addr->getDomain() == AF_INET) {
return new SockAddrInet(*(SockAddrInet *) addr); return new SockAddrInet(*(SockAddrInet *) addr);
} else if (addr->getDomain() == AF_UNIX) { } else if (addr->getDomain() == AF_UNIX) {
return new SockAddrUnix(*(SockAddrUnix *) addr); return new SockAddrUnix(*(SockAddrUnix *) addr);
} }
Error("Unable to create new SockAddr from addr family %d", addr->getDomain()); Error("Unable to create new SockAddr from addr family %d", addr->getDomain());
return nullptr; return nullptr;
} }
ZM::SockAddrInet::SockAddrInet() : SockAddr( (struct sockaddr *)&mAddrIn ) {
}
bool ZM::SockAddrInet::resolve(const char *host, const char *serv, const char *proto) { bool ZM::SockAddrInet::resolve(const char *host, const char *serv, const char *proto) {
memset(&mAddrIn, 0, sizeof(mAddrIn)); memset(&mAddrIn, 0, sizeof(mAddrIn));
struct hostent *hostent = nullptr; hostent *hostent = nullptr;
if (!(hostent = ::gethostbyname(host))) { if (!(hostent = ::gethostbyname(host))) {
Error("gethostbyname(%s), h_errno = %d", host, h_errno); Error("gethostbyname(%s), h_errno = %d", host, h_errno);
return false; return false;
} }
struct servent *servent = nullptr; servent *servent = nullptr;
if (!(servent = ::getservbyname(serv, proto))) { if (!(servent = ::getservbyname(serv, proto))) {
Error("getservbyname( %s ), errno = %d, error = %s", serv, errno, strerror(errno)); Error("getservbyname( %s ), errno = %d, error = %s", serv, errno, strerror(errno));
return false; return false;
@ -152,7 +155,7 @@ bool ZM::SockAddrInet::resolve(const char *host, const char *serv, const char *p
mAddrIn.sin_port = servent->s_port; mAddrIn.sin_port = servent->s_port;
mAddrIn.sin_family = AF_INET; mAddrIn.sin_family = AF_INET;
mAddrIn.sin_addr.s_addr = ((struct in_addr *)(hostent->h_addr))->s_addr; mAddrIn.sin_addr.s_addr = ((in_addr *) (hostent->h_addr))->s_addr;
return true; return true;
} }
@ -160,7 +163,7 @@ bool ZM::SockAddrInet::resolve(const char *host, const char *serv, const char *p
bool ZM::SockAddrInet::resolve(const char *host, int port, const char *proto) { bool ZM::SockAddrInet::resolve(const char *host, int port, const char *proto) {
memset(&mAddrIn, 0, sizeof(mAddrIn)); memset(&mAddrIn, 0, sizeof(mAddrIn));
struct hostent *hostent = nullptr; hostent *hostent = nullptr;
if (!(hostent = ::gethostbyname(host))) { if (!(hostent = ::gethostbyname(host))) {
Error("gethostbyname(%s), h_errno = %d", host, h_errno); Error("gethostbyname(%s), h_errno = %d", host, h_errno);
return false; return false;
@ -168,14 +171,14 @@ bool ZM::SockAddrInet::resolve(const char *host, int port, const char *proto) {
mAddrIn.sin_port = htons(port); mAddrIn.sin_port = htons(port);
mAddrIn.sin_family = AF_INET; mAddrIn.sin_family = AF_INET;
mAddrIn.sin_addr.s_addr = ((struct in_addr *)(hostent->h_addr))->s_addr; mAddrIn.sin_addr.s_addr = ((in_addr *) (hostent->h_addr))->s_addr;
return true; return true;
} }
bool ZM::SockAddrInet::resolve(const char *serv, const char *proto) { bool ZM::SockAddrInet::resolve(const char *serv, const char *proto) {
memset(&mAddrIn, 0, sizeof(mAddrIn)); memset(&mAddrIn, 0, sizeof(mAddrIn));
struct servent *servent = nullptr; servent *servent = nullptr;
if (!(servent = ::getservbyname(serv, proto))) { if (!(servent = ::getservbyname(serv, proto))) {
Error("getservbyname(%s), errno = %d, error = %s", serv, errno, strerror(errno)); Error("getservbyname(%s), errno = %d, error = %s", serv, errno, strerror(errno));
return false; return false;
@ -198,9 +201,6 @@ bool ZM::SockAddrInet::resolve(int port, const char *proto) {
return true; return true;
} }
ZM::SockAddrUnix::SockAddrUnix() : SockAddr((struct sockaddr *)&mAddrUn ) {
}
bool ZM::SockAddrUnix::resolve(const char *path, const char *proto) { bool ZM::SockAddrUnix::resolve(const char *path, const char *proto) {
memset(&mAddrUn, 0, sizeof(mAddrUn)); memset(&mAddrUn, 0, sizeof(mAddrUn));
@ -211,8 +211,9 @@ bool ZM::SockAddrUnix::resolve(const char *path, const char *proto) {
} }
bool ZM::Socket::socket() { bool ZM::Socket::socket() {
if ( mSd >= 0 ) if (mSd >= 0) {
return true; return true;
}
if ((mSd = ::socket(getDomain(), getType(), 0)) < 0) { if ((mSd = ::socket(getDomain(), getType(), 0)) < 0) {
Error("socket(), errno = %d, error = %s", errno, strerror(errno)); Error("socket(), errno = %d, error = %s", errno, strerror(errno));
@ -221,17 +222,17 @@ bool ZM::Socket::socket() {
int val = 1; int val = 1;
(void)::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); ::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
(void)::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); ::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
mState = DISCONNECTED; mState = DISCONNECTED;
return true; return true;
} }
bool ZM::Socket::connect() { bool ZM::Socket::connect() {
if ( !socket() ) if (!socket()) {
return false; return false;
}
if (::connect(mSd, mRemoteAddr->getAddr(), getAddrSize()) == -1) { if (::connect(mSd, mRemoteAddr->getAddr(), getAddrSize()) == -1) {
Error("connect(), errno = %d, error = %s", errno, strerror(errno)); Error("connect(), errno = %d, error = %s", errno, strerror(errno));
@ -240,13 +241,13 @@ bool ZM::Socket::connect() {
} }
mState = CONNECTED; mState = CONNECTED;
return true; return true;
} }
bool ZM::Socket::bind() { bool ZM::Socket::bind() {
if ( !socket() ) if (!socket()) {
return false; return false;
}
if (::bind(mSd, mLocalAddr->getAddr(), getAddrSize()) == -1) { if (::bind(mSd, mLocalAddr->getAddr(), getAddrSize()) == -1) {
Error("bind(), errno = %d, error = %s", errno, strerror(errno)); Error("bind(), errno = %d, error = %s", errno, strerror(errno));
@ -268,7 +269,7 @@ bool ZM::Socket::listen() {
} }
bool ZM::Socket::accept() { bool ZM::Socket::accept() {
struct sockaddr *rem_addr = mLocalAddr->getTempAddr(); sockaddr *rem_addr = mLocalAddr->getTempAddr();
socklen_t rem_addr_size = getAddrSize(); socklen_t rem_addr_size = getAddrSize();
int newSd = -1; int newSd = -1;
@ -282,12 +283,11 @@ bool ZM::Socket::accept() {
mSd = newSd; mSd = newSd;
mState = CONNECTED; mState = CONNECTED;
return true; return true;
} }
bool ZM::Socket::accept(int &newSd) { bool ZM::Socket::accept(int &newSd) {
struct sockaddr *rem_addr = mLocalAddr->getTempAddr(); sockaddr *rem_addr = mLocalAddr->getTempAddr();
socklen_t rem_addr_size = getAddrSize(); socklen_t rem_addr_size = getAddrSize();
newSd = -1; newSd = -1;
@ -301,7 +301,10 @@ bool ZM::Socket::accept(int &newSd) {
} }
bool ZM::Socket::close() { bool ZM::Socket::close() {
if ( mSd > -1 ) ::close(mSd); if (mSd > -1) {
::close(mSd);
}
mSd = -1; mSd = -1;
mState = CLOSED; mState = CLOSED;
return true; return true;
@ -425,13 +428,13 @@ bool ZM::Socket::setNoDelay(bool nodelay) {
} }
bool ZM::InetSocket::connect(const char *host, const char *serv) { bool ZM::InetSocket::connect(const char *host, const char *serv) {
struct addrinfo hints; addrinfo hints;
struct addrinfo *result, *rp; addrinfo *result, *rp;
int s; int s;
char buf[255]; char buf[255];
mAddressFamily = AF_UNSPEC; mAddressFamily = AF_UNSPEC;
memset(&hints, 0, sizeof(struct addrinfo)); memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = getType(); hints.ai_socktype = getType();
hints.ai_flags = 0; hints.ai_flags = 0;
@ -450,34 +453,39 @@ bool ZM::InetSocket::connect(const char *host, const char *serv) {
for (rp = result; rp != nullptr; rp = rp->ai_next) { for (rp = result; rp != nullptr; rp = rp->ai_next) {
if (mSd != -1) { if (mSd != -1) {
if ( ::connect(mSd, rp->ai_addr, rp->ai_addrlen) != -1 ) if (::connect(mSd, rp->ai_addr, rp->ai_addrlen) != -1) {
break; /* Success */ break; /* Success */
}
continue; continue;
} }
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));
if (rp->ai_family == AF_INET) { if (rp->ai_family == AF_INET) {
inet_ntop(AF_INET, &((struct sockaddr_in *)rp->ai_addr)->sin_addr, buf, sizeof(buf)-1); inet_ntop(AF_INET, &((sockaddr_in *) rp->ai_addr)->sin_addr, buf, sizeof(buf) - 1);
} else if (rp->ai_family == AF_INET6) { } else if (rp->ai_family == AF_INET6) {
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr, buf, sizeof(buf)-1); inet_ntop(AF_INET6, &((sockaddr_in6 *) rp->ai_addr)->sin6_addr, buf, sizeof(buf) - 1);
} else { } else {
strncpy(buf, "n/a", sizeof(buf) - 1); strncpy(buf, "n/a", sizeof(buf) - 1);
} }
Debug(1, "connect(): Trying '%s', family '%d', proto '%d'", buf, rp->ai_family, rp->ai_protocol); Debug(1, "connect(): Trying '%s', family '%d', proto '%d'", buf, rp->ai_family, rp->ai_protocol);
mSd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); mSd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if ( mSd == -1 ) if (mSd == -1) {
continue; continue;
}
int val = 1; int val = 1;
(void)::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); ::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
(void)::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); ::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
mAddressFamily = rp->ai_family; /* save AF_ for ctrl and data connections */ mAddressFamily = rp->ai_family; /* save AF_ for ctrl and data connections */
if ( ::connect(mSd, rp->ai_addr, rp->ai_addrlen) != -1 ) if (::connect(mSd, rp->ai_addr, rp->ai_addrlen) != -1) {
break; /* Success */ break; /* Success */
}
::close(mSd); ::close(mSd);
} // end for }
freeaddrinfo(result); /* No longer needed */ freeaddrinfo(result); /* No longer needed */
@ -488,7 +496,6 @@ bool ZM::InetSocket::connect(const char *host, const char *serv) {
} }
mState = CONNECTED; mState = CONNECTED;
return true; return true;
} }
@ -500,9 +507,9 @@ bool ZM::InetSocket::connect(const char *host, int port) {
} }
bool ZM::InetSocket::bind(const char *host, const char *serv) { bool ZM::InetSocket::bind(const char *host, const char *serv) {
struct addrinfo hints; addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo)); memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = getType(); hints.ai_socktype = getType();
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
@ -511,7 +518,7 @@ bool ZM::InetSocket::bind(const char * host, const char * serv) {
hints.ai_addr = nullptr; hints.ai_addr = nullptr;
hints.ai_next = nullptr; hints.ai_next = nullptr;
struct addrinfo *result, *rp; addrinfo *result, *rp;
int s = getaddrinfo(host, serv, &hints, &result); int s = getaddrinfo(host, serv, &hints, &result);
if (s != 0) { if (s != 0) {
Error("bind(): getaddrinfo: %s", gai_strerror(s)); Error("bind(): getaddrinfo: %s", gai_strerror(s));
@ -526,24 +533,27 @@ bool ZM::InetSocket::bind(const char * host, const char * serv) {
for (rp = result; rp != nullptr; rp = rp->ai_next) { for (rp = result; rp != nullptr; rp = rp->ai_next) {
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));
if (rp->ai_family == AF_INET) { if (rp->ai_family == AF_INET) {
inet_ntop(AF_INET, &((struct sockaddr_in *)rp->ai_addr)->sin_addr, buf, sizeof(buf)-1); inet_ntop(AF_INET, &((sockaddr_in *) rp->ai_addr)->sin_addr, buf, sizeof(buf) - 1);
} else if (rp->ai_family == AF_INET6) { } else if (rp->ai_family == AF_INET6) {
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr, buf, sizeof(buf)-1); inet_ntop(AF_INET6, &((sockaddr_in6 *) rp->ai_addr)->sin6_addr, buf, sizeof(buf) - 1);
} else { } else {
strncpy(buf, "n/a", sizeof(buf) - 1); strncpy(buf, "n/a", sizeof(buf) - 1);
} }
Debug(1, "bind(): Trying '%s', family '%d', proto '%d'", buf, rp->ai_family, rp->ai_protocol); Debug(1, "bind(): Trying '%s', family '%d', proto '%d'", buf, rp->ai_family, rp->ai_protocol);
mSd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); mSd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if ( mSd == -1 ) if (mSd == -1) {
continue; continue;
}
mState = DISCONNECTED; mState = DISCONNECTED;
if ( ::bind(mSd, rp->ai_addr, rp->ai_addrlen) == 0 ) if (::bind(mSd, rp->ai_addr, rp->ai_addrlen) == 0) {
break; /* Success */ break; /* Success */
}
::close(mSd); ::close(mSd);
mSd = -1; mSd = -1;
} // end foreach result }
if (rp == nullptr) { /* No address succeeded */ if (rp == nullptr) { /* No address succeeded */
Error("bind(), Could not bind"); Error("bind(), Could not bind");
@ -584,11 +594,11 @@ bool ZM::TcpInetServer::accept(TcpInetSocket *&newSocket) {
int newSd = -1; int newSd = -1;
newSocket = nullptr; newSocket = nullptr;
if ( !Socket::accept(newSd) ) if (!Socket::accept(newSd)) {
return false; return false;
}
newSocket = new TcpInetSocket(*this, newSd); newSocket = new TcpInetSocket(*this, newSd);
return true; return true;
} }
@ -596,29 +606,14 @@ bool ZM::TcpUnixServer::accept(TcpUnixSocket *&newSocket) {
int newSd = -1; int newSd = -1;
newSocket = nullptr; newSocket = nullptr;
if ( !Socket::accept(newSd) ) if (!Socket::accept(newSd)) {
return false; return false;
}
newSocket = new TcpUnixSocket(*this, newSd); newSocket = new TcpUnixSocket(*this, newSd);
return true; return true;
} }
ZM::Select::Select() : mHasTimeout(false), mMaxFd(-1) {
}
ZM::Select::Select(struct timeval timeout) : mMaxFd(-1) {
setTimeout(timeout);
}
ZM::Select::Select(int timeout) : mMaxFd(-1) {
setTimeout(timeout);
}
ZM::Select::Select(double timeout) : mMaxFd(-1) {
setTimeout(timeout);
}
void ZM::Select::setTimeout(int timeout) { void ZM::Select::setTimeout(int timeout) {
mTimeout.tv_sec = timeout; mTimeout.tv_sec = timeout;
mTimeout.tv_usec = 0; mTimeout.tv_usec = 0;
@ -631,7 +626,7 @@ void ZM::Select::setTimeout(double timeout) {
mHasTimeout = true; mHasTimeout = true;
} }
void ZM::Select::setTimeout(struct timeval timeout) { void ZM::Select::setTimeout(timeval timeout) {
mTimeout = timeout; mTimeout = timeout;
mHasTimeout = true; mHasTimeout = true;
} }
@ -659,9 +654,10 @@ bool ZM::Select::addReader(CommsBase *comms) {
} }
std::pair<CommsSet::iterator, bool> result = mReaders.insert(comms); std::pair<CommsSet::iterator, bool> result = mReaders.insert(comms);
if (result.second) { if (result.second) {
if ( comms->getMaxDesc() > mMaxFd ) if (comms->getMaxDesc() > mMaxFd) {
mMaxFd = comms->getMaxDesc(); mMaxFd = comms->getMaxDesc();
} }
}
return result.second; return result.second;
} }
@ -685,9 +681,10 @@ void ZM::Select::clearReaders() {
bool ZM::Select::addWriter(CommsBase *comms) { bool ZM::Select::addWriter(CommsBase *comms) {
std::pair<CommsSet::iterator, bool> result = mWriters.insert(comms); std::pair<CommsSet::iterator, bool> result = mWriters.insert(comms);
if (result.second) { if (result.second) {
if ( comms->getMaxDesc() > mMaxFd ) if (comms->getMaxDesc() > mMaxFd) {
mMaxFd = comms->getMaxDesc(); mMaxFd = comms->getMaxDesc();
} }
}
return result.second; return result.second;
} }
@ -705,21 +702,23 @@ void ZM::Select::clearWriters() {
} }
int ZM::Select::wait() { int ZM::Select::wait() {
struct timeval tempTimeout = mTimeout; timeval tempTimeout = mTimeout;
struct timeval *selectTimeout = mHasTimeout?&tempTimeout:nullptr; timeval *selectTimeout = mHasTimeout ? &tempTimeout : nullptr;
fd_set rfds; fd_set rfds;
fd_set wfds; fd_set wfds;
mReadable.clear(); mReadable.clear();
FD_ZERO(&rfds); FD_ZERO(&rfds);
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter ) for (CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter) {
FD_SET((*iter)->getReadDesc(), &rfds); FD_SET((*iter)->getReadDesc(), &rfds);
}
mWriteable.clear(); mWriteable.clear();
FD_ZERO(&wfds); FD_ZERO(&wfds);
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter ) for (CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter) {
FD_SET((*iter)->getWriteDesc(), &wfds); FD_SET((*iter)->getWriteDesc(), &wfds);
}
int nFound = select(mMaxFd + 1, &rfds, &wfds, nullptr, selectTimeout); int nFound = select(mMaxFd + 1, &rfds, &wfds, nullptr, selectTimeout);
if (nFound == 0) { if (nFound == 0) {
@ -727,20 +726,16 @@ int ZM::Select::wait() {
} else if (nFound < 0) { } else if (nFound < 0) {
Error("Select error: %s", strerror(errno)); Error("Select error: %s", strerror(errno));
} else { } else {
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter ) for (CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter) {
if ( FD_ISSET((*iter)->getReadDesc(), &rfds) ) if (FD_ISSET((*iter)->getReadDesc(), &rfds)) {
mReadable.push_back(*iter); mReadable.push_back(*iter);
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter ) }
if ( FD_ISSET((*iter)->getWriteDesc(), &rfds) ) }
for (CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter) {
if (FD_ISSET((*iter)->getWriteDesc(), &rfds)) {
mWriteable.push_back(*iter); mWriteable.push_back(*iter);
} }
}
}
return nFound; return nFound;
} }
const ZM::Select::CommsList &ZM::Select::getReadable() const {
return mReadable;
}
const ZM::Select::CommsList &ZM::Select::getWriteable() const {
return mWriteable;
}

View File

@ -44,14 +44,8 @@ public:
class CommsBase { class CommsBase {
protected: protected:
const int &mRd; CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) {}
const int &mWd; virtual ~CommsBase() = default;
protected:
CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) {
}
virtual ~CommsBase() {
}
public: public:
virtual bool close() = 0; virtual bool close() = 0;
@ -60,177 +54,153 @@ public:
virtual bool setBlocking(bool blocking) = 0; virtual bool setBlocking(bool blocking) = 0;
public: public:
int getReadDesc() const { int getReadDesc() const { return mRd; }
return mRd; int getWriteDesc() const { return mWd; }
} int getMaxDesc() const { return mRd > mWd ? mRd : mWd; }
int getWriteDesc() const {
return mWd;
}
int getMaxDesc() const {
return( mRd>mWd?mRd:mWd );
}
virtual int read(void *msg, int len) { virtual int read(void *msg, int len) {
ssize_t nBytes = ::read(mRd, msg, len); ssize_t nBytes = ::read(mRd, msg, len);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Read of %d bytes max on rd %d failed: %s", len, mRd, strerror(errno)); Debug(1, "Read of %d bytes max on rd %d failed: %s", len, mRd, strerror(errno));
return( nBytes );
} }
return nBytes;
}
virtual int write(const void *msg, int len) { virtual int write(const void *msg, int len) {
ssize_t nBytes = ::write(mWd, msg, len); ssize_t nBytes = ::write(mWd, msg, len);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Write of %d bytes on wd %d failed: %s", len, mWd, strerror(errno)); Debug(1, "Write of %d bytes on wd %d failed: %s", len, mWd, strerror(errno));
return( nBytes );
} }
virtual int readV( const struct iovec *iov, int iovcnt ) { return nBytes;
}
virtual int readV(const iovec *iov, int iovcnt) {
int nBytes = ::readv(mRd, iov, iovcnt); int nBytes = ::readv(mRd, iov, iovcnt);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno)); Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno));
return( nBytes );
} }
virtual int writeV( const struct iovec *iov, int iovcnt ) { return nBytes;
}
virtual int writeV(const iovec *iov, int iovcnt) {
ssize_t nBytes = ::writev(mWd, iov, iovcnt); ssize_t nBytes = ::writev(mWd, iov, iovcnt);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno)); Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno));
return( nBytes );
} }
return nBytes;
}
virtual int readV(int iovcnt, /* const void *msg1, int len1, */ ...); virtual int readV(int iovcnt, /* const void *msg1, int len1, */ ...);
virtual int writeV(int iovcnt, /* const void *msg1, int len1, */ ...); virtual int writeV(int iovcnt, /* const void *msg1, int len1, */ ...);
protected:
const int &mRd;
const int &mWd;
}; };
class Pipe : public CommsBase { class Pipe : public CommsBase {
protected:
int mFd[2];
public: public:
Pipe() : CommsBase(mFd[0], mFd[1]) { Pipe() : CommsBase(mFd[0], mFd[1]) {
mFd[0] = -1; mFd[0] = -1;
mFd[1] = -1; mFd[1] = -1;
} }
~Pipe() {
close();
}
public: ~Pipe() { close(); }
bool open(); bool open();
bool close(); bool close();
bool isOpen() const bool isOpen() const { return mFd[0] != -1 && mFd[1] != -1; }
{ int getReadDesc() const { return mFd[0]; }
return( mFd[0] != -1 && mFd[1] != -1 ); int getWriteDesc() const { return mFd[1]; }
}
int getReadDesc() const
{
return( mFd[0] );
}
int getWriteDesc() const
{
return( mFd[1] );
}
bool setBlocking(bool blocking); bool setBlocking(bool blocking);
protected:
int mFd[2];
}; };
class SockAddr { class SockAddr {
private:
const struct sockaddr *mAddr;
public: public:
explicit SockAddr( const struct sockaddr *addr ); explicit SockAddr(const sockaddr *addr) : mAddr(addr) {}
virtual ~SockAddr() { virtual ~SockAddr() = default;
}
static SockAddr *newSockAddr( const struct sockaddr &addr, socklen_t len ); static SockAddr *newSockAddr(const sockaddr &addr, socklen_t len);
static SockAddr *newSockAddr(const SockAddr *addr); static SockAddr *newSockAddr(const SockAddr *addr);
int getDomain() const { int getDomain() const { return mAddr ? mAddr->sa_family : AF_UNSPEC; }
return( mAddr?mAddr->sa_family:AF_UNSPEC ); const sockaddr *getAddr() const { return mAddr; }
}
const struct sockaddr *getAddr() const {
return( mAddr );
}
virtual socklen_t getAddrSize() const = 0; virtual socklen_t getAddrSize() const = 0;
virtual struct sockaddr *getTempAddr() const=0; virtual sockaddr *getTempAddr() const = 0;
private:
const sockaddr *mAddr;
}; };
class SockAddrInet : public SockAddr { class SockAddrInet : public SockAddr {
private:
struct sockaddr_in mAddrIn;
struct sockaddr_in mTempAddrIn;
public: public:
SockAddrInet(); SockAddrInet() : SockAddr((sockaddr *) &mAddrIn) {}
explicit SockAddrInet( const SockAddrInet &addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( addr.mAddrIn ) { explicit SockAddrInet(const SockAddrInet &addr)
} : SockAddr((const sockaddr *) &mAddrIn), mAddrIn(addr.mAddrIn) {}
explicit SockAddrInet( const struct sockaddr_in *addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( *addr ) { explicit SockAddrInet(const sockaddr_in *addr)
} : SockAddr((const sockaddr *) &mAddrIn), mAddrIn(*addr) {}
bool resolve(const char *host, const char *serv, const char *proto); bool resolve(const char *host, const char *serv, const char *proto);
bool resolve(const char *host, int port, const char *proto); bool resolve(const char *host, int port, const char *proto);
bool resolve(const char *serv, const char *proto); bool resolve(const char *serv, const char *proto);
bool resolve(int port, const char *proto); bool resolve(int port, const char *proto);
socklen_t getAddrSize() const { socklen_t getAddrSize() const { return sizeof(mAddrIn); }
return( sizeof(mAddrIn) ); sockaddr *getTempAddr() const { return (sockaddr *) &mTempAddrIn; }
}
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrIn );
}
public: static socklen_t addrSize() { return sizeof(sockaddr_in); }
static socklen_t addrSize() {
return( sizeof(sockaddr_in) ); private:
} sockaddr_in mAddrIn;
sockaddr_in mTempAddrIn;
}; };
class SockAddrUnix : public SockAddr { class SockAddrUnix : public SockAddr {
private:
struct sockaddr_un mAddrUn;
struct sockaddr_un mTempAddrUn;
public: public:
SockAddrUnix(); SockAddrUnix() : SockAddr((sockaddr *) &mAddrUn) {}
SockAddrUnix( const SockAddrUnix &addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( addr.mAddrUn ) { SockAddrUnix(const SockAddrUnix &addr)
} : SockAddr((const sockaddr *) &mAddrUn), mAddrUn(addr.mAddrUn) {}
explicit SockAddrUnix( const struct sockaddr_un *addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( *addr ) { explicit SockAddrUnix(const sockaddr_un *addr)
} : SockAddr((const sockaddr *) &mAddrUn), mAddrUn(*addr) {}
bool resolve(const char *path, const char *proto); bool resolve(const char *path, const char *proto);
socklen_t getAddrSize() const { socklen_t getAddrSize() const { return sizeof(mAddrUn); }
return( sizeof(mAddrUn) ); sockaddr *getTempAddr() const { return (sockaddr *) &mTempAddrUn; }
}
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrUn );
}
public: static socklen_t addrSize() { return sizeof(sockaddr_un); }
static socklen_t addrSize() {
return( sizeof(sockaddr_un) ); private:
} sockaddr_un mAddrUn;
sockaddr_un mTempAddrUn;
}; };
class Socket : public CommsBase { class Socket : public CommsBase {
protected: protected:
typedef enum { CLOSED, DISCONNECTED, LISTENING, CONNECTED } State; enum State { CLOSED, DISCONNECTED, LISTENING, CONNECTED };
protected: Socket() : CommsBase(mSd, mSd),
int mSd; mSd(-1),
State mState; mState(CLOSED),
SockAddr *mLocalAddr; mLocalAddr(nullptr),
SockAddr *mRemoteAddr; mRemoteAddr(nullptr) {}
Socket(const Socket &socket, int newSd) : CommsBase(mSd, mSd),
protected: mSd(newSd),
Socket() : CommsBase( mSd, mSd ), mSd( -1 ), mState( CLOSED ), mLocalAddr( 0 ), mRemoteAddr( 0 ) { mState(CONNECTED),
} mLocalAddr(nullptr),
Socket( const Socket &socket, int newSd ) : CommsBase( mSd, mSd ), mSd( newSd ), mState( CONNECTED ), mLocalAddr( 0 ), mRemoteAddr( 0 ) { mRemoteAddr(nullptr) {
if (socket.mLocalAddr) if (socket.mLocalAddr)
mLocalAddr = SockAddr::newSockAddr(mLocalAddr); mLocalAddr = SockAddr::newSockAddr(mLocalAddr);
if (socket.mRemoteAddr) if (socket.mRemoteAddr)
mRemoteAddr = SockAddr::newSockAddr(mRemoteAddr); mRemoteAddr = SockAddr::newSockAddr(mRemoteAddr);
} }
virtual ~Socket() { virtual ~Socket() {
close(); close();
delete mLocalAddr; delete mLocalAddr;
@ -238,81 +208,67 @@ protected:
} }
public: public:
bool isOpen() const { bool isOpen() const { return !isClosed(); }
return( !isClosed() ); bool isClosed() const { return mState == CLOSED; }
} bool isDisconnected() const { return mState == DISCONNECTED; }
bool isClosed() const { bool isConnected() const { return mState == CONNECTED; }
return( mState == CLOSED );
}
bool isDisconnected() const {
return( mState == DISCONNECTED );
}
bool isConnected() const {
return( mState == CONNECTED );
}
virtual bool close(); virtual bool close();
protected:
bool isListening() const {
return( mState == LISTENING );
}
protected:
virtual bool socket();
virtual bool bind();
protected:
virtual bool connect();
virtual bool listen();
virtual bool accept();
virtual bool accept( int & );
public:
virtual int send(const void *msg, int len) const { virtual int send(const void *msg, int len) const {
ssize_t nBytes = ::send(mSd, msg, len, 0); ssize_t nBytes = ::send(mSd, msg, len, 0);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Send of %d bytes on sd %d failed: %s", len, mSd, strerror(errno)); Debug(1, "Send of %d bytes on sd %d failed: %s", len, mSd, strerror(errno));
return( nBytes );
} }
return nBytes;
}
virtual int recv(void *msg, int len) const { virtual int recv(void *msg, int len) const {
ssize_t nBytes = ::recv(mSd, msg, len, 0); ssize_t nBytes = ::recv(mSd, msg, len, 0);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Recv of %d bytes max on sd %d failed: %s", len, mSd, strerror(errno)); Debug(1, "Recv of %d bytes max on sd %d failed: %s", len, mSd, strerror(errno));
return( nBytes );
} }
return nBytes;
}
virtual int send(const std::string &msg) const { virtual int send(const std::string &msg) const {
ssize_t nBytes = ::send(mSd, msg.data(), msg.size(), 0); ssize_t nBytes = ::send(mSd, msg.data(), msg.size(), 0);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug( 1, "Send of string '%s' (%zd bytes) on sd %d failed: %s", msg.c_str(), msg.size(), mSd, strerror(errno) ); Debug(1, "Send of string '%s' (%zd bytes) on sd %d failed: %s",
return( nBytes ); msg.c_str(),
msg.size(),
mSd,
strerror(errno));
} }
return nBytes;
}
virtual int recv(std::string &msg) const { virtual int recv(std::string &msg) const {
char buffer[msg.capacity()]; char buffer[msg.capacity()];
int nBytes = 0; int nBytes = 0;
if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) { if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) {
Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", sizeof(buffer), mSd, strerror(errno)); Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", sizeof(buffer), mSd, strerror(errno));
return( nBytes ); return nBytes;
} }
buffer[nBytes] = '\0'; buffer[nBytes] = '\0';
msg = buffer; msg = buffer;
return( nBytes ); return nBytes;
} }
virtual int recv(std::string &msg, size_t maxLen) const { virtual int recv(std::string &msg, size_t maxLen) const {
char buffer[maxLen]; char buffer[maxLen];
int nBytes = 0; int nBytes = 0;
if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) { if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) {
Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", maxLen, mSd, strerror(errno)); Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", maxLen, mSd, strerror(errno));
return( nBytes ); return nBytes;
} }
buffer[nBytes] = '\0'; buffer[nBytes] = '\0';
msg = buffer; msg = buffer;
return( nBytes ); return nBytes;
} }
virtual int bytesToRead() const; virtual int bytesToRead() const;
int getDesc() const { int getDesc() const { return mSd; }
return( mSd );
}
//virtual bool isOpen() const //virtual bool isOpen() const
//{ //{
//return( mSd != -1 ); //return( mSd != -1 );
@ -322,12 +278,8 @@ public:
virtual int getType() const = 0; virtual int getType() const = 0;
virtual const char *getProtocol() const = 0; virtual const char *getProtocol() const = 0;
const SockAddr *getLocalAddr() const { const SockAddr *getLocalAddr() const { return mLocalAddr; }
return( mLocalAddr ); const SockAddr *getRemoteAddr() const { return mRemoteAddr; }
}
const SockAddr *getRemoteAddr() const {
return( mRemoteAddr );
}
virtual socklen_t getAddrSize() const = 0; virtual socklen_t getAddrSize() const = 0;
bool getBlocking(bool &blocking); bool getBlocking(bool &blocking);
@ -344,20 +296,28 @@ public:
bool getNoDelay(bool &) const; bool getNoDelay(bool &) const;
bool setNoDelay(bool); bool setNoDelay(bool);
protected:
bool isListening() const { return mState == LISTENING; }
virtual bool socket();
virtual bool bind();
virtual bool connect();
virtual bool listen();
virtual bool accept();
virtual bool accept(int &);
int mSd;
State mState;
SockAddr *mLocalAddr;
SockAddr *mRemoteAddr;
}; };
class InetSocket : virtual public Socket class InetSocket : virtual public Socket {
{
protected:
int mAddressFamily;
public: public:
int getDomain() const { int getDomain() const { return mAddressFamily; }
return( mAddressFamily ); virtual socklen_t getAddrSize() const { return SockAddrInet::addrSize(); }
}
virtual socklen_t getAddrSize() const {
return( SockAddrInet::addrSize() );
}
protected: protected:
bool connect(const char *host, const char *serv); bool connect(const char *host, const char *serv);
@ -367,227 +327,217 @@ protected:
bool bind(const char *host, int port); bool bind(const char *host, int port);
bool bind(const char *serv); bool bind(const char *serv);
bool bind(int port); bool bind(int port);
int mAddressFamily;
}; };
class UnixSocket : virtual public Socket { class UnixSocket : virtual public Socket {
public: public:
int getDomain() const { int getDomain() const { return AF_UNIX; }
return( AF_UNIX ); virtual socklen_t getAddrSize() const { return SockAddrUnix::addrSize(); }
}
virtual socklen_t getAddrSize() const {
return( SockAddrUnix::addrSize() );
}
protected: protected:
bool resolveLocal(const char *serv, const char *proto) { bool resolveLocal(const char *serv, const char *proto) {
SockAddrUnix *addr = new SockAddrUnix; SockAddrUnix *addr = new SockAddrUnix;
mLocalAddr = addr; mLocalAddr = addr;
return( addr->resolve( serv, proto ) ); return addr->resolve(serv, proto);
} }
bool resolveRemote(const char *path, const char *proto) { bool resolveRemote(const char *path, const char *proto) {
SockAddrUnix *addr = new SockAddrUnix; SockAddrUnix *addr = new SockAddrUnix;
mRemoteAddr = addr; mRemoteAddr = addr;
return( addr->resolve( path, proto ) ); return addr->resolve(path, proto);
} }
protected:
bool bind(const char *path) { bool bind(const char *path) {
if (!UnixSocket::resolveLocal(path, getProtocol())) if (!UnixSocket::resolveLocal(path, getProtocol()))
return( false ); return false;
return( Socket::bind() ); return Socket::bind();
} }
bool connect(const char *path) { bool connect(const char *path) {
if (!UnixSocket::resolveRemote(path, getProtocol())) if (!UnixSocket::resolveRemote(path, getProtocol()))
return( false ); return false;
return( Socket::connect() ); return Socket::connect();
} }
}; };
class UdpSocket : virtual public Socket { class UdpSocket : virtual public Socket {
public: public:
int getType() const { int getType() const { return SOCK_DGRAM; }
return( SOCK_DGRAM ); const char *getProtocol() const { return "udp"; }
}
const char *getProtocol() const {
return( "udp" );
}
public:
virtual int sendto(const void *msg, int len, const SockAddr *addr = nullptr) const { virtual int sendto(const void *msg, int len, const SockAddr *addr = nullptr) const {
ssize_t nBytes = ::sendto(mSd, msg, len, 0, addr ? addr->getAddr() : nullptr, addr ? addr->getAddrSize() : 0); ssize_t nBytes = ::sendto(mSd, msg, len, 0, addr ? addr->getAddr() : nullptr, addr ? addr->getAddrSize() : 0);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Sendto of %d bytes on sd %d failed: %s", len, mSd, strerror(errno)); Debug(1, "Sendto of %d bytes on sd %d failed: %s", len, mSd, strerror(errno));
return( nBytes );
} }
virtual int recvfrom( void *msg, int len, SockAddr *addr=0 ) const { return nBytes;
}
virtual int recvfrom(void *msg, int len, SockAddr *addr = nullptr) const {
ssize_t nBytes = 0; ssize_t nBytes = 0;
if (addr) { if (addr) {
struct sockaddr sockAddr; sockaddr sockAddr;
socklen_t sockLen; socklen_t sockLen;
nBytes = ::recvfrom(mSd, msg, len, 0, &sockAddr, &sockLen); nBytes = ::recvfrom(mSd, msg, len, 0, &sockAddr, &sockLen);
if (nBytes < 0) { if (nBytes < 0) {
Debug(1, "Recvfrom of %d bytes max on sd %d (with address) failed: %s", len, mSd, strerror(errno)); Debug(1, "Recvfrom of %d bytes max on sd %d (with address) failed: %s", len, mSd, strerror(errno));
} }
} else { } else {
nBytes = ::recvfrom( mSd, msg, len, 0, nullptr, 0 ); nBytes = ::recvfrom(mSd, msg, len, 0, nullptr, nullptr);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug(1, "Recvfrom of %d bytes max on sd %d (no address) failed: %s", len, mSd, strerror(errno)); Debug(1, "Recvfrom of %d bytes max on sd %d (no address) failed: %s", len, mSd, strerror(errno));
} }
return( nBytes ); }
return nBytes;
} }
}; };
class UdpInetSocket : virtual public UdpSocket, virtual public InetSocket { class UdpInetSocket : virtual public UdpSocket, virtual public InetSocket {
public: public:
bool bind(const char *host, const char *serv) { bool bind(const char *host, const char *serv) {
return( InetSocket::bind( host, serv ) ); return InetSocket::bind(host, serv);
} }
bool bind(const char *host, int port) { bool bind(const char *host, int port) {
return( InetSocket::bind( host, port ) ); return InetSocket::bind(host, port);
} }
bool bind(const char *serv) { bool bind(const char *serv) {
return( InetSocket::bind( serv ) ); return InetSocket::bind(serv);
} }
bool bind(int port) { bool bind(int port) {
return( InetSocket::bind( port ) ); return InetSocket::bind(port);
} }
bool connect(const char *host, const char *serv) { bool connect(const char *host, const char *serv) {
return( InetSocket::connect( host, serv ) ); return InetSocket::connect(host, serv);
} }
bool connect(const char *host, int port) { bool connect(const char *host, int port) {
return( InetSocket::connect( host, port ) ); return InetSocket::connect(host, port);
} }
}; };
class UdpUnixSocket : virtual public UdpSocket, virtual public UnixSocket { class UdpUnixSocket : virtual public UdpSocket, virtual public UnixSocket {
public: public:
bool bind(const char *path) { bool bind(const char *path) {
return( UnixSocket::bind( path ) ); return UnixSocket::bind(path);
} }
bool connect(const char *path) { bool connect(const char *path) {
return( UnixSocket::connect( path ) ); return UnixSocket::connect(path);
} }
}; };
class UdpInetClient : public UdpInetSocket { class UdpInetClient : public UdpInetSocket {
public: public:
bool connect(const char *host, const char *serv) { bool connect(const char *host, const char *serv) {
return( UdpInetSocket::connect( host, serv ) ); return UdpInetSocket::connect(host, serv);
} }
bool connect(const char *host, int port) { bool connect(const char *host, int port) {
return( UdpInetSocket::connect( host, port ) ); return UdpInetSocket::connect(host, port);
} }
}; };
class UdpUnixClient : public UdpUnixSocket { class UdpUnixClient : public UdpUnixSocket {
public: public:
bool bind(const char *path) { bool bind(const char *path) {
return( UdpUnixSocket::bind( path ) ); return UdpUnixSocket::bind(path);
} }
public:
bool connect(const char *path) { bool connect(const char *path) {
return( UdpUnixSocket::connect( path) ); return UdpUnixSocket::connect(path);
} }
}; };
class UdpInetServer : public UdpInetSocket { class UdpInetServer : public UdpInetSocket {
public: public:
bool bind(const char *host, const char *serv) { bool bind(const char *host, const char *serv) {
return( UdpInetSocket::bind( host, serv ) ); return UdpInetSocket::bind(host, serv);
} }
bool bind(const char *host, int port) { bool bind(const char *host, int port) {
return( UdpInetSocket::bind( host, port ) ); return UdpInetSocket::bind(host, port);
} }
bool bind(const char *serv) { bool bind(const char *serv) {
return( UdpInetSocket::bind( serv ) ); return UdpInetSocket::bind(serv);
} }
bool bind(int port) { bool bind(int port) {
return( UdpInetSocket::bind( port ) ); return UdpInetSocket::bind(port);
} }
protected: protected:
bool connect(const char *host, const char *serv) { bool connect(const char *host, const char *serv) {
return( UdpInetSocket::connect( host, serv ) ); return UdpInetSocket::connect(host, serv);
} }
bool connect(const char *host, int port) { bool connect(const char *host, int port) {
return( UdpInetSocket::connect( host, port ) ); return UdpInetSocket::connect(host, port);
} }
}; };
class UdpUnixServer : public UdpUnixSocket { class UdpUnixServer : public UdpUnixSocket {
public: public:
bool bind(const char *path) { bool bind(const char *path) {
return( UdpUnixSocket::bind( path ) ); return UdpUnixSocket::bind(path);
} }
protected: protected:
bool connect(const char *path) { bool connect(const char *path) {
return( UdpUnixSocket::connect( path ) ); return UdpUnixSocket::connect(path);
} }
}; };
class TcpSocket : virtual public Socket { class TcpSocket : virtual public Socket {
public: public:
TcpSocket() { TcpSocket() {}
} TcpSocket(const TcpSocket &socket, int newSd) : Socket(socket, newSd) {}
TcpSocket( const TcpSocket &socket, int newSd ) : Socket( socket, newSd ) {
}
public: int getType() const { return SOCK_STREAM; }
int getType() const { const char *getProtocol() const { return "tcp"; }
return( SOCK_STREAM );
}
const char *getProtocol() const {
return( "tcp" );
}
}; };
class TcpInetSocket : virtual public TcpSocket, virtual public InetSocket { class TcpInetSocket : virtual public TcpSocket, virtual public InetSocket {
public: public:
TcpInetSocket() { TcpInetSocket() {}
} TcpInetSocket(const TcpInetSocket &socket, int newSd)
TcpInetSocket( const TcpInetSocket &socket, int newSd ) : TcpSocket( socket, newSd ) { : TcpSocket(socket, newSd) {}
}
}; };
class TcpUnixSocket : virtual public TcpSocket, virtual public UnixSocket { class TcpUnixSocket : virtual public TcpSocket, virtual public UnixSocket {
public: public:
TcpUnixSocket() { TcpUnixSocket() {}
} TcpUnixSocket(const TcpUnixSocket &socket, int newSd)
TcpUnixSocket( const TcpUnixSocket &socket, int newSd ) : TcpSocket( socket, newSd ) { : TcpSocket(socket, newSd) {}
}
}; };
class TcpInetClient : public TcpInetSocket { class TcpInetClient : public TcpInetSocket {
public: public:
bool connect(const char *host, const char *serv) { bool connect(const char *host, const char *serv) {
return( TcpInetSocket::connect( host, serv ) ); return TcpInetSocket::connect(host, serv);
} }
bool connect(const char *host, int port) { bool connect(const char *host, int port) {
return( TcpInetSocket::connect( host, port ) ); return TcpInetSocket::connect(host, port);
} }
}; };
class TcpUnixClient : public TcpUnixSocket { class TcpUnixClient : public TcpUnixSocket {
public: public:
bool connect( const char *path ) { bool connect(const char *path) { return TcpUnixSocket::connect(path); }
return( TcpUnixSocket::connect( path) );
}
}; };
class TcpInetServer : public TcpInetSocket { class TcpInetServer : public TcpInetSocket {
public: public:
bool bind( int port ) { bool bind(int port) { return TcpInetSocket::bind(port); }
return( TcpInetSocket::bind( port ) );
}
public: bool isListening() const { return Socket::isListening(); }
bool isListening() const { return( Socket::isListening() ); }
bool listen(); bool listen();
bool accept(); bool accept();
bool accept(TcpInetSocket *&newSocket); bool accept(TcpInetSocket *&newSocket);
@ -595,12 +545,9 @@ public:
class TcpUnixServer : public TcpUnixSocket { class TcpUnixServer : public TcpUnixSocket {
public: public:
bool bind( const char *path ) { bool bind(const char *path) { return TcpUnixSocket::bind(path); }
return( TcpUnixSocket::bind( path ) );
}
public: bool isListening() const { return Socket::isListening(); }
bool isListening() const { return( Socket::isListening() ); }
bool listen(); bool listen();
bool accept(); bool accept();
bool accept(TcpUnixSocket *&newSocket); bool accept(TcpUnixSocket *&newSocket);
@ -611,24 +558,14 @@ public:
typedef std::set<CommsBase *> CommsSet; typedef std::set<CommsBase *> CommsSet;
typedef std::vector<CommsBase *> CommsList; typedef std::vector<CommsBase *> CommsList;
protected: Select() : mHasTimeout(false), mMaxFd(-1) {}
CommsSet mReaders; explicit Select(timeval timeout) : mMaxFd(-1) { setTimeout(timeout); }
CommsSet mWriters; explicit Select(int timeout) : mMaxFd(-1) { setTimeout(timeout); }
CommsList mReadable; explicit Select(double timeout) : mMaxFd(-1) { setTimeout(timeout); }
CommsList mWriteable;
bool mHasTimeout;
struct timeval mTimeout;
int mMaxFd;
public:
Select();
explicit Select( struct timeval timeout );
explicit Select( int timeout );
explicit Select( double timeout );
void setTimeout(int timeout); void setTimeout(int timeout);
void setTimeout(double timeout); void setTimeout(double timeout);
void setTimeout( struct timeval timeout ); void setTimeout(timeval timeout);
void clearTimeout(); void clearTimeout();
void calcMaxFd(); void calcMaxFd();
@ -643,8 +580,17 @@ public:
int wait(); int wait();
const CommsList &getReadable() const; const CommsList &getReadable() const { return mReadable; }
const CommsList &getWriteable() const; const CommsList &getWriteable() const { return mWriteable; }
protected:
CommsSet mReaders;
CommsSet mWriters;
CommsList mReadable;
CommsList mWriteable;
bool mHasTimeout;
timeval mTimeout;
int mMaxFd;
}; };
} }