2009-08-08 16:01:58 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2009 FastMQ Inc.
|
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
#include <string>
|
2009-08-08 16:01:58 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2009-09-16 16:49:09 +02:00
|
|
|
#include "../bindings/c/zmq.h"
|
2009-08-08 16:01:58 +02:00
|
|
|
|
|
|
|
#include "socket_base.hpp"
|
|
|
|
#include "app_thread.hpp"
|
2009-09-04 16:02:41 +02:00
|
|
|
#include "dispatcher.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
#include "zmq_listener.hpp"
|
2009-08-09 16:12:09 +02:00
|
|
|
#include "zmq_connecter.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "msg_content.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
#include "io_thread.hpp"
|
2009-08-20 11:32:23 +02:00
|
|
|
#include "session.hpp"
|
2009-08-09 11:57:21 +02:00
|
|
|
#include "config.hpp"
|
2009-08-20 11:32:23 +02:00
|
|
|
#include "owned.hpp"
|
2009-08-21 14:29:22 +02:00
|
|
|
#include "uuid.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "pipe.hpp"
|
2009-09-04 16:02:41 +02:00
|
|
|
#include "err.hpp"
|
2009-09-11 17:58:37 +02:00
|
|
|
#include "platform.hpp"
|
|
|
|
#include "pgm_sender.hpp"
|
2009-09-16 10:11:01 +02:00
|
|
|
#include "pgm_receiver.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
zmq::socket_base_t::socket_base_t (app_thread_t *parent_, int type_) :
|
2009-08-08 16:01:58 +02:00
|
|
|
object_t (parent_),
|
2009-09-11 17:58:37 +02:00
|
|
|
type (type_),
|
2009-08-27 10:54:28 +02:00
|
|
|
current (0),
|
|
|
|
active (0),
|
2009-08-08 16:01:58 +02:00
|
|
|
pending_term_acks (0),
|
2009-08-27 10:54:28 +02:00
|
|
|
ticks (0),
|
2009-08-21 14:29:22 +02:00
|
|
|
app_thread (parent_),
|
2009-09-02 16:16:25 +02:00
|
|
|
shutting_down (false),
|
|
|
|
index (-1)
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::socket_base_t::~socket_base_t ()
|
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
shutting_down = true;
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Ask all pipes to terminate.
|
|
|
|
for (in_pipes_t::iterator it = in_pipes.begin ();
|
|
|
|
it != in_pipes.end (); it++)
|
|
|
|
(*it)->term ();
|
|
|
|
in_pipes.clear ();
|
|
|
|
for (out_pipes_t::iterator it = out_pipes.begin ();
|
|
|
|
it != out_pipes.end (); it++)
|
|
|
|
(*it)->term ();
|
|
|
|
out_pipes.clear ();
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// On third pass of the loop there should be no more I/O objects
|
|
|
|
// because all connecters and listerners were destroyed during
|
|
|
|
// the first pass and all engines delivered by delayed 'own' commands
|
|
|
|
// are destroyed during the second pass.
|
|
|
|
if (io_objects.empty () && !pending_term_acks)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Send termination request to all associated I/O objects.
|
2009-08-09 09:24:48 +02:00
|
|
|
for (io_objects_t::iterator it = io_objects.begin ();
|
|
|
|
it != io_objects.end (); it++)
|
|
|
|
send_term (*it);
|
2009-08-08 16:01:58 +02:00
|
|
|
|
|
|
|
// Move the objects to the list of pending term acks.
|
|
|
|
pending_term_acks += io_objects.size ();
|
|
|
|
io_objects.clear ();
|
|
|
|
|
|
|
|
// Process commands till we get all the termination acknowledgements.
|
|
|
|
while (pending_term_acks)
|
2009-08-27 10:54:28 +02:00
|
|
|
app_thread->process_commands (true, false);
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
2009-08-20 11:32:23 +02:00
|
|
|
|
|
|
|
// Check whether there are no session leaks.
|
2009-08-21 14:29:22 +02:00
|
|
|
sessions_sync.lock ();
|
2009-08-20 11:32:23 +02:00
|
|
|
zmq_assert (sessions.empty ());
|
2009-08-21 14:29:22 +02:00
|
|
|
sessions_sync.unlock ();
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq::socket_base_t::setsockopt (int option_, const void *optval_,
|
2009-08-09 09:24:48 +02:00
|
|
|
size_t optvallen_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-09 11:57:21 +02:00
|
|
|
switch (option_) {
|
|
|
|
|
|
|
|
case ZMQ_HWM:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
options.hwm = *((int64_t*) optval_);
|
2009-08-09 11:57:21 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_LWM:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
options.lwm = *((int64_t*) optval_);
|
2009-08-09 11:57:21 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_SWAP:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
options.swap = *((int64_t*) optval_);
|
2009-08-09 11:57:21 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_AFFINITY:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
options.affinity = (uint64_t) *((int64_t*) optval_);
|
2009-08-09 11:57:21 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
case ZMQ_IDENTITY:
|
2009-08-21 14:29:22 +02:00
|
|
|
options.identity.assign ((const char*) optval_, optvallen_);
|
2009-08-09 11:57:21 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-09-10 12:00:47 +02:00
|
|
|
case ZMQ_SUBSCRIBE:
|
|
|
|
case ZMQ_UNSUBSCRIBE:
|
2009-09-11 16:59:47 +02:00
|
|
|
errno = EFAULT;
|
2009-09-10 12:00:47 +02:00
|
|
|
return -1;
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
case ZMQ_RATE:
|
2009-09-14 11:25:57 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-14 11:25:57 +02:00
|
|
|
options.rate = (uint32_t) *((int64_t*) optval_);
|
2009-09-11 17:58:37 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
2009-09-14 11:25:57 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-14 11:25:57 +02:00
|
|
|
options.recovery_ivl = (uint32_t) *((int64_t*) optval_);
|
2009-09-11 17:58:37 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
case ZMQ_MCAST_LOOP:
|
2009-09-16 12:22:36 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-17 11:31:28 +02:00
|
|
|
if ((int64_t) *((int64_t*) optval_) == 0) {
|
|
|
|
|
|
|
|
options.use_multicast_loop = false;
|
|
|
|
|
|
|
|
} else if ((int64_t) *((int64_t*) optval_) == 1) {
|
|
|
|
|
|
|
|
options.use_multicast_loop = true;
|
|
|
|
|
2009-09-16 12:22:36 +02:00
|
|
|
} else {
|
2009-09-16 10:11:01 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2009-08-09 11:57:21 +02:00
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::socket_base_t::bind (const char *addr_)
|
|
|
|
{
|
2009-09-16 10:11:01 +02:00
|
|
|
// Parse addr_ string.
|
|
|
|
std::string addr_type;
|
|
|
|
std::string addr_args;
|
|
|
|
|
|
|
|
std::string addr (addr_);
|
|
|
|
std::string::size_type pos = addr.find ("://");
|
|
|
|
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
errno = EINVAL;
|
2009-08-09 11:21:47 +02:00
|
|
|
return -1;
|
2009-09-16 10:11:01 +02:00
|
|
|
}
|
2009-08-09 11:21:47 +02:00
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
addr_type = addr.substr (0, pos);
|
|
|
|
addr_args = addr.substr (pos + 3);
|
|
|
|
|
|
|
|
if (addr_type == "tcp") {
|
|
|
|
zmq_listener_t *listener = new zmq_listener_t (
|
|
|
|
choose_io_thread (options.affinity), this, options);
|
|
|
|
int rc = listener->set_address (addr_args.c_str ());
|
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
send_plug (listener);
|
|
|
|
send_own (this, listener);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined ZMQ_HAVE_OPENPGM
|
2009-09-16 15:36:38 +02:00
|
|
|
if (addr_type == "pgm" || addr_type == "udp") {
|
2009-09-16 10:11:01 +02:00
|
|
|
// In the case of PGM bind behaves the same like connect.
|
|
|
|
return connect (addr_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Unknown address type.
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq::socket_base_t::connect (const char *addr_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
// Generate a unique name for the session.
|
|
|
|
std::string session_name ("#");
|
|
|
|
session_name += uuid_t ().to_string ();
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Parse addr_ string.
|
|
|
|
std::string addr_type;
|
|
|
|
std::string addr_args;
|
|
|
|
|
|
|
|
std::string addr (addr_);
|
|
|
|
std::string::size_type pos = addr.find ("://");
|
|
|
|
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_type = addr.substr (0, pos);
|
|
|
|
addr_args = addr.substr (pos + 3);
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
// Create the session.
|
|
|
|
io_thread_t *io_thread = choose_io_thread (options.affinity);
|
2009-08-27 10:54:28 +02:00
|
|
|
session_t *session = new session_t (io_thread, this, session_name.c_str (),
|
2009-09-16 11:02:18 +02:00
|
|
|
options, true);
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq_assert (session);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// Create inbound pipe.
|
|
|
|
pipe_t *in_pipe = new pipe_t (this, session, options.hwm, options.lwm);
|
|
|
|
zmq_assert (in_pipe);
|
|
|
|
in_pipe->reader.set_endpoint (this);
|
2009-09-02 10:22:23 +02:00
|
|
|
session->attach_outpipe (&in_pipe->writer);
|
2009-08-28 16:51:46 +02:00
|
|
|
in_pipes.push_back (&in_pipe->reader);
|
|
|
|
in_pipes.back ()->set_index (active);
|
|
|
|
in_pipes [active]->set_index (in_pipes.size () - 1);
|
2009-08-27 10:54:28 +02:00
|
|
|
std::swap (in_pipes.back (), in_pipes [active]);
|
|
|
|
active++;
|
|
|
|
|
|
|
|
// Create outbound pipe.
|
|
|
|
pipe_t *out_pipe = new pipe_t (session, this, options.hwm, options.lwm);
|
|
|
|
zmq_assert (out_pipe);
|
2009-08-28 16:51:46 +02:00
|
|
|
out_pipe->writer.set_endpoint (this);
|
2009-09-02 10:22:23 +02:00
|
|
|
session->attach_inpipe (&out_pipe->reader);
|
2009-08-28 16:51:46 +02:00
|
|
|
out_pipes.push_back (&out_pipe->writer);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// Activate the session.
|
2009-08-21 14:29:22 +02:00
|
|
|
send_plug (session);
|
|
|
|
send_own (this, session);
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
if (addr_type == "tcp") {
|
|
|
|
|
|
|
|
// Create the connecter object. Supply it with the session name so that
|
|
|
|
// it can bind the new connection to the session once it is established.
|
|
|
|
zmq_connecter_t *connecter = new zmq_connecter_t (
|
|
|
|
choose_io_thread (options.affinity), this, options,
|
2009-09-16 11:02:18 +02:00
|
|
|
session_name.c_str (), false);
|
2009-09-11 17:58:37 +02:00
|
|
|
int rc = connecter->set_address (addr_args.c_str ());
|
|
|
|
if (rc != 0) {
|
|
|
|
delete connecter;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
send_plug (connecter);
|
|
|
|
send_own (this, connecter);
|
|
|
|
|
|
|
|
return 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
#if defined ZMQ_HAVE_OPENPGM
|
2009-09-16 15:36:38 +02:00
|
|
|
if (addr_type == "pgm" || addr_type == "udp") {
|
|
|
|
|
|
|
|
// For udp, pgm transport with udp encapsulation is used.
|
|
|
|
bool udp_encapsulation = false;
|
|
|
|
if (addr_type == "udp")
|
|
|
|
udp_encapsulation = true;
|
2009-09-15 09:43:42 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
switch (type) {
|
2009-09-16 10:11:01 +02:00
|
|
|
|
|
|
|
// PGM sender.
|
2009-09-11 17:58:37 +02:00
|
|
|
case ZMQ_PUB:
|
|
|
|
{
|
|
|
|
pgm_sender_t *pgm_sender =
|
|
|
|
new pgm_sender_t (choose_io_thread (options.affinity), options,
|
|
|
|
session_name.c_str ());
|
|
|
|
|
2009-09-16 15:36:38 +02:00
|
|
|
int rc = pgm_sender->init (udp_encapsulation, addr_args.c_str ());
|
2009-09-11 17:58:37 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
delete pgm_sender;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-15 09:43:42 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Reserve a sequence number for following 'attach' command.
|
|
|
|
session->inc_seqnum ();
|
|
|
|
send_attach (session, pgm_sender);
|
|
|
|
|
|
|
|
pgm_sender = NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2009-09-16 10:11:01 +02:00
|
|
|
|
|
|
|
// PGM receiver.
|
2009-09-11 17:58:37 +02:00
|
|
|
case ZMQ_SUB:
|
2009-09-16 10:11:01 +02:00
|
|
|
{
|
|
|
|
pgm_receiver_t *pgm_receiver =
|
|
|
|
new pgm_receiver_t (choose_io_thread (options.affinity), options,
|
|
|
|
session_name.c_str ());
|
|
|
|
|
2009-09-16 15:36:38 +02:00
|
|
|
int rc = pgm_receiver->init (udp_encapsulation, addr_args.c_str ());
|
2009-09-16 10:11:01 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
delete pgm_receiver;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve a sequence number for following 'attach' command.
|
|
|
|
session->inc_seqnum ();
|
|
|
|
send_attach (session, pgm_receiver);
|
|
|
|
|
|
|
|
pgm_receiver = NULL;
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
break;
|
2009-09-16 10:11:01 +02:00
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Unknown address type.
|
2009-09-14 12:28:13 +02:00
|
|
|
errno = EFAULT;
|
2009-09-11 17:58:37 +02:00
|
|
|
return -1;
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq::socket_base_t::send (::zmq_msg_t *msg_, int flags_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-27 10:54:28 +02:00
|
|
|
// Process pending commands, if any.
|
|
|
|
app_thread->process_commands (false, true);
|
|
|
|
|
|
|
|
// Try to send the message.
|
|
|
|
bool sent = distribute (msg_, !(flags_ & ZMQ_NOFLUSH));
|
|
|
|
|
|
|
|
if (!(flags_ & ZMQ_NOBLOCK)) {
|
|
|
|
|
|
|
|
// Oops, we couldn't send the message. Wait for the next
|
|
|
|
// command, process it and try to send the message again.
|
|
|
|
while (!sent) {
|
|
|
|
app_thread->process_commands (true, false);
|
|
|
|
sent = distribute (msg_, !(flags_ & ZMQ_NOFLUSH));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!sent) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::socket_base_t::flush ()
|
|
|
|
{
|
2009-08-27 10:54:28 +02:00
|
|
|
for (out_pipes_t::iterator it = out_pipes.begin (); it != out_pipes.end ();
|
|
|
|
it++)
|
2009-08-28 16:51:46 +02:00
|
|
|
(*it)->flush ();
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
return 0;
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq::socket_base_t::recv (::zmq_msg_t *msg_, int flags_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-27 10:54:28 +02:00
|
|
|
// If the message cannot be fetched immediately, there are two scenarios.
|
|
|
|
// For non-blocking recv, commands are processed in case there's a message
|
|
|
|
// already waiting we don't know about. If it's not, return EAGAIN.
|
|
|
|
// In blocking scenario, commands are processed over and over again until
|
|
|
|
// we are able to fetch a message.
|
|
|
|
bool fetched = fetch (msg_);
|
|
|
|
if (!fetched) {
|
|
|
|
if (flags_ & ZMQ_NOBLOCK) {
|
|
|
|
app_thread->process_commands (false, false);
|
|
|
|
fetched = fetch (msg_);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (!fetched) {
|
|
|
|
app_thread->process_commands (true, false);
|
|
|
|
ticks = 0;
|
|
|
|
fetched = fetch (msg_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once every inbound_poll_rate messages check for signals and process
|
|
|
|
// incoming commands. This happens only if we are not polling altogether
|
|
|
|
// because there are messages available all the time. If poll occurs,
|
|
|
|
// ticks is set to zero and thus we avoid this code.
|
|
|
|
//
|
|
|
|
// Note that 'recv' uses different command throttling algorithm (the one
|
|
|
|
// described above) from the one used by 'send'. This is because counting
|
|
|
|
// ticks is more efficient than doing rdtsc all the time.
|
|
|
|
if (++ticks == inbound_poll_rate) {
|
|
|
|
app_thread->process_commands (false, false);
|
|
|
|
ticks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fetched) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::socket_base_t::close ()
|
|
|
|
{
|
|
|
|
app_thread->remove_socket (this);
|
2009-09-04 16:02:41 +02:00
|
|
|
|
|
|
|
// Pointer to the dispatcher must be retrieved before the socket is
|
|
|
|
// deallocated. Afterwards it is not available.
|
|
|
|
dispatcher_t *dispatcher = get_dispatcher ();
|
2009-08-08 16:01:58 +02:00
|
|
|
delete this;
|
2009-09-04 16:02:41 +02:00
|
|
|
|
|
|
|
// This function must be called after the socket is completely deallocated
|
|
|
|
// as it may cause termination of the whole 0MQ infrastructure.
|
|
|
|
dispatcher->destroy_socket ();
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
bool zmq::socket_base_t::register_session (const char *name_,
|
2009-08-20 11:32:23 +02:00
|
|
|
session_t *session_)
|
|
|
|
{
|
|
|
|
sessions_sync.lock ();
|
2009-08-21 14:29:22 +02:00
|
|
|
bool registered = sessions.insert (std::make_pair (name_, session_)).second;
|
2009-08-20 11:32:23 +02:00
|
|
|
sessions_sync.unlock ();
|
2009-08-21 14:29:22 +02:00
|
|
|
return registered;
|
2009-08-20 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
bool zmq::socket_base_t::unregister_session (const char *name_)
|
2009-08-20 11:32:23 +02:00
|
|
|
{
|
|
|
|
sessions_sync.lock ();
|
|
|
|
sessions_t::iterator it = sessions.find (name_);
|
2009-08-21 14:29:22 +02:00
|
|
|
bool unregistered = (it != sessions.end ());
|
2009-08-20 11:32:23 +02:00
|
|
|
sessions.erase (it);
|
|
|
|
sessions_sync.unlock ();
|
2009-08-21 14:29:22 +02:00
|
|
|
return unregistered;
|
2009-08-20 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::session_t *zmq::socket_base_t::find_session (const char *name_)
|
2009-08-20 11:32:23 +02:00
|
|
|
{
|
|
|
|
sessions_sync.lock ();
|
2009-08-21 14:29:22 +02:00
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
sessions_t::iterator it = sessions.find (name_);
|
2009-08-21 14:29:22 +02:00
|
|
|
if (it == sessions.end ()) {
|
|
|
|
sessions_sync.unlock ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the session for subsequent attach command.
|
|
|
|
it->second->inc_seqnum ();
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
sessions_sync.unlock ();
|
2009-08-21 14:29:22 +02:00
|
|
|
return it->second;
|
2009-08-20 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 10:22:23 +02:00
|
|
|
void zmq::socket_base_t::attach_inpipe (class reader_t *pipe_)
|
|
|
|
{
|
|
|
|
pipe_->set_endpoint (this);
|
|
|
|
in_pipes.push_back (pipe_);
|
|
|
|
in_pipes.back ()->set_index (active);
|
|
|
|
in_pipes [active]->set_index (in_pipes.size () - 1);
|
|
|
|
std::swap (in_pipes.back (), in_pipes [active]);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::socket_base_t::attach_outpipe (class writer_t *pipe_)
|
|
|
|
{
|
|
|
|
pipe_->set_endpoint (this);
|
|
|
|
out_pipes.push_back (pipe_);
|
2009-09-11 15:51:35 +02:00
|
|
|
pipe_->set_index (out_pipes.size () - 1);
|
2009-09-02 10:22:23 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::socket_base_t::revive (reader_t *pipe_)
|
|
|
|
{
|
|
|
|
// Move the pipe to the list of active pipes.
|
|
|
|
in_pipes_t::size_type index = (in_pipes_t::size_type) pipe_->get_index ();
|
2009-08-28 16:51:46 +02:00
|
|
|
in_pipes [index]->set_index (active);
|
|
|
|
in_pipes [active]->set_index (index);
|
2009-08-27 10:54:28 +02:00
|
|
|
std::swap (in_pipes [index], in_pipes [active]);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::socket_base_t::detach_inpipe (class reader_t *pipe_)
|
|
|
|
{
|
|
|
|
// Remove the pipe from the list of inbound pipes.
|
|
|
|
in_pipes_t::size_type index = (in_pipes_t::size_type) pipe_->get_index ();
|
|
|
|
if (index < active) {
|
|
|
|
in_pipes [index]->set_index (active - 1);
|
|
|
|
in_pipes [active - 1]->set_index (index);
|
|
|
|
std::swap (in_pipes [index], in_pipes [active - 1]);
|
|
|
|
active--;
|
|
|
|
index = active;
|
|
|
|
}
|
|
|
|
in_pipes [index]->set_index (in_pipes.size () - 1);
|
|
|
|
in_pipes [in_pipes.size () - 1]->set_index (index);
|
|
|
|
std::swap (in_pipes [index], in_pipes [in_pipes.size () - 1]);
|
|
|
|
in_pipes.pop_back ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::socket_base_t::detach_outpipe (class writer_t *pipe_)
|
|
|
|
{
|
|
|
|
out_pipes_t::size_type index = (out_pipes_t::size_type) pipe_->get_index ();
|
|
|
|
out_pipes [index]->set_index (out_pipes.size () - 1);
|
|
|
|
out_pipes [out_pipes.size () - 1]->set_index (index);
|
|
|
|
std::swap (out_pipes [index], out_pipes [out_pipes.size () - 1]);
|
|
|
|
out_pipes.pop_back ();
|
|
|
|
}
|
|
|
|
|
2009-09-02 16:16:25 +02:00
|
|
|
void zmq::socket_base_t::set_index (int index_)
|
|
|
|
{
|
|
|
|
index = index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::socket_base_t::get_index ()
|
|
|
|
{
|
|
|
|
zmq_assert (index != -1);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::socket_base_t::process_own (owned_t *object_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-09 09:24:48 +02:00
|
|
|
io_objects.insert (object_);
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::socket_base_t::process_bind (owned_t *session_,
|
|
|
|
reader_t *in_pipe_, writer_t *out_pipe_)
|
|
|
|
{
|
|
|
|
zmq_assert (in_pipe_);
|
2009-09-02 10:22:23 +02:00
|
|
|
attach_inpipe (in_pipe_);
|
2009-08-27 10:54:28 +02:00
|
|
|
zmq_assert (out_pipe_);
|
2009-09-02 10:22:23 +02:00
|
|
|
attach_outpipe (out_pipe_);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::socket_base_t::process_term_req (owned_t *object_)
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
// When shutting down we can ignore termination requests from owned
|
|
|
|
// objects. They are going to be terminated anyway.
|
|
|
|
if (shutting_down)
|
|
|
|
return;
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
// If I/O object is well and alive ask it to terminate.
|
|
|
|
io_objects_t::iterator it = std::find (io_objects.begin (),
|
|
|
|
io_objects.end (), object_);
|
|
|
|
|
|
|
|
// If not found, we assume that termination request was already sent to
|
|
|
|
// the object so we can sagely ignore the request.
|
2009-08-09 09:24:48 +02:00
|
|
|
if (it == io_objects.end ())
|
|
|
|
return;
|
|
|
|
|
|
|
|
pending_term_acks++;
|
|
|
|
io_objects.erase (it);
|
|
|
|
send_term (object_);
|
2009-08-08 16:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::socket_base_t::process_term_ack ()
|
|
|
|
{
|
|
|
|
zmq_assert (pending_term_acks);
|
|
|
|
pending_term_acks--;
|
|
|
|
}
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
bool zmq::socket_base_t::distribute (zmq_msg_t *msg_, bool flush_)
|
|
|
|
{
|
|
|
|
int pipes_count = out_pipes.size ();
|
|
|
|
|
|
|
|
// If there are no pipes available, simply drop the message.
|
|
|
|
if (pipes_count == 0) {
|
|
|
|
int rc = zmq_msg_close (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
rc = zmq_msg_init (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check whether all pipes are available for writing.
|
|
|
|
for (out_pipes_t::iterator it = out_pipes.begin (); it != out_pipes.end ();
|
|
|
|
it++)
|
2009-08-28 16:51:46 +02:00
|
|
|
if (!(*it)->check_write (zmq_msg_size (msg_)))
|
2009-08-27 10:54:28 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
msg_content_t *content = (msg_content_t*) msg_->content;
|
|
|
|
|
|
|
|
// For VSMs the copying is straighforward.
|
|
|
|
if (content == (msg_content_t*) ZMQ_VSM) {
|
|
|
|
for (out_pipes_t::iterator it = out_pipes.begin ();
|
|
|
|
it != out_pipes.end (); it++) {
|
2009-08-28 16:51:46 +02:00
|
|
|
(*it)->write (msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
if (flush_)
|
2009-08-28 16:51:46 +02:00
|
|
|
(*it)->flush ();
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
int rc = zmq_msg_init (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimisation for the case when there's only a single pipe
|
|
|
|
// to send the message to - no refcount adjustment i.e. no atomic
|
|
|
|
// operations are needed.
|
|
|
|
if (pipes_count == 1) {
|
2009-08-28 16:51:46 +02:00
|
|
|
(*out_pipes.begin ())->write (msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
if (flush_)
|
2009-08-28 16:51:46 +02:00
|
|
|
(*out_pipes.begin ())->flush ();
|
2009-08-27 10:54:28 +02:00
|
|
|
int rc = zmq_msg_init (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are at least 2 destinations for the message. That means we have
|
|
|
|
// to deal with reference counting. First add N-1 references to
|
|
|
|
// the content (we are holding one reference anyway, that's why -1).
|
|
|
|
if (msg_->shared)
|
|
|
|
content->refcnt.add (pipes_count - 1);
|
|
|
|
else {
|
|
|
|
content->refcnt.set (pipes_count);
|
|
|
|
msg_->shared = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the message to all destinations.
|
|
|
|
for (out_pipes_t::iterator it = out_pipes.begin (); it != out_pipes.end ();
|
|
|
|
it++) {
|
2009-08-28 16:51:46 +02:00
|
|
|
(*it)->write (msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
if (flush_)
|
2009-08-28 16:51:46 +02:00
|
|
|
(*it)->flush ();
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detach the original message from the data buffer.
|
|
|
|
int rc = zmq_msg_init (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::socket_base_t::fetch (zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
// Deallocate old content of the message.
|
|
|
|
zmq_msg_close (msg_);
|
|
|
|
|
|
|
|
// Round-robin over the pipes to get next message.
|
|
|
|
for (int count = active; count != 0; count--) {
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
bool fetched = in_pipes [current]->read (msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// If there's no message in the pipe, move it to the list of
|
|
|
|
// non-active pipes.
|
|
|
|
if (!fetched) {
|
2009-08-28 16:51:46 +02:00
|
|
|
in_pipes [current]->set_index (active - 1);
|
|
|
|
in_pipes [active - 1]->set_index (current);
|
2009-08-27 10:54:28 +02:00
|
|
|
std::swap (in_pipes [current], in_pipes [active - 1]);
|
|
|
|
active--;
|
|
|
|
}
|
|
|
|
|
|
|
|
current ++;
|
|
|
|
if (current >= active)
|
|
|
|
current = 0;
|
|
|
|
|
|
|
|
if (fetched)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No message is available. Initialise the output parameter
|
|
|
|
// to be a 0-byte message.
|
|
|
|
zmq_msg_init (msg_);
|
|
|
|
return false;
|
|
|
|
}
|