2009-08-08 16:01:58 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2009-08-08 16:01:58 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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 GNU Lesser General Public
|
|
|
|
License for more details.
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-08-08 16:01:58 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ZMQ_SOCKET_BASE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_SOCKET_BASE_HPP_INCLUDED__
|
|
|
|
|
2011-07-15 11:24:33 +02:00
|
|
|
#include <string>
|
2012-04-20 18:59:08 +04:00
|
|
|
#include <map>
|
2012-08-24 16:42:31 -07:00
|
|
|
#include <stdarg.h>
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
#include "own.hpp"
|
2010-08-31 21:03:34 +02:00
|
|
|
#include "array.hpp"
|
2014-01-12 21:58:36 +01:00
|
|
|
#include "blob.hpp"
|
2009-08-09 11:57:21 +02:00
|
|
|
#include "stdint.hpp"
|
2011-02-09 22:23:21 +01:00
|
|
|
#include "poller.hpp"
|
2009-11-21 20:59:55 +01:00
|
|
|
#include "atomic_counter.hpp"
|
2011-02-09 22:23:21 +01:00
|
|
|
#include "i_poll_events.hpp"
|
2015-02-11 00:01:50 +02:00
|
|
|
#include "i_mailbox.hpp"
|
2009-12-23 19:37:56 +01:00
|
|
|
#include "stdint.hpp"
|
2012-04-17 10:10:22 +01:00
|
|
|
#include "clock.hpp"
|
2011-05-23 20:30:01 +02:00
|
|
|
#include "pipe.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2012-11-21 18:27:53 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
void zmq_free_event (void *data, void *hint);
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
namespace zmq
|
|
|
|
{
|
|
|
|
|
2011-11-09 15:22:20 +01:00
|
|
|
class ctx_t;
|
|
|
|
class msg_t;
|
|
|
|
class pipe_t;
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
class socket_base_t :
|
2010-08-11 14:09:56 +02:00
|
|
|
public own_t,
|
2011-05-23 21:36:00 +02:00
|
|
|
public array_item_t <>,
|
2011-05-23 20:30:01 +02:00
|
|
|
public i_poll_events,
|
|
|
|
public i_pipe_events
|
2009-08-08 16:01:58 +02:00
|
|
|
{
|
2011-02-09 15:32:15 +01:00
|
|
|
friend class reaper_t;
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
public:
|
|
|
|
|
2011-04-09 09:35:34 +02:00
|
|
|
// Returns false if object is not a socket.
|
|
|
|
bool check_tag ();
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Create a socket of a specified type.
|
2011-11-09 15:22:20 +01:00
|
|
|
static socket_base_t *create (int type_, zmq::ctx_t *parent_,
|
2012-03-19 19:41:20 -05:00
|
|
|
uint32_t tid_, int sid_);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2010-11-05 17:39:51 +01:00
|
|
|
// Returns the mailbox associated with this socket.
|
2015-02-11 00:01:50 +02:00
|
|
|
i_mailbox *get_mailbox ();
|
2010-08-06 17:49:37 +02:00
|
|
|
|
|
|
|
// Interrupt blocking call if the socket is stuck in one.
|
|
|
|
// This function can be called from a different thread!
|
|
|
|
void stop ();
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2009-08-09 16:30:22 +02:00
|
|
|
// Interface for communication with the API layer.
|
2010-04-09 13:04:15 +02:00
|
|
|
int setsockopt (int option_, const void *optval_, size_t optvallen_);
|
|
|
|
int getsockopt (int option_, void *optval_, size_t *optvallen_);
|
2009-09-21 14:39:59 +02:00
|
|
|
int bind (const char *addr_);
|
|
|
|
int connect (const char *addr_);
|
2012-04-20 18:59:08 +04:00
|
|
|
int term_endpoint (const char *addr_);
|
2011-11-09 15:22:20 +01:00
|
|
|
int send (zmq::msg_t *msg_, int flags_);
|
|
|
|
int recv (zmq::msg_t *msg_, int flags_);
|
2015-08-16 11:41:44 +03:00
|
|
|
int add_signaler (signaler_t *s);
|
|
|
|
int remove_signaler (signaler_t *s);
|
2009-09-21 14:39:59 +02:00
|
|
|
int close ();
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2009-10-01 10:56:17 +02:00
|
|
|
// These functions are used by the polling mechanism to determine
|
|
|
|
// which events are to be reported from this socket.
|
|
|
|
bool has_in ();
|
|
|
|
bool has_out ();
|
|
|
|
|
2016-01-27 18:19:14 +02:00
|
|
|
// Joining and leaving groups
|
|
|
|
int join (const char *group);
|
|
|
|
int leave (const char *group);
|
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// Using this function reaper thread ask the socket to register with
|
2011-02-09 22:23:21 +01:00
|
|
|
// its poller.
|
|
|
|
void start_reaping (poller_t *poller_);
|
|
|
|
|
|
|
|
// i_poll_events implementation. This interface is used when socket
|
|
|
|
// is handled by the poller in the reaper thread.
|
|
|
|
void in_event ();
|
|
|
|
void out_event ();
|
|
|
|
void timer_event (int id_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
// i_pipe_events interface implementation.
|
|
|
|
void read_activated (pipe_t *pipe_);
|
|
|
|
void write_activated (pipe_t *pipe_);
|
2011-05-30 10:07:34 +02:00
|
|
|
void hiccuped (pipe_t *pipe_);
|
2013-05-28 16:49:24 +02:00
|
|
|
void pipe_terminated (pipe_t *pipe_);
|
2012-02-04 01:41:09 +11:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
2012-05-04 02:32:46 +01:00
|
|
|
|
2012-11-17 11:29:47 +00:00
|
|
|
int monitor (const char *endpoint_, int events_);
|
|
|
|
|
2014-01-06 10:31:42 +01:00
|
|
|
void set_fd(fd_t fd_);
|
|
|
|
fd_t fd();
|
|
|
|
|
2014-05-20 20:14:28 +02:00
|
|
|
void event_connected (const std::string &addr_, int fd_);
|
|
|
|
void event_connect_delayed (const std::string &addr_, int err_);
|
|
|
|
void event_connect_retried (const std::string &addr_, int interval_);
|
|
|
|
void event_listening (const std::string &addr_, int fd_);
|
|
|
|
void event_bind_failed (const std::string &addr_, int err_);
|
|
|
|
void event_accepted (const std::string &addr_, int fd_);
|
|
|
|
void event_accept_failed (const std::string &addr_, int err_);
|
|
|
|
void event_closed (const std::string &addr_, int fd_);
|
|
|
|
void event_close_failed (const std::string &addr_, int fd_);
|
|
|
|
void event_disconnected (const std::string &addr_, int fd_);
|
2012-05-04 02:32:46 +01:00
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
protected:
|
|
|
|
|
2015-02-11 00:01:50 +02:00
|
|
|
socket_base_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_, bool thread_safe_ = false);
|
2009-09-21 14:39:59 +02:00
|
|
|
virtual ~socket_base_t ();
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Concrete algorithms for the x- methods are to be defined by
|
|
|
|
// individual socket types.
|
2012-02-02 13:07:48 +01:00
|
|
|
virtual void xattach_pipe (zmq::pipe_t *pipe_,
|
2013-08-31 09:53:47 -04:00
|
|
|
bool subscribe_to_all_ = false) = 0;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// The default implementation assumes there are no specific socket
|
2014-01-08 18:25:31 +01:00
|
|
|
// options for the particular socket type. If not so, override this
|
2015-01-09 17:28:50 -06:00
|
|
|
// method.
|
2009-09-21 14:39:59 +02:00
|
|
|
virtual int xsetsockopt (int option_, const void *optval_,
|
2010-08-06 17:49:37 +02:00
|
|
|
size_t optvallen_);
|
|
|
|
|
|
|
|
// The default implementation assumes that send is not supported.
|
|
|
|
virtual bool xhas_out ();
|
2012-11-09 17:08:03 +01:00
|
|
|
virtual int xsend (zmq::msg_t *msg_);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
|
|
|
// The default implementation assumes that recv in not supported.
|
|
|
|
virtual bool xhas_in ();
|
2012-11-09 17:17:43 +01:00
|
|
|
virtual int xrecv (zmq::msg_t *msg_);
|
2009-09-02 16:16:25 +02:00
|
|
|
|
2014-01-12 21:58:36 +01:00
|
|
|
// Returns the credential for the peer from which we have received
|
|
|
|
// the last message. If no message has been received yet,
|
|
|
|
// the function returns empty credential.
|
|
|
|
virtual blob_t get_credential () const;
|
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
// i_pipe_events will be forwarded to these functions.
|
|
|
|
virtual void xread_activated (pipe_t *pipe_);
|
|
|
|
virtual void xwrite_activated (pipe_t *pipe_);
|
2011-05-30 10:07:34 +02:00
|
|
|
virtual void xhiccuped (pipe_t *pipe_);
|
2013-05-28 16:49:24 +02:00
|
|
|
virtual void xpipe_terminated (pipe_t *pipe_) = 0;
|
2010-08-11 14:09:56 +02:00
|
|
|
|
2016-01-27 18:19:14 +02:00
|
|
|
// the default implementation assumes that joub and leave are not supported.
|
|
|
|
virtual int xjoin (const char *group_);
|
|
|
|
virtual int xleave (const char *group_);
|
|
|
|
|
2010-08-12 15:03:51 +02:00
|
|
|
// Delay actual destruction of the socket.
|
|
|
|
void process_destroy ();
|
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// Socket event data dispatch
|
2014-04-28 11:30:04 +02:00
|
|
|
void monitor_event (int event_, int value_, const std::string& addr_);
|
2012-11-17 11:29:47 +00:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Monitor socket cleanup
|
2015-10-09 08:47:11 -04:00
|
|
|
void stop_monitor (bool send_monitor_stopped_event_ = true);
|
2016-01-27 18:19:14 +02:00
|
|
|
|
2014-01-18 17:49:54 -08:00
|
|
|
// Next assigned name on a zmq_connect() call used by ROUTER and STREAM socket types
|
2014-01-19 09:27:57 +01:00
|
|
|
std::string connect_rid;
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
private:
|
2012-04-20 18:59:08 +04:00
|
|
|
// Creates new endpoint ID and adds the endpoint to the map.
|
2013-08-18 07:50:53 +01:00
|
|
|
void add_endpoint (const char *addr_, own_t *endpoint_, pipe_t *pipe);
|
2012-04-20 18:59:08 +04:00
|
|
|
|
|
|
|
// Map of open endpoints.
|
2013-08-18 07:50:53 +01:00
|
|
|
typedef std::pair <own_t *, pipe_t*> endpoint_pipe_t;
|
|
|
|
typedef std::multimap <std::string, endpoint_pipe_t> endpoints_t;
|
2012-04-20 18:59:08 +04:00
|
|
|
endpoints_t endpoints;
|
2010-08-11 14:09:56 +02:00
|
|
|
|
2012-12-04 15:14:21 +01:00
|
|
|
// Map of open inproc endpoints.
|
|
|
|
typedef std::multimap <std::string, pipe_t *> inprocs_t;
|
|
|
|
inprocs_t inprocs;
|
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
// To be called after processing commands or invoking any command
|
|
|
|
// handlers explicitly. If required, it will deallocate the socket.
|
|
|
|
void check_destroy ();
|
|
|
|
|
2011-07-17 23:31:29 +02:00
|
|
|
// Moves the flags from the message to local variables,
|
|
|
|
// to be later retrieved by getsockopt.
|
|
|
|
void extract_flags (msg_t *msg_);
|
|
|
|
|
2011-04-09 09:35:34 +02:00
|
|
|
// Used to check whether the object is a socket.
|
|
|
|
uint32_t tag;
|
|
|
|
|
2010-09-21 09:00:46 +02:00
|
|
|
// If true, associated context was already terminated.
|
|
|
|
bool ctx_terminated;
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2010-08-12 15:03:51 +02:00
|
|
|
// If true, object should have been already destroyed. However,
|
|
|
|
// destruction is delayed while we unwind the stack to the point
|
|
|
|
// where it doesn't intersect the object being destroyed.
|
|
|
|
bool destroyed;
|
|
|
|
|
2010-12-13 15:40:26 +01:00
|
|
|
// Parse URI string.
|
|
|
|
int parse_uri (const char *uri_, std::string &protocol_,
|
|
|
|
std::string &address_);
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// Check whether transport protocol, as specified in connect or
|
|
|
|
// bind, is available and compatible with the socket type.
|
|
|
|
int check_protocol (const std::string &protocol_);
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
// Register the pipe with this socket.
|
2013-08-31 09:53:47 -04:00
|
|
|
void attach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_ = false);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2011-06-17 12:22:02 +02:00
|
|
|
// Processes commands sent to this socket (if any). If timeout is -1,
|
|
|
|
// returns only after at least one command was processed.
|
2010-08-06 17:49:37 +02:00
|
|
|
// If throttle argument is true, commands are processed at most once
|
|
|
|
// in a predefined time period.
|
2011-06-17 12:22:02 +02:00
|
|
|
int process_commands (int timeout_, bool throttle_);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
// Handlers for incoming commands.
|
2010-08-06 17:49:37 +02:00
|
|
|
void process_stop ();
|
2011-11-09 15:22:20 +01:00
|
|
|
void process_bind (zmq::pipe_t *pipe_);
|
2011-05-23 20:30:01 +02:00
|
|
|
void process_term (int linger_);
|
2009-08-08 16:01:58 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
void update_pipe_options(int option_);
|
|
|
|
|
2010-11-05 17:39:51 +01:00
|
|
|
// Socket's mailbox object.
|
2016-02-09 10:51:51 +01:00
|
|
|
i_mailbox *mailbox;
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
// List of attached pipes.
|
|
|
|
typedef array_t <pipe_t, 3> pipes_t;
|
|
|
|
pipes_t pipes;
|
|
|
|
|
2011-02-09 22:23:21 +01:00
|
|
|
// Reaper's poller and handle of this socket within it.
|
|
|
|
poller_t *poller;
|
|
|
|
poller_t::handle_t handle;
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Timestamp of when commands were processed the last time.
|
2010-09-26 16:55:54 +02:00
|
|
|
uint64_t last_tsc;
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// Number of messages received since last command processing.
|
|
|
|
int ticks;
|
|
|
|
|
2011-06-20 11:33:54 +02:00
|
|
|
// True if the last message received had MORE flag set.
|
2010-04-11 10:26:47 +02:00
|
|
|
bool rcvmore;
|
|
|
|
|
2014-01-06 10:31:42 +01:00
|
|
|
// File descriptor if applicable
|
|
|
|
fd_t file_desc;
|
2014-01-07 01:09:51 +01:00
|
|
|
|
2012-04-17 10:10:22 +01:00
|
|
|
// Improves efficiency of time measurement.
|
|
|
|
clock_t clock;
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Monitor socket;
|
|
|
|
void *monitor_socket;
|
|
|
|
|
|
|
|
// Bitmask of events being monitored
|
|
|
|
int monitor_events;
|
|
|
|
|
2013-06-29 17:24:30 +02:00
|
|
|
// Last socket endpoint resolved URI
|
|
|
|
std::string last_endpoint;
|
|
|
|
|
2015-02-11 00:01:50 +02:00
|
|
|
// Indicate if the socket is thread safe
|
|
|
|
bool thread_safe;
|
|
|
|
|
|
|
|
// Signaler to be used in the reaping stage
|
2015-02-12 20:38:49 +02:00
|
|
|
signaler_t* reaper_signaler;
|
2015-02-11 00:01:50 +02:00
|
|
|
|
|
|
|
// Mutex for synchronize access to the socket in thread safe mode
|
2012-02-04 01:41:09 +11:00
|
|
|
mutex_t sync;
|
2015-02-11 00:01:50 +02:00
|
|
|
|
|
|
|
socket_base_t (const socket_base_t&);
|
2016-01-27 18:19:14 +02:00
|
|
|
const socket_base_t &operator = (const socket_base_t&);
|
2009-08-08 16:01:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|