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,40 +36,42 @@
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++) {
iov[i].iov_base = va_arg(arg_ptr, void *); iov[i].iov_base = va_arg(arg_ptr, void *);
iov[i].iov_len = va_arg(arg_ptr, int); iov[i].iov_len = va_arg(arg_ptr, 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++) {
iov[i].iov_base = va_arg(arg_ptr, void *); iov[i].iov_base = va_arg(arg_ptr, void *);
iov[i].iov_len = va_arg(arg_ptr, int); iov[i].iov_len = va_arg(arg_ptr, 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;
} }
bool ZM::Pipe::open() { bool ZM::Pipe::open() {
if ( ::pipe(mFd) < 0 ) { if (::pipe(mFd) < 0) {
Error("pipe(), errno = %d, error = %s", errno, strerror(errno)); Error("pipe(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -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;
} }
@ -89,16 +95,16 @@ bool ZM::Pipe::setBlocking(bool blocking) {
int flags; int flags;
/* Now set it for non-blocking I/O */ /* Now set it for non-blocking I/O */
if ( (flags = fcntl(mFd[1], F_GETFL)) < 0 ) { if ((flags = fcntl(mFd[1], F_GETFL)) < 0) {
Error("fcntl(), errno = %d, error = %s", errno, strerror(errno)); Error("fcntl(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
if ( blocking ) { if (blocking) {
flags &= ~O_NONBLOCK; flags &= ~O_NONBLOCK;
} else { } else {
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
} }
if ( fcntl(mFd[1], F_SETFL, flags) < 0 ) { if (fcntl(mFd[1], F_SETFL, flags) < 0) {
Error("fcntl(), errno = %d, error = %s", errno, strerror(errno)); Error("fcntl(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -106,53 +112,50 @@ 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);
ZM::SockAddr *ZM::SockAddr::newSockAddr(const struct sockaddr &addr, socklen_t len) { } else if ((addr.sa_family == AF_UNIX) && (len == SockAddrUnix::addrSize())) {
if ( (addr.sa_family == AF_INET) && (len == SockAddrInet::addrSize()) ) { return new SockAddrUnix((const sockaddr_un *) &addr);
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 ) {
return new SockAddrInet(*(SockAddrInet *)addr);
} else if ( addr->getDomain() == AF_UNIX ) {
return new SockAddrUnix(*(SockAddrUnix *)addr);
} }
if (addr->getDomain() == AF_INET) {
return new SockAddrInet(*(SockAddrInet *) addr);
} else if (addr->getDomain() == AF_UNIX) {
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;
} }
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,23 +163,23 @@ 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;
} }
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,44 +211,45 @@ 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));
return false; return false;
} }
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));
close(); close();
return false; return false;
} }
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));
close(); close();
return false; return false;
@ -257,7 +258,7 @@ bool ZM::Socket::bind() {
} }
bool ZM::Socket::listen() { bool ZM::Socket::listen() {
if ( ::listen(mSd, SOMAXCONN) == -1 ) { if (::listen(mSd, SOMAXCONN) == -1) {
Error("listen(), errno = %d, error = %s", errno, strerror(errno)); Error("listen(), errno = %d, error = %s", errno, strerror(errno));
close(); close();
return false; return false;
@ -268,11 +269,11 @@ 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;
if ( (newSd = ::accept(mSd, rem_addr, &rem_addr_size)) == -1 ) { if ((newSd = ::accept(mSd, rem_addr, &rem_addr_size)) == -1) {
Error("accept(), errno = %d, error = %s", errno, strerror(errno)); Error("accept(), errno = %d, error = %s", errno, strerror(errno));
close(); close();
return false; return false;
@ -282,16 +283,15 @@ 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;
if ( (newSd = ::accept(mSd, rem_addr, &rem_addr_size)) == -1 ) { if ((newSd = ::accept(mSd, rem_addr, &rem_addr_size)) == -1) {
Error("accept(), errno = %d, error = %s", errno, strerror(errno)); Error("accept(), errno = %d, error = %s", errno, strerror(errno));
close(); close();
return false; return false;
@ -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;
@ -310,7 +313,7 @@ bool ZM::Socket::close() {
int ZM::Socket::bytesToRead() const { int ZM::Socket::bytesToRead() const {
int bytes_to_read = 0; int bytes_to_read = 0;
if ( ioctl(mSd, FIONREAD, &bytes_to_read) < 0 ) { if (ioctl(mSd, FIONREAD, &bytes_to_read) < 0) {
Error("ioctl(), errno = %d, error = %s", errno, strerror(errno)); Error("ioctl(), errno = %d, error = %s", errno, strerror(errno));
return -1; return -1;
} }
@ -320,7 +323,7 @@ int ZM::Socket::bytesToRead() const {
bool ZM::Socket::getBlocking(bool &blocking) { bool ZM::Socket::getBlocking(bool &blocking) {
int flags; int flags;
if ( (flags = fcntl(mSd, F_GETFL)) < 0 ) { if ((flags = fcntl(mSd, F_GETFL)) < 0) {
Error("fcntl(), errno = %d, error = %s", errno, strerror(errno)); Error("fcntl(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -332,16 +335,16 @@ bool ZM::Socket::setBlocking(bool blocking) {
int flags; int flags;
/* Now set it for non-blocking I/O */ /* Now set it for non-blocking I/O */
if ( (flags = fcntl(mSd, F_GETFL)) < 0 ) { if ((flags = fcntl(mSd, F_GETFL)) < 0) {
Error("fcntl(), errno = %d, error = %s", errno, strerror(errno)); Error("fcntl(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
if ( blocking ) { if (blocking) {
flags &= ~O_NONBLOCK; flags &= ~O_NONBLOCK;
} else { } else {
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
} }
if ( fcntl(mSd, F_SETFL, flags) < 0 ) { if (fcntl(mSd, F_SETFL, flags) < 0) {
Error("fcntl(), errno = %d, error = %s", errno, strerror(errno)); Error("fcntl(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -351,7 +354,7 @@ bool ZM::Socket::setBlocking(bool blocking) {
bool ZM::Socket::getSendBufferSize(int &buffersize) const { bool ZM::Socket::getSendBufferSize(int &buffersize) const {
socklen_t optlen = sizeof(buffersize); socklen_t optlen = sizeof(buffersize);
if ( getsockopt(mSd, SOL_SOCKET, SO_SNDBUF, &buffersize, &optlen) < 0 ) { if (getsockopt(mSd, SOL_SOCKET, SO_SNDBUF, &buffersize, &optlen) < 0) {
Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno));
return -1; return -1;
} }
@ -360,7 +363,7 @@ bool ZM::Socket::getSendBufferSize(int &buffersize) const {
bool ZM::Socket::getRecvBufferSize(int &buffersize) const { bool ZM::Socket::getRecvBufferSize(int &buffersize) const {
socklen_t optlen = sizeof(buffersize); socklen_t optlen = sizeof(buffersize);
if ( getsockopt(mSd, SOL_SOCKET, SO_RCVBUF, &buffersize, &optlen) < 0 ) { if (getsockopt(mSd, SOL_SOCKET, SO_RCVBUF, &buffersize, &optlen) < 0) {
Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno));
return -1; return -1;
} }
@ -368,7 +371,7 @@ bool ZM::Socket::getRecvBufferSize(int &buffersize) const {
} }
bool ZM::Socket::setSendBufferSize(int buffersize) { bool ZM::Socket::setSendBufferSize(int buffersize) {
if ( setsockopt(mSd, SOL_SOCKET, SO_SNDBUF, (char *)&buffersize, sizeof(buffersize)) < 0 ) { if (setsockopt(mSd, SOL_SOCKET, SO_SNDBUF, (char *) &buffersize, sizeof(buffersize)) < 0) {
Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -376,7 +379,7 @@ bool ZM::Socket::setSendBufferSize(int buffersize) {
} }
bool ZM::Socket::setRecvBufferSize(int buffersize) { bool ZM::Socket::setRecvBufferSize(int buffersize) {
if ( setsockopt( mSd, SOL_SOCKET, SO_RCVBUF, (char *)&buffersize, sizeof(buffersize)) < 0 ) { if (setsockopt(mSd, SOL_SOCKET, SO_RCVBUF, (char *) &buffersize, sizeof(buffersize)) < 0) {
Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -386,7 +389,7 @@ bool ZM::Socket::setRecvBufferSize(int buffersize) {
bool ZM::Socket::getRouting(bool &route) const { bool ZM::Socket::getRouting(bool &route) const {
int dontRoute; int dontRoute;
socklen_t optlen = sizeof(dontRoute); socklen_t optlen = sizeof(dontRoute);
if ( getsockopt(mSd, SOL_SOCKET, SO_DONTROUTE, &dontRoute, &optlen) < 0 ) { if (getsockopt(mSd, SOL_SOCKET, SO_DONTROUTE, &dontRoute, &optlen) < 0) {
Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -396,7 +399,7 @@ bool ZM::Socket::getRouting(bool &route) const {
bool ZM::Socket::setRouting(bool route) { bool ZM::Socket::setRouting(bool route) {
int dontRoute = !route; int dontRoute = !route;
if ( setsockopt(mSd, SOL_SOCKET, SO_DONTROUTE, (char *)&dontRoute, sizeof(dontRoute)) < 0 ) { if (setsockopt(mSd, SOL_SOCKET, SO_DONTROUTE, (char *) &dontRoute, sizeof(dontRoute)) < 0) {
Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -406,7 +409,7 @@ bool ZM::Socket::setRouting(bool route) {
bool ZM::Socket::getNoDelay(bool &nodelay) const { bool ZM::Socket::getNoDelay(bool &nodelay) const {
int int_nodelay; int int_nodelay;
socklen_t optlen = sizeof(int_nodelay); socklen_t optlen = sizeof(int_nodelay);
if ( getsockopt(mSd, IPPROTO_TCP, TCP_NODELAY, &int_nodelay, &optlen) < 0 ) { if (getsockopt(mSd, IPPROTO_TCP, TCP_NODELAY, &int_nodelay, &optlen) < 0) {
Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("getsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -417,7 +420,7 @@ bool ZM::Socket::getNoDelay(bool &nodelay) const {
bool ZM::Socket::setNoDelay(bool nodelay) { bool ZM::Socket::setNoDelay(bool nodelay) {
int int_nodelay = nodelay; int int_nodelay = nodelay;
if ( setsockopt(mSd, IPPROTO_TCP, TCP_NODELAY, (char *)&int_nodelay, sizeof(int_nodelay)) < 0 ) { if (setsockopt(mSd, IPPROTO_TCP, TCP_NODELAY, (char *) &int_nodelay, sizeof(int_nodelay)) < 0) {
Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno)); Error("setsockopt(), errno = %d, error = %s", errno, strerror(errno));
return false; return false;
} }
@ -425,20 +428,20 @@ 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;
hints.ai_protocol = 0; /* Any protocol */ hints.ai_protocol = 0; /* Any protocol */
s = getaddrinfo(host, serv, &hints, &result); s = getaddrinfo(host, serv, &hints, &result);
if ( s != 0 ) { if (s != 0) {
Error("connect(): getaddrinfo: %s", gai_strerror(s)); Error("connect(): getaddrinfo: %s", gai_strerror(s));
return false; return false;
} }
@ -448,47 +451,51 @@ bool ZM::InetSocket::connect(const char *host, const char *serv) {
* If socket(2) (or connect(2)) fails, we (close the socket * If socket(2) (or connect(2)) fails, we (close the socket
* and) try the next address. */ * and) try the next address. */
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 */
if ( rp == nullptr ) { /* No address succeeded */ if (rp == nullptr) { /* No address succeeded */
Error("connect(), Could not connect"); Error("connect(), Could not connect");
mAddressFamily = AF_UNSPEC; mAddressFamily = AF_UNSPEC;
return false; return false;
} }
mState = CONNECTED; mState = CONNECTED;
return true; return true;
} }
@ -499,10 +506,10 @@ bool ZM::InetSocket::connect(const char *host, int port) {
return connect(host, serv); return connect(host, serv);
} }
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,9 +518,9 @@ 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));
return false; return false;
} }
@ -523,29 +530,32 @@ bool ZM::InetSocket::bind(const char * host, const char * serv) {
* Try each address until we successfully bind(2). * Try each address until we successfully bind(2).
* If socket(2) (or bind(2)) fails, we (close the socket * If socket(2) (or bind(2)) fails, we (close the socket
* and) try the next address. */ * and) try the next address. */
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");
return false; return false;
} }
@ -554,11 +564,11 @@ bool ZM::InetSocket::bind(const char * host, const char * serv) {
return true; return true;
} }
bool ZM::InetSocket::bind(const char * serv) { bool ZM::InetSocket::bind(const char *serv) {
return bind(nullptr, serv); return bind(nullptr, serv);
} }
bool ZM::InetSocket::bind(const char * host, int port) { bool ZM::InetSocket::bind(const char *host, int port) {
char serv[8]; char serv[8];
snprintf(serv, sizeof(serv), "%d", port); snprintf(serv, sizeof(serv), "%d", port);
@ -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;
@ -627,11 +622,11 @@ void ZM::Select::setTimeout(int timeout) {
void ZM::Select::setTimeout(double timeout) { void ZM::Select::setTimeout(double timeout) {
mTimeout.tv_sec = int(timeout); mTimeout.tv_sec = int(timeout);
mTimeout.tv_usec = suseconds_t((timeout-mTimeout.tv_sec)*1000000.0); mTimeout.tv_usec = suseconds_t((timeout - mTimeout.tv_sec) * 1000000.0);
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;
} }
@ -642,35 +637,36 @@ void ZM::Select::clearTimeout() {
void ZM::Select::calcMaxFd() { void ZM::Select::calcMaxFd() {
mMaxFd = -1; mMaxFd = -1;
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter ) { for (CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter) {
if ( (*iter)->getMaxDesc() > mMaxFd ) if ((*iter)->getMaxDesc() > mMaxFd)
mMaxFd = (*iter)->getMaxDesc(); mMaxFd = (*iter)->getMaxDesc();
} }
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter ) { for (CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); ++iter) {
if ( (*iter)->getMaxDesc() > mMaxFd ) if ((*iter)->getMaxDesc() > mMaxFd)
mMaxFd = (*iter)->getMaxDesc(); mMaxFd = (*iter)->getMaxDesc();
} }
} }
bool ZM::Select::addReader(CommsBase *comms) { bool ZM::Select::addReader(CommsBase *comms) {
if ( !comms->isOpen() ) { if (!comms->isOpen()) {
Error("Unable to add closed reader"); Error("Unable to add closed reader");
return false; return false;
} }
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;
} }
bool ZM::Select::deleteReader(CommsBase *comms) { bool ZM::Select::deleteReader(CommsBase *comms) {
if ( !comms->isOpen() ) { if (!comms->isOpen()) {
Error("Unable to add closed reader"); Error("Unable to add closed reader");
return false; return false;
} }
if ( mReaders.erase(comms) ) { if (mReaders.erase(comms)) {
calcMaxFd(); calcMaxFd();
return true; return true;
} }
@ -684,15 +680,16 @@ 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;
} }
bool ZM::Select::deleteWriter(CommsBase *comms) { bool ZM::Select::deleteWriter(CommsBase *comms) {
if ( mWriters.erase(comms) ) { if (mWriters.erase(comms)) {
calcMaxFd(); calcMaxFd();
return true; return true;
} }
@ -705,42 +702,40 @@ 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) {
Debug(1, "Select timed out"); Debug(1, "Select timed out");
} 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

@ -38,613 +38,559 @@
namespace ZM { namespace ZM {
class CommsException : public Exception { class CommsException : public Exception {
public: public:
explicit CommsException( const std::string &message ) : Exception( message ) { } explicit CommsException(const std::string &message) : Exception(message) {}
}; };
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: public:
CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) { virtual bool close() = 0;
} virtual bool isOpen() const = 0;
virtual ~CommsBase() { virtual bool isClosed() const = 0;
virtual bool setBlocking(bool blocking) = 0;
public:
int getReadDesc() const { return mRd; }
int getWriteDesc() const { return mWd; }
int getMaxDesc() const { return mRd > mWd ? mRd : mWd; }
virtual int read(void *msg, int len) {
ssize_t nBytes = ::read(mRd, msg, len);
if (nBytes < 0) {
Debug(1, "Read of %d bytes max on rd %d failed: %s", len, mRd, strerror(errno));
}
return nBytes;
} }
public: virtual int write(const void *msg, int len) {
virtual bool close()=0; ssize_t nBytes = ::write(mWd, msg, len);
virtual bool isOpen() const=0; if (nBytes < 0) {
virtual bool isClosed() const=0; Debug(1, "Write of %d bytes on wd %d failed: %s", len, mWd, strerror(errno));
virtual bool setBlocking( bool blocking )=0; }
return nBytes;
public:
int getReadDesc() const {
return mRd;
}
int getWriteDesc() const {
return mWd;
}
int getMaxDesc() const {
return( mRd>mWd?mRd:mWd );
} }
virtual int read( void *msg, int len ) { virtual int readV(const iovec *iov, int iovcnt) {
ssize_t nBytes = ::read( mRd, msg, len ); int nBytes = ::readv(mRd, iov, iovcnt);
if ( nBytes < 0 ) if (nBytes < 0) {
Debug( 1, "Read of %d bytes max on rd %d failed: %s", len, mRd, strerror(errno) ); Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno));
return( nBytes ); }
return nBytes;
} }
virtual int write( const void *msg, int len ) {
ssize_t nBytes = ::write( mWd, msg, len ); virtual int writeV(const iovec *iov, int iovcnt) {
if ( nBytes < 0 ) ssize_t nBytes = ::writev(mWd, iov, iovcnt);
Debug( 1, "Write of %d bytes on wd %d failed: %s", len, mWd, strerror(errno) ); if (nBytes < 0) {
return( nBytes ); Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno));
}
return nBytes;
} }
virtual int readV( const struct iovec *iov, int iovcnt ) {
int nBytes = ::readv( mRd, iov, iovcnt ); virtual int readV(int iovcnt, /* const void *msg1, int len1, */ ...);
if ( nBytes < 0 ) virtual int writeV(int iovcnt, /* const void *msg1, int len1, */ ...);
Debug( 1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno) );
return( nBytes ); protected:
} const int &mRd;
virtual int writeV( const struct iovec *iov, int iovcnt ) { const int &mWd;
ssize_t nBytes = ::writev( mWd, iov, iovcnt );
if ( nBytes < 0 )
Debug( 1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno) );
return( nBytes );
}
virtual int readV( int iovcnt, /* const void *msg1, int len1, */ ... );
virtual int writeV( int iovcnt, /* const void *msg1, int len1, */ ... );
}; };
class Pipe : public CommsBase { class Pipe : public CommsBase {
protected: public:
int mFd[2]; Pipe() : CommsBase(mFd[0], mFd[1]) {
public:
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: public:
const struct sockaddr *mAddr; explicit SockAddr(const sockaddr *addr) : mAddr(addr) {}
virtual ~SockAddr() = default;
public: static SockAddr *newSockAddr(const sockaddr &addr, socklen_t len);
explicit SockAddr( const struct sockaddr *addr ); static SockAddr *newSockAddr(const SockAddr *addr);
virtual ~SockAddr() {
}
static SockAddr *newSockAddr( const struct sockaddr &addr, socklen_t len ); int getDomain() const { return mAddr ? mAddr->sa_family : AF_UNSPEC; }
static SockAddr *newSockAddr( const SockAddr *addr ); const sockaddr *getAddr() const { return mAddr; }
int getDomain() const { virtual socklen_t getAddrSize() const = 0;
return( mAddr?mAddr->sa_family:AF_UNSPEC ); virtual sockaddr *getTempAddr() const = 0;
}
const struct sockaddr *getAddr() const { private:
return( mAddr ); const sockaddr *mAddr;
}
virtual socklen_t getAddrSize() const=0;
virtual struct sockaddr *getTempAddr() const=0;
}; };
class SockAddrInet : public SockAddr { class SockAddrInet : public SockAddr {
private: public:
struct sockaddr_in mAddrIn; SockAddrInet() : SockAddr((sockaddr *) &mAddrIn) {}
struct sockaddr_in mTempAddrIn; explicit SockAddrInet(const SockAddrInet &addr)
: SockAddr((const sockaddr *) &mAddrIn), mAddrIn(addr.mAddrIn) {}
explicit SockAddrInet(const sockaddr_in *addr)
: SockAddr((const sockaddr *) &mAddrIn), mAddrIn(*addr) {}
public: bool resolve(const char *host, const char *serv, const char *proto);
SockAddrInet(); bool resolve(const char *host, int port, const char *proto);
explicit SockAddrInet( const SockAddrInet &addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( addr.mAddrIn ) { bool resolve(const char *serv, const char *proto);
} bool resolve(int port, const char *proto);
explicit SockAddrInet( const struct sockaddr_in *addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( *addr ) {
}
socklen_t getAddrSize() const { return sizeof(mAddrIn); }
sockaddr *getTempAddr() const { return (sockaddr *) &mTempAddrIn; }
bool resolve( const char *host, const char *serv, const char *proto ); static socklen_t addrSize() { return sizeof(sockaddr_in); }
bool resolve( const char *host, int port, const char *proto );
bool resolve( const char *serv, const char *proto );
bool resolve( int port, const char *proto );
socklen_t getAddrSize() const { private:
return( sizeof(mAddrIn) ); sockaddr_in mAddrIn;
} sockaddr_in mTempAddrIn;
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrIn );
}
public:
static socklen_t addrSize() {
return( sizeof(sockaddr_in) );
}
}; };
class SockAddrUnix : public SockAddr { class SockAddrUnix : public SockAddr {
private: public:
struct sockaddr_un mAddrUn; SockAddrUnix() : SockAddr((sockaddr *) &mAddrUn) {}
struct sockaddr_un mTempAddrUn; SockAddrUnix(const SockAddrUnix &addr)
: SockAddr((const sockaddr *) &mAddrUn), mAddrUn(addr.mAddrUn) {}
explicit SockAddrUnix(const sockaddr_un *addr)
: SockAddr((const sockaddr *) &mAddrUn), mAddrUn(*addr) {}
public: bool resolve(const char *path, const char *proto);
SockAddrUnix();
SockAddrUnix( const SockAddrUnix &addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( addr.mAddrUn ) {
}
explicit SockAddrUnix( const struct sockaddr_un *addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( *addr ) {
}
bool resolve( const char *path, const char *proto ); socklen_t getAddrSize() const { return sizeof(mAddrUn); }
sockaddr *getTempAddr() const { return (sockaddr *) &mTempAddrUn; }
socklen_t getAddrSize() const { static socklen_t addrSize() { return sizeof(sockaddr_un); }
return( sizeof(mAddrUn) );
}
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrUn );
}
public: private:
static socklen_t addrSize() { sockaddr_un mAddrUn;
return( sizeof(sockaddr_un) ); 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),
mSd(newSd),
mState(CONNECTED),
mLocalAddr(nullptr),
mRemoteAddr(nullptr) {
if (socket.mLocalAddr)
mLocalAddr = SockAddr::newSockAddr(mLocalAddr);
if (socket.mRemoteAddr)
mRemoteAddr = SockAddr::newSockAddr(mRemoteAddr);
}
protected:
Socket() : CommsBase( mSd, mSd ), mSd( -1 ), mState( CLOSED ), mLocalAddr( 0 ), mRemoteAddr( 0 ) {
}
Socket( const Socket &socket, int newSd ) : CommsBase( mSd, mSd ), mSd( newSd ), mState( CONNECTED ), mLocalAddr( 0 ), mRemoteAddr( 0 ) {
if ( socket.mLocalAddr )
mLocalAddr = SockAddr::newSockAddr( mLocalAddr );
if ( socket.mRemoteAddr )
mRemoteAddr = SockAddr::newSockAddr( mRemoteAddr );
}
virtual ~Socket() { virtual ~Socket() {
close(); close();
delete mLocalAddr; delete mLocalAddr;
delete mRemoteAddr; delete mRemoteAddr;
} }
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: virtual int send(const void *msg, int len) const {
bool isListening() const { ssize_t nBytes = ::send(mSd, msg, len, 0);
return( mState == LISTENING ); if (nBytes < 0) {
Debug(1, "Send of %d bytes on sd %d failed: %s", len, mSd, strerror(errno));
}
return nBytes;
} }
protected: virtual int recv(void *msg, int len) const {
ssize_t nBytes = ::recv(mSd, msg, len, 0);
if (nBytes < 0) {
Debug(1, "Recv of %d bytes max on sd %d failed: %s", len, mSd, strerror(errno));
}
return nBytes;
}
virtual int send(const std::string &msg) const {
ssize_t nBytes = ::send(mSd, msg.data(), msg.size(), 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));
}
return nBytes;
}
virtual int recv(std::string &msg) const {
char buffer[msg.capacity()];
int nBytes = 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));
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
return nBytes;
}
virtual int recv(std::string &msg, size_t maxLen) const {
char buffer[maxLen];
int nBytes = 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));
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
return nBytes;
}
virtual int bytesToRead() const;
int getDesc() const { return mSd; }
//virtual bool isOpen() const
//{
//return( mSd != -1 );
//}
virtual int getDomain() const = 0;
virtual int getType() const = 0;
virtual const char *getProtocol() const = 0;
const SockAddr *getLocalAddr() const { return mLocalAddr; }
const SockAddr *getRemoteAddr() const { return mRemoteAddr; }
virtual socklen_t getAddrSize() const = 0;
bool getBlocking(bool &blocking);
bool setBlocking(bool blocking);
bool getSendBufferSize(int &) const;
bool getRecvBufferSize(int &) const;
bool setSendBufferSize(int);
bool setRecvBufferSize(int);
bool getRouting(bool &) const;
bool setRouting(bool);
bool getNoDelay(bool &) const;
bool setNoDelay(bool);
protected:
bool isListening() const { return mState == LISTENING; }
virtual bool socket(); virtual bool socket();
virtual bool bind(); virtual bool bind();
protected:
virtual bool connect(); virtual bool connect();
virtual bool listen(); virtual bool listen();
virtual bool accept(); virtual bool accept();
virtual bool accept( int & ); virtual bool accept(int &);
public: int mSd;
virtual int send( const void *msg, int len ) const { State mState;
ssize_t nBytes = ::send( mSd, msg, len, 0 ); SockAddr *mLocalAddr;
if ( nBytes < 0 ) SockAddr *mRemoteAddr;
Debug( 1, "Send of %d bytes on sd %d failed: %s", len, mSd, strerror(errno) );
return( nBytes );
}
virtual int recv( void *msg, int len ) const {
ssize_t nBytes = ::recv( mSd, msg, len, 0 );
if ( nBytes < 0 )
Debug( 1, "Recv of %d bytes max on sd %d failed: %s", len, mSd, strerror(errno) );
return( nBytes );
}
virtual int send( const std::string &msg ) const {
ssize_t nBytes = ::send( mSd, msg.data(), msg.size(), 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) );
return( nBytes );
}
virtual int recv( std::string &msg ) const {
char buffer[msg.capacity()];
int nBytes = 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) );
return( nBytes );
}
buffer[nBytes] = '\0';
msg = buffer;
return( nBytes );
}
virtual int recv( std::string &msg, size_t maxLen ) const {
char buffer[maxLen];
int nBytes = 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) );
return( nBytes );
}
buffer[nBytes] = '\0';
msg = buffer;
return( nBytes );
}
virtual int bytesToRead() const;
int getDesc() const {
return( mSd );
}
//virtual bool isOpen() const
//{
//return( mSd != -1 );
//}
virtual int getDomain() const=0;
virtual int getType() const=0;
virtual const char *getProtocol() const=0;
const SockAddr *getLocalAddr() const {
return( mLocalAddr );
}
const SockAddr *getRemoteAddr() const {
return( mRemoteAddr );
}
virtual socklen_t getAddrSize() const=0;
bool getBlocking( bool &blocking );
bool setBlocking( bool blocking );
bool getSendBufferSize( int & ) const;
bool getRecvBufferSize( int & ) const;
bool setSendBufferSize( int );
bool setRecvBufferSize( int );
bool getRouting( bool & ) const;
bool setRouting( bool );
bool getNoDelay( bool & ) const;
bool setNoDelay( bool );
}; };
class InetSocket : virtual public Socket class InetSocket : virtual public Socket {
{ public:
protected: int getDomain() const { return mAddressFamily; }
int mAddressFamily; virtual socklen_t getAddrSize() const { return SockAddrInet::addrSize(); }
public: protected:
int getDomain() const { bool connect(const char *host, const char *serv);
return( mAddressFamily ); bool connect(const char *host, int port);
}
virtual socklen_t getAddrSize() const {
return( SockAddrInet::addrSize() );
}
protected: bool bind(const char *host, const char *serv);
bool connect( const char *host, const char *serv ); bool bind(const char *host, int port);
bool connect( const char *host, int port ); bool bind(const char *serv);
bool bind(int port);
bool bind( const char *host, const char *serv ); int mAddressFamily;
bool bind( const char *host, int port );
bool bind( const char *serv );
bool bind( int port );
}; };
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 { virtual int sendto(const void *msg, int len, const SockAddr *addr = nullptr) const {
return( "udp" ); ssize_t nBytes = ::sendto(mSd, msg, len, 0, addr ? addr->getAddr() : nullptr, addr ? addr->getAddrSize() : 0);
if (nBytes < 0) {
Debug(1, "Sendto of %d bytes on sd %d failed: %s", len, mSd, strerror(errno));
}
return nBytes;
} }
public: virtual int recvfrom(void *msg, int len, 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 );
if ( nBytes < 0 )
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 {
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 ) {
return( InetSocket::bind( host, port ) );
}
bool bind( const char *serv ) {
return( InetSocket::bind( serv ) );
}
bool bind( int port ) {
return( InetSocket::bind( port ) );
} }
bool connect( const char *host, const char *serv ) { bool bind(const char *host, int port) {
return( InetSocket::connect( host, serv ) ); return InetSocket::bind(host, port);
} }
bool connect( const char *host, int port ) {
return( InetSocket::connect( host, port ) ); bool bind(const char *serv) {
return InetSocket::bind(serv);
}
bool bind(int port) {
return InetSocket::bind(port);
}
bool connect(const char *host, const char *serv) {
return InetSocket::connect(host, serv);
}
bool connect(const char *host, int 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 ) {
return( UdpInetSocket::connect( host, port ) ); bool connect(const char *host, int 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 ) {
return( UdpInetSocket::bind( host, port ) );
}
bool bind( const char *serv ) {
return( UdpInetSocket::bind( serv ) );
}
bool bind( int port ) {
return( UdpInetSocket::bind( port ) );
} }
protected: bool bind(const char *host, int port) {
bool connect( const char *host, const char *serv ) { return UdpInetSocket::bind(host, port);
return( UdpInetSocket::connect( host, serv ) );
} }
bool connect( const char *host, int port ) {
return( UdpInetSocket::connect( host, port ) ); bool bind(const char *serv) {
return UdpInetSocket::bind(serv);
}
bool bind(int port) {
return UdpInetSocket::bind(port);
}
protected:
bool connect(const char *host, const char *serv) {
return UdpInetSocket::connect(host, serv);
}
bool connect(const char *host, int 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 ) {
return( TcpInetSocket::connect( host, port ) ); bool connect(const char *host, int 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);
}; };
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);
}; };
class Select { class Select {
public: 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: void setTimeout(int timeout);
Select(); void setTimeout(double timeout);
explicit Select( struct timeval timeout ); void setTimeout(timeval timeout);
explicit Select( int timeout );
explicit Select( double timeout );
void setTimeout( int timeout );
void setTimeout( double timeout );
void setTimeout( struct timeval timeout );
void clearTimeout(); void clearTimeout();
void calcMaxFd(); void calcMaxFd();
bool addReader( CommsBase *comms ); bool addReader(CommsBase *comms);
bool deleteReader( CommsBase *comms ); bool deleteReader(CommsBase *comms);
void clearReaders(); void clearReaders();
bool addWriter( CommsBase *comms ); bool addWriter(CommsBase *comms);
bool deleteWriter( CommsBase *comms ); bool deleteWriter(CommsBase *comms);
void clearWriters(); void clearWriters();
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;
}; };
} }