You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-30 19:23:07 +03:00
clang format apply
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -14,70 +14,70 @@
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
|
||||
#include "bytestreampool.h"
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
ByteStreamPool::ByteStreamPool()
|
||||
{
|
||||
maxBufferSize = 1 << 20; // 1MB
|
||||
maxFreeBuffers = 10;
|
||||
maxBufferSize = 1 << 20; // 1MB
|
||||
maxFreeBuffers = 10;
|
||||
}
|
||||
|
||||
ByteStreamPool::ByteStreamPool(uint largeBufferSize)
|
||||
{
|
||||
maxBufferSize = largeBufferSize;
|
||||
maxFreeBuffers = 10;
|
||||
maxBufferSize = largeBufferSize;
|
||||
maxFreeBuffers = 10;
|
||||
}
|
||||
|
||||
ByteStreamPool::ByteStreamPool(uint largeBufferSize, uint freeBufferLimit)
|
||||
{
|
||||
maxBufferSize = largeBufferSize;
|
||||
maxFreeBuffers = freeBufferLimit;
|
||||
maxBufferSize = largeBufferSize;
|
||||
maxFreeBuffers = freeBufferLimit;
|
||||
}
|
||||
|
||||
ByteStreamPool::~ByteStreamPool()
|
||||
{
|
||||
while (!freeByteStreams.empty())
|
||||
{
|
||||
ByteStream *next = freeByteStreams.front();
|
||||
freeByteStreams.pop_front();
|
||||
delete next;
|
||||
}
|
||||
while (!freeByteStreams.empty())
|
||||
{
|
||||
ByteStream* next = freeByteStreams.front();
|
||||
freeByteStreams.pop_front();
|
||||
delete next;
|
||||
}
|
||||
}
|
||||
|
||||
ByteStream * ByteStreamPool::getByteStream()
|
||||
ByteStream* ByteStreamPool::getByteStream()
|
||||
{
|
||||
boost::mutex::scoped_lock s(mutex);
|
||||
ByteStream* ret;
|
||||
|
||||
if (!freeByteStreams.empty())
|
||||
{
|
||||
ret = freeByteStreams.front();
|
||||
freeByteStreams.pop_front();
|
||||
}
|
||||
else
|
||||
ret = new ByteStream();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ByteStreamPool::returnByteStream(ByteStream* bs)
|
||||
{
|
||||
if (bs->getBufferSize() > maxBufferSize)
|
||||
delete bs;
|
||||
else
|
||||
{
|
||||
boost::mutex::scoped_lock s(mutex);
|
||||
ByteStream *ret;
|
||||
|
||||
if (!freeByteStreams.empty())
|
||||
{
|
||||
ret = freeByteStreams.front();
|
||||
freeByteStreams.pop_front();
|
||||
}
|
||||
if (freeByteStreams.size() > maxFreeBuffers)
|
||||
delete bs;
|
||||
else
|
||||
ret = new ByteStream();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ByteStreamPool::returnByteStream(ByteStream *bs)
|
||||
{
|
||||
if (bs->getBufferSize() > maxBufferSize)
|
||||
delete bs;
|
||||
else
|
||||
{
|
||||
boost::mutex::scoped_lock s(mutex);
|
||||
if (freeByteStreams.size() > maxFreeBuffers)
|
||||
delete bs;
|
||||
else {
|
||||
bs->restart();
|
||||
freeByteStreams.push_back(bs);
|
||||
}
|
||||
bs->restart();
|
||||
freeByteStreams.push_back(bs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace messageqcpp
|
||||
|
@ -14,12 +14,12 @@
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
/* This class defines a pool of bytestreams to improve BS reuse, reducing
|
||||
the need to use the dynamic allocator. It allocates as many BS's as needed,
|
||||
and automatically disposes of buffers that are 'large' to reduce mem usage.
|
||||
and automatically disposes of buffers that are 'large' to reduce mem usage.
|
||||
Initially, 'large' is defined as 1MB.
|
||||
*/
|
||||
|
||||
@ -29,24 +29,23 @@ Initially, 'large' is defined as 1MB.
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
class ByteStreamPool
|
||||
{
|
||||
public:
|
||||
ByteStreamPool();
|
||||
ByteStreamPool(uint largeBufferSize); // BS's above largeBufferSize get deallocated on return to the pool
|
||||
ByteStreamPool(uint largeBufferSize, uint freeBufferLimit); // freeBufferLimit is the max # of BSs to reserve
|
||||
virtual ~ByteStreamPool();
|
||||
|
||||
ByteStream * getByteStream();
|
||||
void returnByteStream(ByteStream *);
|
||||
|
||||
private:
|
||||
std::deque<ByteStream *> freeByteStreams;
|
||||
boost::mutex mutex;
|
||||
uint maxBufferSize;
|
||||
uint maxFreeBuffers;
|
||||
public:
|
||||
ByteStreamPool();
|
||||
ByteStreamPool(uint largeBufferSize); // BS's above largeBufferSize get deallocated on return to the pool
|
||||
ByteStreamPool(uint largeBufferSize,
|
||||
uint freeBufferLimit); // freeBufferLimit is the max # of BSs to reserve
|
||||
virtual ~ByteStreamPool();
|
||||
|
||||
ByteStream* getByteStream();
|
||||
void returnByteStream(ByteStream*);
|
||||
|
||||
private:
|
||||
std::deque<ByteStream*> freeByteStreams;
|
||||
boost::mutex mutex;
|
||||
uint maxBufferSize;
|
||||
uint maxFreeBuffers;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -10,31 +10,30 @@ using namespace config;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Config* cf = Config::makeConfig("./Columnstore.xml");
|
||||
MessageQueueClient mqc("server1", cf);
|
||||
Config* cf = Config::makeConfig("./Columnstore.xml");
|
||||
MessageQueueClient mqc("server1", cf);
|
||||
|
||||
ByteStream obs;
|
||||
string msg("Hello, world!");
|
||||
ByteStream ibs;
|
||||
uint32_t qb;
|
||||
ByteStream obs;
|
||||
string msg("Hello, world!");
|
||||
ByteStream ibs;
|
||||
uint32_t qb;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
obs.restart();
|
||||
obs << msg;
|
||||
cout << "writing " << obs.length() << " bytes to " << mqc.addr2String() << endl;
|
||||
mqc.write(obs);
|
||||
ibs = mqc.read();
|
||||
ibs >> qb;
|
||||
|
||||
if (qb != 0)
|
||||
{
|
||||
obs.restart();
|
||||
obs << msg;
|
||||
cout << "writing " << obs.length() << " bytes to " << mqc.addr2String() << endl;
|
||||
mqc.write(obs);
|
||||
ibs = mqc.read();
|
||||
ibs >> qb;
|
||||
|
||||
if (qb != 0)
|
||||
{
|
||||
string emsg("server did not ack message!");
|
||||
cerr << emsg << endl;
|
||||
throw runtime_error(emsg);
|
||||
}
|
||||
string emsg("server did not ack message!");
|
||||
cerr << emsg << endl;
|
||||
throw runtime_error(emsg);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id$
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id$
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
#include "mcsconfig.h"
|
||||
|
||||
#include <stdexcept>
|
||||
@ -59,99 +59,99 @@ using namespace compress;
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
CompressedInetStreamSocket::CompressedInetStreamSocket()
|
||||
{
|
||||
config::Config* config = config::Config::makeConfig();
|
||||
string val;
|
||||
string compressionType;
|
||||
config::Config* config = config::Config::makeConfig();
|
||||
string val;
|
||||
string compressionType;
|
||||
|
||||
try
|
||||
{
|
||||
val = config->getConfig("NetworkCompression", "Enabled");
|
||||
}
|
||||
catch (...) { }
|
||||
try
|
||||
{
|
||||
val = config->getConfig("NetworkCompression", "Enabled");
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
if (val == "" || val == "Y")
|
||||
useCompression = true;
|
||||
else
|
||||
useCompression = false;
|
||||
if (val == "" || val == "Y")
|
||||
useCompression = true;
|
||||
else
|
||||
useCompression = false;
|
||||
|
||||
try
|
||||
{
|
||||
compressionType =
|
||||
config->getConfig("NetworkCompression", "NetworkCompression");
|
||||
}
|
||||
catch (...) { }
|
||||
try
|
||||
{
|
||||
compressionType = config->getConfig("NetworkCompression", "NetworkCompression");
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
auto* compressInterface = compress::getCompressInterfaceByName(compressionType);
|
||||
if (!compressInterface)
|
||||
compressInterface = new compress::CompressInterfaceSnappy();
|
||||
auto* compressInterface = compress::getCompressInterfaceByName(compressionType);
|
||||
if (!compressInterface)
|
||||
compressInterface = new compress::CompressInterfaceSnappy();
|
||||
|
||||
alg.reset(compressInterface);
|
||||
alg.reset(compressInterface);
|
||||
}
|
||||
|
||||
Socket* CompressedInetStreamSocket::clone() const
|
||||
{
|
||||
return new CompressedInetStreamSocket(*this);
|
||||
return new CompressedInetStreamSocket(*this);
|
||||
}
|
||||
|
||||
const SBS CompressedInetStreamSocket::read(const struct timespec* timeout, bool* isTimeOut,
|
||||
Stats* stats) const
|
||||
Stats* stats) const
|
||||
{
|
||||
SBS readBS, ret;
|
||||
size_t uncompressedSize;
|
||||
SBS readBS, ret;
|
||||
size_t uncompressedSize;
|
||||
|
||||
readBS = InetStreamSocket::read(timeout, isTimeOut, stats);
|
||||
readBS = InetStreamSocket::read(timeout, isTimeOut, stats);
|
||||
|
||||
if (readBS->length() == 0 || fMagicBuffer == BYTESTREAM_MAGIC)
|
||||
return readBS;
|
||||
if (readBS->length() == 0 || fMagicBuffer == BYTESTREAM_MAGIC)
|
||||
return readBS;
|
||||
|
||||
// Read stored len, first 4 bytes.
|
||||
uint32_t storedLen = *(uint32_t*) readBS->buf();
|
||||
// Read stored len, first 4 bytes.
|
||||
uint32_t storedLen = *(uint32_t*)readBS->buf();
|
||||
|
||||
if (!storedLen)
|
||||
return SBS(new ByteStream(0));
|
||||
if (!storedLen)
|
||||
return SBS(new ByteStream(0));
|
||||
|
||||
uncompressedSize = storedLen;
|
||||
ret.reset(new ByteStream(uncompressedSize));
|
||||
uncompressedSize = storedLen;
|
||||
ret.reset(new ByteStream(uncompressedSize));
|
||||
|
||||
alg->uncompress((char*) readBS->buf() + HEADER_SIZE,
|
||||
readBS->length() - HEADER_SIZE, (char*) ret->getInputPtr(),
|
||||
&uncompressedSize);
|
||||
alg->uncompress((char*)readBS->buf() + HEADER_SIZE, readBS->length() - HEADER_SIZE,
|
||||
(char*)ret->getInputPtr(), &uncompressedSize);
|
||||
|
||||
ret->advanceInputPtr(uncompressedSize);
|
||||
ret->advanceInputPtr(uncompressedSize);
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CompressedInetStreamSocket::write(const ByteStream& msg, Stats* stats)
|
||||
{
|
||||
size_t len = msg.length();
|
||||
size_t len = msg.length();
|
||||
|
||||
if (useCompression && (len > 512))
|
||||
{
|
||||
size_t outLen = alg->maxCompressedSize(len) + HEADER_SIZE;
|
||||
ByteStream smsg(outLen);
|
||||
if (useCompression && (len > 512))
|
||||
{
|
||||
size_t outLen = alg->maxCompressedSize(len) + HEADER_SIZE;
|
||||
ByteStream smsg(outLen);
|
||||
|
||||
alg->compress((char*) msg.buf(), len,
|
||||
(char*) smsg.getInputPtr() + HEADER_SIZE, &outLen);
|
||||
// Save original len.
|
||||
*(uint32_t*) smsg.getInputPtr() = len;
|
||||
smsg.advanceInputPtr(outLen + HEADER_SIZE);
|
||||
alg->compress((char*)msg.buf(), len, (char*)smsg.getInputPtr() + HEADER_SIZE, &outLen);
|
||||
// Save original len.
|
||||
*(uint32_t*)smsg.getInputPtr() = len;
|
||||
smsg.advanceInputPtr(outLen + HEADER_SIZE);
|
||||
|
||||
if (outLen < len)
|
||||
do_write(smsg, COMPRESSED_BYTESTREAM_MAGIC, stats);
|
||||
else
|
||||
InetStreamSocket::write(msg, stats);
|
||||
}
|
||||
if (outLen < len)
|
||||
do_write(smsg, COMPRESSED_BYTESTREAM_MAGIC, stats);
|
||||
else
|
||||
InetStreamSocket::write(msg, stats);
|
||||
InetStreamSocket::write(msg, stats);
|
||||
}
|
||||
else
|
||||
InetStreamSocket::write(msg, stats);
|
||||
}
|
||||
|
||||
void CompressedInetStreamSocket::write(SBS msg, Stats* stats)
|
||||
{
|
||||
write(*msg, stats);
|
||||
write(*msg, stats);
|
||||
}
|
||||
|
||||
/* this was cut & pasted from InetStreamSocket;
|
||||
@ -159,34 +159,33 @@ void CompressedInetStreamSocket::write(SBS msg, Stats* stats)
|
||||
*/
|
||||
const IOSocket CompressedInetStreamSocket::accept(const struct timespec* timeout)
|
||||
{
|
||||
int clientfd;
|
||||
long msecs = 0;
|
||||
int clientfd;
|
||||
long msecs = 0;
|
||||
|
||||
struct pollfd pfd[1];
|
||||
pfd[0].fd = socketParms().sd();
|
||||
pfd[0].events = POLLIN;
|
||||
struct pollfd pfd[1];
|
||||
pfd[0].fd = socketParms().sd();
|
||||
pfd[0].events = POLLIN;
|
||||
|
||||
if (timeout != 0)
|
||||
{
|
||||
msecs = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000;
|
||||
if (timeout != 0)
|
||||
{
|
||||
msecs = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000;
|
||||
|
||||
if (poll(pfd, 1, msecs) != 1 || (pfd[0].revents & POLLIN) == 0 ||
|
||||
pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL))
|
||||
return IOSocket(new CompressedInetStreamSocket());
|
||||
}
|
||||
if (poll(pfd, 1, msecs) != 1 || (pfd[0].revents & POLLIN) == 0 ||
|
||||
pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL))
|
||||
return IOSocket(new CompressedInetStreamSocket());
|
||||
}
|
||||
|
||||
struct sockaddr sa;
|
||||
struct sockaddr sa;
|
||||
|
||||
socklen_t sl = sizeof(sa);
|
||||
socklen_t sl = sizeof(sa);
|
||||
|
||||
int e;
|
||||
int e;
|
||||
|
||||
do
|
||||
{
|
||||
clientfd = ::accept(socketParms().sd(), &sa, &sl);
|
||||
e = errno;
|
||||
}
|
||||
while (clientfd < 0 && (e == EINTR ||
|
||||
do
|
||||
{
|
||||
clientfd = ::accept(socketParms().sd(), &sa, &sl);
|
||||
e = errno;
|
||||
} while (clientfd < 0 && (e == EINTR ||
|
||||
#ifdef ERESTART
|
||||
e == ERESTART ||
|
||||
#endif
|
||||
@ -195,90 +194,86 @@ const IOSocket CompressedInetStreamSocket::accept(const struct timespec* timeout
|
||||
#endif
|
||||
false));
|
||||
|
||||
if (clientfd < 0)
|
||||
{
|
||||
string msg = "CompressedInetStreamSocket::accept: accept() error: ";
|
||||
scoped_array<char> buf(new char[80]);
|
||||
if (clientfd < 0)
|
||||
{
|
||||
string msg = "CompressedInetStreamSocket::accept: accept() error: ";
|
||||
scoped_array<char> buf(new char[80]);
|
||||
#if STRERROR_R_CHAR_P
|
||||
const char* p;
|
||||
const char* p;
|
||||
|
||||
if ((p = strerror_r(e, buf.get(), 80)) != 0)
|
||||
msg += p;
|
||||
if ((p = strerror_r(e, buf.get(), 80)) != 0)
|
||||
msg += p;
|
||||
|
||||
#else
|
||||
int p;
|
||||
int p;
|
||||
|
||||
if ((p = strerror_r(e, buf.get(), 80)) == 0)
|
||||
msg += buf.get();
|
||||
if ((p = strerror_r(e, buf.get(), 80)) == 0)
|
||||
msg += buf.get();
|
||||
|
||||
#endif
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
|
||||
if (fSyncProto)
|
||||
if (fSyncProto)
|
||||
{
|
||||
/* send a byte to artificially synchronize with connect() on the remote */
|
||||
char b = 'A';
|
||||
int ret;
|
||||
|
||||
ret = ::send(clientfd, &b, 1, 0);
|
||||
e = errno;
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
/* send a byte to artificially synchronize with connect() on the remote */
|
||||
char b = 'A';
|
||||
int ret;
|
||||
|
||||
ret = ::send(clientfd, &b, 1, 0);
|
||||
e = errno;
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
ostringstream os;
|
||||
char blah[80];
|
||||
ostringstream os;
|
||||
char blah[80];
|
||||
#if STRERROR_R_CHAR_P
|
||||
const char* p;
|
||||
const char* p;
|
||||
|
||||
if ((p = strerror_r(e, blah, 80)) != 0)
|
||||
os << "CompressedInetStreamSocket::accept sync: " << p;
|
||||
if ((p = strerror_r(e, blah, 80)) != 0)
|
||||
os << "CompressedInetStreamSocket::accept sync: " << p;
|
||||
|
||||
#else
|
||||
int p;
|
||||
int p;
|
||||
|
||||
if ((p = strerror_r(e, blah, 80)) == 0)
|
||||
os << "CompressedInetStreamSocket::accept sync: " << blah;
|
||||
if ((p = strerror_r(e, blah, 80)) == 0)
|
||||
os << "CompressedInetStreamSocket::accept sync: " << blah;
|
||||
|
||||
#endif
|
||||
::close(clientfd);
|
||||
throw runtime_error(os.str());
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
::close(clientfd);
|
||||
throw runtime_error("CompressedInetStreamSocket::accept sync: got unexpected error code");
|
||||
}
|
||||
::close(clientfd);
|
||||
throw runtime_error(os.str());
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
::close(clientfd);
|
||||
throw runtime_error("CompressedInetStreamSocket::accept sync: got unexpected error code");
|
||||
}
|
||||
}
|
||||
|
||||
CompressedInetStreamSocket* ciss = new CompressedInetStreamSocket();
|
||||
IOSocket ios;
|
||||
sockaddr_in* sin = (sockaddr_in*) &sa;
|
||||
CompressedInetStreamSocket* ciss = new CompressedInetStreamSocket();
|
||||
IOSocket ios;
|
||||
sockaddr_in* sin = (sockaddr_in*)&sa;
|
||||
|
||||
if ((sin->sin_addr.s_addr == fSa.sin_addr.s_addr) ||
|
||||
sin->sin_addr.s_addr == inet_addr("127.0.0.1"))
|
||||
ciss->useCompression = false;
|
||||
if ((sin->sin_addr.s_addr == fSa.sin_addr.s_addr) || sin->sin_addr.s_addr == inet_addr("127.0.0.1"))
|
||||
ciss->useCompression = false;
|
||||
|
||||
ios.setSocketImpl(ciss);
|
||||
SocketParms sp;
|
||||
sp = ios.socketParms();
|
||||
sp.sd(clientfd);
|
||||
ios.socketParms(sp);
|
||||
ios.sa(&sa);
|
||||
return ios;
|
||||
ios.setSocketImpl(ciss);
|
||||
SocketParms sp;
|
||||
sp = ios.socketParms();
|
||||
sp.sd(clientfd);
|
||||
ios.socketParms(sp);
|
||||
ios.sa(&sa);
|
||||
return ios;
|
||||
}
|
||||
|
||||
void CompressedInetStreamSocket::connect(const sockaddr* serv_addr)
|
||||
{
|
||||
sockaddr_in* sin = (sockaddr_in*) serv_addr;
|
||||
sockaddr_in* sin = (sockaddr_in*)serv_addr;
|
||||
|
||||
if (sin->sin_addr.s_addr == fSa.sin_addr.s_addr ||
|
||||
sin->sin_addr.s_addr == inet_addr("127.0.0.1"))
|
||||
useCompression = false;
|
||||
if (sin->sin_addr.s_addr == fSa.sin_addr.s_addr || sin->sin_addr.s_addr == inet_addr("127.0.0.1"))
|
||||
useCompression = false;
|
||||
|
||||
InetStreamSocket::connect(serv_addr);
|
||||
InetStreamSocket::connect(serv_addr);
|
||||
}
|
||||
|
||||
|
||||
} //namespace messageqcpp
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id$
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id$
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -36,29 +36,28 @@
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
class CompressedInetStreamSocket : public InetStreamSocket
|
||||
{
|
||||
public:
|
||||
CompressedInetStreamSocket();
|
||||
CompressedInetStreamSocket(const CompressedInetStreamSocket&) = default;
|
||||
virtual ~CompressedInetStreamSocket(){};
|
||||
public:
|
||||
CompressedInetStreamSocket();
|
||||
CompressedInetStreamSocket(const CompressedInetStreamSocket&) = default;
|
||||
virtual ~CompressedInetStreamSocket(){};
|
||||
|
||||
using InetStreamSocket::operator=;
|
||||
virtual Socket* clone() const;
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const;
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL);
|
||||
virtual void write(SBS msg, Stats* stats = NULL);
|
||||
virtual const IOSocket accept(const struct timespec* timeout);
|
||||
virtual void connect(const sockaddr* addr);
|
||||
private:
|
||||
std::shared_ptr<compress::CompressInterface> alg;
|
||||
bool useCompression;
|
||||
static const uint32_t HEADER_SIZE = 4;
|
||||
using InetStreamSocket::operator=;
|
||||
virtual Socket* clone() const;
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const;
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL);
|
||||
virtual void write(SBS msg, Stats* stats = NULL);
|
||||
virtual const IOSocket accept(const struct timespec* timeout);
|
||||
virtual void connect(const sockaddr* addr);
|
||||
|
||||
private:
|
||||
std::shared_ptr<compress::CompressInterface> alg;
|
||||
bool useCompression;
|
||||
static const uint32_t HEADER_SIZE = 4;
|
||||
};
|
||||
|
||||
} //namespace messageqcpp
|
||||
} // namespace messageqcpp
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: inetstreamsocket.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: inetstreamsocket.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -55,223 +55,224 @@ const uint32_t COMPRESSED_BYTESTREAM_MAGIC = 0x14fbc138;
|
||||
*/
|
||||
class InetStreamSocket : public Socket
|
||||
{
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit InetStreamSocket(size_t blocksize = ByteStream::BlockSize);
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit InetStreamSocket(size_t blocksize = ByteStream::BlockSize);
|
||||
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~InetStreamSocket();
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~InetStreamSocket();
|
||||
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
InetStreamSocket(const InetStreamSocket& rhs);
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
InetStreamSocket(const InetStreamSocket& rhs);
|
||||
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
virtual InetStreamSocket& operator=(const InetStreamSocket& rhs);
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
virtual InetStreamSocket& operator=(const InetStreamSocket& rhs);
|
||||
|
||||
/** fSocket mutator
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socket);
|
||||
/** fSocket mutator
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socket);
|
||||
|
||||
/** fSocket accessor
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
/** fSocket accessor
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
|
||||
/** sockaddr mutator
|
||||
*
|
||||
*/
|
||||
inline virtual void sa(const sockaddr* sa);
|
||||
/** sockaddr mutator
|
||||
*
|
||||
*/
|
||||
inline virtual void sa(const sockaddr* sa);
|
||||
|
||||
/** call socket() to get a sd
|
||||
*
|
||||
*/
|
||||
virtual void open();
|
||||
/** call socket() to get a sd
|
||||
*
|
||||
*/
|
||||
virtual void open();
|
||||
|
||||
/** close the sd
|
||||
*
|
||||
*/
|
||||
virtual void close();
|
||||
/** close the sd
|
||||
*
|
||||
*/
|
||||
virtual void close();
|
||||
|
||||
/** test if this socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
/** test if this socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
|
||||
/** read a message from the socket
|
||||
*
|
||||
* wait for and return a message from the socket. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has milisecond resolution.
|
||||
* @warning If you specify a timeout, the stream can be corrupted in certain
|
||||
* extreme circumstances. The circumstance: receiving a portion of the message
|
||||
* followed by a timeout. If the rest of the message is ever received, it
|
||||
* will be misinterpreted by the following read(). Symptom: The caller will
|
||||
* receive an incomplete ByteStream
|
||||
* (do try-catch around all ">>" operations to detect underflow). Mitigation:
|
||||
* the caller should not perform another read(). Caller should close the connection.
|
||||
* The behavior will be unpredictable and possibly fatal.
|
||||
* @note A fix is being reviewed but this is low-priority.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL, Stats* stats = NULL) const;
|
||||
/** read a message from the socket
|
||||
*
|
||||
* wait for and return a message from the socket. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has milisecond resolution.
|
||||
* @warning If you specify a timeout, the stream can be corrupted in certain
|
||||
* extreme circumstances. The circumstance: receiving a portion of the message
|
||||
* followed by a timeout. If the rest of the message is ever received, it
|
||||
* will be misinterpreted by the following read(). Symptom: The caller will
|
||||
* receive an incomplete ByteStream
|
||||
* (do try-catch around all ">>" operations to detect underflow). Mitigation:
|
||||
* the caller should not perform another read(). Caller should close the connection.
|
||||
* The behavior will be unpredictable and possibly fatal.
|
||||
* @note A fix is being reviewed but this is low-priority.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const;
|
||||
|
||||
/** write a message to the socket
|
||||
*
|
||||
* write a message to the socket
|
||||
*/
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL);
|
||||
virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
/** write a message to the socket
|
||||
*
|
||||
* write a message to the socket
|
||||
*/
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL);
|
||||
virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
|
||||
/** this version of write takes ownership of the bytestream
|
||||
*/
|
||||
virtual void write(SBS msg, Stats* stats = NULL);
|
||||
/** this version of write takes ownership of the bytestream
|
||||
*/
|
||||
virtual void write(SBS msg, Stats* stats = NULL);
|
||||
|
||||
/** bind to a port
|
||||
*
|
||||
*/
|
||||
virtual void bind(const sockaddr* serv_addr);
|
||||
/** bind to a port
|
||||
*
|
||||
*/
|
||||
virtual void bind(const sockaddr* serv_addr);
|
||||
|
||||
/** listen for connections
|
||||
*
|
||||
*/
|
||||
virtual void listen(int backlog = 5);
|
||||
/** listen for connections
|
||||
*
|
||||
*/
|
||||
virtual void listen(int backlog = 5);
|
||||
|
||||
/** return an (accepted) IOSocket ready for I/O
|
||||
*
|
||||
*/
|
||||
virtual const IOSocket accept(const struct timespec* timeout = 0);
|
||||
/** return an (accepted) IOSocket ready for I/O
|
||||
*
|
||||
*/
|
||||
virtual const IOSocket accept(const struct timespec* timeout = 0);
|
||||
|
||||
/** connect to a server socket
|
||||
*
|
||||
*/
|
||||
virtual void connect(const sockaddr* serv_addr);
|
||||
/** connect to a server socket
|
||||
*
|
||||
*/
|
||||
virtual void connect(const sockaddr* serv_addr);
|
||||
|
||||
/** dynamically allocate a copy of this object
|
||||
*
|
||||
*/
|
||||
virtual Socket* clone() const;
|
||||
/** dynamically allocate a copy of this object
|
||||
*
|
||||
*/
|
||||
virtual Socket* clone() const;
|
||||
|
||||
/** get a string rep of the object
|
||||
*
|
||||
*/
|
||||
virtual const std::string toString() const;
|
||||
/** get a string rep of the object
|
||||
*
|
||||
*/
|
||||
virtual const std::string toString() const;
|
||||
|
||||
/** set the connection timeout (in ms)
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct ::timespec* timeout)
|
||||
{
|
||||
if (timeout) fConnectionTimeout = *timeout;
|
||||
}
|
||||
/** set the connection timeout (in ms)
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct ::timespec* timeout)
|
||||
{
|
||||
if (timeout)
|
||||
fConnectionTimeout = *timeout;
|
||||
}
|
||||
|
||||
/** set the connection protocol to be synchronous
|
||||
*
|
||||
*/
|
||||
virtual void syncProto(bool use)
|
||||
{
|
||||
fSyncProto = use;
|
||||
}
|
||||
/** set the connection protocol to be synchronous
|
||||
*
|
||||
*/
|
||||
virtual void syncProto(bool use)
|
||||
{
|
||||
fSyncProto = use;
|
||||
}
|
||||
|
||||
int getConnectionNum() const
|
||||
{
|
||||
return fSocketParms.sd();
|
||||
}
|
||||
int getConnectionNum() const
|
||||
{
|
||||
return fSocketParms.sd();
|
||||
}
|
||||
|
||||
/* The caller needs to know when/if the remote closes the connection or sends data.
|
||||
* Returns 0 on timeout, 1 if there is data to read, 2 if the connection was dropped.
|
||||
* On error 3 is returned.
|
||||
*/
|
||||
static int pollConnection(int connectionNum, long msecs);
|
||||
/* The caller needs to know when/if the remote closes the connection or sends data.
|
||||
* Returns 0 on timeout, 1 if there is data to read, 2 if the connection was dropped.
|
||||
* On error 3 is returned.
|
||||
*/
|
||||
static int pollConnection(int connectionNum, long msecs);
|
||||
|
||||
/** return the address as a string
|
||||
*
|
||||
*/
|
||||
virtual const std::string addr2String() const;
|
||||
/** return the address as a string
|
||||
*
|
||||
*/
|
||||
virtual const std::string addr2String() const;
|
||||
|
||||
/** compare 2 addresses
|
||||
*
|
||||
*/
|
||||
virtual bool isSameAddr(const Socket* rhs) const;
|
||||
/** compare 2 addresses
|
||||
*
|
||||
*/
|
||||
virtual bool isSameAddr(const Socket* rhs) const;
|
||||
|
||||
/** ping an ip address
|
||||
*
|
||||
*/
|
||||
EXPORT static int ping(const std::string& ipaddr, const struct timespec* timeout = 0);
|
||||
/** ping an ip address
|
||||
*
|
||||
*/
|
||||
EXPORT static int ping(const std::string& ipaddr, const struct timespec* timeout = 0);
|
||||
|
||||
// Check if we are still connected
|
||||
virtual bool isConnected() const;
|
||||
// Check if we are still connected
|
||||
virtual bool isConnected() const;
|
||||
|
||||
// Check if the socket still has data pending
|
||||
// Check if the socket still has data pending
|
||||
|
||||
virtual bool hasData() const;
|
||||
virtual bool hasData() const;
|
||||
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
protected:
|
||||
static const int KERR_ERESTARTSYS = 512;
|
||||
protected:
|
||||
static const int KERR_ERESTARTSYS = 512;
|
||||
|
||||
void logIoError(const char* errMsg, int errNum) const;
|
||||
void logIoError(const char* errMsg, int errNum) const;
|
||||
|
||||
/** Empty the stream up to the beginning of the next ByteStream.
|
||||
*
|
||||
* Reads until the beginning of the next ByteStream is found.
|
||||
* @param msecs An optional timeout value.
|
||||
* @param residual Pass in an array of at least 8 bytes, on return it will contain
|
||||
* the first bytes of the stream.
|
||||
* @param reslen On return, it will contain the # of bytes in residual.
|
||||
* @return true if the next byte in the stream is the beginning of a ByteStream,
|
||||
* false otherwise.
|
||||
*/
|
||||
virtual bool readToMagic(long msecs, bool* isTimeOut, Stats* stats) const;
|
||||
/** Empty the stream up to the beginning of the next ByteStream.
|
||||
*
|
||||
* Reads until the beginning of the next ByteStream is found.
|
||||
* @param msecs An optional timeout value.
|
||||
* @param residual Pass in an array of at least 8 bytes, on return it will contain
|
||||
* the first bytes of the stream.
|
||||
* @param reslen On return, it will contain the # of bytes in residual.
|
||||
* @return true if the next byte in the stream is the beginning of a ByteStream,
|
||||
* false otherwise.
|
||||
*/
|
||||
virtual bool readToMagic(long msecs, bool* isTimeOut, Stats* stats) const;
|
||||
|
||||
void do_write(const ByteStream& msg, uint32_t magic, Stats* stats = NULL) const;
|
||||
ssize_t written(int fd, const uint8_t* ptr, size_t nbytes) const;
|
||||
bool readFixedSizeData(struct pollfd* pfd, uint8_t* buffer, const size_t numberOfBytes,
|
||||
const struct ::timespec* timeout, bool* isTimeOut, Stats* stats,
|
||||
int64_t msec) const;
|
||||
void do_write(const ByteStream& msg, uint32_t magic, Stats* stats = NULL) const;
|
||||
ssize_t written(int fd, const uint8_t* ptr, size_t nbytes) const;
|
||||
bool readFixedSizeData(struct pollfd* pfd, uint8_t* buffer, const size_t numberOfBytes,
|
||||
const struct ::timespec* timeout, bool* isTimeOut, Stats* stats, int64_t msec) const;
|
||||
|
||||
SocketParms fSocketParms; /// The socket parms
|
||||
size_t fBlocksize;
|
||||
sockaddr_in fSa;
|
||||
SocketParms fSocketParms; /// The socket parms
|
||||
size_t fBlocksize;
|
||||
sockaddr_in fSa;
|
||||
|
||||
// how long to wait for a connect() call to complete (in ms)
|
||||
struct ::timespec fConnectionTimeout;
|
||||
// how long to wait for a connect() call to complete (in ms)
|
||||
struct ::timespec fConnectionTimeout;
|
||||
|
||||
// use sync proto
|
||||
bool fSyncProto;
|
||||
// use sync proto
|
||||
bool fSyncProto;
|
||||
|
||||
/// The buffer used to scan for the ByteStream magic in the stream.
|
||||
mutable uint32_t fMagicBuffer;
|
||||
/// The buffer used to scan for the ByteStream magic in the stream.
|
||||
mutable uint32_t fMagicBuffer;
|
||||
|
||||
private:
|
||||
void doCopy(const InetStreamSocket& rhs);
|
||||
private:
|
||||
void doCopy(const InetStreamSocket& rhs);
|
||||
};
|
||||
|
||||
inline bool InetStreamSocket::isOpen() const
|
||||
{
|
||||
return (fSocketParms.sd() >= 0);
|
||||
return (fSocketParms.sd() >= 0);
|
||||
}
|
||||
inline const SocketParms InetStreamSocket::socketParms() const
|
||||
{
|
||||
return fSocketParms;
|
||||
return fSocketParms;
|
||||
}
|
||||
inline void InetStreamSocket::socketParms(const SocketParms& socketParms)
|
||||
{
|
||||
fSocketParms = socketParms;
|
||||
fSocketParms = socketParms;
|
||||
}
|
||||
inline void InetStreamSocket::sa(const sockaddr* sa)
|
||||
{
|
||||
memcpy(&fSa, sa, sizeof(sockaddr_in));
|
||||
memcpy(&fSa, sa, sizeof(sockaddr_in));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -279,12 +280,10 @@ inline void InetStreamSocket::sa(const sockaddr* sa)
|
||||
*/
|
||||
inline std::ostream& operator<<(std::ostream& os, const InetStreamSocket& rhs)
|
||||
{
|
||||
os << rhs.toString();
|
||||
return os;
|
||||
os << rhs.toString();
|
||||
return os;
|
||||
}
|
||||
|
||||
} //namespace messageqcpp
|
||||
} // namespace messageqcpp
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: iosocket.cpp 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: iosocket.cpp 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
@ -42,58 +42,55 @@ using namespace std;
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
IOSocket::IOSocket(Socket* socket) :
|
||||
fSocket(socket), sockID(0)
|
||||
IOSocket::IOSocket(Socket* socket) : fSocket(socket), sockID(0)
|
||||
{
|
||||
memset(&fSa, 0, sizeof(fSa));
|
||||
memset(&fSa, 0, sizeof(fSa));
|
||||
}
|
||||
|
||||
IOSocket::~IOSocket()
|
||||
{
|
||||
delete fSocket;
|
||||
delete fSocket;
|
||||
}
|
||||
|
||||
void IOSocket::doCopy(const IOSocket& rhs)
|
||||
{
|
||||
fSocket = rhs.fSocket->clone();
|
||||
fSa = rhs.fSa;
|
||||
sockID = rhs.sockID;
|
||||
fSocket = rhs.fSocket->clone();
|
||||
fSa = rhs.fSa;
|
||||
sockID = rhs.sockID;
|
||||
}
|
||||
|
||||
IOSocket::IOSocket(const IOSocket& rhs)
|
||||
{
|
||||
doCopy(rhs);
|
||||
doCopy(rhs);
|
||||
}
|
||||
|
||||
IOSocket& IOSocket::operator=(const IOSocket& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
delete fSocket;
|
||||
doCopy(rhs);
|
||||
}
|
||||
if (this != &rhs)
|
||||
{
|
||||
delete fSocket;
|
||||
doCopy(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const string IOSocket::toString() const
|
||||
{
|
||||
#ifdef NOSSTREAM
|
||||
return "IOSocket";
|
||||
return "IOSocket";
|
||||
#else
|
||||
ostringstream oss;
|
||||
char buf[INET_ADDRSTRLEN];
|
||||
SocketParms sp = fSocket->socketParms();
|
||||
const sockaddr_in* sinp = reinterpret_cast<const sockaddr_in*>(&fSa);
|
||||
oss << "IOSocket: sd: " << sp.sd() <<
|
||||
ostringstream oss;
|
||||
char buf[INET_ADDRSTRLEN];
|
||||
SocketParms sp = fSocket->socketParms();
|
||||
const sockaddr_in* sinp = reinterpret_cast<const sockaddr_in*>(&fSa);
|
||||
oss << "IOSocket: sd: " << sp.sd() <<
|
||||
#ifndef _MSC_VER
|
||||
" inet: " << inet_ntop(AF_INET, &sinp->sin_addr, buf, INET_ADDRSTRLEN) <<
|
||||
" inet: " << inet_ntop(AF_INET, &sinp->sin_addr, buf, INET_ADDRSTRLEN) <<
|
||||
#endif
|
||||
" port: " << ntohs(sinp->sin_port);
|
||||
return oss.str();
|
||||
" port: " << ntohs(sinp->sin_port);
|
||||
return oss.str();
|
||||
#endif
|
||||
}
|
||||
|
||||
} //namespace messageqcpp
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: iosocket.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: iosocket.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -60,238 +60,237 @@ class ServerSocket;
|
||||
*/
|
||||
class IOSocket
|
||||
{
|
||||
public:
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
EXPORT explicit IOSocket(Socket* socket = 0);
|
||||
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
EXPORT explicit IOSocket(Socket* socket = 0);
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
EXPORT IOSocket(const IOSocket& rhs);
|
||||
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
EXPORT IOSocket(const IOSocket& rhs);
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
EXPORT IOSocket& operator=(const IOSocket& rhs);
|
||||
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
EXPORT IOSocket& operator=(const IOSocket& rhs);
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
EXPORT virtual ~IOSocket();
|
||||
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
EXPORT virtual ~IOSocket();
|
||||
/** read a ByteStream from this socket
|
||||
*
|
||||
* This socket needs to be connected first. Will throw runtime_error on I/O error. Caller should
|
||||
* call close() method if exception is thrown.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const;
|
||||
|
||||
/** read a ByteStream from this socket
|
||||
*
|
||||
* This socket needs to be connected first. Will throw runtime_error on I/O error. Caller should
|
||||
* call close() method if exception is thrown.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL, Stats* stats = NULL) const;
|
||||
/** write a ByteStream to this socket
|
||||
*
|
||||
* This socket needs to be connected first. Will throw runtime_error on I/O error. Caller should
|
||||
* call close() method if exception is thrown.
|
||||
*/
|
||||
EXPORT virtual void write(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
EXPORT virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
EXPORT virtual void write(SBS msg, Stats* stats = NULL) const;
|
||||
|
||||
/** write a ByteStream to this socket
|
||||
*
|
||||
* This socket needs to be connected first. Will throw runtime_error on I/O error. Caller should
|
||||
* call close() method if exception is thrown.
|
||||
*/
|
||||
EXPORT virtual void write(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
EXPORT virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const;
|
||||
EXPORT virtual void write(SBS msg, Stats* stats = NULL) const;
|
||||
/** access the sockaddr member
|
||||
*/
|
||||
inline virtual const sockaddr sa() const;
|
||||
|
||||
/** access the sockaddr member
|
||||
*/
|
||||
inline virtual const sockaddr sa() const;
|
||||
/** modify the sockaddr member
|
||||
*/
|
||||
inline virtual void sa(const sockaddr* sa);
|
||||
|
||||
/** modify the sockaddr member
|
||||
*/
|
||||
inline virtual void sa(const sockaddr* sa);
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void open();
|
||||
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void open();
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void close();
|
||||
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void close();
|
||||
/** test if the socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
|
||||
/** test if the socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
/** get the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
|
||||
/** get the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
/** set the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socketParms);
|
||||
|
||||
/** set the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socketParms);
|
||||
/** set the socket implementation
|
||||
*
|
||||
* Install a socket implementation that meets the Socket interface
|
||||
*/
|
||||
EXPORT virtual void setSocketImpl(Socket* socket);
|
||||
|
||||
/** set the socket implementation
|
||||
*
|
||||
* Install a socket implementation that meets the Socket interface
|
||||
*/
|
||||
EXPORT virtual void setSocketImpl(Socket* socket);
|
||||
/** get a string rep of the IOSocket
|
||||
*
|
||||
*/
|
||||
EXPORT virtual const std::string toString() const;
|
||||
|
||||
/** get a string rep of the IOSocket
|
||||
*
|
||||
*/
|
||||
EXPORT virtual const std::string toString() const;
|
||||
/** syncProto() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
EXPORT virtual void syncProto(bool use)
|
||||
{
|
||||
fSocket->syncProto(use);
|
||||
}
|
||||
|
||||
/** syncProto() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
EXPORT virtual void syncProto(bool use)
|
||||
{
|
||||
fSocket->syncProto(use);
|
||||
}
|
||||
EXPORT virtual int getConnectionNum() const;
|
||||
|
||||
EXPORT virtual int getConnectionNum() const;
|
||||
// Debug
|
||||
EXPORT void setSockID(uint32_t id)
|
||||
{
|
||||
sockID = id;
|
||||
}
|
||||
EXPORT uint32_t getSockID()
|
||||
{
|
||||
return sockID;
|
||||
}
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
/**
|
||||
* @brief return the address as a string
|
||||
*/
|
||||
virtual const std::string addr2String() const
|
||||
{
|
||||
return fSocket->addr2String();
|
||||
}
|
||||
|
||||
// Debug
|
||||
EXPORT void setSockID(uint32_t id)
|
||||
{
|
||||
sockID = id;
|
||||
}
|
||||
EXPORT uint32_t getSockID()
|
||||
{
|
||||
return sockID;
|
||||
}
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
/**
|
||||
* @brief return the address as a string
|
||||
*/
|
||||
virtual const std::string addr2String() const
|
||||
{
|
||||
return fSocket->addr2String();
|
||||
}
|
||||
/**
|
||||
* @brief compare 2 addresses
|
||||
*/
|
||||
virtual bool isSameAddr(const IOSocket* rhs) const
|
||||
{
|
||||
return fSocket->isSameAddr(rhs->fSocket);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief compare 2 addresses
|
||||
*/
|
||||
virtual bool isSameAddr(const IOSocket* rhs) const
|
||||
{
|
||||
return fSocket->isSameAddr(rhs->fSocket);
|
||||
}
|
||||
/** connect() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
virtual void connect(const struct sockaddr* serv_addr)
|
||||
{
|
||||
fSocket->connect(serv_addr);
|
||||
}
|
||||
|
||||
/** connect() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
virtual void connect(const struct sockaddr* serv_addr)
|
||||
{
|
||||
fSocket->connect(serv_addr);
|
||||
}
|
||||
/** connectionTimeout() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct timespec* timeout)
|
||||
{
|
||||
fSocket->connectionTimeout(timeout);
|
||||
}
|
||||
|
||||
/** connectionTimeout() forwarder for inherited classes
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct timespec* timeout)
|
||||
{
|
||||
fSocket->connectionTimeout(timeout);
|
||||
}
|
||||
inline virtual bool isConnected() const;
|
||||
inline virtual bool hasData() const;
|
||||
inline bool hasSocketDescriptor() const;
|
||||
|
||||
inline virtual bool isConnected() const;
|
||||
inline virtual bool hasData() const;
|
||||
inline bool hasSocketDescriptor() const;
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
friend class ::MessageQTestSuite;
|
||||
protected:
|
||||
private:
|
||||
void doCopy(const IOSocket& rhs);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
private:
|
||||
void doCopy(const IOSocket& rhs);
|
||||
|
||||
Socket* fSocket;
|
||||
sockaddr fSa;
|
||||
uint32_t sockID; // For debug purposes
|
||||
Socket* fSocket;
|
||||
sockaddr fSa;
|
||||
uint32_t sockID; // For debug purposes
|
||||
};
|
||||
|
||||
|
||||
inline const sockaddr IOSocket::sa() const
|
||||
{
|
||||
return fSa;
|
||||
return fSa;
|
||||
}
|
||||
|
||||
inline bool IOSocket::hasSocketDescriptor() const
|
||||
{
|
||||
return fSocket && utils::is_nonnegative(fSocket->socketParms().sd());
|
||||
return fSocket && utils::is_nonnegative(fSocket->socketParms().sd());
|
||||
}
|
||||
|
||||
inline void IOSocket::sa(const sockaddr* sa)
|
||||
{
|
||||
fSa = *sa;
|
||||
fSa = *sa;
|
||||
|
||||
if (fSocket)
|
||||
fSocket->sa( sa );
|
||||
if (fSocket)
|
||||
fSocket->sa(sa);
|
||||
}
|
||||
inline void IOSocket::open()
|
||||
{
|
||||
idbassert(fSocket);
|
||||
fSocket->open();
|
||||
idbassert(fSocket);
|
||||
fSocket->open();
|
||||
}
|
||||
//RJD: changing close() to simply bail on null fSocket. I'm not really sure what's best here, but this is probably
|
||||
// RJD: changing close() to simply bail on null fSocket. I'm not really sure what's best here, but this is
|
||||
// probably
|
||||
// better that asserting...
|
||||
inline void IOSocket::close()
|
||||
{
|
||||
if (fSocket) fSocket->close();
|
||||
if (fSocket)
|
||||
fSocket->close();
|
||||
}
|
||||
inline bool IOSocket::isOpen() const
|
||||
{
|
||||
return (fSocket && fSocket->isOpen());
|
||||
return (fSocket && fSocket->isOpen());
|
||||
}
|
||||
inline const SBS IOSocket::read(const struct timespec* timeout, bool* isTimeOut, Stats* stats) const
|
||||
{
|
||||
idbassert(fSocket);
|
||||
return fSocket->read(timeout, isTimeOut, stats);
|
||||
idbassert(fSocket);
|
||||
return fSocket->read(timeout, isTimeOut, stats);
|
||||
}
|
||||
inline void IOSocket::write(const ByteStream& msg, Stats* stats) const
|
||||
{
|
||||
idbassert(fSocket);
|
||||
fSocket->write(msg, stats);
|
||||
idbassert(fSocket);
|
||||
fSocket->write(msg, stats);
|
||||
}
|
||||
inline void IOSocket::write_raw(const ByteStream& msg, Stats* stats) const
|
||||
{
|
||||
idbassert(fSocket);
|
||||
fSocket->write_raw(msg, stats);
|
||||
idbassert(fSocket);
|
||||
fSocket->write_raw(msg, stats);
|
||||
}
|
||||
inline void IOSocket::write(SBS msg, Stats* stats) const
|
||||
{
|
||||
idbassert(fSocket);
|
||||
fSocket->write(msg, stats);
|
||||
idbassert(fSocket);
|
||||
fSocket->write(msg, stats);
|
||||
}
|
||||
inline const SocketParms IOSocket::socketParms() const
|
||||
{
|
||||
idbassert(fSocket);
|
||||
return fSocket->socketParms();
|
||||
idbassert(fSocket);
|
||||
return fSocket->socketParms();
|
||||
}
|
||||
inline void IOSocket::socketParms(const SocketParms& socketParms)
|
||||
{
|
||||
idbassert(fSocket);
|
||||
fSocket->socketParms(socketParms);
|
||||
idbassert(fSocket);
|
||||
fSocket->socketParms(socketParms);
|
||||
}
|
||||
inline void IOSocket::setSocketImpl(Socket* socket)
|
||||
{
|
||||
delete fSocket;
|
||||
fSocket = socket;
|
||||
delete fSocket;
|
||||
fSocket = socket;
|
||||
}
|
||||
inline int IOSocket::getConnectionNum() const
|
||||
{
|
||||
return fSocket->getConnectionNum();
|
||||
return fSocket->getConnectionNum();
|
||||
}
|
||||
inline bool IOSocket::isConnected() const
|
||||
{
|
||||
return fSocket->isConnected();
|
||||
return fSocket->isConnected();
|
||||
}
|
||||
inline bool IOSocket::hasData() const
|
||||
{
|
||||
return fSocket->hasData();
|
||||
return fSocket->hasData();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -299,12 +298,10 @@ inline bool IOSocket::hasData() const
|
||||
*/
|
||||
inline std::ostream& operator<<(std::ostream& os, const IOSocket& rhs)
|
||||
{
|
||||
os << rhs.toString();
|
||||
return os;
|
||||
os << rhs.toString();
|
||||
return os;
|
||||
}
|
||||
|
||||
} //namespace messageqcpp
|
||||
} // namespace messageqcpp
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: messagequeue.cpp 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: messagequeue.cpp 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
@ -55,327 +55,323 @@ using namespace config;
|
||||
namespace messageqcpp
|
||||
{
|
||||
// Aux function to try to resolve supplied identifier to fill struct addrinfo.
|
||||
struct sockaddr* hostnameResolver(const std::string& dnOrIp,
|
||||
const uint16_t port,
|
||||
logging::Logger& aLogger,
|
||||
struct sockaddr* hostnameResolver(const std::string& dnOrIp, const uint16_t port, logging::Logger& aLogger,
|
||||
struct sockaddr* sockAddrPtr)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *servinfo;
|
||||
int rc = 0;
|
||||
struct addrinfo hints;
|
||||
struct addrinfo* servinfo;
|
||||
int rc = 0;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
// ATM We support IPv4 only.
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
// ATM We support IPv4 only.
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if( !(rc = getaddrinfo(dnOrIp.c_str(), nullptr, &hints, &servinfo)) )
|
||||
{
|
||||
memset(sockAddrPtr, 0, sizeof(*sockAddrPtr));
|
||||
sockaddr_in* sinp = reinterpret_cast<sockaddr_in*>(sockAddrPtr);
|
||||
*sinp = *reinterpret_cast<sockaddr_in*>(servinfo->ai_addr);
|
||||
sinp->sin_port = htons(port);
|
||||
freeaddrinfo(servinfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
string msg = "messageqcpp::hostnameResolver ";
|
||||
msg.append(gai_strerror(rc));
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add(msg);
|
||||
aLogger.logMessage(logging::LOG_TYPE_ERROR, logging::M0000, args, li);
|
||||
}
|
||||
return sockAddrPtr;
|
||||
if (!(rc = getaddrinfo(dnOrIp.c_str(), nullptr, &hints, &servinfo)))
|
||||
{
|
||||
memset(sockAddrPtr, 0, sizeof(*sockAddrPtr));
|
||||
sockaddr_in* sinp = reinterpret_cast<sockaddr_in*>(sockAddrPtr);
|
||||
*sinp = *reinterpret_cast<sockaddr_in*>(servinfo->ai_addr);
|
||||
sinp->sin_port = htons(port);
|
||||
freeaddrinfo(servinfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
string msg = "messageqcpp::hostnameResolver ";
|
||||
msg.append(gai_strerror(rc));
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add(msg);
|
||||
aLogger.logMessage(logging::LOG_TYPE_ERROR, logging::M0000, args, li);
|
||||
}
|
||||
return sockAddrPtr;
|
||||
}
|
||||
|
||||
void MessageQueueServer::setup(size_t blocksize, int backlog, bool syncProto)
|
||||
{
|
||||
string thisEndPortStr;
|
||||
string thisEndPortStr;
|
||||
|
||||
thisEndPortStr = fConfig->getConfig(fThisEnd, "Port");
|
||||
uint16_t port;
|
||||
thisEndPortStr = fConfig->getConfig(fThisEnd, "Port");
|
||||
uint16_t port;
|
||||
|
||||
if (thisEndPortStr.length() == 0 || (port = static_cast<uint16_t>(strtol(thisEndPortStr.c_str(), 0, 0))) == 0)
|
||||
{
|
||||
string msg = "MessageQueueServer::MessageQueueServer: config error: Invalid/Missing Port "
|
||||
"attribute for " + fThisEnd;
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
if (thisEndPortStr.length() == 0 ||
|
||||
(port = static_cast<uint16_t>(strtol(thisEndPortStr.c_str(), 0, 0))) == 0)
|
||||
{
|
||||
string msg =
|
||||
"MessageQueueServer::MessageQueueServer: config error: Invalid/Missing Port "
|
||||
"attribute for " +
|
||||
fThisEnd;
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
|
||||
in_addr listenAddr;
|
||||
listenAddr.s_addr = INADDR_ANY;
|
||||
string listenAddrStr = fConfig->getConfig(fThisEnd, "ListenAddr");
|
||||
in_addr listenAddr;
|
||||
listenAddr.s_addr = INADDR_ANY;
|
||||
string listenAddrStr = fConfig->getConfig(fThisEnd, "ListenAddr");
|
||||
|
||||
if (listenAddrStr.length() > 0)
|
||||
{
|
||||
struct in_addr la;
|
||||
if (listenAddrStr.length() > 0)
|
||||
{
|
||||
struct in_addr la;
|
||||
|
||||
if (inet_aton(listenAddrStr.c_str(), &la) != 0)
|
||||
listenAddr = la;
|
||||
}
|
||||
if (inet_aton(listenAddrStr.c_str(), &la) != 0)
|
||||
listenAddr = la;
|
||||
}
|
||||
|
||||
memset(&fServ_addr, 0, sizeof(fServ_addr));
|
||||
sockaddr_in* sinp = reinterpret_cast<sockaddr_in*>(&fServ_addr);
|
||||
sinp->sin_family = AF_INET;
|
||||
sinp->sin_addr.s_addr = listenAddr.s_addr;
|
||||
sinp->sin_port = htons(port);
|
||||
memset(&fServ_addr, 0, sizeof(fServ_addr));
|
||||
sockaddr_in* sinp = reinterpret_cast<sockaddr_in*>(&fServ_addr);
|
||||
sinp->sin_family = AF_INET;
|
||||
sinp->sin_addr.s_addr = listenAddr.s_addr;
|
||||
sinp->sin_port = htons(port);
|
||||
|
||||
#ifdef SKIP_IDB_COMPRESSION
|
||||
fListenSock.setSocketImpl(new InetStreamSocket(blocksize));
|
||||
fListenSock.setSocketImpl(new InetStreamSocket(blocksize));
|
||||
#else
|
||||
fListenSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
fListenSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
#endif
|
||||
fListenSock.syncProto(syncProto);
|
||||
fListenSock.open();
|
||||
fListenSock.bind(&fServ_addr);
|
||||
fListenSock.listen(backlog);
|
||||
fListenSock.syncProto(syncProto);
|
||||
fListenSock.open();
|
||||
fListenSock.bind(&fServ_addr);
|
||||
fListenSock.listen(backlog);
|
||||
|
||||
#ifdef SKIP_IDB_COMPRESSION
|
||||
fClientSock.setSocketImpl(new InetStreamSocket(blocksize));
|
||||
fClientSock.setSocketImpl(new InetStreamSocket(blocksize));
|
||||
#else
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
#endif
|
||||
fClientSock.syncProto(syncProto);
|
||||
fClientSock.syncProto(syncProto);
|
||||
}
|
||||
|
||||
MessageQueueServer::MessageQueueServer(const string& thisEnd, const string& config,
|
||||
size_t blocksize, int backlog, bool syncProto) :
|
||||
fThisEnd(thisEnd),
|
||||
fConfig(Config::makeConfig(config)),
|
||||
fLogger(31)
|
||||
MessageQueueServer::MessageQueueServer(const string& thisEnd, const string& config, size_t blocksize,
|
||||
int backlog, bool syncProto)
|
||||
: fThisEnd(thisEnd), fConfig(Config::makeConfig(config)), fLogger(31)
|
||||
{
|
||||
setup(blocksize, backlog, syncProto);
|
||||
setup(blocksize, backlog, syncProto);
|
||||
}
|
||||
|
||||
MessageQueueServer::MessageQueueServer(const string& thisEnd, Config* config,
|
||||
size_t blocksize, int backlog, bool syncProto) :
|
||||
fThisEnd(thisEnd),
|
||||
fConfig(config),
|
||||
fLogger(31)
|
||||
MessageQueueServer::MessageQueueServer(const string& thisEnd, Config* config, size_t blocksize, int backlog,
|
||||
bool syncProto)
|
||||
: fThisEnd(thisEnd), fConfig(config), fLogger(31)
|
||||
{
|
||||
if (fConfig == 0)
|
||||
fConfig = Config::makeConfig();
|
||||
if (fConfig == 0)
|
||||
fConfig = Config::makeConfig();
|
||||
|
||||
setup(blocksize, backlog, syncProto);
|
||||
setup(blocksize, backlog, syncProto);
|
||||
}
|
||||
|
||||
MessageQueueServer::~MessageQueueServer()
|
||||
{
|
||||
fClientSock.close();
|
||||
fListenSock.close();
|
||||
fClientSock.close();
|
||||
fListenSock.close();
|
||||
}
|
||||
|
||||
const IOSocket MessageQueueServer::accept(const struct timespec* timeout) const
|
||||
{
|
||||
return fListenSock.accept(timeout);
|
||||
return fListenSock.accept(timeout);
|
||||
}
|
||||
|
||||
void MessageQueueServer::syncProto(bool use)
|
||||
{
|
||||
fListenSock.syncProto(use);
|
||||
fClientSock.syncProto(use);
|
||||
fListenSock.syncProto(use);
|
||||
fClientSock.syncProto(use);
|
||||
}
|
||||
|
||||
MessageQueueClient::~MessageQueueClient()
|
||||
{
|
||||
fClientSock.close();
|
||||
fClientSock.close();
|
||||
}
|
||||
|
||||
void MessageQueueClient::shutdown()
|
||||
{
|
||||
fClientSock.close();
|
||||
fClientSock.close();
|
||||
}
|
||||
|
||||
std::pair<std::string, uint16_t> getAddressAndPort(config::Config* config, const std::string& fOtherEnd)
|
||||
{
|
||||
std::string otherEndDnOrIPStr = config->getConfig(fOtherEnd, "IPAddr");
|
||||
std::string otherEndPortStr = config->getConfig(fOtherEnd, "Port");
|
||||
uint16_t port = otherEndPortStr.length() > 0
|
||||
? static_cast<uint16_t>(strtol(otherEndPortStr.c_str(), 0, 0))
|
||||
: 0;
|
||||
std::string otherEndDnOrIPStr = config->getConfig(fOtherEnd, "IPAddr");
|
||||
std::string otherEndPortStr = config->getConfig(fOtherEnd, "Port");
|
||||
uint16_t port =
|
||||
otherEndPortStr.length() > 0 ? static_cast<uint16_t>(strtol(otherEndPortStr.c_str(), 0, 0)) : 0;
|
||||
|
||||
if (otherEndDnOrIPStr == "unassigned")
|
||||
return {"0.0.0.0", port};
|
||||
if (otherEndDnOrIPStr == "unassigned")
|
||||
return {"0.0.0.0", port};
|
||||
|
||||
if (otherEndDnOrIPStr.empty())
|
||||
return {"127.0.0.1", port};
|
||||
if (otherEndDnOrIPStr.empty())
|
||||
return {"127.0.0.1", port};
|
||||
|
||||
return {otherEndDnOrIPStr, port};
|
||||
return {otherEndDnOrIPStr, port};
|
||||
}
|
||||
|
||||
void MessageQueueClient::setup(bool syncProto)
|
||||
{
|
||||
auto addressAndPort = getAddressAndPort(fConfig, fOtherEnd);
|
||||
if (!addressAndPort.second)
|
||||
{
|
||||
string msg = "MessageQueueClient::setup(): config error: Invalid/Missing Port attribute";
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
auto addressAndPort = getAddressAndPort(fConfig, fOtherEnd);
|
||||
if (!addressAndPort.second)
|
||||
{
|
||||
string msg = "MessageQueueClient::setup(): config error: Invalid/Missing Port attribute";
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
#ifdef SKIP_IDB_COMPRESSION
|
||||
fClientSock.setSocketImpl(new InetStreamSocket());
|
||||
fClientSock.setSocketImpl(new InetStreamSocket());
|
||||
#else
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
#endif
|
||||
fClientSock.syncProto(syncProto);
|
||||
fClientSock.sa(hostnameResolver(addressAndPort.first, addressAndPort.second, fLogger, &fServ_addr));
|
||||
fClientSock.syncProto(syncProto);
|
||||
fClientSock.sa(hostnameResolver(addressAndPort.first, addressAndPort.second, fLogger, &fServ_addr));
|
||||
}
|
||||
|
||||
MessageQueueClient::MessageQueueClient(const string& otherEnd, const string& config, bool syncProto) :
|
||||
fOtherEnd(otherEnd), fConfig(Config::makeConfig(config)), fLogger(31), fIsAvailable(true)
|
||||
MessageQueueClient::MessageQueueClient(const string& otherEnd, const string& config, bool syncProto)
|
||||
: fOtherEnd(otherEnd), fConfig(Config::makeConfig(config)), fLogger(31), fIsAvailable(true)
|
||||
{
|
||||
setup(syncProto);
|
||||
setup(syncProto);
|
||||
}
|
||||
|
||||
MessageQueueClient::MessageQueueClient(const string& otherEnd, Config* config, bool syncProto) :
|
||||
fOtherEnd(otherEnd), fConfig(config), fLogger(31), fIsAvailable(true)
|
||||
MessageQueueClient::MessageQueueClient(const string& otherEnd, Config* config, bool syncProto)
|
||||
: fOtherEnd(otherEnd), fConfig(config), fLogger(31), fIsAvailable(true)
|
||||
{
|
||||
if (fConfig == 0)
|
||||
fConfig = Config::makeConfig();
|
||||
if (fConfig == 0)
|
||||
fConfig = Config::makeConfig();
|
||||
|
||||
setup(syncProto);
|
||||
setup(syncProto);
|
||||
}
|
||||
|
||||
MessageQueueClient::MessageQueueClient(const string& dnOrIp, uint16_t port, bool syncProto) :
|
||||
fLogger(31), fIsAvailable(true)
|
||||
MessageQueueClient::MessageQueueClient(const string& dnOrIp, uint16_t port, bool syncProto)
|
||||
: fLogger(31), fIsAvailable(true)
|
||||
{
|
||||
#ifdef SKIP_IDB_COMPRESSION
|
||||
fClientSock.setSocketImpl(new InetStreamSocket());
|
||||
fClientSock.setSocketImpl(new InetStreamSocket());
|
||||
#else
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
fClientSock.setSocketImpl(new CompressedInetStreamSocket());
|
||||
#endif
|
||||
fClientSock.syncProto(syncProto);
|
||||
fClientSock.sa(hostnameResolver(dnOrIp, port, fLogger, &fServ_addr));
|
||||
fClientSock.syncProto(syncProto);
|
||||
fClientSock.sa(hostnameResolver(dnOrIp, port, fLogger, &fServ_addr));
|
||||
}
|
||||
|
||||
const SBS MessageQueueClient::read(const struct timespec* timeout, bool* isTimeOut, Stats* stats) const
|
||||
{
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
|
||||
try
|
||||
{
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
SBS res;
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
|
||||
try
|
||||
{
|
||||
res = fClientSock.read(timeout, isTimeOut, stats);
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (runtime_error& re)
|
||||
catch (...)
|
||||
{
|
||||
// This is an I/O error from IOSocket::read()
|
||||
// cerr << "MessageQueueClient::read: close socket for " << re.what() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add("Client read close socket for");
|
||||
args.add(re.what());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
catch (SocketClosed& e)
|
||||
{
|
||||
// cerr << "MessageQueueClient::read: close socket for " << e.what() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add("Client read close socket for");
|
||||
args.add(e.what());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
fClientSock.close();
|
||||
throw;
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
SBS res;
|
||||
|
||||
try
|
||||
{
|
||||
res = fClientSock.read(timeout, isTimeOut, stats);
|
||||
}
|
||||
catch (runtime_error& re)
|
||||
{
|
||||
// This is an I/O error from IOSocket::read()
|
||||
// cerr << "MessageQueueClient::read: close socket for " << re.what() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add("Client read close socket for");
|
||||
args.add(re.what());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
catch (SocketClosed& e)
|
||||
{
|
||||
// cerr << "MessageQueueClient::read: close socket for " << e.what() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add("Client read close socket for");
|
||||
args.add(e.what());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void MessageQueueClient::write(const ByteStream& msg, const struct timespec* timeout, Stats* stats) const
|
||||
{
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
|
||||
try
|
||||
{
|
||||
fClientSock.connectionTimeout(timeout);
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
|
||||
try
|
||||
{
|
||||
fClientSock.write(msg, stats);
|
||||
fClientSock.connectionTimeout(timeout);
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (runtime_error& e)
|
||||
catch (...)
|
||||
{
|
||||
try
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "MessageQueueClient::write: error writing " << msg.length() << " bytes to "
|
||||
<< fClientSock << ". Socket error was " << e.what() << endl;
|
||||
// cerr << oss.str() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add(oss.str());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
fClientSock.close();
|
||||
throw;
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
fClientSock.write(msg, stats);
|
||||
}
|
||||
catch (runtime_error& e)
|
||||
{
|
||||
try
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "MessageQueueClient::write: error writing " << msg.length() << " bytes to " << fClientSock
|
||||
<< ". Socket error was " << e.what() << endl;
|
||||
// cerr << oss.str() << endl;
|
||||
logging::Message::Args args;
|
||||
logging::LoggingID li(31);
|
||||
args.add(oss.str());
|
||||
fLogger.logMessage(logging::LOG_TYPE_WARNING, logging::M0000, args, li);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
fClientSock.close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool MessageQueueClient::connect() const
|
||||
{
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
if (!fClientSock.isOpen())
|
||||
{
|
||||
fClientSock.open();
|
||||
|
||||
try
|
||||
{
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (runtime_error& re)
|
||||
{
|
||||
string what = re.what();
|
||||
|
||||
if (what.find("Connection refused") != string::npos)
|
||||
{
|
||||
try
|
||||
{
|
||||
fClientSock.connect(&fServ_addr);
|
||||
}
|
||||
catch (runtime_error& re)
|
||||
{
|
||||
string what = re.what();
|
||||
|
||||
if (what.find("Connection refused") != string::npos)
|
||||
{
|
||||
try
|
||||
{
|
||||
fClientSock.close();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
throw;
|
||||
fClientSock.close();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
else
|
||||
throw;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return fClientSock.isOpen();
|
||||
return fClientSock.isOpen();
|
||||
}
|
||||
|
||||
}//namespace messageqcpp
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/******************************************************************************************
|
||||
* $Id: messagequeue.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
******************************************************************************************/
|
||||
* $Id: messagequeue.h 3632 2013-03-13 18:08:46Z pleblanc $
|
||||
*
|
||||
*
|
||||
******************************************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
#include <string>
|
||||
@ -54,7 +54,6 @@ class MessageQTestSuite;
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
using AddrAndPortPair = std::pair<std::string, uint16_t>;
|
||||
// utility f-s
|
||||
// Extracts a pair of address and port from the XML configuration.
|
||||
@ -81,76 +80,77 @@ AddrAndPortPair getAddressAndPort(config::Config* config, const std::string& fOt
|
||||
*/
|
||||
class MessageQueueServer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief construct a server queue for thisEnd
|
||||
*
|
||||
* construct a server queue for thisEnd. Optionally specify a Config object to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueServer(const std::string& thisEnd, config::Config* config = 0,
|
||||
size_t blocksize = ByteStream::BlockSize, int backlog = 5, bool syncProto = true);
|
||||
public:
|
||||
/**
|
||||
* @brief construct a server queue for thisEnd
|
||||
*
|
||||
* construct a server queue for thisEnd. Optionally specify a Config object to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueServer(const std::string& thisEnd, config::Config* config = 0,
|
||||
size_t blocksize = ByteStream::BlockSize, int backlog = 5,
|
||||
bool syncProto = true);
|
||||
|
||||
/**
|
||||
* @brief construct a server queue for thisEnd
|
||||
*
|
||||
* construct a server queue for thisEnd, specifying the name of a config file to use.
|
||||
*/
|
||||
EXPORT MessageQueueServer(const std::string& thisEnd, const std::string& config,
|
||||
size_t blocksize = ByteStream::BlockSize, int backlog = 5, bool syncProto = true);
|
||||
/**
|
||||
* @brief construct a server queue for thisEnd
|
||||
*
|
||||
* construct a server queue for thisEnd, specifying the name of a config file to use.
|
||||
*/
|
||||
EXPORT MessageQueueServer(const std::string& thisEnd, const std::string& config,
|
||||
size_t blocksize = ByteStream::BlockSize, int backlog = 5, bool syncProto = true);
|
||||
|
||||
/**
|
||||
* @brief destructor
|
||||
*/
|
||||
EXPORT ~MessageQueueServer();
|
||||
//
|
||||
/**
|
||||
* @brief wait for a connection and return an IOSocket
|
||||
*
|
||||
* This method can be used by a main thread to wait for an incoming connection. The IOSocket
|
||||
* that is returned can be passed to a thread to handle the socket connection. The main thread
|
||||
* is then free to wait again for another connection. The IOSocket is already open and ready for
|
||||
* read() and/or write(). The caller is responsible for calling close() when it is done.
|
||||
*/
|
||||
EXPORT const IOSocket accept(const struct timespec* timeout = 0) const;
|
||||
/**
|
||||
* @brief destructor
|
||||
*/
|
||||
EXPORT ~MessageQueueServer();
|
||||
//
|
||||
/**
|
||||
* @brief wait for a connection and return an IOSocket
|
||||
*
|
||||
* This method can be used by a main thread to wait for an incoming connection. The IOSocket
|
||||
* that is returned can be passed to a thread to handle the socket connection. The main thread
|
||||
* is then free to wait again for another connection. The IOSocket is already open and ready for
|
||||
* read() and/or write(). The caller is responsible for calling close() when it is done.
|
||||
*/
|
||||
EXPORT const IOSocket accept(const struct timespec* timeout = 0) const;
|
||||
|
||||
/**
|
||||
* @brief get a mutable pointer to the client IOSocket
|
||||
*/
|
||||
inline IOSocket& clientSock() const;
|
||||
/**
|
||||
* @brief get a mutable pointer to the client IOSocket
|
||||
*/
|
||||
inline IOSocket& clientSock() const;
|
||||
|
||||
/**
|
||||
* @brief set the sync proto
|
||||
*/
|
||||
EXPORT void syncProto(bool use);
|
||||
/**
|
||||
* @brief set the sync proto
|
||||
*/
|
||||
EXPORT void syncProto(bool use);
|
||||
|
||||
/**
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
/**
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
private:
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
MessageQueueServer(const MessageQueueServer& rhs);
|
||||
private:
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
MessageQueueServer(const MessageQueueServer& rhs);
|
||||
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
MessageQueueServer& operator=(const MessageQueueServer& rhs);
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
MessageQueueServer& operator=(const MessageQueueServer& rhs);
|
||||
|
||||
/** ctor helper
|
||||
*
|
||||
*/
|
||||
void setup(size_t blocksize, int backlog, bool syncProto);
|
||||
/** ctor helper
|
||||
*
|
||||
*/
|
||||
void setup(size_t blocksize, int backlog, bool syncProto);
|
||||
|
||||
std::string fThisEnd; /// the process name for this process
|
||||
struct sockaddr fServ_addr; /// the addr of the server (may be this process)
|
||||
config::Config* fConfig; /// config file has the IP addrs and port numbers
|
||||
mutable ServerSocket fListenSock; /// the socket the server listens on for new connections
|
||||
mutable IOSocket fClientSock; /// the socket connected to a client
|
||||
std::string fThisEnd; /// the process name for this process
|
||||
struct sockaddr fServ_addr; /// the addr of the server (may be this process)
|
||||
config::Config* fConfig; /// config file has the IP addrs and port numbers
|
||||
mutable ServerSocket fListenSock; /// the socket the server listens on for new connections
|
||||
mutable IOSocket fClientSock; /// the socket connected to a client
|
||||
|
||||
mutable logging::Logger fLogger;
|
||||
mutable logging::Logger fLogger;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -169,170 +169,171 @@ private:
|
||||
*/
|
||||
class MessageQueueClient
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd. Optionally specify a Config object to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& otherEnd, config::Config* config = 0, bool syncProto = true);
|
||||
public:
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd. Optionally specify a Config object to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& otherEnd, config::Config* config = 0,
|
||||
bool syncProto = true);
|
||||
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd, specifying the name of a config file to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& otherEnd, const std::string& config, bool syncProto = true);
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd, specifying the name of a config file to use.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& otherEnd, const std::string& config,
|
||||
bool syncProto = true);
|
||||
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd on the given IP and Port.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& dnOrIp, uint16_t port, bool syncProto=true);
|
||||
/**
|
||||
* @brief construct a queue to otherEnd
|
||||
*
|
||||
* construct a queue from this process to otherEnd on the given IP and Port.
|
||||
*/
|
||||
EXPORT explicit MessageQueueClient(const std::string& dnOrIp, uint16_t port, bool syncProto = true);
|
||||
|
||||
/**
|
||||
* @brief destructor
|
||||
*
|
||||
* calls shutdown() method.
|
||||
*/
|
||||
EXPORT ~MessageQueueClient();
|
||||
|
||||
/**
|
||||
* @brief destructor
|
||||
*
|
||||
* calls shutdown() method.
|
||||
*/
|
||||
EXPORT ~MessageQueueClient();
|
||||
/**
|
||||
* @brief read a message from the queue
|
||||
*
|
||||
* wait for and return a message from otherEnd. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has milisecond resolution.
|
||||
*/
|
||||
EXPORT const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const;
|
||||
|
||||
/**
|
||||
* @brief read a message from the queue
|
||||
*
|
||||
* wait for and return a message from otherEnd. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has milisecond resolution.
|
||||
*/
|
||||
EXPORT const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL, Stats* stats = NULL) const;
|
||||
/**
|
||||
* @brief write a message to the queue
|
||||
*
|
||||
* write a message to otherEnd. If the socket is not open, the timeout parm (in ms) will be used
|
||||
* to establish a sync connection w/ the server
|
||||
*/
|
||||
EXPORT void write(const ByteStream& msg, const struct timespec* timeout = 0, Stats* stats = NULL) const;
|
||||
|
||||
/**
|
||||
* @brief write a message to the queue
|
||||
*
|
||||
* write a message to otherEnd. If the socket is not open, the timeout parm (in ms) will be used
|
||||
* to establish a sync connection w/ the server
|
||||
*/
|
||||
EXPORT void write(const ByteStream& msg, const struct timespec* timeout = 0, Stats* stats = NULL) const;
|
||||
/**
|
||||
* @brief shutdown the connection to the server
|
||||
*
|
||||
* indicate to the class that the user is done with the socket
|
||||
* and the other class methods won't be used.
|
||||
*/
|
||||
EXPORT void shutdown();
|
||||
|
||||
/**
|
||||
* @brief shutdown the connection to the server
|
||||
*
|
||||
* indicate to the class that the user is done with the socket
|
||||
* and the other class methods won't be used.
|
||||
*/
|
||||
EXPORT void shutdown();
|
||||
/**
|
||||
* @brief connect to the server. Returns true if connection was successful.
|
||||
*
|
||||
* read() and write() automatically connect, but this method can be used to verify a server is listening
|
||||
* before that.
|
||||
*/
|
||||
EXPORT bool connect() const;
|
||||
|
||||
/**
|
||||
* @brief connect to the server. Returns true if connection was successful.
|
||||
*
|
||||
* read() and write() automatically connect, but this method can be used to verify a server is listening
|
||||
* before that.
|
||||
*/
|
||||
EXPORT bool connect() const;
|
||||
/**
|
||||
* @brief accessors and mutators
|
||||
*/
|
||||
EXPORT const sockaddr serv_addr() const
|
||||
{
|
||||
return fServ_addr;
|
||||
}
|
||||
EXPORT const std::string otherEnd() const
|
||||
{
|
||||
return fOtherEnd;
|
||||
}
|
||||
EXPORT bool isAvailable() const
|
||||
{
|
||||
return fIsAvailable;
|
||||
}
|
||||
EXPORT void isAvailable(const bool isAvailable)
|
||||
{
|
||||
fIsAvailable = isAvailable;
|
||||
}
|
||||
EXPORT const std::string moduleName() const
|
||||
{
|
||||
return fModuleName;
|
||||
}
|
||||
EXPORT void moduleName(const std::string& moduleName)
|
||||
{
|
||||
fModuleName = moduleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief accessors and mutators
|
||||
*/
|
||||
EXPORT const sockaddr serv_addr() const
|
||||
{
|
||||
return fServ_addr;
|
||||
}
|
||||
EXPORT const std::string otherEnd() const
|
||||
{
|
||||
return fOtherEnd;
|
||||
}
|
||||
EXPORT bool isAvailable() const
|
||||
{
|
||||
return fIsAvailable;
|
||||
}
|
||||
EXPORT void isAvailable (const bool isAvailable)
|
||||
{
|
||||
fIsAvailable = isAvailable;
|
||||
}
|
||||
EXPORT const std::string moduleName() const
|
||||
{
|
||||
return fModuleName;
|
||||
}
|
||||
EXPORT void moduleName(const std::string& moduleName)
|
||||
{
|
||||
fModuleName = moduleName;
|
||||
}
|
||||
/**
|
||||
* @brief set the sync proto
|
||||
*/
|
||||
inline void syncProto(bool use);
|
||||
|
||||
/**
|
||||
* @brief set the sync proto
|
||||
*/
|
||||
inline void syncProto(bool use);
|
||||
/**
|
||||
* @brief return the address as a string
|
||||
*/
|
||||
inline const std::string addr2String() const;
|
||||
|
||||
/**
|
||||
* @brief return the address as a string
|
||||
*/
|
||||
inline const std::string addr2String() const;
|
||||
/**
|
||||
* @brief compare the addresses of 2 MessageQueueClient
|
||||
*/
|
||||
inline bool isSameAddr(const MessageQueueClient& rhs) const;
|
||||
|
||||
/**
|
||||
* @brief compare the addresses of 2 MessageQueueClient
|
||||
*/
|
||||
inline bool isSameAddr(const MessageQueueClient& rhs) const;
|
||||
bool isConnected()
|
||||
{
|
||||
return fClientSock.isConnected();
|
||||
}
|
||||
|
||||
bool isConnected()
|
||||
{
|
||||
return fClientSock.isConnected();
|
||||
}
|
||||
bool hasData()
|
||||
{
|
||||
return fClientSock.hasData();
|
||||
}
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
bool hasData()
|
||||
{
|
||||
return fClientSock.hasData();
|
||||
}
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
private:
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
MessageQueueClient(const MessageQueueClient& rhs);
|
||||
|
||||
private:
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
MessageQueueClient(const MessageQueueClient& rhs);
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
MessageQueueClient& operator=(const MessageQueueClient& rhs);
|
||||
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
MessageQueueClient& operator=(const MessageQueueClient& rhs);
|
||||
/** ctor helper
|
||||
*
|
||||
*/
|
||||
void setup(bool syncProto);
|
||||
|
||||
/** ctor helper
|
||||
*
|
||||
*/
|
||||
void setup(bool syncProto);
|
||||
|
||||
std::string fOtherEnd; /// the process name for this process
|
||||
struct sockaddr fServ_addr; /// the addr of the server (may be this process)
|
||||
config::Config* fConfig; /// config file has the IP addrs and port numbers
|
||||
mutable IOSocket fClientSock; /// the socket to communicate with the server
|
||||
mutable logging::Logger fLogger;
|
||||
bool fIsAvailable;
|
||||
std::string fModuleName;
|
||||
std::string fOtherEnd; /// the process name for this process
|
||||
struct sockaddr fServ_addr; /// the addr of the server (may be this process)
|
||||
config::Config* fConfig; /// config file has the IP addrs and port numbers
|
||||
mutable IOSocket fClientSock; /// the socket to communicate with the server
|
||||
mutable logging::Logger fLogger;
|
||||
bool fIsAvailable;
|
||||
std::string fModuleName;
|
||||
};
|
||||
|
||||
inline IOSocket& MessageQueueServer::clientSock() const
|
||||
{
|
||||
return fClientSock;
|
||||
return fClientSock;
|
||||
}
|
||||
inline const std::string MessageQueueClient::addr2String() const
|
||||
{
|
||||
return fClientSock.addr2String();
|
||||
return fClientSock.addr2String();
|
||||
}
|
||||
inline bool MessageQueueClient::isSameAddr(const MessageQueueClient& rhs) const
|
||||
{
|
||||
return fClientSock.isSameAddr(&rhs.fClientSock);
|
||||
return fClientSock.isSameAddr(&rhs.fClientSock);
|
||||
}
|
||||
inline void MessageQueueClient::syncProto(bool use)
|
||||
{
|
||||
fClientSock.syncProto(use);
|
||||
fClientSock.syncProto(use);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace messageqcpp
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
// vim:ts=4 sw=4:
|
||||
|
||||
|
@ -24,7 +24,6 @@
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
boost::mutex queueMutex;
|
||||
// Make linker happy
|
||||
std::multimap<std::string, ClientObject*> MessageQueueClientPool::clientMap;
|
||||
@ -34,177 +33,175 @@ std::multimap<std::string, ClientObject*> MessageQueueClientPool::clientMap;
|
||||
|
||||
static uint64_t TimeSpecToSeconds(struct timespec* ts)
|
||||
{
|
||||
return (uint64_t)ts->tv_sec + (uint64_t)ts->tv_nsec / 1000000000;
|
||||
return (uint64_t)ts->tv_sec + (uint64_t)ts->tv_nsec / 1000000000;
|
||||
}
|
||||
|
||||
MessageQueueClient *MessageQueueClientPool::getInstance(const std::string &dnOrIp, uint64_t port)
|
||||
MessageQueueClient* MessageQueueClientPool::getInstance(const std::string& dnOrIp, uint64_t port)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << dnOrIp << "_" << port;
|
||||
std::string searchString = oss.str();
|
||||
std::ostringstream oss;
|
||||
oss << dnOrIp << "_" << port;
|
||||
std::string searchString = oss.str();
|
||||
|
||||
MessageQueueClient* returnClient = MessageQueueClientPool::findInPool(searchString);
|
||||
MessageQueueClient* returnClient = MessageQueueClientPool::findInPool(searchString);
|
||||
|
||||
// We found one, return it
|
||||
if (returnClient != NULL)
|
||||
{
|
||||
return returnClient;
|
||||
}
|
||||
// We found one, return it
|
||||
if (returnClient != NULL)
|
||||
{
|
||||
return returnClient;
|
||||
}
|
||||
|
||||
// We didn't find one, create new one
|
||||
ClientObject* newClientObject = new ClientObject();
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
// We didn't find one, create new one
|
||||
ClientObject* newClientObject = new ClientObject();
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
|
||||
newClientObject->client = new MessageQueueClient(dnOrIp, port);
|
||||
newClientObject->inUse = true;
|
||||
newClientObject->lastUsed = nowSeconds;
|
||||
clientMap.insert(std::pair<std::string, ClientObject*>(searchString, newClientObject));
|
||||
return newClientObject->client;
|
||||
newClientObject->client = new MessageQueueClient(dnOrIp, port);
|
||||
newClientObject->inUse = true;
|
||||
newClientObject->lastUsed = nowSeconds;
|
||||
clientMap.insert(std::pair<std::string, ClientObject*>(searchString, newClientObject));
|
||||
return newClientObject->client;
|
||||
}
|
||||
|
||||
MessageQueueClient* MessageQueueClientPool::getInstance(const std::string& module)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
|
||||
MessageQueueClient* returnClient = MessageQueueClientPool::findInPool(module);
|
||||
MessageQueueClient* returnClient = MessageQueueClientPool::findInPool(module);
|
||||
|
||||
// We found one, return it
|
||||
if (returnClient != NULL)
|
||||
{
|
||||
return returnClient;
|
||||
}
|
||||
// We found one, return it
|
||||
if (returnClient != NULL)
|
||||
{
|
||||
return returnClient;
|
||||
}
|
||||
|
||||
// We didn't find one, create new one
|
||||
ClientObject* newClientObject = new ClientObject();
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
// We didn't find one, create new one
|
||||
ClientObject* newClientObject = new ClientObject();
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
|
||||
newClientObject->client = new MessageQueueClient(module);
|
||||
newClientObject->inUse = true;
|
||||
newClientObject->lastUsed = nowSeconds;
|
||||
clientMap.insert(std::pair<std::string, ClientObject*>(module, newClientObject));
|
||||
return newClientObject->client;
|
||||
newClientObject->client = new MessageQueueClient(module);
|
||||
newClientObject->inUse = true;
|
||||
newClientObject->lastUsed = nowSeconds;
|
||||
clientMap.insert(std::pair<std::string, ClientObject*>(module, newClientObject));
|
||||
return newClientObject->client;
|
||||
}
|
||||
|
||||
MessageQueueClient* MessageQueueClientPool::findInPool(const std::string& search)
|
||||
{
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
MessageQueueClient* returnClient = NULL;
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
MessageQueueClient* returnClient = NULL;
|
||||
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
|
||||
// Scan pool
|
||||
while (it != clientMap.end())
|
||||
// Scan pool
|
||||
while (it != clientMap.end())
|
||||
{
|
||||
ClientObject* clientObject = it->second;
|
||||
uint64_t elapsedTime = nowSeconds - clientObject->lastUsed;
|
||||
|
||||
// If connection hasn't been used for MAX_IDLE_TIME we probably don't need it so drop it
|
||||
// Don't drop in use connections that have been in use a long time
|
||||
if ((elapsedTime >= MAX_IDLE_TIME) && (!clientObject->inUse))
|
||||
{
|
||||
ClientObject* clientObject = it->second;
|
||||
uint64_t elapsedTime = nowSeconds - clientObject->lastUsed;
|
||||
|
||||
// If connection hasn't been used for MAX_IDLE_TIME we probably don't need it so drop it
|
||||
// Don't drop in use connections that have been in use a long time
|
||||
if ((elapsedTime >= MAX_IDLE_TIME) && (!clientObject->inUse))
|
||||
{
|
||||
delete clientObject->client;
|
||||
delete clientObject;
|
||||
// Do this so we don't invalidate current interator
|
||||
std::multimap<std::string, ClientObject*>::iterator toDelete = it;
|
||||
it++;
|
||||
clientMap.erase(toDelete);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!clientObject->inUse)
|
||||
{
|
||||
MessageQueueClient* client = clientObject->client;
|
||||
|
||||
// If the unused socket isn't connected or has data pending read, destroy it
|
||||
if (!client->isConnected() || client->hasData())
|
||||
{
|
||||
delete client;
|
||||
delete clientObject;
|
||||
// Do this so we don't invalidate current interator
|
||||
std::multimap<std::string, ClientObject*>::iterator toDelete = it;
|
||||
it++;
|
||||
clientMap.erase(toDelete);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// If connection matches store it for later, but keep scanning the pool for more timeout prunes
|
||||
if (it->first.compare(search) == 0)
|
||||
{
|
||||
if ((returnClient == NULL) && (!clientObject->inUse))
|
||||
{
|
||||
returnClient = clientObject->client;
|
||||
clientObject->inUse = true;
|
||||
return returnClient;
|
||||
}
|
||||
}
|
||||
|
||||
it++;
|
||||
delete clientObject->client;
|
||||
delete clientObject;
|
||||
// Do this so we don't invalidate current interator
|
||||
std::multimap<std::string, ClientObject*>::iterator toDelete = it;
|
||||
it++;
|
||||
clientMap.erase(toDelete);
|
||||
continue;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
if (!clientObject->inUse)
|
||||
{
|
||||
MessageQueueClient* client = clientObject->client;
|
||||
|
||||
// If the unused socket isn't connected or has data pending read, destroy it
|
||||
if (!client->isConnected() || client->hasData())
|
||||
{
|
||||
delete client;
|
||||
delete clientObject;
|
||||
// Do this so we don't invalidate current interator
|
||||
std::multimap<std::string, ClientObject*>::iterator toDelete = it;
|
||||
it++;
|
||||
clientMap.erase(toDelete);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If connection matches store it for later, but keep scanning the pool for more timeout prunes
|
||||
if (it->first.compare(search) == 0)
|
||||
{
|
||||
if ((returnClient == NULL) && (!clientObject->inUse))
|
||||
{
|
||||
returnClient = clientObject->client;
|
||||
clientObject->inUse = true;
|
||||
return returnClient;
|
||||
}
|
||||
}
|
||||
|
||||
it++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void MessageQueueClientPool::releaseInstance(MessageQueueClient* client)
|
||||
{
|
||||
// Scan pool for pointer and release
|
||||
// Set the last used and mark as not in use
|
||||
// Scan pool for pointer and release
|
||||
// Set the last used and mark as not in use
|
||||
|
||||
if (client == NULL)
|
||||
return;
|
||||
if (client == NULL)
|
||||
return;
|
||||
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
|
||||
while (it != clientMap.end())
|
||||
while (it != clientMap.end())
|
||||
{
|
||||
if (it->second->client == client)
|
||||
{
|
||||
if (it->second->client == client)
|
||||
{
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
it->second->inUse = false;
|
||||
it->second->lastUsed = nowSeconds;
|
||||
return;
|
||||
}
|
||||
|
||||
it++;
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
uint64_t nowSeconds = TimeSpecToSeconds(&now);
|
||||
it->second->inUse = false;
|
||||
it->second->lastUsed = nowSeconds;
|
||||
return;
|
||||
}
|
||||
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
// WriteEngine needs this as it forces connections closed and can't reuse. Also good for connection errors
|
||||
void MessageQueueClientPool::deleteInstance(MessageQueueClient* client)
|
||||
{
|
||||
// Scan pool for pointer and delete
|
||||
// Set the last used and mark as not in use
|
||||
// Scan pool for pointer and delete
|
||||
// Set the last used and mark as not in use
|
||||
|
||||
if (client == NULL)
|
||||
return;
|
||||
if (client == NULL)
|
||||
return;
|
||||
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
boost::mutex::scoped_lock lock(queueMutex);
|
||||
std::multimap<std::string, ClientObject*>::iterator it = clientMap.begin();
|
||||
|
||||
while (it != clientMap.end())
|
||||
while (it != clientMap.end())
|
||||
{
|
||||
if (it->second->client == client)
|
||||
{
|
||||
if (it->second->client == client)
|
||||
{
|
||||
delete it->second->client;
|
||||
delete it->second;
|
||||
clientMap.erase(it);
|
||||
return;
|
||||
}
|
||||
|
||||
it++;
|
||||
delete it->second->client;
|
||||
delete it->second;
|
||||
clientMap.erase(it);
|
||||
return;
|
||||
}
|
||||
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace messageqcpp
|
||||
|
@ -22,35 +22,31 @@
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
|
||||
struct ClientObject
|
||||
{
|
||||
MessageQueueClient* client;
|
||||
uint64_t lastUsed;
|
||||
bool inUse;
|
||||
MessageQueueClient* client;
|
||||
uint64_t lastUsed;
|
||||
bool inUse;
|
||||
|
||||
ClientObject() :
|
||||
client(NULL),
|
||||
lastUsed(0),
|
||||
inUse(false)
|
||||
{}
|
||||
ClientObject() : client(NULL), lastUsed(0), inUse(false)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class MessageQueueClientPool
|
||||
{
|
||||
public:
|
||||
static MessageQueueClient* getInstance(const std::string& module);
|
||||
static MessageQueueClient *getInstance(const std::string &dnOrIp, uint64_t port);
|
||||
static void releaseInstance(MessageQueueClient* client);
|
||||
static void deleteInstance(MessageQueueClient* client);
|
||||
static MessageQueueClient* findInPool(const std::string& search);
|
||||
public:
|
||||
static MessageQueueClient* getInstance(const std::string& module);
|
||||
static MessageQueueClient* getInstance(const std::string& dnOrIp, uint64_t port);
|
||||
static void releaseInstance(MessageQueueClient* client);
|
||||
static void deleteInstance(MessageQueueClient* client);
|
||||
static MessageQueueClient* findInPool(const std::string& search);
|
||||
|
||||
private:
|
||||
MessageQueueClientPool() { };
|
||||
~MessageQueueClientPool() { };
|
||||
private:
|
||||
MessageQueueClientPool(){};
|
||||
~MessageQueueClientPool(){};
|
||||
|
||||
static std::multimap<std::string, ClientObject*> clientMap;
|
||||
static std::multimap<std::string, ClientObject*> clientMap;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace messageqcpp
|
||||
|
@ -28,32 +28,30 @@
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
class ByteStream;
|
||||
|
||||
/** This is an abstract class that defines the interface ByteStream will
|
||||
use to serialize and deserialize your class.
|
||||
|
||||
To serialize an object, do 'ByteStream << object'
|
||||
To deserialize an object, instantiate one of its type and do 'ByteStream >> object'
|
||||
To serialize an object, do 'ByteStream << object'
|
||||
To deserialize an object, instantiate one of its type and do 'ByteStream >> object'
|
||||
*/
|
||||
|
||||
class Serializeable
|
||||
{
|
||||
public:
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~Serializeable() { };
|
||||
/** serialize interface
|
||||
*
|
||||
*/
|
||||
virtual void serialize(ByteStream&) const = 0;
|
||||
/** deserialize interface
|
||||
*
|
||||
*/
|
||||
virtual void deserialize(ByteStream&) = 0;
|
||||
public:
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~Serializeable(){};
|
||||
/** serialize interface
|
||||
*
|
||||
*/
|
||||
virtual void serialize(ByteStream&) const = 0;
|
||||
/** deserialize interface
|
||||
*
|
||||
*/
|
||||
virtual void deserialize(ByteStream&) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: serversocket.h 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: serversocket.h 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -46,128 +46,127 @@ class SocketParms;
|
||||
*/
|
||||
class ServerSocket
|
||||
{
|
||||
public:
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit ServerSocket(Socket* socket = 0) : fSocket(socket)
|
||||
{
|
||||
}
|
||||
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit ServerSocket(Socket* socket = 0) : fSocket(socket) {}
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~ServerSocket()
|
||||
{
|
||||
delete fSocket;
|
||||
}
|
||||
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~ServerSocket()
|
||||
{
|
||||
delete fSocket;
|
||||
}
|
||||
/** bind to an port
|
||||
*
|
||||
* bind this ServerSocket to the address/port specified in serv_addr
|
||||
*/
|
||||
inline virtual void bind(const struct sockaddr* serv_addr);
|
||||
|
||||
/** bind to an port
|
||||
*
|
||||
* bind this ServerSocket to the address/port specified in serv_addr
|
||||
*/
|
||||
inline virtual void bind(const struct sockaddr* serv_addr);
|
||||
/** setup to listen for incoming connections
|
||||
*
|
||||
*/
|
||||
inline virtual void listen(int backlog = 5);
|
||||
|
||||
/** setup to listen for incoming connections
|
||||
*
|
||||
*/
|
||||
inline virtual void listen(int backlog = 5);
|
||||
/** accept an incoming connection
|
||||
*
|
||||
* accepts a new incoming connection and returns an IOSocket to communicate over
|
||||
*/
|
||||
inline virtual const IOSocket accept(const struct timespec* timeout = 0);
|
||||
|
||||
/** accept an incoming connection
|
||||
*
|
||||
* accepts a new incoming connection and returns an IOSocket to communicate over
|
||||
*/
|
||||
inline virtual const IOSocket accept(const struct timespec* timeout = 0);
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void open();
|
||||
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void open();
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void close();
|
||||
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
inline virtual void close();
|
||||
/** test if the socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
|
||||
/** test if the socket is open
|
||||
*
|
||||
*/
|
||||
inline virtual bool isOpen() const;
|
||||
/** get the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
|
||||
/** get the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual const SocketParms socketParms() const;
|
||||
/** set the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socketParms);
|
||||
|
||||
/** set the socket params
|
||||
*
|
||||
*/
|
||||
inline virtual void socketParms(const SocketParms& socketParms);
|
||||
/** set the socket implementation
|
||||
*
|
||||
* Install a socket implementation that meets the Socket interface
|
||||
*/
|
||||
inline virtual void setSocketImpl(Socket* socket);
|
||||
|
||||
/** set the socket implementation
|
||||
*
|
||||
* Install a socket implementation that meets the Socket interface
|
||||
*/
|
||||
inline virtual void setSocketImpl(Socket* socket);
|
||||
/** set the socket sync proto
|
||||
*
|
||||
*/
|
||||
inline virtual void syncProto(bool use);
|
||||
|
||||
/** set the socket sync proto
|
||||
*
|
||||
*/
|
||||
inline virtual void syncProto(bool use);
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
private:
|
||||
ServerSocket(const ServerSocket& rhs);
|
||||
ServerSocket& operator=(const ServerSocket& rhs);
|
||||
|
||||
private:
|
||||
ServerSocket(const ServerSocket& rhs);
|
||||
ServerSocket& operator=(const ServerSocket& rhs);
|
||||
|
||||
Socket* fSocket;
|
||||
Socket* fSocket;
|
||||
};
|
||||
|
||||
inline void ServerSocket::bind(const struct sockaddr* serv_addr)
|
||||
{
|
||||
fSocket->bind(serv_addr);
|
||||
fSocket->bind(serv_addr);
|
||||
}
|
||||
inline void ServerSocket::listen(int backlog)
|
||||
{
|
||||
fSocket->listen(backlog);
|
||||
fSocket->listen(backlog);
|
||||
}
|
||||
inline const IOSocket ServerSocket::accept(const struct timespec* timeout)
|
||||
{
|
||||
return fSocket->accept(timeout);
|
||||
return fSocket->accept(timeout);
|
||||
}
|
||||
inline void ServerSocket::open()
|
||||
{
|
||||
fSocket->open();
|
||||
fSocket->open();
|
||||
}
|
||||
inline void ServerSocket::close()
|
||||
{
|
||||
fSocket->close();
|
||||
fSocket->close();
|
||||
}
|
||||
inline bool ServerSocket::isOpen() const
|
||||
{
|
||||
return fSocket->isOpen();
|
||||
return fSocket->isOpen();
|
||||
}
|
||||
inline const SocketParms ServerSocket::socketParms() const
|
||||
{
|
||||
return fSocket->socketParms();
|
||||
return fSocket->socketParms();
|
||||
}
|
||||
inline void ServerSocket::socketParms(const SocketParms& socketParms)
|
||||
{
|
||||
fSocket->socketParms(socketParms);
|
||||
fSocket->socketParms(socketParms);
|
||||
}
|
||||
inline void ServerSocket::setSocketImpl(Socket* socket)
|
||||
{
|
||||
delete fSocket;
|
||||
fSocket = socket;
|
||||
delete fSocket;
|
||||
fSocket = socket;
|
||||
}
|
||||
inline void ServerSocket::syncProto(bool use)
|
||||
{
|
||||
fSocket->syncProto(use);
|
||||
fSocket->syncProto(use);
|
||||
}
|
||||
|
||||
} //namespace messageqcpp
|
||||
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: socket.h 3633 2013-03-13 20:50:23Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: socket.h 3633 2013-03-13 20:50:23Z pleblanc $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -36,34 +36,37 @@ namespace messageqcpp
|
||||
class IOSocket;
|
||||
class SocketParms;
|
||||
|
||||
|
||||
// Might want to expand on this / derive from it to include things like uncompressed
|
||||
// data size, etc...
|
||||
class Stats
|
||||
{
|
||||
public:
|
||||
Stats() : data_sent(0), data_recvd(0)
|
||||
{ }
|
||||
virtual ~Stats() { }
|
||||
virtual uint64_t dataSent()
|
||||
{
|
||||
return data_sent;
|
||||
}
|
||||
virtual uint64_t dataRecvd()
|
||||
{
|
||||
return data_recvd;
|
||||
}
|
||||
virtual void dataSent(uint64_t amt)
|
||||
{
|
||||
data_sent += amt;
|
||||
}
|
||||
virtual void dataRecvd(uint64_t amt)
|
||||
{
|
||||
data_recvd += amt;
|
||||
}
|
||||
private:
|
||||
uint64_t data_sent;
|
||||
uint64_t data_recvd;
|
||||
public:
|
||||
Stats() : data_sent(0), data_recvd(0)
|
||||
{
|
||||
}
|
||||
virtual ~Stats()
|
||||
{
|
||||
}
|
||||
virtual uint64_t dataSent()
|
||||
{
|
||||
return data_sent;
|
||||
}
|
||||
virtual uint64_t dataRecvd()
|
||||
{
|
||||
return data_recvd;
|
||||
}
|
||||
virtual void dataSent(uint64_t amt)
|
||||
{
|
||||
data_sent += amt;
|
||||
}
|
||||
virtual void dataRecvd(uint64_t amt)
|
||||
{
|
||||
data_recvd += amt;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t data_sent;
|
||||
uint64_t data_recvd;
|
||||
};
|
||||
|
||||
/** an abstract socket class interface
|
||||
@ -71,116 +74,114 @@ private:
|
||||
*/
|
||||
class Socket
|
||||
{
|
||||
public:
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~Socket() {}
|
||||
public:
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~Socket()
|
||||
{
|
||||
}
|
||||
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
virtual void open() = 0;
|
||||
/** open the socket
|
||||
*
|
||||
*/
|
||||
virtual void open() = 0;
|
||||
|
||||
/** read a message from the socket
|
||||
*
|
||||
* wait for and return a message from the socket. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has millisecond resolution.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL, Stats* stats = NULL) const = 0;
|
||||
/** read a message from the socket
|
||||
*
|
||||
* wait for and return a message from the socket. The deafult timeout waits forever. Note that
|
||||
* eventhough struct timespec has nanosecond resolution, this method only has millisecond resolution.
|
||||
*/
|
||||
virtual const SBS read(const struct timespec* timeout = 0, bool* isTimeOut = NULL,
|
||||
Stats* stats = NULL) const = 0;
|
||||
|
||||
/** write a message to the socket
|
||||
*
|
||||
* write a message to the socket
|
||||
*/
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL) = 0;
|
||||
virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const = 0;
|
||||
virtual void write(SBS msg, Stats* stats = NULL) = 0;
|
||||
/** write a message to the socket
|
||||
*
|
||||
* write a message to the socket
|
||||
*/
|
||||
virtual void write(const ByteStream& msg, Stats* stats = NULL) = 0;
|
||||
virtual void write_raw(const ByteStream& msg, Stats* stats = NULL) const = 0;
|
||||
virtual void write(SBS msg, Stats* stats = NULL) = 0;
|
||||
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
virtual void close() = 0;
|
||||
/** close the socket
|
||||
*
|
||||
*/
|
||||
virtual void close() = 0;
|
||||
|
||||
/** bind to a port
|
||||
*
|
||||
*/
|
||||
virtual void bind(const struct sockaddr* serv_addr) = 0;
|
||||
/** bind to a port
|
||||
*
|
||||
*/
|
||||
virtual void bind(const struct sockaddr* serv_addr) = 0;
|
||||
|
||||
/** listen for connections
|
||||
*
|
||||
*/
|
||||
virtual void listen(int backlog = 5) = 0;
|
||||
/** listen for connections
|
||||
*
|
||||
*/
|
||||
virtual void listen(int backlog = 5) = 0;
|
||||
|
||||
/** return an (accepted) IOSocket ready for I/O
|
||||
*
|
||||
*/
|
||||
virtual const IOSocket accept(const struct timespec* timeout = 0) = 0;
|
||||
/** return an (accepted) IOSocket ready for I/O
|
||||
*
|
||||
*/
|
||||
virtual const IOSocket accept(const struct timespec* timeout = 0) = 0;
|
||||
|
||||
/** connect to a server socket
|
||||
*
|
||||
*/
|
||||
virtual void connect(const sockaddr* serv_addr) = 0;
|
||||
/** connect to a server socket
|
||||
*
|
||||
*/
|
||||
virtual void connect(const sockaddr* serv_addr) = 0;
|
||||
|
||||
/** test if this socket is open
|
||||
*
|
||||
*/
|
||||
virtual bool isOpen() const = 0;
|
||||
/** test if this socket is open
|
||||
*
|
||||
*/
|
||||
virtual bool isOpen() const = 0;
|
||||
|
||||
/** get the SocketParms
|
||||
*
|
||||
*/
|
||||
virtual const SocketParms socketParms() const = 0;
|
||||
/** get the SocketParms
|
||||
*
|
||||
*/
|
||||
virtual const SocketParms socketParms() const = 0;
|
||||
|
||||
/** set the SocketParms
|
||||
*
|
||||
*/
|
||||
virtual void socketParms(const SocketParms& socketParms) = 0;
|
||||
/** set the SocketParms
|
||||
*
|
||||
*/
|
||||
virtual void socketParms(const SocketParms& socketParms) = 0;
|
||||
|
||||
/** set the sockaddr struct
|
||||
*
|
||||
*/
|
||||
virtual void sa(const sockaddr* sa) = 0;
|
||||
/** set the sockaddr struct
|
||||
*
|
||||
*/
|
||||
virtual void sa(const sockaddr* sa) = 0;
|
||||
|
||||
/** dynamically allocate a copy of this object
|
||||
*
|
||||
*/
|
||||
virtual Socket* clone() const = 0;
|
||||
/** dynamically allocate a copy of this object
|
||||
*
|
||||
*/
|
||||
virtual Socket* clone() const = 0;
|
||||
|
||||
/** set the connection timeout (in ms)
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct ::timespec* timeout) = 0;
|
||||
/** set the connection timeout (in ms)
|
||||
*
|
||||
*/
|
||||
virtual void connectionTimeout(const struct ::timespec* timeout) = 0;
|
||||
|
||||
/** set the connection protocol to be synchronous
|
||||
*
|
||||
*/
|
||||
virtual void syncProto(bool use) = 0;
|
||||
/** set the connection protocol to be synchronous
|
||||
*
|
||||
*/
|
||||
virtual void syncProto(bool use) = 0;
|
||||
|
||||
virtual int getConnectionNum() const = 0;
|
||||
virtual int getConnectionNum() const = 0;
|
||||
|
||||
/** return the address as a string
|
||||
*
|
||||
*/
|
||||
virtual const std::string addr2String() const = 0;
|
||||
/** return the address as a string
|
||||
*
|
||||
*/
|
||||
virtual const std::string addr2String() const = 0;
|
||||
|
||||
/** compare 2 addresses
|
||||
*
|
||||
*/
|
||||
virtual bool isSameAddr(const Socket* rhs) const = 0;
|
||||
/** compare 2 addresses
|
||||
*
|
||||
*/
|
||||
virtual bool isSameAddr(const Socket* rhs) const = 0;
|
||||
|
||||
virtual bool isConnected() const = 0;
|
||||
virtual bool hasData() const = 0;
|
||||
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
virtual bool isConnected() const = 0;
|
||||
virtual bool hasData() const = 0;
|
||||
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} //namespace messageqcpp
|
||||
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -29,31 +29,30 @@
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
/** @brief A closed socket exception class
|
||||
*
|
||||
* Some sort of activity has been requested on a closed socket
|
||||
*/
|
||||
*
|
||||
* Some sort of activity has been requested on a closed socket
|
||||
*/
|
||||
class SocketClosed : public std::exception
|
||||
{
|
||||
std::string _M_msg;
|
||||
std::string _M_msg;
|
||||
|
||||
public:
|
||||
/** Takes a character string describing the error. */
|
||||
explicit
|
||||
SocketClosed(const std::string& __arg) : _M_msg(__arg) { }
|
||||
public:
|
||||
/** Takes a character string describing the error. */
|
||||
explicit SocketClosed(const std::string& __arg) : _M_msg(__arg)
|
||||
{
|
||||
}
|
||||
|
||||
virtual
|
||||
~SocketClosed() throw() { }
|
||||
virtual ~SocketClosed() throw()
|
||||
{
|
||||
}
|
||||
|
||||
/** Returns a C-style character string describing the general cause of
|
||||
* the current error (the same string passed to the ctor). */
|
||||
virtual const char*
|
||||
what() const throw()
|
||||
{
|
||||
return _M_msg.c_str();
|
||||
}
|
||||
/** Returns a C-style character string describing the general cause of
|
||||
* the current error (the same string passed to the ctor). */
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return _M_msg.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,18 +16,17 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: socketparms.cpp 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: socketparms.cpp 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
#include "socketparms.h"
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
SocketParms::SocketParms(int domain, int type, int protocol) :
|
||||
fSd(-1), fDomain(domain), fType(type), fProtocol(protocol)
|
||||
SocketParms::SocketParms(int domain, int type, int protocol)
|
||||
: fSd(-1), fDomain(domain), fType(type), fProtocol(protocol)
|
||||
{
|
||||
}
|
||||
|
||||
@ -37,26 +36,25 @@ SocketParms::~SocketParms()
|
||||
|
||||
void SocketParms::doCopy(const SocketParms& rhs)
|
||||
{
|
||||
fSd = rhs.fSd;
|
||||
fDomain = rhs.fDomain;
|
||||
fType = rhs.fType;
|
||||
fProtocol = rhs.fProtocol;
|
||||
fSd = rhs.fSd;
|
||||
fDomain = rhs.fDomain;
|
||||
fType = rhs.fType;
|
||||
fProtocol = rhs.fProtocol;
|
||||
}
|
||||
|
||||
SocketParms::SocketParms(const SocketParms& rhs)
|
||||
{
|
||||
doCopy(rhs);
|
||||
doCopy(rhs);
|
||||
}
|
||||
|
||||
SocketParms& SocketParms::operator=(const SocketParms& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
doCopy(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
if (this != &rhs)
|
||||
{
|
||||
doCopy(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -16,10 +16,10 @@
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
/***********************************************************************
|
||||
* $Id: socketparms.h 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
* $Id: socketparms.h 3495 2013-01-21 14:09:51Z rdempsey $
|
||||
*
|
||||
*
|
||||
***********************************************************************/
|
||||
/** @file */
|
||||
#pragma once
|
||||
|
||||
@ -27,129 +27,126 @@ class MessageQTestSuite;
|
||||
|
||||
namespace messageqcpp
|
||||
{
|
||||
|
||||
/** a simple socket parameters class
|
||||
*
|
||||
*/
|
||||
class SocketParms
|
||||
{
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit SocketParms(int domain = -1, int type = -1, int protocol = -1);
|
||||
public:
|
||||
/** ctor
|
||||
*
|
||||
*/
|
||||
explicit SocketParms(int domain = -1, int type = -1, int protocol = -1);
|
||||
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~SocketParms();
|
||||
/** dtor
|
||||
*
|
||||
*/
|
||||
virtual ~SocketParms();
|
||||
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
SocketParms(const SocketParms& rhs);
|
||||
/** copy ctor
|
||||
*
|
||||
*/
|
||||
SocketParms(const SocketParms& rhs);
|
||||
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
SocketParms& operator=(const SocketParms& rhs);
|
||||
/** assign op
|
||||
*
|
||||
*/
|
||||
SocketParms& operator=(const SocketParms& rhs);
|
||||
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int sd() const;
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int sd() const;
|
||||
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int domain() const;
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int domain() const;
|
||||
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int type() const;
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int type() const;
|
||||
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int protocol() const;
|
||||
/** accessor
|
||||
*
|
||||
*/
|
||||
inline int protocol() const;
|
||||
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void sd(int sd);
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void sd(int sd);
|
||||
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void domain(int domain);
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void domain(int domain);
|
||||
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void type(int type);
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void type(int type);
|
||||
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void protocol(int protocol);
|
||||
/** mutator
|
||||
*
|
||||
*/
|
||||
inline void protocol(int protocol);
|
||||
|
||||
/** isOpen test
|
||||
*
|
||||
*/
|
||||
inline bool isOpen() const;
|
||||
/** isOpen test
|
||||
*
|
||||
*/
|
||||
inline bool isOpen() const;
|
||||
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
/*
|
||||
* allow test suite access to private data for OOB test
|
||||
*/
|
||||
friend class ::MessageQTestSuite;
|
||||
|
||||
private:
|
||||
void doCopy(const SocketParms& rhs);
|
||||
private:
|
||||
void doCopy(const SocketParms& rhs);
|
||||
|
||||
int fSd; /// the socket descriptor
|
||||
int fDomain; /// the socket domain
|
||||
int fType; /// the socket type
|
||||
int fProtocol; /// the socket protocol
|
||||
int fSd; /// the socket descriptor
|
||||
int fDomain; /// the socket domain
|
||||
int fType; /// the socket type
|
||||
int fProtocol; /// the socket protocol
|
||||
};
|
||||
|
||||
inline int SocketParms::sd() const
|
||||
{
|
||||
return fSd;
|
||||
return fSd;
|
||||
}
|
||||
inline int SocketParms::domain() const
|
||||
{
|
||||
return fDomain;
|
||||
return fDomain;
|
||||
}
|
||||
inline int SocketParms::type() const
|
||||
{
|
||||
return fType;
|
||||
return fType;
|
||||
}
|
||||
inline int SocketParms::protocol() const
|
||||
{
|
||||
return fProtocol;
|
||||
return fProtocol;
|
||||
}
|
||||
inline bool SocketParms::isOpen() const
|
||||
{
|
||||
return (fSd >= 0);
|
||||
return (fSd >= 0);
|
||||
}
|
||||
inline void SocketParms::sd(int sd)
|
||||
{
|
||||
fSd = sd;
|
||||
fSd = sd;
|
||||
}
|
||||
inline void SocketParms::domain(int domain)
|
||||
{
|
||||
fDomain = domain;
|
||||
fDomain = domain;
|
||||
}
|
||||
inline void SocketParms::type(int type)
|
||||
{
|
||||
fType = type;
|
||||
fType = type;
|
||||
}
|
||||
inline void SocketParms::protocol(int protocol)
|
||||
{
|
||||
fProtocol = protocol;
|
||||
fProtocol = protocol;
|
||||
}
|
||||
|
||||
} //namespace messageqcpp
|
||||
|
||||
|
||||
} // namespace messageqcpp
|
||||
|
@ -9,33 +9,32 @@ using namespace config;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Config* cf = Config::makeConfig("./Columnstore.xml");
|
||||
MessageQueueServer mqs("server1", cf);
|
||||
Config* cf = Config::makeConfig("./Columnstore.xml");
|
||||
MessageQueueServer mqs("server1", cf);
|
||||
|
||||
cout << "server ready..." << endl;
|
||||
cout << "server ready..." << endl;
|
||||
|
||||
IOSocket ios;
|
||||
ByteStream ibs;
|
||||
ByteStream obs;
|
||||
uint32_t qb = 0;
|
||||
IOSocket ios;
|
||||
ByteStream ibs;
|
||||
ByteStream obs;
|
||||
uint32_t qb = 0;
|
||||
|
||||
while (1)
|
||||
while (1)
|
||||
{
|
||||
ios = mqs.accept();
|
||||
ibs = ios.read();
|
||||
|
||||
while (ibs.length() > 0)
|
||||
{
|
||||
ios = mqs.accept();
|
||||
ibs = ios.read();
|
||||
|
||||
while (ibs.length() > 0)
|
||||
{
|
||||
cout << "read " << ibs.length() << " bytes from " << ios << endl;
|
||||
obs.restart();
|
||||
obs << qb;
|
||||
ios.write(obs);
|
||||
ibs = ios.read();
|
||||
}
|
||||
|
||||
ios.close();
|
||||
cout << "read " << ibs.length() << " bytes from " << ios << endl;
|
||||
obs.restart();
|
||||
obs << qb;
|
||||
ios.write(obs);
|
||||
ibs = ios.read();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
ios.close();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user