Add manual control over subscriptions to Pub

This commit is contained in:
somdoron
2014-11-26 10:38:54 +02:00
parent cefce68a97
commit 96e29f1455
5 changed files with 145 additions and 24 deletions

View File

@@ -28,9 +28,11 @@ zmq::xpub_t::xpub_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
socket_base_t (parent_, tid_, sid_),
verbose (false),
more (false),
lossy (true)
lossy (true),
manual(false)
{
options.type = ZMQ_XPUB;
last_pipe = NULL;
options.type = ZMQ_XPUB;
}
zmq::xpub_t::~xpub_t ()
@@ -61,18 +63,27 @@ void zmq::xpub_t::xread_activated (pipe_t *pipe_)
unsigned char *const data = (unsigned char *) sub.data ();
const size_t size = sub.size ();
if (size > 0 && (*data == 0 || *data == 1)) {
bool unique;
if (*data == 0)
unique = subscriptions.rm (data + 1, size - 1, pipe_);
else
unique = subscriptions.add (data + 1, size - 1, pipe_);
if (manual)
{
last_pipe = pipe_;
pending_data.push_back(blob_t(data, size));
pending_flags.push_back(0);
}
else
{
bool unique;
if (*data == 0)
unique = subscriptions.rm(data + 1, size - 1, pipe_);
else
unique = subscriptions.add(data + 1, size - 1, pipe_);
// If the subscription is not a duplicate store it so that it can be
// passed to used on next recv call. (Unsubscribe is not verbose.)
if (options.type == ZMQ_XPUB && (unique || (*data && verbose))) {
pending_data.push_back (blob_t (data, size));
pending_flags.push_back (0);
}
// If the subscription is not a duplicate store it so that it can be
// passed to used on next recv call. (Unsubscribe is not verbose.)
if (options.type == ZMQ_XPUB && (unique || (*data && verbose))) {
pending_data.push_back(blob_t(data, size));
pending_flags.push_back(0);
}
}
}
else {
// Process user message coming upstream from xsub socket
@@ -90,16 +101,33 @@ void zmq::xpub_t::xwrite_activated (pipe_t *pipe_)
int zmq::xpub_t::xsetsockopt (int option_, const void *optval_,
size_t optvallen_)
{
if (optvallen_ != sizeof (int) || *static_cast <const int*> (optval_) < 0) {
errno = EINVAL;
return -1;
}
if (option_ == ZMQ_XPUB_VERBOSE)
verbose = (*static_cast <const int*> (optval_) != 0);
else
if (option_ == ZMQ_XPUB_NODROP)
lossy = (*static_cast <const int*> (optval_) == 0);
{
if (option_ == ZMQ_XPUB_VERBOSE || option_ == ZMQ_XPUB_NODROP || option_ == ZMQ_XPUB_MANUAL)
{
if (optvallen_ != sizeof(int) || *static_cast <const int*> (optval_) < 0) {
errno = EINVAL;
return -1;
}
if (option_ == ZMQ_XPUB_VERBOSE)
verbose = (*static_cast <const int*> (optval_) != 0);
else
if (option_ == ZMQ_XPUB_NODROP)
lossy = (*static_cast <const int*> (optval_) == 0);
else
if (option_ == ZMQ_XPUB_MANUAL)
manual = (*static_cast <const int*> (optval_) != 0);
}
else
if (option_ == ZMQ_SUBSCRIBE && manual && last_pipe != NULL)
{
subscriptions.add((unsigned char *)optval_, optvallen_, last_pipe);
}
else
if (option_ == ZMQ_UNSUBSCRIBE && manual && last_pipe != NULL)
{
subscriptions.rm((unsigned char *)optval_, optvallen_, last_pipe);
}
else {
errno = EINVAL;
return -1;