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, */ ...) {
va_list arg_ptr;
struct iovec iov[iovcnt];
iovec iov[iovcnt];
va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) {
@ -46,14 +46,15 @@ int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
va_end(arg_ptr);
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));
}
return nBytes;
}
int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
va_list arg_ptr;
struct iovec iov[iovcnt];
iovec iov[iovcnt];
va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) {
@ -63,8 +64,9 @@ int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
va_end(arg_ptr);
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));
}
return nBytes;
}
@ -78,9 +80,13 @@ bool ZM::Pipe::open() {
}
bool ZM::Pipe::close() {
if ( mFd[0] > -1 ) ::close( mFd[0] );
if (mFd[0] > -1) {
::close(mFd[0]);
}
mFd[0] = -1;
if ( mFd[1] > -1 ) ::close( mFd[1] );
if (mFd[1] > -1) {
::close(mFd[1]);
}
mFd[1] = -1;
return true;
}
@ -106,45 +112,42 @@ bool ZM::Pipe::setBlocking(bool blocking) {
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);
return nullptr;
}
ZM::SockAddr *ZM::SockAddr::newSockAddr(const SockAddr *addr) {
if ( !addr )
if (!addr) {
return nullptr;
}
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());
return nullptr;
}
ZM::SockAddrInet::SockAddrInet() : SockAddr( (struct sockaddr *)&mAddrIn ) {
}
bool ZM::SockAddrInet::resolve(const char *host, const char *serv, const char *proto) {
memset(&mAddrIn, 0, sizeof(mAddrIn));
struct hostent *hostent = nullptr;
hostent *hostent = nullptr;
if (!(hostent = ::gethostbyname(host))) {
Error("gethostbyname(%s), h_errno = %d", host, h_errno);
return false;
}
struct servent *servent = nullptr;
servent *servent = nullptr;
if (!(servent = ::getservbyname(serv, proto))) {
Error("getservbyname( %s ), errno = %d, error = %s", serv, errno, strerror(errno));
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_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;
}
@ -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) {
memset(&mAddrIn, 0, sizeof(mAddrIn));
struct hostent *hostent = nullptr;
hostent *hostent = nullptr;
if (!(hostent = ::gethostbyname(host))) {
Error("gethostbyname(%s), h_errno = %d", host, h_errno);
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_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;
}
bool ZM::SockAddrInet::resolve(const char *serv, const char *proto) {
memset(&mAddrIn, 0, sizeof(mAddrIn));
struct servent *servent = nullptr;
servent *servent = nullptr;
if (!(servent = ::getservbyname(serv, proto))) {
Error("getservbyname(%s), errno = %d, error = %s", serv, errno, strerror(errno));
return false;
@ -198,9 +201,6 @@ bool ZM::SockAddrInet::resolve(int port, const char *proto) {
return true;
}
ZM::SockAddrUnix::SockAddrUnix() : SockAddr((struct sockaddr *)&mAddrUn ) {
}
bool ZM::SockAddrUnix::resolve(const char *path, const char *proto) {
memset(&mAddrUn, 0, sizeof(mAddrUn));
@ -211,8 +211,9 @@ bool ZM::SockAddrUnix::resolve(const char *path, const char *proto) {
}
bool ZM::Socket::socket() {
if ( mSd >= 0 )
if (mSd >= 0) {
return true;
}
if ((mSd = ::socket(getDomain(), getType(), 0)) < 0) {
Error("socket(), errno = %d, error = %s", errno, strerror(errno));
@ -221,17 +222,17 @@ bool ZM::Socket::socket() {
int val = 1;
(void)::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
(void)::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
mState = DISCONNECTED;
return true;
}
bool ZM::Socket::connect() {
if ( !socket() )
if (!socket()) {
return false;
}
if (::connect(mSd, mRemoteAddr->getAddr(), getAddrSize()) == -1) {
Error("connect(), errno = %d, error = %s", errno, strerror(errno));
@ -240,13 +241,13 @@ bool ZM::Socket::connect() {
}
mState = CONNECTED;
return true;
}
bool ZM::Socket::bind() {
if ( !socket() )
if (!socket()) {
return false;
}
if (::bind(mSd, mLocalAddr->getAddr(), getAddrSize()) == -1) {
Error("bind(), errno = %d, error = %s", errno, strerror(errno));
@ -268,7 +269,7 @@ bool ZM::Socket::listen() {
}
bool ZM::Socket::accept() {
struct sockaddr *rem_addr = mLocalAddr->getTempAddr();
sockaddr *rem_addr = mLocalAddr->getTempAddr();
socklen_t rem_addr_size = getAddrSize();
int newSd = -1;
@ -282,12 +283,11 @@ bool ZM::Socket::accept() {
mSd = newSd;
mState = CONNECTED;
return true;
}
bool ZM::Socket::accept(int &newSd) {
struct sockaddr *rem_addr = mLocalAddr->getTempAddr();
sockaddr *rem_addr = mLocalAddr->getTempAddr();
socklen_t rem_addr_size = getAddrSize();
newSd = -1;
@ -301,7 +301,10 @@ bool ZM::Socket::accept(int &newSd) {
}
bool ZM::Socket::close() {
if ( mSd > -1 ) ::close(mSd);
if (mSd > -1) {
::close(mSd);
}
mSd = -1;
mState = CLOSED;
return true;
@ -425,13 +428,13 @@ bool ZM::Socket::setNoDelay(bool nodelay) {
}
bool ZM::InetSocket::connect(const char *host, const char *serv) {
struct addrinfo hints;
struct addrinfo *result, *rp;
addrinfo hints;
addrinfo *result, *rp;
int s;
char buf[255];
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_socktype = getType();
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) {
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 */
}
continue;
}
memset(&buf, 0, sizeof(buf));
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) {
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 {
strncpy(buf, "n/a", sizeof(buf) - 1);
}
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);
if ( mSd == -1 )
if (mSd == -1) {
continue;
}
int val = 1;
(void)::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
(void)::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
::setsockopt(mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
::setsockopt(mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
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 */
}
::close(mSd);
} // end for
}
freeaddrinfo(result); /* No longer needed */
@ -488,7 +496,6 @@ bool ZM::InetSocket::connect(const char *host, const char *serv) {
}
mState = CONNECTED;
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) {
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_socktype = getType();
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_next = nullptr;
struct addrinfo *result, *rp;
addrinfo *result, *rp;
int s = getaddrinfo(host, serv, &hints, &result);
if (s != 0) {
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) {
memset(&buf, 0, sizeof(buf));
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) {
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 {
strncpy(buf, "n/a", sizeof(buf) - 1);
}
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);
if ( mSd == -1 )
if (mSd == -1) {
continue;
}
mState = DISCONNECTED;
if ( ::bind(mSd, rp->ai_addr, rp->ai_addrlen) == 0 )
if (::bind(mSd, rp->ai_addr, rp->ai_addrlen) == 0) {
break; /* Success */
}
::close(mSd);
mSd = -1;
} // end foreach result
}
if (rp == nullptr) { /* No address succeeded */
Error("bind(), Could not bind");
@ -584,11 +594,11 @@ bool ZM::TcpInetServer::accept(TcpInetSocket *&newSocket) {
int newSd = -1;
newSocket = nullptr;
if ( !Socket::accept(newSd) )
if (!Socket::accept(newSd)) {
return false;
}
newSocket = new TcpInetSocket(*this, newSd);
return true;
}
@ -596,29 +606,14 @@ bool ZM::TcpUnixServer::accept(TcpUnixSocket *&newSocket) {
int newSd = -1;
newSocket = nullptr;
if ( !Socket::accept(newSd) )
if (!Socket::accept(newSd)) {
return false;
}
newSocket = new TcpUnixSocket(*this, newSd);
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) {
mTimeout.tv_sec = timeout;
mTimeout.tv_usec = 0;
@ -631,7 +626,7 @@ void ZM::Select::setTimeout(double timeout) {
mHasTimeout = true;
}
void ZM::Select::setTimeout(struct timeval timeout) {
void ZM::Select::setTimeout(timeval timeout) {
mTimeout = timeout;
mHasTimeout = true;
}
@ -659,9 +654,10 @@ bool ZM::Select::addReader(CommsBase *comms) {
}
std::pair<CommsSet::iterator, bool> result = mReaders.insert(comms);
if (result.second) {
if ( comms->getMaxDesc() > mMaxFd )
if (comms->getMaxDesc() > mMaxFd) {
mMaxFd = comms->getMaxDesc();
}
}
return result.second;
}
@ -685,9 +681,10 @@ void ZM::Select::clearReaders() {
bool ZM::Select::addWriter(CommsBase *comms) {
std::pair<CommsSet::iterator, bool> result = mWriters.insert(comms);
if (result.second) {
if ( comms->getMaxDesc() > mMaxFd )
if (comms->getMaxDesc() > mMaxFd) {
mMaxFd = comms->getMaxDesc();
}
}
return result.second;
}
@ -705,21 +702,23 @@ void ZM::Select::clearWriters() {
}
int ZM::Select::wait() {
struct timeval tempTimeout = mTimeout;
struct timeval *selectTimeout = mHasTimeout?&tempTimeout:nullptr;
timeval tempTimeout = mTimeout;
timeval *selectTimeout = mHasTimeout ? &tempTimeout : nullptr;
fd_set rfds;
fd_set wfds;
mReadable.clear();
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);
}
mWriteable.clear();
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);
}
int nFound = select(mMaxFd + 1, &rfds, &wfds, nullptr, selectTimeout);
if (nFound == 0) {
@ -727,20 +726,16 @@ int ZM::Select::wait() {
} else if (nFound < 0) {
Error("Select error: %s", strerror(errno));
} else {
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter )
if ( FD_ISSET((*iter)->getReadDesc(), &rfds) )
for (CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); ++iter) {
if (FD_ISSET((*iter)->getReadDesc(), &rfds)) {
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);
}
}
}
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 {
protected:
const int &mRd;
const int &mWd;
protected:
CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) {
}
virtual ~CommsBase() {
}
CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) {}
virtual ~CommsBase() = default;
public:
virtual bool close() = 0;
@ -60,177 +54,153 @@ public:
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 );
}
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 )
if (nBytes < 0) {
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) {
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));
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);
if ( nBytes < 0 )
if (nBytes < 0) {
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);
if ( nBytes < 0 )
if (nBytes < 0) {
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 writeV(int iovcnt, /* const void *msg1, int len1, */ ...);
protected:
const int &mRd;
const int &mWd;
};
class Pipe : public CommsBase {
protected:
int mFd[2];
public:
Pipe() : CommsBase(mFd[0], mFd[1]) {
mFd[0] = -1;
mFd[1] = -1;
}
~Pipe() {
close();
}
public:
~Pipe() { close(); }
bool open();
bool close();
bool isOpen() const
{
return( mFd[0] != -1 && mFd[1] != -1 );
}
int getReadDesc() const
{
return( mFd[0] );
}
int getWriteDesc() const
{
return( mFd[1] );
}
bool isOpen() const { return mFd[0] != -1 && mFd[1] != -1; }
int getReadDesc() const { return mFd[0]; }
int getWriteDesc() const { return mFd[1]; }
bool setBlocking(bool blocking);
protected:
int mFd[2];
};
class SockAddr {
private:
const struct sockaddr *mAddr;
public:
explicit SockAddr( const struct sockaddr *addr );
virtual ~SockAddr() {
}
explicit SockAddr(const sockaddr *addr) : mAddr(addr) {}
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);
int getDomain() const {
return( mAddr?mAddr->sa_family:AF_UNSPEC );
}
int getDomain() const { 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 struct sockaddr *getTempAddr() const=0;
virtual sockaddr *getTempAddr() const = 0;
private:
const sockaddr *mAddr;
};
class SockAddrInet : public SockAddr {
private:
struct sockaddr_in mAddrIn;
struct sockaddr_in mTempAddrIn;
public:
SockAddrInet();
explicit SockAddrInet( const SockAddrInet &addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( addr.mAddrIn ) {
}
explicit SockAddrInet( const struct sockaddr_in *addr ) : SockAddr( (const struct sockaddr *)&mAddrIn ), mAddrIn( *addr ) {
}
SockAddrInet() : SockAddr((sockaddr *) &mAddrIn) {}
explicit SockAddrInet(const SockAddrInet &addr)
: SockAddr((const sockaddr *) &mAddrIn), mAddrIn(addr.mAddrIn) {}
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, int port, const char *proto);
bool resolve(const char *serv, const char *proto);
bool resolve(int port, const char *proto);
socklen_t getAddrSize() const {
return( sizeof(mAddrIn) );
}
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrIn );
}
socklen_t getAddrSize() const { return sizeof(mAddrIn); }
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 {
private:
struct sockaddr_un mAddrUn;
struct sockaddr_un mTempAddrUn;
public:
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 ) {
}
SockAddrUnix() : SockAddr((sockaddr *) &mAddrUn) {}
SockAddrUnix(const SockAddrUnix &addr)
: SockAddr((const sockaddr *) &mAddrUn), mAddrUn(addr.mAddrUn) {}
explicit SockAddrUnix(const sockaddr_un *addr)
: SockAddr((const sockaddr *) &mAddrUn), mAddrUn(*addr) {}
bool resolve(const char *path, const char *proto);
socklen_t getAddrSize() const {
return( sizeof(mAddrUn) );
}
struct sockaddr *getTempAddr() const {
return( (sockaddr *)&mTempAddrUn );
}
socklen_t getAddrSize() const { return sizeof(mAddrUn); }
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 {
protected:
typedef enum { CLOSED, DISCONNECTED, LISTENING, CONNECTED } State;
enum State { CLOSED, DISCONNECTED, LISTENING, CONNECTED };
protected:
int mSd;
State mState;
SockAddr *mLocalAddr;
SockAddr *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 ) {
Socket() : CommsBase(mSd, mSd),
mSd(-1),
mState(CLOSED),
mLocalAddr(nullptr),
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);
}
virtual ~Socket() {
close();
delete mLocalAddr;
@ -238,81 +208,67 @@ protected:
}
public:
bool isOpen() const {
return( !isClosed() );
}
bool isClosed() const {
return( mState == CLOSED );
}
bool isDisconnected() const {
return( mState == DISCONNECTED );
}
bool isConnected() const {
return( mState == CONNECTED );
}
bool isOpen() const { return !isClosed(); }
bool isClosed() const { return mState == CLOSED; }
bool isDisconnected() const { return mState == DISCONNECTED; }
bool isConnected() const { return mState == CONNECTED; }
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 {
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));
return( nBytes );
}
return nBytes;
}
virtual int recv(void *msg, int len) const {
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));
return( nBytes );
}
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 );
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 );
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
return( nBytes );
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 );
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
return( nBytes );
return nBytes;
}
virtual int bytesToRead() const;
int getDesc() const {
return( mSd );
}
int getDesc() const { return mSd; }
//virtual bool isOpen() const
//{
//return( mSd != -1 );
@ -322,12 +278,8 @@ public:
virtual int getType() const = 0;
virtual const char *getProtocol() const = 0;
const SockAddr *getLocalAddr() const {
return( mLocalAddr );
}
const SockAddr *getRemoteAddr() const {
return( mRemoteAddr );
}
const SockAddr *getLocalAddr() const { return mLocalAddr; }
const SockAddr *getRemoteAddr() const { return mRemoteAddr; }
virtual socklen_t getAddrSize() const = 0;
bool getBlocking(bool &blocking);
@ -344,20 +296,28 @@ public:
bool getNoDelay(bool &) const;
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
{
protected:
int mAddressFamily;
class InetSocket : virtual public Socket {
public:
int getDomain() const {
return( mAddressFamily );
}
virtual socklen_t getAddrSize() const {
return( SockAddrInet::addrSize() );
}
int getDomain() const { return mAddressFamily; }
virtual socklen_t getAddrSize() const { return SockAddrInet::addrSize(); }
protected:
bool connect(const char *host, const char *serv);
@ -367,227 +327,217 @@ protected:
bool bind(const char *host, int port);
bool bind(const char *serv);
bool bind(int port);
int mAddressFamily;
};
class UnixSocket : virtual public Socket {
public:
int getDomain() const {
return( AF_UNIX );
}
virtual socklen_t getAddrSize() const {
return( SockAddrUnix::addrSize() );
}
int getDomain() const { return AF_UNIX; }
virtual socklen_t getAddrSize() const { return SockAddrUnix::addrSize(); }
protected:
bool resolveLocal(const char *serv, const char *proto) {
SockAddrUnix *addr = new SockAddrUnix;
mLocalAddr = addr;
return( addr->resolve( serv, proto ) );
return addr->resolve(serv, proto);
}
bool resolveRemote(const char *path, const char *proto) {
SockAddrUnix *addr = new SockAddrUnix;
mRemoteAddr = addr;
return( addr->resolve( path, proto ) );
return addr->resolve(path, proto);
}
protected:
bool bind(const char *path) {
if (!UnixSocket::resolveLocal(path, getProtocol()))
return( false );
return( Socket::bind() );
return false;
return Socket::bind();
}
bool connect(const char *path) {
if (!UnixSocket::resolveRemote(path, getProtocol()))
return( false );
return( Socket::connect() );
return false;
return Socket::connect();
}
};
class UdpSocket : virtual public Socket {
public:
int getType() const {
return( SOCK_DGRAM );
}
const char *getProtocol() const {
return( "udp" );
}
int getType() const { return SOCK_DGRAM; }
const char *getProtocol() const { return "udp"; }
public:
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 )
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 {
return nBytes;
}
virtual int recvfrom(void *msg, int len, SockAddr *addr = nullptr) const {
ssize_t nBytes = 0;
if (addr) {
struct sockaddr sockAddr;
sockaddr sockAddr;
socklen_t sockLen;
nBytes = ::recvfrom(mSd, msg, len, 0, &sockAddr, &sockLen);
if (nBytes < 0) {
Debug(1, "Recvfrom of %d bytes max on sd %d (with address) failed: %s", len, mSd, strerror(errno));
}
} else {
nBytes = ::recvfrom( mSd, msg, len, 0, nullptr, 0 );
if ( nBytes < 0 )
nBytes = ::recvfrom(mSd, msg, len, 0, nullptr, nullptr);
if (nBytes < 0) {
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 {
public:
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 ) );
return InetSocket::bind(host, port);
}
bool bind(const char *serv) {
return( InetSocket::bind( serv ) );
return InetSocket::bind(serv);
}
bool bind(int port) {
return( InetSocket::bind( port ) );
return InetSocket::bind(port);
}
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) {
return( InetSocket::connect( host, port ) );
return InetSocket::connect(host, port);
}
};
class UdpUnixSocket : virtual public UdpSocket, virtual public UnixSocket {
public:
bool bind(const char *path) {
return( UnixSocket::bind( path ) );
return UnixSocket::bind(path);
}
bool connect(const char *path) {
return( UnixSocket::connect( path ) );
return UnixSocket::connect(path);
}
};
class UdpInetClient : public UdpInetSocket {
public:
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 ) );
return UdpInetSocket::connect(host, port);
}
};
class UdpUnixClient : public UdpUnixSocket {
public:
bool bind(const char *path) {
return( UdpUnixSocket::bind( path ) );
return UdpUnixSocket::bind(path);
}
public:
bool connect(const char *path) {
return( UdpUnixSocket::connect( path) );
return UdpUnixSocket::connect(path);
}
};
class UdpInetServer : public UdpInetSocket {
public:
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 ) );
return UdpInetSocket::bind(host, port);
}
bool bind(const char *serv) {
return( UdpInetSocket::bind( serv ) );
return UdpInetSocket::bind(serv);
}
bool bind(int port) {
return( UdpInetSocket::bind( port ) );
return UdpInetSocket::bind(port);
}
protected:
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 ) );
return UdpInetSocket::connect(host, port);
}
};
class UdpUnixServer : public UdpUnixSocket {
public:
bool bind(const char *path) {
return( UdpUnixSocket::bind( path ) );
return UdpUnixSocket::bind(path);
}
protected:
bool connect(const char *path) {
return( UdpUnixSocket::connect( path ) );
return UdpUnixSocket::connect(path);
}
};
class TcpSocket : virtual public Socket {
public:
TcpSocket() {
}
TcpSocket( const TcpSocket &socket, int newSd ) : Socket( socket, newSd ) {
}
TcpSocket() {}
TcpSocket(const TcpSocket &socket, int newSd) : Socket(socket, newSd) {}
public:
int getType() const {
return( SOCK_STREAM );
}
const char *getProtocol() const {
return( "tcp" );
}
int getType() const { return SOCK_STREAM; }
const char *getProtocol() const { return "tcp"; }
};
class TcpInetSocket : virtual public TcpSocket, virtual public InetSocket {
public:
TcpInetSocket() {
}
TcpInetSocket( const TcpInetSocket &socket, int newSd ) : TcpSocket( socket, newSd ) {
}
TcpInetSocket() {}
TcpInetSocket(const TcpInetSocket &socket, int newSd)
: TcpSocket(socket, newSd) {}
};
class TcpUnixSocket : virtual public TcpSocket, virtual public UnixSocket {
public:
TcpUnixSocket() {
}
TcpUnixSocket( const TcpUnixSocket &socket, int newSd ) : TcpSocket( socket, newSd ) {
}
TcpUnixSocket() {}
TcpUnixSocket(const TcpUnixSocket &socket, int newSd)
: TcpSocket(socket, newSd) {}
};
class TcpInetClient : public TcpInetSocket {
public:
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 ) );
return TcpInetSocket::connect(host, port);
}
};
class TcpUnixClient : public TcpUnixSocket {
public:
bool connect( const char *path ) {
return( TcpUnixSocket::connect( path) );
}
bool connect(const char *path) { return TcpUnixSocket::connect(path); }
};
class TcpInetServer : public TcpInetSocket {
public:
bool bind( int port ) {
return( TcpInetSocket::bind( port ) );
}
bool bind(int port) { return TcpInetSocket::bind(port); }
public:
bool isListening() const { return( Socket::isListening() ); }
bool isListening() const { return Socket::isListening(); }
bool listen();
bool accept();
bool accept(TcpInetSocket *&newSocket);
@ -595,12 +545,9 @@ public:
class TcpUnixServer : public TcpUnixSocket {
public:
bool bind( const char *path ) {
return( TcpUnixSocket::bind( path ) );
}
bool bind(const char *path) { return TcpUnixSocket::bind(path); }
public:
bool isListening() const { return( Socket::isListening() ); }
bool isListening() const { return Socket::isListening(); }
bool listen();
bool accept();
bool accept(TcpUnixSocket *&newSocket);
@ -611,24 +558,14 @@ public:
typedef std::set<CommsBase *> CommsSet;
typedef std::vector<CommsBase *> CommsList;
protected:
CommsSet mReaders;
CommsSet mWriters;
CommsList mReadable;
CommsList mWriteable;
bool mHasTimeout;
struct timeval mTimeout;
int mMaxFd;
public:
Select();
explicit Select( struct timeval timeout );
explicit Select( int timeout );
explicit Select( double timeout );
Select() : mHasTimeout(false), mMaxFd(-1) {}
explicit Select(timeval timeout) : mMaxFd(-1) { setTimeout(timeout); }
explicit Select(int timeout) : mMaxFd(-1) { setTimeout(timeout); }
explicit Select(double timeout) : mMaxFd(-1) { setTimeout(timeout); }
void setTimeout(int timeout);
void setTimeout(double timeout);
void setTimeout( struct timeval timeout );
void setTimeout(timeval timeout);
void clearTimeout();
void calcMaxFd();
@ -643,8 +580,17 @@ public:
int wait();
const CommsList &getReadable() const;
const CommsList &getWriteable() const;
const CommsList &getReadable() const { return mReadable; }
const CommsList &getWriteable() const { return mWriteable; }
protected:
CommsSet mReaders;
CommsSet mWriters;
CommsList mReadable;
CommsList mWriteable;
bool mHasTimeout;
timeval mTimeout;
int mMaxFd;
};
}