├── doc ├── zmq_epgm.txt ├── zmq_msg_data.txt ├── zmq_msg_size.txt ├── zmq_version.txt ├── zmq_init.txt ├── zmq_strerror.txt ├── zmq_errno.txt ├── zmq_msg_move.txt ├── zmq_close.txt ├── Makefile.am ├── zmq_msg_close.txt ├── asciidoc.conf ├── zmq_msg_init.txt ├── zmq_msg_copy.txt ├── zmq_msg_init_size.txt ├── zmq_term.txt ├── zmq_msg_init_data.txt ├── zmq_ipc.txt ├── zmq_inproc.txt ├── zmq_bind.txt └── zmq_connect.txt ├── .hgeol ├── branding.bmp ├── installer.ico ├── foreign └── openpgm │ ├── libpgm-5.1.118~dfsg.tar.gz │ └── Makefile.am ├── .travis.yml ├── src ├── libzmq.pc.in ├── pub.cpp ├── device.hpp ├── likely.hpp ├── blob.hpp ├── pub.hpp ├── command.cpp ├── fd.hpp ├── transient_session.cpp ├── sub.hpp ├── i_poll_events.hpp ├── i_inout.hpp ├── stdint.hpp ├── msg_content.hpp ├── pull.cpp ├── push.cpp ├── trie.hpp ├── transient_session.hpp ├── pull.hpp ├── push.hpp ├── rep.hpp ├── i_engine.hpp ├── clock.hpp ├── req.hpp ├── named_session.hpp ├── xpub.hpp ├── xreq.hpp ├── signaler.hpp ├── xreq.cpp ├── xpub.cpp ├── connect_session.hpp ├── zmq_listener.hpp ├── sub.cpp ├── poller.hpp ├── tcp_listener.hpp ├── reaper.hpp ├── mailbox.hpp ├── pair.hpp ├── ip.hpp ├── tcp_socket.hpp ├── xsub.hpp ├── lb.hpp ├── thread.hpp ├── tcp_connecter.hpp ├── zmq_listener.cpp ├── io_object.hpp ├── fq.hpp ├── zmq_engine.hpp ├── mailbox.cpp ├── epoll.hpp ├── poller_base.hpp ├── io_thread.hpp ├── thread.cpp ├── io_object.cpp ├── named_session.cpp ├── zmq_init.hpp ├── options.hpp ├── io_thread.cpp ├── devpoll.hpp ├── encoder.cpp ├── dist.hpp ├── mutex.hpp ├── zmq_connecter.hpp ├── kqueue.hpp ├── pgm_sender.hpp ├── poll.hpp └── poller_base.cpp ├── perf ├── Makefile.am ├── remote_thr.cpp └── local_lat.cpp ├── version.sh ├── tests ├── Makefile.am ├── test_pair_inproc.cpp ├── test_pair_ipc.cpp ├── test_pair_tcp.cpp ├── test_reqrep_inproc.cpp ├── test_reqrep_ipc.cpp ├── test_reqrep_tcp.cpp ├── test_hwm.cpp └── test_shutdown_stress.cpp ├── .gitignore ├── README ├── Makefile.am ├── MAINTAINERS ├── autogen.sh └── include └── zmq_utils.h /doc/zmq_epgm.txt: -------------------------------------------------------------------------------- 1 | zmq_pgm.txt -------------------------------------------------------------------------------- /.hgeol: -------------------------------------------------------------------------------- 1 | [patterns] 2 | ** = native 3 | -------------------------------------------------------------------------------- /branding.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeromq/zeromq2-x/HEAD/branding.bmp -------------------------------------------------------------------------------- /installer.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeromq/zeromq2-x/HEAD/installer.ico -------------------------------------------------------------------------------- /foreign/openpgm/libpgm-5.1.118~dfsg.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeromq/zeromq2-x/HEAD/foreign/openpgm/libpgm-5.1.118~dfsg.tar.gz -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | script: ./autogen.sh && ./configure && make && make check 2 | language: c 3 | before_install: 4 | - sudo apt-get install uuid-dev -------------------------------------------------------------------------------- /foreign/openpgm/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = $(pgm_subdir) 2 | DIST_SUBDIRS = 3 | # Override Automake's installation targets with the command ":" that does nothing. 4 | install:; @: 5 | install-exec:; @: 6 | install-data:; @: 7 | uninstall:; @: 8 | dist:; @; 9 | -------------------------------------------------------------------------------- /src/libzmq.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libzmq 7 | Description: 0MQ c++ library 8 | Version: @VERSION@ 9 | Libs: -L${libdir} -lzmq 10 | Cflags: -I${includedir} 11 | -------------------------------------------------------------------------------- /perf/Makefile.am: -------------------------------------------------------------------------------- 1 | INCLUDES = -I$(top_builddir)/include 2 | 3 | noinst_PROGRAMS = local_lat remote_lat local_thr remote_thr inproc_lat inproc_thr 4 | 5 | local_lat_LDADD = $(top_builddir)/src/libzmq.la 6 | local_lat_SOURCES = local_lat.cpp 7 | 8 | remote_lat_LDADD = $(top_builddir)/src/libzmq.la 9 | remote_lat_SOURCES = remote_lat.cpp 10 | 11 | local_thr_LDADD = $(top_builddir)/src/libzmq.la 12 | local_thr_SOURCES = local_thr.cpp 13 | 14 | remote_thr_LDADD = $(top_builddir)/src/libzmq.la 15 | remote_thr_SOURCES = remote_thr.cpp 16 | 17 | inproc_lat_LDADD = $(top_builddir)/src/libzmq.la 18 | inproc_lat_SOURCES = inproc_lat.cpp 19 | 20 | inproc_thr_LDADD = $(top_builddir)/src/libzmq.la 21 | inproc_thr_SOURCES = inproc_thr.cpp 22 | -------------------------------------------------------------------------------- /version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script extracts the 0MQ version from include/zmq.h, which is the master 4 | # location for this information. 5 | # 6 | if [ ! -f include/zmq.h ]; then 7 | echo "version.sh: error: include/zmq.h does not exist" 1>&2 8 | exit 1 9 | fi 10 | MAJOR=`egrep '^#define +ZMQ_VERSION_MAJOR +[0-9]+$' include/zmq.h` 11 | MINOR=`egrep '^#define +ZMQ_VERSION_MINOR +[0-9]+$' include/zmq.h` 12 | PATCH=`egrep '^#define +ZMQ_VERSION_PATCH +[0-9]+$' include/zmq.h` 13 | if [ -z "$MAJOR" -o -z "$MINOR" -o -z "$PATCH" ]; then 14 | echo "version.sh: error: could not extract version from include/zmq.h" 1>&2 15 | exit 1 16 | fi 17 | MAJOR=`echo $MAJOR | awk '{ print $3 }'` 18 | MINOR=`echo $MINOR | awk '{ print $3 }'` 19 | PATCH=`echo $PATCH | awk '{ print $3 }'` 20 | echo $MAJOR.$MINOR.$PATCH | tr -d '\n' 21 | 22 | -------------------------------------------------------------------------------- /doc/zmq_msg_data.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_data(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_data - retrieve pointer to message content 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *void *zmq_msg_data (zmq_msg_t '*msg');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_data()_ function shall return a pointer to the message content of 18 | the message object referenced by 'msg'. 19 | 20 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 21 | _zmq_msg_ family of functions. 22 | 23 | 24 | RETURN VALUE 25 | ------------ 26 | Upon successful completion, _zmq_msg_data()_ shall return a pointer to the 27 | message content. 28 | 29 | 30 | ERRORS 31 | ------ 32 | No errors are defined. 33 | 34 | 35 | SEE ALSO 36 | -------- 37 | linkzmq:zmq_msg_size[3] 38 | linkzmq:zmq_msg_init[3] 39 | linkzmq:zmq_msg_init_size[3] 40 | linkzmq:zmq_msg_init_data[3] 41 | linkzmq:zmq_msg_close[3] 42 | linkzmq:zmq[7] 43 | 44 | 45 | AUTHORS 46 | ------- 47 | This manual page was written by the 0MQ community. 48 | -------------------------------------------------------------------------------- /doc/zmq_msg_size.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_size(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_size - retrieve message content size in bytes 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *size_t zmq_msg_size (zmq_msg_t '*msg');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_size()_ function shall return the size in bytes of the content of 18 | the message object referenced by 'msg'. 19 | 20 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 21 | _zmq_msg_ family of functions. 22 | 23 | 24 | RETURN VALUE 25 | ------------ 26 | Upon successful completion, _zmq_msg_size()_ shall return the size of the 27 | message content in bytes. 28 | 29 | 30 | ERRORS 31 | ------ 32 | No errors are defined. 33 | 34 | 35 | SEE ALSO 36 | -------- 37 | linkzmq:zmq_msg_data[3] 38 | linkzmq:zmq_msg_init[3] 39 | linkzmq:zmq_msg_init_size[3] 40 | linkzmq:zmq_msg_init_data[3] 41 | linkzmq:zmq_msg_close[3] 42 | linkzmq:zmq[7] 43 | 44 | 45 | AUTHORS 46 | ------- 47 | This manual page was written by the 0MQ community. 48 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | INCLUDES = -I$(top_builddir)/include 2 | LDADD = $(top_builddir)/src/libzmq.la 3 | 4 | noinst_PROGRAMS = test_pair_inproc \ 5 | test_pair_tcp \ 6 | test_reqrep_inproc \ 7 | test_reqrep_tcp \ 8 | test_hwm 9 | 10 | if !ON_MINGW 11 | noinst_PROGRAMS += test_shutdown_stress \ 12 | test_pair_ipc \ 13 | test_reqrep_ipc \ 14 | test_timeo 15 | endif 16 | 17 | test_pair_inproc_SOURCES = test_pair_inproc.cpp testutil.hpp 18 | test_pair_tcp_SOURCES = test_pair_tcp.cpp testutil.hpp 19 | 20 | test_reqrep_inproc_SOURCES = test_reqrep_inproc.cpp testutil.hpp 21 | test_reqrep_tcp_SOURCES = test_reqrep_tcp.cpp testutil.hpp 22 | 23 | test_hwm_SOURCES = test_hwm.cpp 24 | 25 | if !ON_MINGW 26 | test_shutdown_stress_SOURCES = test_shutdown_stress.cpp 27 | test_pair_ipc_SOURCES = test_pair_ipc.cpp testutil.hpp 28 | test_reqrep_ipc_SOURCES = test_reqrep_ipc.cpp testutil.hpp 29 | test_timeo_SOURCES = test_timeo.cpp 30 | endif 31 | 32 | TESTS = $(noinst_PROGRAMS) 33 | -------------------------------------------------------------------------------- /src/pub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "pub.hpp" 22 | 23 | zmq::pub_t::pub_t (class ctx_t *parent_, uint32_t tid_) : 24 | xpub_t (parent_, tid_) 25 | { 26 | options.type = ZMQ_PUB; 27 | } 28 | 29 | zmq::pub_t::~pub_t () 30 | { 31 | } 32 | -------------------------------------------------------------------------------- /src/device.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_DEVICE_HPP_INCLUDED__ 22 | #define __ZMQ_DEVICE_HPP_INCLUDED__ 23 | 24 | namespace zmq 25 | { 26 | 27 | int device (class socket_base_t *insocket_, 28 | class socket_base_t *outsocket_); 29 | 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /tests/test_pair_inproc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "inproc://tester" ; 29 | basic_tests (transport, ZMQ_PAIR, ZMQ_PAIR); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /tests/test_pair_ipc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "ipc:///tmp/tester" ; 29 | basic_tests (transport, ZMQ_PAIR, ZMQ_PAIR); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /tests/test_pair_tcp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "tcp://127.0.0.1:5560"; 29 | basic_tests (transport, ZMQ_PAIR, ZMQ_PAIR); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /tests/test_reqrep_inproc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "inproc://tester" ; 29 | basic_tests (transport, ZMQ_REQ, ZMQ_REP); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /tests/test_reqrep_ipc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "ipc:///tmp/tester" ; 29 | basic_tests (transport, ZMQ_REQ, ZMQ_REP); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /tests/test_reqrep_tcp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | 23 | using namespace std; 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *transport = "tcp://127.0.0.1:5560" ; 29 | basic_tests (transport, ZMQ_REQ, ZMQ_REP); 30 | return 0 ; 31 | } 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | syntax: glob # for hg-git users 2 | Makefile 3 | Makefile.in 4 | configure 5 | libtool 6 | config 7 | config.status 8 | config.log 9 | aclocal.m4 10 | autom4te.cache 11 | *.o 12 | *.gcno 13 | *.class 14 | *.ncb 15 | *.lo 16 | *.loT 17 | *.la 18 | *.html 19 | *.pdf 20 | *.ps 21 | .* 22 | *~ 23 | .*~ 24 | tests/test_pair_inproc 25 | tests/test_pair_ipc 26 | tests/test_pair_tcp 27 | tests/test_reqrep_inproc 28 | tests/test_reqrep_ipc 29 | tests/test_reqrep_tcp 30 | tests/test_shutdown_stress 31 | tests/test_hwm 32 | tests/test_timeo 33 | src/platform.hpp* 34 | src/stamp-h1 35 | devices/zmq_forwarder/zmq_forwarder 36 | devices/zmq_queue/zmq_queue 37 | devices/zmq_streamer/zmq_streamer 38 | perf/local_lat 39 | perf/local_thr 40 | perf/remote_lat 41 | perf/remote_thr 42 | perf/inproc_lat 43 | perf/inproc_thr 44 | doc/*.1 45 | doc/*.3 46 | doc/*.7 47 | doc/*.html 48 | doc/*.xml 49 | src/libzmq.pc 50 | bin/ 51 | lib/ 52 | builds/msvc/*.suo 53 | builds/msvc/*/*.user 54 | builds/msvc/*/Debug 55 | builds/msvc/*/Release 56 | foreign/openpgm/* 57 | !foreign/openpgm/*.tar.bz2 58 | !foreign/openpgm/*.tar.gz 59 | !foreign/openpgm/Makefile.am 60 | zeromq-*.tar.gz 61 | zeromq-*.zip 62 | builds/redhat/zeromq.spec 63 | 64 | -------------------------------------------------------------------------------- /src/likely.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_LIKELY_HPP_INCLUDED__ 22 | #define __ZMQ_LIKELY_HPP_INCLUDED__ 23 | 24 | #if defined __GNUC__ 25 | #define likely(x) __builtin_expect ((x), 1) 26 | #define unlikely(x) __builtin_expect ((x), 0) 27 | #else 28 | #define likely(x) (x) 29 | #define unlikely(x) (x) 30 | #endif 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /doc/zmq_version.txt: -------------------------------------------------------------------------------- 1 | zmq_version(3) 2 | ============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_version - report 0MQ library version 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *void zmq_version (int '*major', int '*minor', int '*patch');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_version()_ function shall fill in the integer variables pointed to by 18 | the 'major', 'minor' and 'patch' arguments with the major, minor and patch level 19 | components of the 0MQ library version. 20 | 21 | This functionality is intended for applications or language bindings 22 | dynamically linking to the 0MQ library that wish to determine the actual 23 | version of the 0MQ library they are using. 24 | 25 | 26 | RETURN VALUE 27 | ------------ 28 | There is no return value. 29 | 30 | 31 | ERRORS 32 | ------ 33 | No errors are defined. 34 | 35 | 36 | EXAMPLE 37 | ------- 38 | .Printing out the version of the 0MQ library 39 | ---- 40 | int major, minor, patch; 41 | zmq_version (&major, &minor, &patch); 42 | printf ("Current 0MQ version is %d.%d.%d\n", major, minor, patch); 43 | ---- 44 | 45 | 46 | SEE ALSO 47 | -------- 48 | linkzmq:zmq[7] 49 | 50 | 51 | AUTHORS 52 | ------- 53 | This manual page was written by the 0MQ community. 54 | -------------------------------------------------------------------------------- /doc/zmq_init.txt: -------------------------------------------------------------------------------- 1 | zmq_init(3) 2 | =========== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_init - initialise 0MQ context 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *void *zmq_init (int 'io_threads');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_init()_ function initialises a 0MQ 'context'. 18 | 19 | The 'io_threads' argument specifies the size of the 0MQ thread pool to handle 20 | I/O operations. If your application is using only the 'inproc' transport for 21 | messaging you may set this to zero, otherwise set it to at least one. 22 | 23 | .Thread safety 24 | A 0MQ 'context' is thread safe and may be shared among as many application 25 | threads as necessary, without any additional locking required on the part of 26 | the caller. 27 | 28 | 29 | RETURN VALUE 30 | ------------ 31 | The _zmq_init()_ function shall return an opaque handle to the initialised 32 | 'context' if successful. Otherwise it shall return NULL and set 'errno' to one 33 | of the values defined below. 34 | 35 | 36 | ERRORS 37 | ------ 38 | *EINVAL*:: 39 | An invalid number of 'io_threads' was requested. 40 | 41 | 42 | SEE ALSO 43 | -------- 44 | linkzmq:zmq[7] 45 | linkzmq:zmq_term[3] 46 | 47 | 48 | AUTHORS 49 | ------- 50 | This manual page was written by the 0MQ community. 51 | -------------------------------------------------------------------------------- /doc/zmq_strerror.txt: -------------------------------------------------------------------------------- 1 | zmq_strerror(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_strerror - get 0MQ error message string 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *const char *zmq_strerror (int 'errnum');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_strerror()_ function shall return a pointer to an error message string 18 | corresponding to the error number specified by the 'errnum' argument. As 0MQ 19 | defines additional error numbers over and above those defined by the operating 20 | system, applications should use _zmq_strerror()_ in preference to the standard 21 | _strerror()_ function. 22 | 23 | 24 | RETURN VALUE 25 | ------------ 26 | The _zmq_strerror()_ function shall return a pointer to an error message 27 | string. 28 | 29 | 30 | ERRORS 31 | ------ 32 | No errors are defined. 33 | 34 | 35 | EXAMPLE 36 | ------- 37 | .Displaying an error message when a 0MQ context cannot be initialised 38 | ---- 39 | void *ctx = zmq_init (1, 1, 0); 40 | if (!ctx) { 41 | printf ("Error occurred during zmq_init(): %s\n", zmq_strerror (errno)); 42 | abort (); 43 | } 44 | ---- 45 | 46 | 47 | SEE ALSO 48 | -------- 49 | linkzmq:zmq[7] 50 | 51 | 52 | AUTHORS 53 | ------- 54 | This manual page was written by the 0MQ community. 55 | -------------------------------------------------------------------------------- /doc/zmq_errno.txt: -------------------------------------------------------------------------------- 1 | zmq_errno(3) 2 | ============ 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_errno - retrieve value of errno for the calling thread 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_errno (void);* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_errno()_ function shall retrieve the value of the 'errno' variable for 18 | the calling thread. 19 | 20 | The _zmq_errno()_ function is provided to assist users on non-POSIX systems who 21 | are experiencing issues with retrieving the correct value of 'errno' directly. 22 | Specifically, users on Win32 systems whose application is using a different C 23 | run-time library from the C run-time library in use by 0MQ will need to use 24 | _zmq_errno()_ for correct operation. 25 | 26 | IMPORTANT: Users not experiencing issues with retrieving the correct value of 27 | 'errno' should not use this function and should instead access the 'errno' 28 | variable directly. 29 | 30 | 31 | RETURN VALUE 32 | ------------ 33 | The _zmq_errno()_ function shall return the value of the 'errno' variable for 34 | the calling thread. 35 | 36 | 37 | ERRORS 38 | ------ 39 | No errors are defined. 40 | 41 | 42 | SEE ALSO 43 | -------- 44 | linkzmq:zmq[7] 45 | 46 | 47 | AUTHORS 48 | ------- 49 | This manual page was written by the 0MQ community. 50 | -------------------------------------------------------------------------------- /src/blob.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_BLOB_HPP_INCLUDED__ 22 | #define __ZMQ_BLOB_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #if (defined(__GNUC__) && (__GNUC__ == 3)) 27 | #include "gcc_421_char_traits.hpp" 28 | #endif 29 | 30 | namespace zmq 31 | { 32 | 33 | // Object to hold dynamically allocated opaque binary data. 34 | typedef std::basic_string blob_t; 35 | 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Welcome 2 | ------- 3 | 4 | The 0MQ lightweight messaging kernel is a library which extends the 5 | standard socket interfaces with features traditionally provided by 6 | specialised messaging middleware products. 0MQ sockets provide an 7 | abstraction of asynchronous message queues, multiple messaging patterns, 8 | message filtering (subscriptions), seamless access to multiple transport 9 | protocols and more. 10 | 11 | 12 | Building and installation 13 | ------------------------- 14 | 15 | See the INSTALL file included with the distribution. 16 | 17 | 18 | Resources 19 | --------- 20 | 21 | A reference manual is provided with the distribution. Refer to "man zmq" 22 | after you have installed 0MQ on your system, or see http://api.zeromq.org. 23 | 24 | Website: http://www.zeromq.org/ 25 | 26 | Development mailing list: zeromq-dev@lists.zeromq.org 27 | Announcements mailing list: zeromq-announce@lists.zeromq.org 28 | 29 | Git repository: http://github.com/zeromq/libzmq 30 | 31 | 0MQ developers can also be found on the IRC channel #zeromq, on the 32 | Freenode network (irc.freenode.net). 33 | 34 | Copying 35 | ------- 36 | 37 | Free use of this software is granted under the terms of the GNU Lesser General 38 | Public License (LGPL). For details see the files `COPYING` and `COPYING.LESSER` 39 | included with the 0MQ distribution. 40 | -------------------------------------------------------------------------------- /src/pub.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_PUB_HPP_INCLUDED__ 22 | #define __ZMQ_PUB_HPP_INCLUDED__ 23 | 24 | #include "xpub.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | class pub_t : public xpub_t 30 | { 31 | public: 32 | 33 | pub_t (class ctx_t *parent_, uint32_t tid_); 34 | ~pub_t (); 35 | 36 | private: 37 | 38 | pub_t (const pub_t&); 39 | const pub_t &operator = (const pub_t&); 40 | }; 41 | 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /doc/zmq_msg_move.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_move(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_move - move content of a message to another message 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_msg_move (zmq_msg_t '*dest', zmq_msg_t '*src');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_move()_ function shall move the content of the message object 18 | referenced by 'src' to the message object referenced by 'dest'. No actual 19 | copying of message content is performed, 'dest' is simply updated to reference 20 | the new content. 'src' becomes an empty message after calling _zmq_msg_move()_. 21 | The original content of 'dest', if any, shall be released. 22 | 23 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 24 | _zmq_msg_ family of functions. 25 | 26 | 27 | RETURN VALUE 28 | ------------ 29 | The _zmq_msg_move()_ function shall return zero if successful. Otherwise it 30 | shall return `-1` and set 'errno' to one of the values defined below. 31 | 32 | 33 | ERRORS 34 | ------ 35 | *EFAULT*:: 36 | Invalid message. 37 | 38 | 39 | SEE ALSO 40 | -------- 41 | linkzmq:zmq_msg_copy[3] 42 | linkzmq:zmq_msg_init[3] 43 | linkzmq:zmq_msg_init_size[3] 44 | linkzmq:zmq_msg_init_data[3] 45 | linkzmq:zmq_msg_close[3] 46 | linkzmq:zmq[7] 47 | 48 | 49 | AUTHORS 50 | ------- 51 | This manual page was written by the 0MQ community. 52 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I config 2 | 3 | if BUILD_PGM 4 | SUBDIRS = foreign/openpgm src doc perf tests 5 | else 6 | SUBDIRS = src doc perf tests 7 | endif 8 | 9 | DIST_SUBDIRS = foreign/openpgm src doc perf tests builds/msvc 10 | 11 | EXTRA_DIST = \ 12 | autogen.sh \ 13 | version.sh \ 14 | MAINTAINERS \ 15 | foreign/openpgm/@pgm_basename@.tar.gz \ 16 | foreign/xmlParser/xmlParser.cpp \ 17 | foreign/xmlParser/xmlParser.hpp 18 | MAINTAINERCLEANFILES = \ 19 | $(srcdir)/aclocal.m4 \ 20 | $(srcdir)/autom4te.cache \ 21 | $(srcdir)/configure \ 22 | `find "$(srcdir)" -type f -name Makefile.in -print` 23 | 24 | dist-hook: 25 | @if test -d "$(srcdir)/.git"; \ 26 | then \ 27 | echo Creating ChangeLog && \ 28 | ( cd "$(top_srcdir)" && \ 29 | echo '# Generated by Makefile. Do not edit.'; echo; \ 30 | $(top_srcdir)/config/missing --run git log --stat ) > ChangeLog.tmp \ 31 | && mv -f ChangeLog.tmp $(top_distdir)/ChangeLog \ 32 | || ( rm -f ChangeLog.tmp ; \ 33 | echo Failed to generate ChangeLog >&2 ); \ 34 | else \ 35 | echo A git clone is required to generate a ChangeLog >&2; \ 36 | fi 37 | -cp $(top_srcdir)/builds/redhat/zeromq.spec $(distdir)/zeromq.spec 38 | -rm -rf $(distdir)/foreign/openpgm/build-staging 39 | distclean-local: 40 | -rm -rf $(top_srcdir)/foreign/openpgm/build-staging 41 | maintainer-clean-local: 42 | -rm -rf $(top_srcdir)/config 43 | -------------------------------------------------------------------------------- /src/command.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include "command.hpp" 24 | 25 | void zmq::deallocate_command (command_t *cmd_) 26 | { 27 | switch (cmd_->type) { 28 | case command_t::attach: 29 | if (cmd_->args.attach.peer_identity) 30 | free (cmd_->args.attach.peer_identity); 31 | break; 32 | case command_t::bind: 33 | if (cmd_->args.bind.peer_identity) 34 | free (cmd_->args.bind.peer_identity); 35 | break; 36 | default: 37 | /* noop */; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/fd.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_FD_HPP_INCLUDED__ 22 | #define __ZMQ_FD_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #ifdef ZMQ_HAVE_WINDOWS 27 | #include "windows.hpp" 28 | #endif 29 | 30 | namespace zmq 31 | { 32 | #ifdef ZMQ_HAVE_WINDOWS 33 | #if defined _MSC_VER &&_MSC_VER <= 1400 34 | typedef UINT_PTR fd_t; 35 | enum {retired_fd = (fd_t)(~0)}; 36 | #else 37 | typedef SOCKET fd_t; 38 | enum {retired_fd = INVALID_SOCKET}; 39 | #endif 40 | #else 41 | typedef int fd_t; 42 | enum {retired_fd = -1}; 43 | #endif 44 | } 45 | #endif 46 | -------------------------------------------------------------------------------- /doc/zmq_close.txt: -------------------------------------------------------------------------------- 1 | zmq_close(3) 2 | ============ 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_close - close 0MQ socket 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_close (void '*socket');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_close()_ function shall destroy the socket referenced by the 'socket' 18 | argument. Any outstanding messages physically received from the network but not 19 | yet received by the application with _zmq_recv()_ shall be discarded. The 20 | behaviour for discarding messages sent by the application with _zmq_send()_ but 21 | not yet physically transferred to the network depends on the value of the 22 | _ZMQ_LINGER_ socket option for the specified 'socket'. 23 | 24 | NOTE: The default setting of _ZMQ_LINGER_ does not discard unsent messages; 25 | this behaviour may cause the application to block when calling _zmq_term()_. 26 | For details refer to linkzmq:zmq_setsockopt[3] and linkzmq:zmq_term[3]. 27 | 28 | 29 | RETURN VALUE 30 | ------------ 31 | The _zmq_close()_ function shall return zero if successful. Otherwise it shall 32 | return `-1` and set 'errno' to one of the values defined below. 33 | 34 | 35 | ERRORS 36 | ------ 37 | *ENOTSOCK*:: 38 | The provided 'socket' was invalid. 39 | 40 | 41 | SEE ALSO 42 | -------- 43 | linkzmq:zmq_socket[3] 44 | linkzmq:zmq_term[3] 45 | linkzmq:zmq_setsockopt[3] 46 | linkzmq:zmq[7] 47 | 48 | 49 | AUTHORS 50 | ------- 51 | This manual page was written by the 0MQ community. 52 | -------------------------------------------------------------------------------- /doc/Makefile.am: -------------------------------------------------------------------------------- 1 | MAN1 = 2 | MAN3 = zmq_bind.3 zmq_close.3 zmq_connect.3 zmq_device.3 zmq_init.3 \ 3 | zmq_msg_close.3 zmq_msg_copy.3 zmq_msg_data.3 zmq_msg_init.3 \ 4 | zmq_msg_init_data.3 zmq_msg_init_size.3 zmq_msg_move.3 zmq_msg_size.3 \ 5 | zmq_poll.3 zmq_recv.3 zmq_send.3 zmq_setsockopt.3 zmq_socket.3 \ 6 | zmq_strerror.3 zmq_term.3 zmq_version.3 zmq_getsockopt.3 zmq_errno.3 7 | MAN7 = zmq.7 zmq_tcp.7 zmq_pgm.7 zmq_epgm.7 zmq_inproc.7 zmq_ipc.7 \ 8 | zmq_cpp.7 9 | MAN_DOC = $(MAN1) $(MAN3) $(MAN7) 10 | 11 | MAN_TXT = $(MAN1:%.1=%.txt) 12 | MAN_TXT += $(MAN3:%.3=%.txt) 13 | MAN_TXT += $(MAN7:%.7=%.txt) 14 | MAN_HTML = $(MAN_TXT:%.txt=%.html) 15 | 16 | if INSTALL_MAN 17 | dist_man_MANS = $(MAN_DOC) 18 | endif 19 | 20 | EXTRA_DIST = asciidoc.conf $(MAN_TXT) 21 | if BUILD_DOC 22 | EXTRA_DIST += $(MAN_HTML) 23 | endif 24 | 25 | MAINTAINERCLEANFILES = $(MAN_DOC) $(MAN_HTML) 26 | 27 | dist-hook : $(MAN_DOC) $(MAN_HTML) 28 | 29 | if BUILD_DOC 30 | SUFFIXES=.html .txt .xml .1 .3 .7 31 | 32 | .txt.html: 33 | $(AM_V_GEN)$(ASCIIDOC) -d manpage -b xhtml11 -f asciidoc.conf \ 34 | -azmq_version=@PACKAGE_VERSION@ $< 35 | .txt.xml: 36 | $(AM_V_GEN)$(ASCIIDOC) -d manpage -b docbook -f asciidoc.conf \ 37 | -azmq_version=@PACKAGE_VERSION@ $< 38 | .xml.1: 39 | $(AM_V_GEN)$(XMLTO) man $< 40 | .xml.3: 41 | $(AM_V_GEN)$(XMLTO) man $< 42 | .xml.7: 43 | $(AM_V_GEN)$(XMLTO) man $< 44 | zmq_epgm.7: zmq_pgm.7 45 | $(AM_V_GEN)cp zmq_pgm.7 $@ 46 | endif 47 | -------------------------------------------------------------------------------- /doc/zmq_msg_close.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_close(3) 2 | ================ 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_close - release 0MQ message 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_msg_close (zmq_msg_t '*msg');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_close()_ function shall inform the 0MQ infrastructure that any 18 | resources associated with the message object referenced by 'msg' are no longer 19 | required and may be released. Actual release of resources associated with the 20 | message object shall be postponed by 0MQ until all users of the message or 21 | underlying data buffer have indicated it is no longer required. 22 | 23 | Applications should ensure that _zmq_msg_close()_ is called once a message is 24 | no longer required, otherwise memory leaks may occur. 25 | 26 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 27 | _zmq_msg_ family of functions. 28 | 29 | 30 | RETURN VALUE 31 | ------------ 32 | The _zmq_msg_close()_ function shall return zero if successful. Otherwise 33 | it shall return `-1` and set 'errno' to one of the values defined below. 34 | 35 | 36 | ERRORS 37 | ------ 38 | *EFAULT*:: 39 | Invalid message. 40 | 41 | 42 | SEE ALSO 43 | -------- 44 | linkzmq:zmq_msg_init[3] 45 | linkzmq:zmq_msg_init_size[3] 46 | linkzmq:zmq_msg_init_data[3] 47 | linkzmq:zmq_msg_data[3] 48 | linkzmq:zmq_msg_size[3] 49 | linkzmq:zmq[7] 50 | 51 | 52 | AUTHORS 53 | ------- 54 | This manual page was written by the 0MQ community. 55 | -------------------------------------------------------------------------------- /src/transient_session.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "transient_session.hpp" 22 | 23 | zmq::transient_session_t::transient_session_t (class io_thread_t *io_thread_, 24 | class socket_base_t *socket_, const options_t &options_) : 25 | session_t (io_thread_, socket_, options_) 26 | { 27 | } 28 | 29 | zmq::transient_session_t::~transient_session_t () 30 | { 31 | } 32 | 33 | void zmq::transient_session_t::attached (const blob_t &peer_identity_) 34 | { 35 | } 36 | 37 | void zmq::transient_session_t::detached () 38 | { 39 | // There's no way to reestablish a transient session. Tear it down. 40 | terminate (); 41 | } 42 | -------------------------------------------------------------------------------- /MAINTAINERS: -------------------------------------------------------------------------------- 1 | Component: Atomic Operations 2 | Maintainer: Martin Sustrik 3 | Contact: sustrik@250bpm.com 4 | 5 | Component: Lock-free Algorithms 6 | Maintainer: Martin Sustrik 7 | Contact: sustrik@250bpm.com 8 | 9 | Component: TCP transport 10 | Maintainer: Martin Sustrik 11 | Contact: sustrik@250bpm.com 12 | 13 | Component: IPC transport 14 | Maintainer: Martin Sustrik 15 | Contact: sustrik@250bpm.com 16 | 17 | Component: PGM transport 18 | Maintainer: Martin Sustrik 19 | Contact: sustrik@250bpm.com 20 | 21 | Component: In-process transport 22 | Maintainer: Martin Sustrik 23 | Contact: sustrik@250bpm.com 24 | 25 | Component: I/O Threads 26 | Maintainer: Martin Sustrik 27 | Contact: sustrik@250bpm.com 28 | 29 | Component: Application Threads and Differnet Socket Types 30 | Maintainer: Martin Sustrik 31 | Contact: sustrik@250bpm.com 32 | 33 | Component: Multiplexing (zmq_poll) 34 | Maintainer: Martin Sustrik 35 | Contact: sustrik@250bpm.com 36 | 37 | Component: Generic Infrastructure (context, mailbox, command, pipe) 38 | Maintainer: Martin Sustrik 39 | Contact: sustrik@250bpm.com 40 | 41 | Component: Documentation 42 | Maintainer: Martin Sustrik 43 | Contact: sustrik@250bpm.com 44 | 45 | Component: Autotools build system 46 | Maintainer: Mikko Koppanen 47 | Contact: mikko.koppanen@gmail.com 48 | 49 | Component: MSVC build system 50 | Maintainer: Martin Sustrik 51 | Contact: sustrik@250bpm.com 52 | 53 | Component: Performance tests 54 | Maintainer: Martin Sustrik 55 | Contact: sustrik@250bpm.com 56 | 57 | -------------------------------------------------------------------------------- /doc/asciidoc.conf: -------------------------------------------------------------------------------- 1 | [paradef-default] 2 | literal-style=template="literalparagraph" 3 | 4 | [macros] 5 | (?su)[\\]?(?Plinkzmq):(?P\S*?)\[(?P.*?)\]= 6 | 7 | ifdef::backend-docbook[] 8 | [linkzmq-inlinemacro] 9 | {0%{target}} 10 | {0#} 11 | {0#{target}{0}} 12 | {0#} 13 | endif::backend-docbook[] 14 | 15 | ifdef::backend-xhtml11[] 16 | [linkzmq-inlinemacro] 17 | {target}{0?({0})} 18 | endif::backend-xhtml11[] 19 | 20 | ifdef::doctype-manpage[] 21 | ifdef::backend-docbook[] 22 | [header] 23 | template::[header-declarations] 24 | 25 | 26 | {mantitle} 27 | {manvolnum} 28 | 0MQ 29 | {zmq_version} 30 | 0MQ Manual 31 | 32 | 33 | {manname} 34 | {manpurpose} 35 | 36 | endif::backend-docbook[] 37 | endif::doctype-manpage[] 38 | 39 | ifdef::backend-xhtml11[] 40 | [footer] 41 | 42 | {disable-javascript%

} 43 | 49 | 50 | 51 | endif::backend-xhtml11[] 52 | 53 | [replacements] 54 | ifdef::backend-xhtml11[] 55 | 0MQ=ØMQ 56 | endif::backend-xhtml11[] 57 | -------------------------------------------------------------------------------- /src/sub.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_SUB_HPP_INCLUDED__ 22 | #define __ZMQ_SUB_HPP_INCLUDED__ 23 | 24 | #include "xsub.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | class sub_t : public xsub_t 30 | { 31 | public: 32 | 33 | sub_t (class ctx_t *parent_, uint32_t tid_); 34 | ~sub_t (); 35 | 36 | protected: 37 | 38 | int xsetsockopt (int option_, const void *optval_, size_t optvallen_); 39 | int xsend (zmq_msg_t *msg_, int options_); 40 | bool xhas_out (); 41 | 42 | private: 43 | 44 | sub_t (const sub_t&); 45 | const sub_t &operator = (const sub_t&); 46 | }; 47 | 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /doc/zmq_msg_init.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_init(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_init - initialise empty 0MQ message 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_msg_init (zmq_msg_t '*msg');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_init()_ function shall initialise the message object referenced by 18 | 'msg' to represent an empty message. This function is most useful when called 19 | before receiving a message with _zmq_recv()_. 20 | 21 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 22 | _zmq_msg_ family of functions. 23 | 24 | CAUTION: The functions _zmq_msg_init()_, _zmq_msg_init_data()_ and 25 | _zmq_msg_init_size()_ are mutually exclusive. Never initialize the same 26 | 'zmq_msg_t' twice. 27 | 28 | 29 | RETURN VALUE 30 | ------------ 31 | The _zmq_msg_init()_ function shall return zero if successful. Otherwise it 32 | shall return `-1` and set 'errno' to one of the values defined below. 33 | 34 | 35 | ERRORS 36 | ------ 37 | No errors are defined. 38 | 39 | 40 | EXAMPLE 41 | ------- 42 | .Receiving a message from a socket 43 | ---- 44 | zmq_msg_t msg; 45 | rc = zmq_msg_init (&msg); 46 | assert (rc == 0); 47 | rc = zmq_recv (socket, &msg, 0); 48 | assert (rc == 0); 49 | ---- 50 | 51 | 52 | SEE ALSO 53 | -------- 54 | linkzmq:zmq_msg_init_size[3] 55 | linkzmq:zmq_msg_init_data[3] 56 | linkzmq:zmq_msg_close[3] 57 | linkzmq:zmq_msg_data[3] 58 | linkzmq:zmq_msg_size[3] 59 | linkzmq:zmq[7] 60 | 61 | 62 | AUTHORS 63 | ------- 64 | This manual page was written by the 0MQ community. 65 | -------------------------------------------------------------------------------- /doc/zmq_msg_copy.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_copy(3) 2 | =============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_copy - copy content of a message to another message 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_msg_copy (zmq_msg_t '*dest', zmq_msg_t '*src');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_copy()_ function shall copy the message object referenced by 'src' 18 | to the message object referenced by 'dest'. The original content of 'dest', if 19 | any, shall be released. 20 | 21 | CAUTION: The implementation may choose not to physically copy the message 22 | content, rather to share the underlying buffer between 'src' and 'dest'. Avoid 23 | modifying message content after a message has been copied with 24 | _zmq_msg_copy()_, doing so can result in undefined behaviour. If what you need 25 | is an actual hard copy, allocate a new message using _zmq_msg_init_size()_ and 26 | copy the message content using _memcpy()_. 27 | 28 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 29 | _zmq_msg_ family of functions. 30 | 31 | 32 | RETURN VALUE 33 | ------------ 34 | The _zmq_msg_copy()_ function shall return zero if successful. Otherwise it 35 | shall return `-1` and set 'errno' to one of the values defined below. 36 | 37 | 38 | ERRORS 39 | ------ 40 | *EFAULT*:: 41 | Invalid message. 42 | 43 | 44 | SEE ALSO 45 | -------- 46 | linkzmq:zmq_msg_move[3] 47 | linkzmq:zmq_msg_init[3] 48 | linkzmq:zmq_msg_init_size[3] 49 | linkzmq:zmq_msg_init_data[3] 50 | linkzmq:zmq_msg_close[3] 51 | linkzmq:zmq[7] 52 | 53 | 54 | AUTHORS 55 | ------- 56 | This manual page was written by the 0MQ community. 57 | -------------------------------------------------------------------------------- /doc/zmq_msg_init_size.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_init_size(3) 2 | ==================== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_init_size - initialise 0MQ message of a specified size 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_msg_init_size (zmq_msg_t '*msg', size_t 'size');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_msg_init_size()_ function shall allocate any resources required to 18 | store a message 'size' bytes long and initialise the message object referenced 19 | by 'msg' to represent the newly allocated message. 20 | 21 | The implementation shall choose whether to store message content on the stack 22 | (small messages) or on the heap (large messages). For performance reasons 23 | _zmq_msg_init_size()_ shall not clear the message data. 24 | 25 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 26 | _zmq_msg_ family of functions. 27 | 28 | CAUTION: The functions _zmq_msg_init()_, _zmq_msg_init_data()_ and 29 | _zmq_msg_init_size()_ are mutually exclusive. Never initialize the same 30 | 'zmq_msg_t' twice. 31 | 32 | 33 | RETURN VALUE 34 | ------------ 35 | The _zmq_msg_init_size()_ function shall return zero if successful. Otherwise 36 | it shall return `-1` and set 'errno' to one of the values defined below. 37 | 38 | 39 | ERRORS 40 | ------ 41 | *ENOMEM*:: 42 | Insufficient storage space is available. 43 | 44 | 45 | SEE ALSO 46 | -------- 47 | linkzmq:zmq_msg_init_data[3] 48 | linkzmq:zmq_msg_init[3] 49 | linkzmq:zmq_msg_close[3] 50 | linkzmq:zmq_msg_data[3] 51 | linkzmq:zmq_msg_size[3] 52 | linkzmq:zmq[7] 53 | 54 | 55 | AUTHORS 56 | ------- 57 | This manual page was written by the 0MQ community. 58 | -------------------------------------------------------------------------------- /src/i_poll_events.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_I_POLL_EVENTS_HPP_INCLUDED__ 22 | #define __ZMQ_I_POLL_EVENTS_HPP_INCLUDED__ 23 | 24 | namespace zmq 25 | { 26 | 27 | // Virtual interface to be exposed by object that want to be notified 28 | // about events on file descriptors. 29 | 30 | struct i_poll_events 31 | { 32 | virtual ~i_poll_events () {} 33 | 34 | // Called by I/O thread when file descriptor is ready for reading. 35 | virtual void in_event () = 0; 36 | 37 | // Called by I/O thread when file descriptor is ready for writing. 38 | virtual void out_event () = 0; 39 | 40 | // Called when timer expires. 41 | virtual void timer_event (int id_) = 0; 42 | }; 43 | 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/i_inout.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_I_INOUT_HPP_INCLUDED__ 22 | #define __ZMQ_I_INOUT_HPP_INCLUDED__ 23 | 24 | #include "../include/zmq.h" 25 | 26 | #include "stdint.hpp" 27 | 28 | namespace zmq 29 | { 30 | 31 | struct i_inout 32 | { 33 | virtual ~i_inout () {} 34 | 35 | // Engine asks for a message to send to the network. 36 | virtual bool read (::zmq_msg_t *msg_) = 0; 37 | 38 | // Engine received message from the network and sends it further on. 39 | virtual bool write (::zmq_msg_t *msg_) = 0; 40 | 41 | // Flush all the previously written messages. 42 | virtual void flush () = 0; 43 | 44 | // Engine is dead. Drop all the references to it. 45 | virtual void detach () = 0; 46 | }; 47 | 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright (c) 2007-2011 iMatix Corporation 4 | # Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 5 | # 6 | # This file is part of 0MQ. 7 | # 8 | # 0MQ is free software; you can redistribute it and/or modify it under 9 | # the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation; either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # 0MQ is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU Lesser General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this program. If not, see . 20 | 21 | # Script to generate all required files from fresh git checkout. 22 | 23 | command -v libtool >/dev/null 2>&1 24 | if [ $? -ne 0 ]; then 25 | echo "autogen.sh: error: could not find libtool. libtool is required to run autogen.sh." 1>&2 26 | exit 1 27 | fi 28 | 29 | command -v autoreconf >/dev/null 2>&1 30 | if [ $? -ne 0 ]; then 31 | echo "autogen.sh: error: could not find autoreconf. autoconf and automake are required to run autogen.sh." 1>&2 32 | exit 1 33 | fi 34 | 35 | mkdir -p ./config 36 | if [ $? -ne 0 ]; then 37 | echo "autogen.sh: error: could not create directory: ./config." 1>&2 38 | exit 1 39 | fi 40 | 41 | autoreconf --install --force --verbose -I config 42 | if [ $? -ne 0 ]; then 43 | echo "autogen.sh: error: autoreconf exited with status $?" 1>&2 44 | exit 1 45 | fi 46 | -------------------------------------------------------------------------------- /src/stdint.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_STDINT_HPP_INCLUDED__ 22 | #define __ZMQ_STDINT_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #if defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_OPENVMS 27 | 28 | #include 29 | 30 | #elif defined _MSC_VER 31 | 32 | #ifndef int8_t 33 | typedef __int8 int8_t; 34 | #endif 35 | #ifndef int16_t 36 | typedef __int16 int16_t; 37 | #endif 38 | #ifndef int32_t 39 | typedef __int32 int32_t; 40 | #endif 41 | #ifndef int64_t 42 | typedef __int64 int64_t; 43 | #endif 44 | #ifndef uint8_t 45 | typedef unsigned __int8 uint8_t; 46 | #endif 47 | #ifndef uint16_t 48 | typedef unsigned __int16 uint16_t; 49 | #endif 50 | #ifndef uint32_t 51 | typedef unsigned __int32 uint32_t; 52 | #endif 53 | #ifndef uint64_t 54 | typedef unsigned __int64 uint64_t; 55 | #endif 56 | 57 | #else 58 | 59 | #include 60 | 61 | #endif 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/msg_content.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_MSG_HPP_INCLUDE__ 22 | #define __ZMQ_MSG_HPP_INCLUDE__ 23 | 24 | #include 25 | 26 | #include "../include/zmq.h" 27 | 28 | #include "atomic_counter.hpp" 29 | 30 | namespace zmq 31 | { 32 | 33 | // Shared message buffer. Message data are either allocated in one 34 | // continuous block along with this structure - thus avoiding one 35 | // malloc/free pair or they are stored in used-supplied memory. 36 | // In the latter case, ffn member stores pointer to the function to be 37 | // used to deallocate the data. If the buffer is actually shared (there 38 | // are at least 2 references to it) refcount member contains number of 39 | // references. 40 | 41 | struct msg_content_t 42 | { 43 | void *data; 44 | size_t size; 45 | zmq_free_fn *ffn; 46 | void *hint; 47 | zmq::atomic_counter_t refcnt; 48 | }; 49 | 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/pull.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | 23 | #include "pull.hpp" 24 | #include "err.hpp" 25 | 26 | zmq::pull_t::pull_t (class ctx_t *parent_, uint32_t tid_) : 27 | socket_base_t (parent_, tid_), 28 | fq (this) 29 | { 30 | options.type = ZMQ_PULL; 31 | options.requires_in = true; 32 | options.requires_out = false; 33 | } 34 | 35 | zmq::pull_t::~pull_t () 36 | { 37 | } 38 | 39 | void zmq::pull_t::xattach_pipes (class reader_t *inpipe_, 40 | class writer_t *outpipe_, const blob_t &peer_identity_) 41 | { 42 | zmq_assert (inpipe_ && !outpipe_); 43 | fq.attach (inpipe_); 44 | } 45 | 46 | void zmq::pull_t::process_term (int linger_) 47 | { 48 | fq.terminate (); 49 | socket_base_t::process_term (linger_); 50 | } 51 | 52 | int zmq::pull_t::xrecv (zmq_msg_t *msg_, int flags_) 53 | { 54 | return fq.recv (msg_, flags_); 55 | } 56 | 57 | bool zmq::pull_t::xhas_in () 58 | { 59 | return fq.has_in (); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/push.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | 23 | #include "push.hpp" 24 | #include "err.hpp" 25 | #include "pipe.hpp" 26 | 27 | zmq::push_t::push_t (class ctx_t *parent_, uint32_t tid_) : 28 | socket_base_t (parent_, tid_), 29 | lb (this) 30 | { 31 | options.type = ZMQ_PUSH; 32 | options.requires_in = false; 33 | options.requires_out = true; 34 | } 35 | 36 | zmq::push_t::~push_t () 37 | { 38 | } 39 | 40 | void zmq::push_t::xattach_pipes (class reader_t *inpipe_, 41 | class writer_t *outpipe_, const blob_t &peer_identity_) 42 | { 43 | zmq_assert (!inpipe_ && outpipe_); 44 | lb.attach (outpipe_); 45 | } 46 | 47 | void zmq::push_t::process_term (int linger_) 48 | { 49 | lb.terminate (); 50 | socket_base_t::process_term (linger_); 51 | } 52 | 53 | int zmq::push_t::xsend (zmq_msg_t *msg_, int flags_) 54 | { 55 | return lb.send (msg_, flags_); 56 | } 57 | 58 | bool zmq::push_t::xhas_out () 59 | { 60 | return lb.has_out (); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/trie.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2011-2012 Spotify AB 4 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 5 | 6 | This file is part of 0MQ. 7 | 8 | 0MQ is free software; you can redistribute it and/or modify it under 9 | the terms of the GNU Lesser General Public License as published by 10 | the Free Software Foundation; either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | 0MQ is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General Public License 19 | along with this program. If not, see . 20 | */ 21 | 22 | #ifndef __ZMQ_TRIE_HPP_INCLUDED__ 23 | #define __ZMQ_TRIE_HPP_INCLUDED__ 24 | 25 | #include 26 | 27 | #include "stdint.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class trie_t 33 | { 34 | public: 35 | 36 | trie_t (); 37 | ~trie_t (); 38 | 39 | void add (unsigned char *prefix_, size_t size_); 40 | bool rm (unsigned char *prefix_, size_t size_); 41 | bool check (unsigned char *data_, size_t size_); 42 | 43 | private: 44 | bool is_redundant () const; 45 | 46 | uint32_t refcnt; 47 | unsigned char min; 48 | unsigned short count; 49 | unsigned short live_nodes; 50 | union { 51 | class trie_t *node; 52 | class trie_t **table; 53 | } next; 54 | 55 | trie_t (const trie_t&); 56 | const trie_t &operator = (const trie_t&); 57 | }; 58 | 59 | } 60 | 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /src/transient_session.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_TRANSIENT_SESSION_HPP_INCLUDED__ 22 | #define __ZMQ_TRANSIENT_SESSION_HPP_INCLUDED__ 23 | 24 | #include "session.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | // Transient session is created by the listener when the connected peer 30 | // stays anonymous. Transient session is destroyed on disconnect. 31 | 32 | class transient_session_t : public session_t 33 | { 34 | public: 35 | 36 | transient_session_t (class io_thread_t *io_thread_, 37 | class socket_base_t *socket_, const options_t &options_); 38 | ~transient_session_t (); 39 | 40 | private: 41 | 42 | // Handlers for events from session base class. 43 | void attached (const blob_t &peer_identity_); 44 | void detached (); 45 | 46 | transient_session_t (const transient_session_t&); 47 | const transient_session_t &operator = (const transient_session_t&); 48 | }; 49 | 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/pull.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_PULL_HPP_INCLUDED__ 22 | #define __ZMQ_PULL_HPP_INCLUDED__ 23 | 24 | #include "socket_base.hpp" 25 | #include "fq.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | class pull_t : public socket_base_t 31 | { 32 | public: 33 | 34 | pull_t (class ctx_t *parent_, uint32_t tid_); 35 | ~pull_t (); 36 | 37 | protected: 38 | 39 | // Overloads of functions from socket_base_t. 40 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 41 | const blob_t &peer_identity_); 42 | int xrecv (zmq_msg_t *msg_, int flags_); 43 | bool xhas_in (); 44 | 45 | private: 46 | 47 | // Hook into the termination process. 48 | void process_term (int linger_); 49 | 50 | // Fair queueing object for inbound pipes. 51 | fq_t fq; 52 | 53 | pull_t (const pull_t&); 54 | const pull_t &operator = (const pull_t&); 55 | 56 | }; 57 | 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/push.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_PUSH_HPP_INCLUDED__ 22 | #define __ZMQ_PUSH_HPP_INCLUDED__ 23 | 24 | #include "socket_base.hpp" 25 | #include "lb.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | class push_t : public socket_base_t 31 | { 32 | public: 33 | 34 | push_t (class ctx_t *parent_, uint32_t tid_); 35 | ~push_t (); 36 | 37 | protected: 38 | 39 | // Overloads of functions from socket_base_t. 40 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 41 | const blob_t &peer_identity_); 42 | int xsend (zmq_msg_t *msg_, int flags_); 43 | bool xhas_out (); 44 | 45 | private: 46 | 47 | // Hook into the termination process. 48 | void process_term (int linger_); 49 | 50 | // Load balancer managing the outbound pipes. 51 | lb_t lb; 52 | 53 | push_t (const push_t&); 54 | const push_t &operator = (const push_t&); 55 | }; 56 | 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /doc/zmq_term.txt: -------------------------------------------------------------------------------- 1 | zmq_term(3) 2 | =========== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_term - terminate 0MQ context 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_term (void '*context');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_term()_ function shall terminate the 0MQ context 'context'. 18 | 19 | Context termination is performed in the following steps: 20 | 21 | 1. Any blocking operations currently in progress on sockets open within 22 | 'context' shall return immediately with an error code of ETERM. With the 23 | exception of _zmq_close()_, any further operations on sockets open within 24 | 'context' shall fail with an error code of ETERM. 25 | 26 | 2. After interrupting all blocking calls, _zmq_term()_ shall _block_ until the 27 | following conditions are satisfied: 28 | + 29 | * All sockets open within 'context' have been closed with _zmq_close()_. 30 | 31 | * For each socket within 'context', all messages sent by the application 32 | with _zmq_send()_ have either been physically transferred to a network 33 | peer, or the socket's linger period set with the _ZMQ_LINGER_ socket 34 | option has expired. 35 | 36 | For further details regarding socket linger behaviour refer to the _ZMQ_LINGER_ 37 | option in linkzmq:zmq_setsockopt[3]. 38 | 39 | 40 | RETURN VALUE 41 | ------------ 42 | The _zmq_term()_ function shall return zero if successful. Otherwise it shall 43 | return `-1` and set 'errno' to one of the values defined below. 44 | 45 | 46 | ERRORS 47 | ------ 48 | *EFAULT*:: 49 | The provided 'context' was invalid. 50 | *EINTR*:: 51 | Termination was interrupted by a signal. It can be restarted if needed. 52 | 53 | 54 | SEE ALSO 55 | -------- 56 | linkzmq:zmq[7] 57 | linkzmq:zmq_init[3] 58 | linkzmq:zmq_close[3] 59 | linkzmq:zmq_setsockopt[3] 60 | 61 | 62 | AUTHORS 63 | ------- 64 | This manual page was written by the 0MQ community. 65 | -------------------------------------------------------------------------------- /src/rep.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_REP_HPP_INCLUDED__ 22 | #define __ZMQ_REP_HPP_INCLUDED__ 23 | 24 | #include "xrep.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | class rep_t : public xrep_t 30 | { 31 | public: 32 | 33 | rep_t (class ctx_t *parent_, uint32_t tid_); 34 | ~rep_t (); 35 | 36 | // Overloads of functions from socket_base_t. 37 | int xsend (zmq_msg_t *msg_, int flags_); 38 | int xrecv (zmq_msg_t *msg_, int flags_); 39 | bool xhas_in (); 40 | bool xhas_out (); 41 | 42 | private: 43 | 44 | // If true, we are in process of sending the reply. If false we are 45 | // in process of receiving a request. 46 | bool sending_reply; 47 | 48 | // If true, we are starting to receive a request. The beginning 49 | // of the request is the backtrace stack. 50 | bool request_begins; 51 | 52 | rep_t (const rep_t&); 53 | const rep_t &operator = (const rep_t&); 54 | 55 | }; 56 | 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/i_engine.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_I_ENGINE_HPP_INCLUDED__ 22 | #define __ZMQ_I_ENGINE_HPP_INCLUDED__ 23 | 24 | namespace zmq 25 | { 26 | 27 | struct i_engine 28 | { 29 | virtual ~i_engine () {} 30 | 31 | // Plug the engine to the session. 32 | virtual void plug (class io_thread_t *io_thread_, 33 | struct i_inout *inout_) = 0; 34 | 35 | // Unplug the engine from the session. 36 | virtual void unplug () = 0; 37 | 38 | // Terminate and deallocate the engine. Note that 'detached' 39 | // events are not fired on termination. 40 | virtual void terminate () = 0; 41 | 42 | // This method is called by the session to signalise that more 43 | // messages can be written to the pipe. 44 | virtual void activate_in () = 0; 45 | 46 | // This method is called by the session to signalise that there 47 | // are messages to send available. 48 | virtual void activate_out () = 0; 49 | }; 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/clock.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_CLOCK_HPP_INCLUDED__ 22 | #define __ZMQ_CLOCK_HPP_INCLUDED__ 23 | 24 | #include "stdint.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | class clock_t 30 | { 31 | public: 32 | 33 | clock_t (); 34 | ~clock_t (); 35 | 36 | // CPU's timestamp counter. Returns 0 if it's not available. 37 | static uint64_t rdtsc (); 38 | 39 | // High precision timestamp. 40 | static uint64_t now_us (); 41 | 42 | // Low precision timestamp. In tight loops generating it can be 43 | // 10 to 100 times faster than the high precision timestamp. 44 | uint64_t now_ms (); 45 | 46 | private: 47 | 48 | // TSC timestamp of when last time measurement was made. 49 | uint64_t last_tsc; 50 | 51 | // Physical time corresponding to the TSC above (in milliseconds). 52 | uint64_t last_time; 53 | 54 | clock_t (const clock_t&); 55 | const clock_t &operator = (const clock_t&); 56 | }; 57 | 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/req.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_REQ_HPP_INCLUDED__ 22 | #define __ZMQ_REQ_HPP_INCLUDED__ 23 | 24 | #include "xreq.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | class req_t : public xreq_t 30 | { 31 | public: 32 | 33 | req_t (class ctx_t *parent_, uint32_t tid_); 34 | ~req_t (); 35 | 36 | // Overloads of functions from socket_base_t. 37 | int xsend (zmq_msg_t *msg_, int flags_); 38 | int xrecv (zmq_msg_t *msg_, int flags_); 39 | bool xhas_in (); 40 | bool xhas_out (); 41 | 42 | private: 43 | 44 | // If true, request was already sent and reply wasn't received yet or 45 | // was raceived partially. 46 | bool receiving_reply; 47 | 48 | // If true, we are starting to send/recv a message. The first part 49 | // of the message must be empty message part (backtrace stack bottom). 50 | bool message_begins; 51 | 52 | req_t (const req_t&); 53 | const req_t &operator = (const req_t&); 54 | }; 55 | 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/named_session.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_NAMED_SESSION_HPP_INCLUDED__ 22 | #define __ZMQ_NAMED_SESSION_HPP_INCLUDED__ 23 | 24 | #include "session.hpp" 25 | #include "blob.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | // Named session is created by listener object when the peer identifies 31 | // itself by a strong name. Named session survives reconnections. 32 | 33 | class named_session_t : public session_t 34 | { 35 | public: 36 | 37 | named_session_t (class io_thread_t *io_thread_, 38 | class socket_base_t *socket_, const options_t &options_, 39 | const blob_t &name_); 40 | ~named_session_t (); 41 | 42 | // Handlers for events from session base class. 43 | void attached (const blob_t &peer_identity_); 44 | void detached (); 45 | 46 | private: 47 | 48 | // Name of the session. Corresponds to the peer's strong identity. 49 | blob_t name; 50 | 51 | named_session_t (const named_session_t&); 52 | const named_session_t &operator = (const named_session_t&); 53 | }; 54 | 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/xpub.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_XPUB_HPP_INCLUDED__ 22 | #define __ZMQ_XPUB_HPP_INCLUDED__ 23 | 24 | #include "socket_base.hpp" 25 | #include "array.hpp" 26 | #include "pipe.hpp" 27 | #include "dist.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class xpub_t : public socket_base_t 33 | { 34 | public: 35 | 36 | xpub_t (class ctx_t *parent_, uint32_t tid_); 37 | ~xpub_t (); 38 | 39 | // Implementations of virtual functions from socket_base_t. 40 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 41 | const blob_t &peer_identity_); 42 | int xsend (zmq_msg_t *msg_, int flags_); 43 | bool xhas_out (); 44 | int xrecv (zmq_msg_t *msg_, int flags_); 45 | bool xhas_in (); 46 | 47 | private: 48 | 49 | // Hook into the termination process. 50 | void process_term (int linger_); 51 | 52 | // Distributor of messages holding the list of outbound pipes. 53 | dist_t dist; 54 | 55 | xpub_t (const xpub_t&); 56 | const xpub_t &operator = (const xpub_t&); 57 | }; 58 | 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/xreq.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | 6 | This file is part of 0MQ. 7 | 8 | 0MQ is free software; you can redistribute it and/or modify it under 9 | the terms of the GNU Lesser General Public License as published by 10 | the Free Software Foundation; either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | 0MQ is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General Public License 19 | along with this program. If not, see . 20 | */ 21 | 22 | #ifndef __ZMQ_XREQ_HPP_INCLUDED__ 23 | #define __ZMQ_XREQ_HPP_INCLUDED__ 24 | 25 | #include "socket_base.hpp" 26 | #include "fq.hpp" 27 | #include "lb.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class xreq_t : public socket_base_t 33 | { 34 | public: 35 | 36 | xreq_t (class ctx_t *parent_, uint32_t tid_); 37 | ~xreq_t (); 38 | 39 | protected: 40 | 41 | // Overloads of functions from socket_base_t. 42 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 43 | const blob_t &peer_identity_); 44 | int xsend (zmq_msg_t *msg_, int flags_); 45 | int xrecv (zmq_msg_t *msg_, int flags_); 46 | bool xhas_in (); 47 | bool xhas_out (); 48 | 49 | private: 50 | 51 | // Hook into the termination process. 52 | void process_term (int linger_); 53 | 54 | // Messages are fair-queued from inbound pipes. And load-balanced to 55 | // the outbound pipes. 56 | fq_t fq; 57 | lb_t lb; 58 | 59 | xreq_t (const xreq_t&); 60 | const xreq_t &operator = (const xreq_t&); 61 | }; 62 | 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/signaler.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_SIGNALER_HPP_INCLUDED__ 22 | #define __ZMQ_SIGNALER_HPP_INCLUDED__ 23 | 24 | #include "fd.hpp" 25 | 26 | namespace zmq 27 | { 28 | 29 | // This is a cross-platform equivalent to signal_fd. However, as opposed 30 | // to signal_fd there can be at most one signal in the signaler at any 31 | // given moment. Attempt to send a signal before receiving the previous 32 | // one will result in undefined behaviour. 33 | 34 | class signaler_t 35 | { 36 | public: 37 | 38 | signaler_t (); 39 | ~signaler_t (); 40 | 41 | fd_t get_fd (); 42 | void send (); 43 | int wait (int timeout_); 44 | void recv (); 45 | 46 | private: 47 | 48 | // Creates a pair of filedescriptors that will be used 49 | // to pass the signals. 50 | static int make_fdpair (fd_t *r_, fd_t *w_); 51 | 52 | // Write & read end of the socketpair. 53 | fd_t w; 54 | fd_t r; 55 | 56 | // Disable copying of signaler_t object. 57 | signaler_t (const signaler_t&); 58 | const signaler_t &operator = (const signaler_t&); 59 | }; 60 | 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/xreq.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | 23 | #include "xreq.hpp" 24 | #include "err.hpp" 25 | 26 | zmq::xreq_t::xreq_t (class ctx_t *parent_, uint32_t tid_) : 27 | socket_base_t (parent_, tid_), 28 | fq (this), 29 | lb (this) 30 | { 31 | options.type = ZMQ_XREQ; 32 | options.requires_in = true; 33 | options.requires_out = true; 34 | } 35 | 36 | zmq::xreq_t::~xreq_t () 37 | { 38 | } 39 | 40 | void zmq::xreq_t::xattach_pipes (class reader_t *inpipe_, 41 | class writer_t *outpipe_, const blob_t &peer_identity_) 42 | { 43 | zmq_assert (inpipe_ && outpipe_); 44 | fq.attach (inpipe_); 45 | lb.attach (outpipe_); 46 | } 47 | 48 | void zmq::xreq_t::process_term (int linger_) 49 | { 50 | fq.terminate (); 51 | lb.terminate (); 52 | socket_base_t::process_term (linger_); 53 | } 54 | 55 | int zmq::xreq_t::xsend (zmq_msg_t *msg_, int flags_) 56 | { 57 | return lb.send (msg_, flags_); 58 | } 59 | 60 | int zmq::xreq_t::xrecv (zmq_msg_t *msg_, int flags_) 61 | { 62 | return fq.recv (msg_, flags_); 63 | } 64 | 65 | bool zmq::xreq_t::xhas_in () 66 | { 67 | return fq.has_in (); 68 | } 69 | 70 | bool zmq::xreq_t::xhas_out () 71 | { 72 | return lb.has_out (); 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/xpub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | 23 | #include "xpub.hpp" 24 | #include "err.hpp" 25 | #include "pipe.hpp" 26 | 27 | zmq::xpub_t::xpub_t (class ctx_t *parent_, uint32_t tid_) : 28 | socket_base_t (parent_, tid_), 29 | dist (this) 30 | { 31 | options.type = ZMQ_XPUB; 32 | options.requires_in = false; 33 | options.requires_out = true; 34 | } 35 | 36 | zmq::xpub_t::~xpub_t () 37 | { 38 | } 39 | 40 | void zmq::xpub_t::xattach_pipes (class reader_t *inpipe_, 41 | class writer_t *outpipe_, const blob_t &peer_identity_) 42 | { 43 | zmq_assert (!inpipe_ && outpipe_); 44 | dist.attach (outpipe_); 45 | } 46 | 47 | void zmq::xpub_t::process_term (int linger_) 48 | { 49 | // Terminate the outbound pipes. 50 | dist.terminate (); 51 | 52 | // Continue with the termination immediately. 53 | socket_base_t::process_term (linger_); 54 | } 55 | 56 | int zmq::xpub_t::xsend (zmq_msg_t *msg_, int flags_) 57 | { 58 | return dist.send (msg_, flags_); 59 | } 60 | 61 | bool zmq::xpub_t::xhas_out () 62 | { 63 | return dist.has_out (); 64 | } 65 | 66 | int zmq::xpub_t::xrecv (zmq_msg_t *msg_, int flags_) 67 | { 68 | errno = EAGAIN; 69 | return -1; 70 | } 71 | 72 | bool zmq::xpub_t::xhas_in () 73 | { 74 | return false; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/connect_session.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_CONNECT_SESSION_HPP_INCLUDED__ 22 | #define __ZMQ_CONNECT_SESSION_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "session.hpp" 27 | 28 | namespace zmq 29 | { 30 | 31 | // Connect session contains an address to connect to. On disconnect it 32 | // attempts to reconnect. 33 | 34 | class connect_session_t : public session_t 35 | { 36 | public: 37 | 38 | connect_session_t (class io_thread_t *io_thread_, 39 | class socket_base_t *socket_, const options_t &options_, 40 | const char *protocol_, const char *address_); 41 | ~connect_session_t (); 42 | 43 | private: 44 | 45 | // Handlers for events from session base class. 46 | void attached (const blob_t &peer_identity_); 47 | void detached (); 48 | 49 | // Start the connection process. 50 | void start_connecting (bool wait_); 51 | 52 | // Command handlers. 53 | void process_plug (); 54 | 55 | // Address to connect to. 56 | std::string protocol; 57 | std::string address; 58 | 59 | connect_session_t (const connect_session_t&); 60 | const connect_session_t &operator = (const connect_session_t&); 61 | }; 62 | 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/zmq_listener.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_ZMQ_LISTENER_HPP_INCLUDED__ 22 | #define __ZMQ_ZMQ_LISTENER_HPP_INCLUDED__ 23 | 24 | #include "own.hpp" 25 | #include "io_object.hpp" 26 | #include "tcp_listener.hpp" 27 | #include "stdint.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class zmq_listener_t : public own_t, public io_object_t 33 | { 34 | public: 35 | 36 | zmq_listener_t (class io_thread_t *io_thread_, 37 | class socket_base_t *socket_, const options_t &options_); 38 | ~zmq_listener_t (); 39 | 40 | // Set address to listen on. 41 | int set_address (const char* protocol_, const char *addr_); 42 | 43 | private: 44 | 45 | // Handlers for incoming commands. 46 | void process_plug (); 47 | void process_term (int linger_); 48 | 49 | // Handlers for I/O events. 50 | void in_event (); 51 | 52 | // Actual listening socket. 53 | tcp_listener_t tcp_listener; 54 | 55 | // Handle corresponding to the listening socket. 56 | handle_t handle; 57 | 58 | // Socket the listerner belongs to. 59 | class socket_base_t *socket; 60 | 61 | zmq_listener_t (const zmq_listener_t&); 62 | const zmq_listener_t &operator = (const zmq_listener_t&); 63 | }; 64 | 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /tests/test_hwm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "testutil.hpp" 22 | #include "../src/stdint.hpp" 23 | 24 | using namespace zmqtestutil; 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | uint64_t hwm = 5; 29 | int linger = 0; 30 | bool status; 31 | 32 | zmq::context_t context (1); 33 | zmq::socket_t s1 (context, ZMQ_PULL); 34 | zmq::socket_t s2 (context, ZMQ_PUSH); 35 | 36 | s2.setsockopt (ZMQ_LINGER, &linger, sizeof (int)); 37 | s2.setsockopt (ZMQ_HWM, &hwm, sizeof (uint64_t)); 38 | 39 | s1.bind ("tcp://127.0.0.1:5858"); 40 | s2.connect ("tcp://127.0.0.1:5858"); 41 | 42 | for (int i = 0; i < 10; i++) 43 | { 44 | zmq::message_t msg (sizeof ("test") - 1); 45 | memcpy (msg.data (), "test", sizeof ("test") - 1); 46 | 47 | status = s2.send (msg, ZMQ_NOBLOCK); 48 | 49 | // Anything below HWM should be status 50 | if (i < 5) { 51 | assert (status); 52 | } else { 53 | assert (!status && errno == EAGAIN); 54 | } 55 | } 56 | 57 | // There should be now 5 messages pending, consume one 58 | zmq::message_t msg; 59 | 60 | status = s1.recv (&msg, 0); 61 | assert (status); 62 | 63 | // Now it should be possible to send one more 64 | status = s2.send (msg, 0); 65 | assert (status); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /src/sub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | 23 | #include "sub.hpp" 24 | 25 | zmq::sub_t::sub_t (class ctx_t *parent_, uint32_t tid_) : 26 | xsub_t (parent_, tid_) 27 | { 28 | options.type = ZMQ_SUB; 29 | } 30 | 31 | zmq::sub_t::~sub_t () 32 | { 33 | } 34 | 35 | int zmq::sub_t::xsetsockopt (int option_, const void *optval_, 36 | size_t optvallen_) 37 | { 38 | if (option_ != ZMQ_SUBSCRIBE && option_ != ZMQ_UNSUBSCRIBE) { 39 | errno = EINVAL; 40 | return -1; 41 | } 42 | 43 | // Create the subscription message. 44 | zmq_msg_t msg; 45 | zmq_msg_init_size (&msg, optvallen_ + 1); 46 | unsigned char *data = (unsigned char*) zmq_msg_data (&msg); 47 | if (option_ == ZMQ_SUBSCRIBE) 48 | *data = 1; 49 | else if (option_ == ZMQ_UNSUBSCRIBE) 50 | *data = 0; 51 | memcpy (data + 1, optval_, optvallen_); 52 | 53 | // Pass it further on in the stack. 54 | int err = 0; 55 | int rc = xsub_t::xsend (&msg, 0); 56 | if (rc != 0) 57 | err = errno; 58 | zmq_msg_close (&msg); 59 | if (rc != 0) 60 | errno = err; 61 | return rc; 62 | } 63 | 64 | int zmq::sub_t::xsend (zmq_msg_t *msg_, int options_) 65 | { 66 | // Overload the XSUB's send. 67 | errno = ENOTSUP; 68 | return -1; 69 | } 70 | 71 | bool zmq::sub_t::xhas_out () 72 | { 73 | // Overload the XSUB's send. 74 | return false; 75 | } 76 | -------------------------------------------------------------------------------- /doc/zmq_msg_init_data.txt: -------------------------------------------------------------------------------- 1 | zmq_msg_init_data(3) 2 | ==================== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_msg_init_data - initialise 0MQ message from a supplied buffer 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *typedef void (zmq_free_fn) (void '*data', void '*hint');* 13 | 14 | *int zmq_msg_init_data (zmq_msg_t '*msg', void '*data', size_t 'size', zmq_free_fn '*ffn', void '*hint');* 15 | 16 | 17 | DESCRIPTION 18 | ----------- 19 | The _zmq_msg_init_data()_ function shall initialise the message object 20 | referenced by 'msg' to represent the content referenced by the buffer located 21 | at address 'data', 'size' bytes long. No copy of 'data' shall be performed and 22 | 0MQ shall take ownership of the supplied buffer. 23 | 24 | If provided, the deallocation function 'ffn' shall be called once the data 25 | buffer is no longer required by 0MQ, with the 'data' and 'hint' arguments 26 | supplied to _zmq_msg_init_data()_. 27 | 28 | CAUTION: Never access 'zmq_msg_t' members directly, instead always use the 29 | _zmq_msg_ family of functions. 30 | 31 | CAUTION: The deallocation function 'ffn' needs to be thread-safe, since it 32 | will be called from an arbitrary thread. 33 | 34 | CAUTION: The functions _zmq_msg_init()_, _zmq_msg_init_data()_ and 35 | _zmq_msg_init_size()_ are mutually exclusive. Never initialize the same 36 | 'zmq_msg_t' twice. 37 | 38 | 39 | RETURN VALUE 40 | ------------ 41 | The _zmq_msg_init_data()_ function shall return zero if successful. Otherwise 42 | it shall return `-1` and set 'errno' to one of the values defined below. 43 | 44 | 45 | ERRORS 46 | ------ 47 | No errors are defined. 48 | 49 | 50 | EXAMPLE 51 | ------- 52 | .Initialising a message from a supplied buffer 53 | ---- 54 | void my_free (void *data, void *hint) 55 | { 56 | free (data); 57 | } 58 | 59 | /* ... */ 60 | 61 | void *data = malloc (6); 62 | assert (data); 63 | memcpy (data, "ABCDEF", 6); 64 | zmq_msg_t msg; 65 | rc = zmq_msg_init_data (&msg, data, 6, my_free, NULL); 66 | assert (rc == 0); 67 | ---- 68 | 69 | 70 | SEE ALSO 71 | -------- 72 | linkzmq:zmq_msg_init_size[3] 73 | linkzmq:zmq_msg_init[3] 74 | linkzmq:zmq_msg_close[3] 75 | linkzmq:zmq_msg_data[3] 76 | linkzmq:zmq_msg_size[3] 77 | linkzmq:zmq[7] 78 | 79 | 80 | AUTHORS 81 | ------- 82 | This manual page was written by the 0MQ community. 83 | -------------------------------------------------------------------------------- /src/poller.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_POLLER_HPP_INCLUDED__ 22 | #define __ZMQ_POLLER_HPP_INCLUDED__ 23 | 24 | #include "epoll.hpp" 25 | #include "poll.hpp" 26 | #include "select.hpp" 27 | #include "devpoll.hpp" 28 | #include "kqueue.hpp" 29 | 30 | namespace zmq 31 | { 32 | 33 | #if defined ZMQ_FORCE_SELECT 34 | typedef select_t poller_t; 35 | #elif defined ZMQ_FORCE_POLL 36 | typedef poll_t poller_t; 37 | #elif defined ZMQ_FORCE_EPOLL 38 | typedef epoll_t poller_t; 39 | #elif defined ZMQ_FORCE_DEVPOLL 40 | typedef devpoll_t poller_t; 41 | #elif defined ZMQ_FORCE_KQUEUE 42 | typedef kqueue_t poller_t; 43 | #elif defined ZMQ_HAVE_LINUX 44 | typedef epoll_t poller_t; 45 | #elif defined ZMQ_HAVE_WINDOWS 46 | typedef select_t poller_t; 47 | #elif defined ZMQ_HAVE_FREEBSD 48 | typedef kqueue_t poller_t; 49 | #elif defined ZMQ_HAVE_OPENBSD 50 | typedef kqueue_t poller_t; 51 | #elif defined ZMQ_HAVE_NETBSD 52 | typedef kqueue_t poller_t; 53 | #elif defined ZMQ_HAVE_SOLARIS 54 | typedef devpoll_t poller_t; 55 | #elif defined ZMQ_HAVE_OSX 56 | typedef kqueue_t poller_t; 57 | #elif defined ZMQ_HAVE_QNXNTO 58 | typedef poll_t poller_t; 59 | #elif defined ZMQ_HAVE_AIX 60 | typedef poll_t poller_t; 61 | #elif defined ZMQ_HAVE_HPUX 62 | typedef devpoll_t poller_t; 63 | #elif defined ZMQ_HAVE_OPENVMS 64 | typedef select_t poller_t; 65 | #elif defined ZMQ_HAVE_CYGWIN 66 | typedef select_t poller_t; 67 | #else 68 | #error Unsupported platform 69 | #endif 70 | 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/tcp_listener.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_TCP_LISTENER_HPP_INCLUDED__ 22 | #define __ZMQ_TCP_LISTENER_HPP_INCLUDED__ 23 | 24 | #include "fd.hpp" 25 | #include "ip.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | // The class encapsulating simple TCP listening socket. 31 | 32 | class tcp_listener_t 33 | { 34 | public: 35 | 36 | tcp_listener_t (); 37 | ~tcp_listener_t (); 38 | 39 | // Start listening on the interface. 40 | int set_address (const char *protocol_, const char *addr_, 41 | int backlog_); 42 | 43 | // Close the listening socket. 44 | int close (); 45 | 46 | // Get the file descriptor to poll on to get notified about 47 | // newly created connections. 48 | fd_t get_fd (); 49 | 50 | // Accept the new connection. Returns the file descriptor of the 51 | // newly created connection. The function may return retired_fd 52 | // if the connection was dropped while waiting in the listen backlog. 53 | fd_t accept (); 54 | 55 | private: 56 | 57 | // Address to listen on. 58 | sockaddr_storage addr; 59 | socklen_t addr_len; 60 | 61 | // True, if the undelying file for UNIX domain socket exists. 62 | bool has_file; 63 | 64 | // Underlying socket. 65 | fd_t s; 66 | 67 | tcp_listener_t (const tcp_listener_t&); 68 | const tcp_listener_t &operator = (const tcp_listener_t&); 69 | }; 70 | 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /include/zmq_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_UTILS_H_INCLUDED__ 22 | #define __ZMQ_UTILS_H_INCLUDED__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Handle DSO symbol visibility */ 29 | #if defined _WIN32 30 | # if defined DLL_EXPORT 31 | # define ZMQ_EXPORT __declspec(dllexport) 32 | # else 33 | # define ZMQ_EXPORT __declspec(dllimport) 34 | # endif 35 | #else 36 | # if defined __SUNPRO_C || defined __SUNPRO_CC 37 | # define ZMQ_EXPORT __global 38 | # elif (defined __GNUC__ && __GNUC__ >= 4) || defined __INTEL_COMPILER 39 | # define ZMQ_EXPORT __attribute__ ((visibility("default"))) 40 | # else 41 | # define ZMQ_EXPORT 42 | # endif 43 | #endif 44 | 45 | /* Helper functions are used by perf tests so that they don't have to care */ 46 | /* about minutiae of time-related functions on different OS platforms. */ 47 | 48 | /* Starts the stopwatch. Returns the handle to the watch. */ 49 | ZMQ_EXPORT void *zmq_stopwatch_start (void); 50 | 51 | /* Stops the stopwatch. Returns the number of microseconds elapsed since */ 52 | /* the stopwatch was started. */ 53 | ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_); 54 | 55 | /* Sleeps for specified number of seconds. */ 56 | ZMQ_EXPORT void zmq_sleep (int seconds_); 57 | 58 | #undef ZMQ_EXPORT 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /doc/zmq_ipc.txt: -------------------------------------------------------------------------------- 1 | zmq_ipc(7) 2 | ========== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_ipc - 0MQ local inter-process communication transport 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | The inter-process transport passes messages between local processes using a 13 | system-dependent IPC mechanism. 14 | 15 | NOTE: The inter-process transport is currently only implemented on operating 16 | systems that provide UNIX domain sockets. 17 | 18 | 19 | ADDRESSING 20 | ---------- 21 | A 0MQ address string consists of two parts as follows: 22 | 'transport'`://`'endpoint'. The 'transport' part specifies the underlying 23 | transport protocol to use, and for the inter-process transport shall be set to 24 | `ipc`. The meaning of the 'endpoint' part for the inter-process transport is 25 | defined below. 26 | 27 | 28 | Assigning a local address to a socket 29 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30 | When assigning a local address to a 'socket' using _zmq_bind()_ with the 'ipc' 31 | transport, the 'endpoint' shall be interpreted as an arbitrary string 32 | identifying the 'pathname' to create. The 'pathname' must be unique within the 33 | operating system namespace used by the 'ipc' implementation, and must fulfill 34 | any restrictions placed by the operating system on the format and length of a 35 | 'pathname'. 36 | 37 | Connecting a socket 38 | ~~~~~~~~~~~~~~~~~~~ 39 | When connecting a 'socket' to a peer address using _zmq_connect()_ with the 40 | 'ipc' transport, the 'endpoint' shall be interpreted as an arbitrary string 41 | identifying the 'pathname' to connect to. The 'pathname' must have been 42 | previously created within the operating system namespace by assigning it to a 43 | 'socket' with _zmq_bind()_. 44 | 45 | 46 | WIRE FORMAT 47 | ----------- 48 | Not applicable. 49 | 50 | 51 | EXAMPLES 52 | -------- 53 | .Assigning a local address to a socket 54 | ---- 55 | /* Assign the pathname "/tmp/feeds/0" */ 56 | rc = zmq_bind(socket, "ipc:///tmp/feeds/0"); 57 | assert (rc == 0); 58 | ---- 59 | 60 | .Connecting a socket 61 | ---- 62 | /* Connect to the pathname "/tmp/feeds/0" */ 63 | rc = zmq_connect(socket, "ipc:///tmp/feeds/0"); 64 | assert (rc == 0); 65 | ---- 66 | 67 | SEE ALSO 68 | -------- 69 | linkzmq:zmq_bind[3] 70 | linkzmq:zmq_connect[3] 71 | linkzmq:zmq_inproc[7] 72 | linkzmq:zmq_tcp[7] 73 | linkzmq:zmq_pgm[7] 74 | linkzmq:zmq[7] 75 | 76 | 77 | AUTHORS 78 | ------- 79 | This manual page was written by the 0MQ community. 80 | -------------------------------------------------------------------------------- /src/reaper.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_REAPER_HPP_INCLUDED__ 22 | #define __ZMQ_REAPER_HPP_INCLUDED__ 23 | 24 | #include "object.hpp" 25 | #include "mailbox.hpp" 26 | #include "poller.hpp" 27 | #include "i_poll_events.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class reaper_t : public object_t, public i_poll_events 33 | { 34 | public: 35 | 36 | reaper_t (class ctx_t *ctx_, uint32_t tid_); 37 | ~reaper_t (); 38 | 39 | mailbox_t *get_mailbox (); 40 | 41 | void start (); 42 | void stop (); 43 | 44 | // i_poll_events implementation. 45 | void in_event (); 46 | void out_event (); 47 | void timer_event (int id_); 48 | 49 | private: 50 | 51 | // Command handlers. 52 | void process_stop (); 53 | void process_reap (class socket_base_t *socket_); 54 | void process_reaped (); 55 | 56 | // Reaper thread accesses incoming commands via this mailbox. 57 | mailbox_t mailbox; 58 | 59 | // Handle associated with mailbox' file descriptor. 60 | poller_t::handle_t mailbox_handle; 61 | 62 | // I/O multiplexing is performed using a poller object. 63 | poller_t *poller; 64 | 65 | // Number of sockets being reaped at the moment. 66 | int sockets; 67 | 68 | // If true, we were already asked to terminate. 69 | bool terminating; 70 | 71 | reaper_t (const reaper_t&); 72 | const reaper_t &operator = (const reaper_t&); 73 | }; 74 | 75 | } 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/mailbox.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_MAILBOX_HPP_INCLUDED__ 22 | #define __ZMQ_MAILBOX_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "platform.hpp" 27 | #include "signaler.hpp" 28 | #include "fd.hpp" 29 | #include "config.hpp" 30 | #include "command.hpp" 31 | #include "ypipe.hpp" 32 | #include "mutex.hpp" 33 | 34 | namespace zmq 35 | { 36 | 37 | class mailbox_t 38 | { 39 | public: 40 | 41 | mailbox_t (); 42 | ~mailbox_t (); 43 | 44 | fd_t get_fd (); 45 | void send (const command_t &cmd_); 46 | int recv (command_t *cmd_, int timeout_); 47 | 48 | private: 49 | 50 | // The pipe to store actual commands. 51 | typedef ypipe_t cpipe_t; 52 | cpipe_t cpipe; 53 | 54 | // Signaler to pass signals from writer thread to reader thread. 55 | signaler_t signaler; 56 | 57 | // There's only one thread receiving from the mailbox, but there 58 | // is arbitrary number of threads sending. Given that ypipe requires 59 | // synchronised access on both of its endpoints, we have to synchronise 60 | // the sending side. 61 | mutex_t sync; 62 | 63 | // True if the underlying pipe is active, ie. when we are allowed to 64 | // read commands from it. 65 | bool active; 66 | 67 | // Disable copying of mailbox_t object. 68 | mailbox_t (const mailbox_t&); 69 | const mailbox_t &operator = (const mailbox_t&); 70 | }; 71 | 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/pair.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_PAIR_HPP_INCLUDED__ 22 | #define __ZMQ_PAIR_HPP_INCLUDED__ 23 | 24 | #include "socket_base.hpp" 25 | #include "pipe.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | class pair_t : 31 | public socket_base_t, 32 | public i_reader_events, 33 | public i_writer_events 34 | { 35 | public: 36 | 37 | pair_t (class ctx_t *parent_, uint32_t tid_); 38 | ~pair_t (); 39 | 40 | // Overloads of functions from socket_base_t. 41 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 42 | const blob_t &peer_identity_); 43 | int xsend (zmq_msg_t *msg_, int flags_); 44 | int xrecv (zmq_msg_t *msg_, int flags_); 45 | bool xhas_in (); 46 | bool xhas_out (); 47 | 48 | // i_reader_events interface implementation. 49 | void activated (class reader_t *pipe_); 50 | void terminated (class reader_t *pipe_); 51 | void delimited (class reader_t *pipe_); 52 | 53 | // i_writer_events interface implementation. 54 | void activated (class writer_t *pipe_); 55 | void terminated (class writer_t *pipe_); 56 | 57 | private: 58 | 59 | // Hook into termination process. 60 | void process_term (int linger_); 61 | 62 | class reader_t *inpipe; 63 | class writer_t *outpipe; 64 | 65 | bool inpipe_alive; 66 | bool outpipe_alive; 67 | 68 | bool terminating; 69 | 70 | pair_t (const pair_t&); 71 | const pair_t &operator = (const pair_t&); 72 | }; 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/ip.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_IP_HPP_INCLUDED__ 22 | #define __ZMQ_IP_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #ifdef ZMQ_HAVE_WINDOWS 27 | #include "windows.hpp" 28 | #else 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #endif 35 | 36 | #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS 37 | #include 38 | #endif 39 | 40 | // Some platforms (notably Darwin/OSX and NetBSD) do not define all AI_ 41 | // flags for getaddrinfo(). This can be worked around safely by defining 42 | // these to 0. 43 | #ifndef AI_ADDRCONFIG 44 | #define AI_ADDRCONFIG 0 45 | #endif 46 | #ifndef AI_NUMERICSERV 47 | #define AI_NUMERICSERV 0 48 | #endif 49 | 50 | namespace zmq 51 | { 52 | // Same as socket(2), but allows for transparent tweaking the options. 53 | int open_socket (int domain_, int type_, int protocol_); 54 | 55 | // Resolves network interface name in : format. Symbol "*" 56 | // (asterisk) resolves to INADDR_ANY (all network interfaces). 57 | int resolve_ip_interface (sockaddr_storage *addr_, socklen_t *addr_len_, 58 | char const *interface_); 59 | 60 | // This function resolves a string in : format. 61 | // Hostname can be either the name of the host or its IP address. 62 | int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_, 63 | const char *hostname_); 64 | 65 | // This function sets up address for UNIX domain transport. 66 | int resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_, 67 | const char* pathname_); 68 | } 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /src/tcp_socket.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_TCP_SOCKET_HPP_INCLUDED__ 22 | #define __ZMQ_TCP_SOCKET_HPP_INCLUDED__ 23 | 24 | #include "fd.hpp" 25 | #include "stdint.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | // The class encapsulating simple TCP read/write socket. 31 | 32 | class tcp_socket_t 33 | { 34 | public: 35 | 36 | tcp_socket_t (); 37 | ~tcp_socket_t (); 38 | 39 | // Associates a socket with a native socket descriptor. 40 | int open (fd_t fd_, uint64_t sndbuf_, uint64_t rcvbuf_); 41 | 42 | // Closes the underlying socket. 43 | int close (); 44 | 45 | // Returns the underlying socket. Returns retired_fd when the socket 46 | // is in the closed state. 47 | fd_t get_fd (); 48 | 49 | // Writes data to the socket. Returns the number of bytes actually 50 | // written (even zero is to be considered to be a success). In case 51 | // of error or orderly shutdown by the other peer -1 is returned. 52 | int write (const void *data, int size); 53 | 54 | // Reads data from the socket (up to 'size' bytes). Returns the number 55 | // of bytes actually read (even zero is to be considered to be 56 | // a success). In case of error or orderly shutdown by the other 57 | // peer -1 is returned. 58 | int read (void *data, int size); 59 | 60 | private: 61 | 62 | // Underlying socket. 63 | fd_t s; 64 | 65 | // Disable copy construction of tcp_socket. 66 | tcp_socket_t (const tcp_socket_t&); 67 | const tcp_socket_t &operator = (const tcp_socket_t&); 68 | }; 69 | 70 | } 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /tests/test_shutdown_stress.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | #include 23 | #include 24 | #include 25 | 26 | #define THREAD_COUNT 100 27 | 28 | extern "C" 29 | { 30 | static void *worker (void *s) 31 | { 32 | int rc; 33 | 34 | rc = zmq_connect (s, "tcp://127.0.0.1:5560"); 35 | assert (rc == 0); 36 | 37 | // Start closing the socket while the connecting process is underway. 38 | rc = zmq_close (s); 39 | assert (rc == 0); 40 | 41 | return NULL; 42 | } 43 | } 44 | 45 | int main (int argc, char *argv []) 46 | { 47 | void *ctx; 48 | void *s1; 49 | void *s2; 50 | int i; 51 | int j; 52 | int rc; 53 | pthread_t threads [THREAD_COUNT]; 54 | 55 | for (j = 0; j != 10; j++) { 56 | 57 | // Check the shutdown with many parallel I/O threads. 58 | ctx = zmq_init (7); 59 | assert (ctx); 60 | 61 | s1 = zmq_socket (ctx, ZMQ_REP); 62 | assert (s1); 63 | 64 | rc = zmq_bind (s1, "tcp://127.0.0.1:5560"); 65 | assert (rc == 0); 66 | 67 | for (i = 0; i != THREAD_COUNT; i++) { 68 | s2 = zmq_socket (ctx, ZMQ_SUB); 69 | assert (s2); 70 | rc = pthread_create (&threads [i], NULL, worker, s2); 71 | assert (rc == 0); 72 | } 73 | 74 | for (i = 0; i != THREAD_COUNT; i++) { 75 | rc = pthread_join (threads [i], NULL); 76 | assert (rc == 0); 77 | } 78 | 79 | rc = zmq_close (s1); 80 | assert (rc == 0); 81 | 82 | rc = zmq_term (ctx); 83 | assert (rc == 0); 84 | } 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /src/xsub.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_XSUB_HPP_INCLUDED__ 22 | #define __ZMQ_XSUB_HPP_INCLUDED__ 23 | 24 | #include "../include/zmq.h" 25 | 26 | #include "trie.hpp" 27 | #include "socket_base.hpp" 28 | #include "fq.hpp" 29 | 30 | namespace zmq 31 | { 32 | 33 | class xsub_t : public socket_base_t 34 | { 35 | public: 36 | 37 | xsub_t (class ctx_t *parent_, uint32_t tid_); 38 | ~xsub_t (); 39 | 40 | protected: 41 | 42 | // Overloads of functions from socket_base_t. 43 | void xattach_pipes (class reader_t *inpipe_, class writer_t *outpipe_, 44 | const blob_t &peer_identity_); 45 | int xsend (zmq_msg_t *msg_, int options_); 46 | bool xhas_out (); 47 | int xrecv (zmq_msg_t *msg_, int flags_); 48 | bool xhas_in (); 49 | 50 | private: 51 | 52 | // Hook into the termination process. 53 | void process_term (int linger_); 54 | 55 | // Check whether the message matches at least one subscription. 56 | bool match (zmq_msg_t *msg_); 57 | 58 | // Fair queueing object for inbound pipes. 59 | fq_t fq; 60 | 61 | // The repository of subscriptions. 62 | trie_t subscriptions; 63 | 64 | // If true, 'message' contains a matching message to return on the 65 | // next recv call. 66 | bool has_message; 67 | zmq_msg_t message; 68 | 69 | // If true, part of a multipart message was already received, but 70 | // there are following parts still waiting. 71 | bool more; 72 | 73 | xsub_t (const xsub_t&); 74 | const xsub_t &operator = (const xsub_t&); 75 | }; 76 | 77 | } 78 | 79 | #endif 80 | 81 | -------------------------------------------------------------------------------- /src/lb.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_LB_HPP_INCLUDED__ 22 | #define __ZMQ_LB_HPP_INCLUDED__ 23 | 24 | #include "array.hpp" 25 | #include "pipe.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | // Class manages a set of outbound pipes. On send it load balances 31 | // messages fairly among the pipes. 32 | class lb_t : public i_writer_events 33 | { 34 | public: 35 | 36 | lb_t (class own_t *sink_); 37 | ~lb_t (); 38 | 39 | void attach (writer_t *pipe_); 40 | void terminate (); 41 | int send (zmq_msg_t *msg_, int flags_); 42 | bool has_out (); 43 | 44 | // i_writer_events interface implementation. 45 | void activated (writer_t *pipe_); 46 | void terminated (writer_t *pipe_); 47 | 48 | private: 49 | 50 | // List of outbound pipes. 51 | typedef array_t pipes_t; 52 | pipes_t pipes; 53 | 54 | // Number of active pipes. All the active pipes are located at the 55 | // beginning of the pipes array. 56 | pipes_t::size_type active; 57 | 58 | // Points to the last pipe that the most recent message was sent to. 59 | pipes_t::size_type current; 60 | 61 | // True if last we are in the middle of a multipart message. 62 | bool more; 63 | 64 | // True if we are dropping current message. 65 | bool dropping; 66 | 67 | // Object to send events to. 68 | class own_t *sink; 69 | 70 | // If true, termination process is already underway. 71 | bool terminating; 72 | 73 | lb_t (const lb_t&); 74 | const lb_t &operator = (const lb_t&); 75 | }; 76 | 77 | } 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/thread.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_THREAD_HPP_INCLUDED__ 22 | #define __ZMQ_THREAD_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #ifdef ZMQ_HAVE_WINDOWS 27 | #include "windows.hpp" 28 | #else 29 | #include 30 | #endif 31 | 32 | namespace zmq 33 | { 34 | 35 | typedef void (thread_fn) (void*); 36 | 37 | // Class encapsulating OS thread. Thread initiation/termination is done 38 | // using special functions rather than in constructor/destructor so that 39 | // thread isn't created during object construction by accident, causing 40 | // newly created thread to access half-initialised object. Same applies 41 | // to the destruction process: Thread should be terminated before object 42 | // destruction begins, otherwise it can access half-destructed object. 43 | 44 | class thread_t 45 | { 46 | public: 47 | 48 | inline thread_t () 49 | { 50 | } 51 | 52 | // Creates OS thread. 'tfn' is main thread function. It'll be passed 53 | // 'arg' as an argument. 54 | void start (thread_fn *tfn_, void *arg_); 55 | 56 | // Waits for thread termination. 57 | void stop (); 58 | 59 | // These are internal members. They should be private, however then 60 | // they would not be accessible from the main C routine of the thread. 61 | thread_fn *tfn; 62 | void *arg; 63 | 64 | private: 65 | 66 | #ifdef ZMQ_HAVE_WINDOWS 67 | HANDLE descriptor; 68 | #else 69 | pthread_t descriptor; 70 | #endif 71 | 72 | thread_t (const thread_t&); 73 | const thread_t &operator = (const thread_t&); 74 | }; 75 | 76 | } 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/tcp_connecter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_TCP_CONNECTER_HPP_INCLUDED__ 22 | #define __ZMQ_TCP_CONNECTER_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | #include "fd.hpp" 26 | 27 | #ifdef ZMQ_HAVE_WINDOWS 28 | #include "windows.hpp" 29 | #else 30 | #include 31 | #include 32 | #endif 33 | 34 | namespace zmq 35 | { 36 | 37 | // The class encapsulating simple TCP listening socket. 38 | 39 | class tcp_connecter_t 40 | { 41 | public: 42 | 43 | tcp_connecter_t (); 44 | ~tcp_connecter_t (); 45 | 46 | // Set address to connect to. 47 | int set_address (const char *protocol, const char *addr_); 48 | 49 | // Open TCP connecting socket. Address is in 50 | // : format. Returns -1 in case of error, 51 | // 0 if connect was successfull immediately and 1 if async connect 52 | // was launched. 53 | int open (); 54 | 55 | // Close the connecting socket. 56 | int close (); 57 | 58 | // Get the file descriptor to poll on to get notified about 59 | // connection success. 60 | fd_t get_fd (); 61 | 62 | // Get the file descriptor of newly created connection. Returns 63 | // retired_fd if the connection was unsuccessfull. 64 | fd_t connect (); 65 | 66 | private: 67 | 68 | // Address to connect to. 69 | sockaddr_storage addr; 70 | socklen_t addr_len; 71 | 72 | // Underlying socket. 73 | fd_t s; 74 | 75 | tcp_connecter_t (const tcp_connecter_t&); 76 | const tcp_connecter_t &operator = (const tcp_connecter_t&); 77 | }; 78 | 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/zmq_listener.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include "zmq_listener.hpp" 24 | #include "zmq_init.hpp" 25 | #include "io_thread.hpp" 26 | #include "err.hpp" 27 | 28 | zmq::zmq_listener_t::zmq_listener_t (io_thread_t *io_thread_, 29 | socket_base_t *socket_, const options_t &options_) : 30 | own_t (io_thread_, options_), 31 | io_object_t (io_thread_), 32 | socket (socket_) 33 | { 34 | } 35 | 36 | zmq::zmq_listener_t::~zmq_listener_t () 37 | { 38 | } 39 | 40 | int zmq::zmq_listener_t::set_address (const char *protocol_, const char *addr_) 41 | { 42 | return tcp_listener.set_address (protocol_, addr_, options.backlog); 43 | } 44 | 45 | void zmq::zmq_listener_t::process_plug () 46 | { 47 | // Start polling for incoming connections. 48 | handle = add_fd (tcp_listener.get_fd ()); 49 | set_pollin (handle); 50 | } 51 | 52 | void zmq::zmq_listener_t::process_term (int linger_) 53 | { 54 | rm_fd (handle); 55 | own_t::process_term (linger_); 56 | } 57 | 58 | void zmq::zmq_listener_t::in_event () 59 | { 60 | fd_t fd = tcp_listener.accept (); 61 | 62 | // If connection was reset by the peer in the meantime, just ignore it. 63 | // TODO: Handle specific errors like ENFILE/EMFILE etc. 64 | if (fd == retired_fd) 65 | return; 66 | 67 | // Choose I/O thread to run connecter in. Given that we are already 68 | // running in an I/O thread, there must be at least one available. 69 | io_thread_t *io_thread = choose_io_thread (options.affinity); 70 | zmq_assert (io_thread); 71 | 72 | // Create and launch an init object. 73 | zmq_init_t *init = new (std::nothrow) zmq_init_t (io_thread, socket, 74 | NULL, fd, options); 75 | alloc_assert (init); 76 | launch_child (init); 77 | } 78 | 79 | -------------------------------------------------------------------------------- /src/io_object.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_IO_OBJECT_HPP_INCLUDED__ 22 | #define __ZMQ_IO_OBJECT_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "stdint.hpp" 27 | #include "poller.hpp" 28 | #include "i_poll_events.hpp" 29 | 30 | namespace zmq 31 | { 32 | 33 | // Simple base class for objects that live in I/O threads. 34 | // It makes communication with the poller object easier and 35 | // makes defining unneeded event handlers unnecessary. 36 | 37 | class io_object_t : public i_poll_events 38 | { 39 | public: 40 | 41 | io_object_t (class io_thread_t *io_thread_ = NULL); 42 | ~io_object_t (); 43 | 44 | // When migrating an object from one I/O thread to another, first 45 | // unplug it, then migrate it, then plug it to the new thread. 46 | void plug (class io_thread_t *io_thread_); 47 | void unplug (); 48 | 49 | protected: 50 | 51 | typedef poller_t::handle_t handle_t; 52 | 53 | // Methods to access underlying poller object. 54 | handle_t add_fd (fd_t fd_); 55 | void rm_fd (handle_t handle_); 56 | void set_pollin (handle_t handle_); 57 | void reset_pollin (handle_t handle_); 58 | void set_pollout (handle_t handle_); 59 | void reset_pollout (handle_t handle_); 60 | void add_timer (int timout_, int id_); 61 | void cancel_timer (int id_); 62 | 63 | // i_poll_events interface implementation. 64 | void in_event (); 65 | void out_event (); 66 | void timer_event (int id_); 67 | 68 | private: 69 | 70 | poller_t *poller; 71 | 72 | io_object_t (const io_object_t&); 73 | const io_object_t &operator = (const io_object_t&); 74 | }; 75 | 76 | } 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/fq.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_FQ_HPP_INCLUDED__ 22 | #define __ZMQ_FQ_HPP_INCLUDED__ 23 | 24 | #include "array.hpp" 25 | #include "pipe.hpp" 26 | 27 | namespace zmq 28 | { 29 | 30 | // Class manages a set of inbound pipes. On receive it performs fair 31 | // queueing (RFC970) so that senders gone berserk won't cause denial of 32 | // service for decent senders. 33 | class fq_t : public i_reader_events 34 | { 35 | public: 36 | 37 | fq_t (class own_t *sink_); 38 | ~fq_t (); 39 | 40 | void attach (reader_t *pipe_); 41 | void terminate (); 42 | 43 | int recv (zmq_msg_t *msg_, int flags_); 44 | bool has_in (); 45 | 46 | // i_reader_events implementation. 47 | void activated (reader_t *pipe_); 48 | void terminated (reader_t *pipe_); 49 | void delimited (reader_t *pipe_); 50 | 51 | private: 52 | 53 | // Inbound pipes. 54 | typedef array_t pipes_t; 55 | pipes_t pipes; 56 | 57 | // Number of active pipes. All the active pipes are located at the 58 | // beginning of the pipes array. 59 | pipes_t::size_type active; 60 | 61 | // Index of the next bound pipe to read a message from. 62 | pipes_t::size_type current; 63 | 64 | // If true, part of a multipart message was already received, but 65 | // there are following parts still waiting in the current pipe. 66 | bool more; 67 | 68 | // Object to send events to. 69 | class own_t *sink; 70 | 71 | // If true, termination process is already underway. 72 | bool terminating; 73 | 74 | fq_t (const fq_t&); 75 | const fq_t &operator = (const fq_t&); 76 | }; 77 | 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/zmq_engine.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__ 22 | #define __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include "i_engine.hpp" 29 | #include "io_object.hpp" 30 | #include "tcp_socket.hpp" 31 | #include "encoder.hpp" 32 | #include "decoder.hpp" 33 | #include "options.hpp" 34 | 35 | namespace zmq 36 | { 37 | 38 | class zmq_engine_t : public io_object_t, public i_engine 39 | { 40 | public: 41 | 42 | zmq_engine_t (fd_t fd_, const options_t &options_); 43 | ~zmq_engine_t (); 44 | 45 | // i_engine interface implementation. 46 | void plug (class io_thread_t *io_thread_, struct i_inout *inout_); 47 | void unplug (); 48 | void terminate (); 49 | void activate_in (); 50 | void activate_out (); 51 | 52 | // i_poll_events interface implementation. 53 | void in_event (); 54 | void out_event (); 55 | 56 | private: 57 | 58 | // Function to handle network disconnections. 59 | void error (); 60 | 61 | tcp_socket_t tcp_socket; 62 | handle_t handle; 63 | 64 | unsigned char *inpos; 65 | size_t insize; 66 | decoder_t decoder; 67 | 68 | unsigned char *outpos; 69 | size_t outsize; 70 | encoder_t encoder; 71 | 72 | i_inout *inout; 73 | 74 | // Detached transient inout handler. 75 | i_inout *ephemeral_inout; 76 | 77 | options_t options; 78 | 79 | bool plugged; 80 | 81 | // True if input error has occurred. 82 | bool input_error; 83 | 84 | zmq_engine_t (const zmq_engine_t&); 85 | const zmq_engine_t &operator = (const zmq_engine_t&); 86 | }; 87 | 88 | } 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/mailbox.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "mailbox.hpp" 22 | #include "platform.hpp" 23 | #include "err.hpp" 24 | 25 | zmq::mailbox_t::mailbox_t () 26 | { 27 | // Get the pipe into passive state. That way, if the users starts by 28 | // polling on the associated file descriptor it will get woken up when 29 | // new command is posted. 30 | bool ok = cpipe.read (NULL); 31 | zmq_assert (!ok); 32 | active = false; 33 | } 34 | 35 | zmq::mailbox_t::~mailbox_t () 36 | { 37 | // TODO: Retrieve and deallocate commands inside the cpipe. 38 | 39 | // Work around problem that other threads might still be in our 40 | // send() method, by waiting on the mutex before disappearing. 41 | sync.lock (); 42 | sync.unlock (); 43 | } 44 | 45 | zmq::fd_t zmq::mailbox_t::get_fd () 46 | { 47 | return signaler.get_fd (); 48 | } 49 | 50 | void zmq::mailbox_t::send (const command_t &cmd_) 51 | { 52 | sync.lock (); 53 | cpipe.write (cmd_, false); 54 | bool ok = cpipe.flush (); 55 | sync.unlock (); 56 | if (!ok) 57 | signaler.send (); 58 | } 59 | 60 | int zmq::mailbox_t::recv (command_t *cmd_, int timeout_) 61 | { 62 | // Try to get the command straight away. 63 | if (active) { 64 | bool ok = cpipe.read (cmd_); 65 | if (ok) 66 | return 0; 67 | 68 | // If there are no more commands available, switch into passive state. 69 | active = false; 70 | signaler.recv (); 71 | } 72 | 73 | // Wait for signal from the command sender. 74 | int rc = signaler.wait (timeout_); 75 | if (rc != 0 && (errno == EAGAIN || errno == EINTR)) 76 | return -1; 77 | 78 | // We've got the signal. Now we can switch into active state. 79 | active = true; 80 | 81 | // Get a command. 82 | errno_assert (rc == 0); 83 | bool ok = cpipe.read (cmd_); 84 | zmq_assert (ok); 85 | return 0; 86 | } 87 | 88 | -------------------------------------------------------------------------------- /doc/zmq_inproc.txt: -------------------------------------------------------------------------------- 1 | zmq_inproc(7) 2 | ============= 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_inproc - 0MQ local in-process (inter-thread) communication transport 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | The in-process transport passes messages via memory directly between threads 13 | sharing a single 0MQ 'context'. 14 | 15 | NOTE: No I/O threads are involved in passing messages using the 'inproc' 16 | transport. Therefore, if you are using a 0MQ 'context' for in-process messaging 17 | only you can initialise the 'context' with zero I/O threads. See 18 | linkzmq:zmq_init[3] for details. 19 | 20 | 21 | ADDRESSING 22 | ---------- 23 | A 0MQ address string consists of two parts as follows: 24 | 'transport'`://`'endpoint'. The 'transport' part specifies the underlying 25 | transport protocol to use, and for the in-process transport shall be set to 26 | `inproc`. The meaning of the 'endpoint' part for the in-process transport is 27 | defined below. 28 | 29 | 30 | Assigning a local address to a socket 31 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 32 | When assigning a local address to a 'socket' using _zmq_bind()_ with the 33 | 'inproc' transport, the 'endpoint' shall be interpreted as an arbitrary string 34 | identifying the 'name' to create. The 'name' must be unique within the 0MQ 35 | 'context' associated with the 'socket' and may be up to 256 characters in 36 | length. No other restrictions are placed on the format of the 'name'. 37 | 38 | 39 | Connecting a socket 40 | ~~~~~~~~~~~~~~~~~~~ 41 | When connecting a 'socket' to a peer address using _zmq_connect()_ with the 42 | 'inproc' transport, the 'endpoint' shall be interpreted as an arbitrary string 43 | identifying the 'name' to connect to. The 'name' must have been previously 44 | created by assigning it to at least one 'socket' within the same 0MQ 'context' 45 | as the 'socket' being connected. 46 | 47 | 48 | WIRE FORMAT 49 | ----------- 50 | Not applicable. 51 | 52 | 53 | EXAMPLES 54 | -------- 55 | .Assigning a local address to a socket 56 | ---- 57 | /* Assign the in-process name "#1" */ 58 | rc = zmq_bind(socket, "inproc://#1"); 59 | assert (rc == 0); 60 | /* Assign the in-process name "my-endpoint" */ 61 | rc = zmq_bind(socket, "inproc://my-endpoint"); 62 | assert (rc == 0); 63 | ---- 64 | 65 | .Connecting a socket 66 | ---- 67 | /* Connect to the in-process name "#1" */ 68 | rc = zmq_connect(socket, "inproc://#1"); 69 | assert (rc == 0); 70 | /* Connect to the in-process name "my-endpoint" */ 71 | rc = zmq_connect(socket, "inproc://my-endpoint"); 72 | assert (rc == 0); 73 | ---- 74 | 75 | 76 | SEE ALSO 77 | -------- 78 | linkzmq:zmq_bind[3] 79 | linkzmq:zmq_connect[3] 80 | linkzmq:zmq_ipc[7] 81 | linkzmq:zmq_tcp[7] 82 | linkzmq:zmq_pgm[7] 83 | linkzmq:zmq[7] 84 | 85 | 86 | AUTHORS 87 | ------- 88 | This manual page was written by the 0MQ community. 89 | -------------------------------------------------------------------------------- /src/epoll.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_EPOLL_HPP_INCLUDED__ 22 | #define __ZMQ_EPOLL_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #ifdef ZMQ_HAVE_LINUX 27 | 28 | #include 29 | #include 30 | 31 | #include "fd.hpp" 32 | #include "thread.hpp" 33 | #include "poller_base.hpp" 34 | 35 | namespace zmq 36 | { 37 | 38 | // This class implements socket polling mechanism using the Linux-specific 39 | // epoll mechanism. 40 | 41 | class epoll_t : public poller_base_t 42 | { 43 | public: 44 | 45 | typedef void* handle_t; 46 | 47 | epoll_t (); 48 | ~epoll_t (); 49 | 50 | // "poller" concept. 51 | handle_t add_fd (fd_t fd_, struct i_poll_events *events_); 52 | void rm_fd (handle_t handle_); 53 | void set_pollin (handle_t handle_); 54 | void reset_pollin (handle_t handle_); 55 | void set_pollout (handle_t handle_); 56 | void reset_pollout (handle_t handle_); 57 | void start (); 58 | void stop (); 59 | 60 | private: 61 | 62 | // Main worker thread routine. 63 | static void worker_routine (void *arg_); 64 | 65 | // Main event loop. 66 | void loop (); 67 | 68 | // Main epoll file descriptor 69 | fd_t epoll_fd; 70 | 71 | struct poll_entry_t 72 | { 73 | fd_t fd; 74 | epoll_event ev; 75 | struct i_poll_events *events; 76 | }; 77 | 78 | // List of retired event sources. 79 | typedef std::vector retired_t; 80 | retired_t retired; 81 | 82 | // If true, thread is in the process of shutting down. 83 | bool stopping; 84 | 85 | // Handle of the physical thread doing the I/O work. 86 | thread_t worker; 87 | 88 | epoll_t (const epoll_t&); 89 | const epoll_t &operator = (const epoll_t&); 90 | }; 91 | 92 | } 93 | 94 | #endif 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/poller_base.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_POLLER_BASE_HPP_INCLUDED__ 22 | #define __ZMQ_POLLER_BASE_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "clock.hpp" 27 | #include "atomic_counter.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class poller_base_t 33 | { 34 | public: 35 | 36 | poller_base_t (); 37 | virtual ~poller_base_t (); 38 | 39 | // Returns load of the poller. Note that this function can be 40 | // invoked from a different thread! 41 | int get_load (); 42 | 43 | // Add a timeout to expire in timeout_ milliseconds. After the 44 | // expiration timer_event on sink_ object will be called with 45 | // argument set to id_. 46 | void add_timer (int timeout_, struct i_poll_events *sink_, int id_); 47 | 48 | // Cancel the timer created by sink_ object with ID equal to id_. 49 | void cancel_timer (struct i_poll_events *sink_, int id_); 50 | 51 | protected: 52 | 53 | // Called by individual poller implementations to manage the load. 54 | void adjust_load (int amount_); 55 | 56 | // Executes any timers that are due. Returns number of milliseconds 57 | // to wait to match the next timer or 0 meaning "no timers". 58 | uint64_t execute_timers (); 59 | 60 | private: 61 | 62 | // Clock instance private to this I/O thread. 63 | clock_t clock; 64 | 65 | // List of active timers. 66 | struct timer_info_t 67 | { 68 | struct i_poll_events *sink; 69 | int id; 70 | }; 71 | typedef std::multimap timers_t; 72 | timers_t timers; 73 | 74 | // Load of the poller. Currently the number of file descriptors 75 | // registered. 76 | atomic_counter_t load; 77 | 78 | poller_base_t (const poller_base_t&); 79 | const poller_base_t &operator = (const poller_base_t&); 80 | }; 81 | 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /src/io_thread.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_IO_THREAD_HPP_INCLUDED__ 22 | #define __ZMQ_IO_THREAD_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "stdint.hpp" 27 | #include "object.hpp" 28 | #include "poller.hpp" 29 | #include "i_poll_events.hpp" 30 | #include "mailbox.hpp" 31 | 32 | namespace zmq 33 | { 34 | 35 | // Generic part of the I/O thread. Polling-mechanism-specific features 36 | // are implemented in separate "polling objects". 37 | 38 | class io_thread_t : public object_t, public i_poll_events 39 | { 40 | public: 41 | 42 | io_thread_t (class ctx_t *ctx_, uint32_t tid_); 43 | 44 | // Clean-up. If the thread was started, it's neccessary to call 'stop' 45 | // before invoking destructor. Otherwise the destructor would hang up. 46 | ~io_thread_t (); 47 | 48 | // Launch the physical thread. 49 | void start (); 50 | 51 | // Ask underlying thread to stop. 52 | void stop (); 53 | 54 | // Returns mailbox associated with this I/O thread. 55 | mailbox_t *get_mailbox (); 56 | 57 | // i_poll_events implementation. 58 | void in_event (); 59 | void out_event (); 60 | void timer_event (int id_); 61 | 62 | // Used by io_objects to retrieve the assciated poller object. 63 | poller_t *get_poller (); 64 | 65 | // Command handlers. 66 | void process_stop (); 67 | 68 | // Returns load experienced by the I/O thread. 69 | int get_load (); 70 | 71 | private: 72 | 73 | // I/O thread accesses incoming commands via this mailbox. 74 | mailbox_t mailbox; 75 | 76 | // Handle associated with mailbox' file descriptor. 77 | poller_t::handle_t mailbox_handle; 78 | 79 | // I/O multiplexing is performed using a poller object. 80 | poller_t *poller; 81 | 82 | io_thread_t (const io_thread_t&); 83 | const io_thread_t &operator = (const io_thread_t&); 84 | }; 85 | 86 | } 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /src/thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "thread.hpp" 22 | #include "err.hpp" 23 | #include "platform.hpp" 24 | 25 | #ifdef ZMQ_HAVE_WINDOWS 26 | 27 | extern "C" 28 | { 29 | static unsigned int __stdcall thread_routine (void *arg_) 30 | { 31 | zmq::thread_t *self = (zmq::thread_t*) arg_; 32 | self->tfn (self->arg); 33 | return 0; 34 | } 35 | } 36 | 37 | void zmq::thread_t::start (thread_fn *tfn_, void *arg_) 38 | { 39 | tfn = tfn_; 40 | arg =arg_; 41 | descriptor = (HANDLE) _beginthreadex (NULL, 0, 42 | &::thread_routine, this, 0 , NULL); 43 | win_assert (descriptor != NULL); 44 | } 45 | 46 | void zmq::thread_t::stop () 47 | { 48 | DWORD rc = WaitForSingleObject (descriptor, INFINITE); 49 | win_assert (rc != WAIT_FAILED); 50 | BOOL rc2 = CloseHandle (descriptor); 51 | win_assert (rc2 != 0); 52 | } 53 | 54 | #else 55 | 56 | #include 57 | 58 | extern "C" 59 | { 60 | static void *thread_routine (void *arg_) 61 | { 62 | #if !defined ZMQ_HAVE_OPENVMS 63 | // Following code will guarantee more predictable latecnies as it'll 64 | // disallow any signal handling in the I/O thread. 65 | sigset_t signal_set; 66 | int rc = sigfillset (&signal_set); 67 | errno_assert (rc == 0); 68 | # if !defined ZMQ_HAVE_ANDROID 69 | rc = pthread_sigmask (SIG_BLOCK, &signal_set, NULL); 70 | posix_assert (rc); 71 | # endif 72 | #endif 73 | 74 | zmq::thread_t *self = (zmq::thread_t*) arg_; 75 | self->tfn (self->arg); 76 | return NULL; 77 | } 78 | } 79 | 80 | void zmq::thread_t::start (thread_fn *tfn_, void *arg_) 81 | { 82 | tfn = tfn_; 83 | arg =arg_; 84 | int rc = pthread_create (&descriptor, NULL, thread_routine, this); 85 | posix_assert (rc); 86 | } 87 | 88 | void zmq::thread_t::stop () 89 | { 90 | int rc = pthread_join (descriptor, NULL); 91 | posix_assert (rc); 92 | } 93 | 94 | #endif 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /src/io_object.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "io_object.hpp" 22 | #include "io_thread.hpp" 23 | #include "err.hpp" 24 | 25 | zmq::io_object_t::io_object_t (io_thread_t *io_thread_) : 26 | poller (NULL) 27 | { 28 | if (io_thread_) 29 | plug (io_thread_); 30 | } 31 | 32 | zmq::io_object_t::~io_object_t () 33 | { 34 | } 35 | 36 | void zmq::io_object_t::plug (io_thread_t *io_thread_) 37 | { 38 | zmq_assert (io_thread_); 39 | zmq_assert (!poller); 40 | 41 | // Retrieve the poller from the thread we are running in. 42 | poller = io_thread_->get_poller (); 43 | } 44 | 45 | void zmq::io_object_t::unplug () 46 | { 47 | zmq_assert (poller); 48 | 49 | // Forget about old poller in preparation to be migrated 50 | // to a different I/O thread. 51 | poller = NULL; 52 | } 53 | 54 | zmq::io_object_t::handle_t zmq::io_object_t::add_fd (fd_t fd_) 55 | { 56 | return poller->add_fd (fd_, this); 57 | } 58 | 59 | void zmq::io_object_t::rm_fd (handle_t handle_) 60 | { 61 | poller->rm_fd (handle_); 62 | } 63 | 64 | void zmq::io_object_t::set_pollin (handle_t handle_) 65 | { 66 | poller->set_pollin (handle_); 67 | } 68 | 69 | void zmq::io_object_t::reset_pollin (handle_t handle_) 70 | { 71 | poller->reset_pollin (handle_); 72 | } 73 | 74 | void zmq::io_object_t::set_pollout (handle_t handle_) 75 | { 76 | poller->set_pollout (handle_); 77 | } 78 | 79 | void zmq::io_object_t::reset_pollout (handle_t handle_) 80 | { 81 | poller->reset_pollout (handle_); 82 | } 83 | 84 | void zmq::io_object_t::add_timer (int timeout_, int id_) 85 | { 86 | poller->add_timer (timeout_, this, id_); 87 | } 88 | 89 | void zmq::io_object_t::cancel_timer (int id_) 90 | { 91 | poller->cancel_timer (this, id_); 92 | } 93 | 94 | void zmq::io_object_t::in_event () 95 | { 96 | zmq_assert (false); 97 | } 98 | 99 | void zmq::io_object_t::out_event () 100 | { 101 | zmq_assert (false); 102 | } 103 | 104 | void zmq::io_object_t::timer_event (int id_) 105 | { 106 | zmq_assert (false); 107 | } 108 | -------------------------------------------------------------------------------- /doc/zmq_bind.txt: -------------------------------------------------------------------------------- 1 | zmq_bind(3) 2 | =========== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_bind - accept connections on a socket 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_bind (void '*socket', const char '*endpoint');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_bind()_ function shall create an endpoint for accepting connections 18 | and bind it to the socket referenced by the 'socket' argument. 19 | 20 | The 'endpoint' argument is a string consisting of two parts as follows: 21 | 'transport'`://`'address'. The 'transport' part specifies the underlying 22 | transport protocol to use. The meaning of the 'address' part is specific to 23 | the underlying transport protocol selected. 24 | 25 | The following transports are defined: 26 | 27 | 'inproc':: local in-process (inter-thread) communication transport, see linkzmq:zmq_inproc[7] 28 | 'ipc':: local inter-process communication transport, see linkzmq:zmq_ipc[7] 29 | 'tcp':: unicast transport using TCP, see linkzmq:zmq_tcp[7] 30 | 'pgm', 'epgm':: reliable multicast transport using PGM, see linkzmq:zmq_pgm[7] 31 | 32 | With the exception of 'ZMQ_PAIR' sockets, a single socket may be connected to 33 | multiple endpoints using _zmq_connect()_, while simultaneously accepting 34 | incoming connections from multiple endpoints bound to the socket using 35 | _zmq_bind()_. Refer to linkzmq:zmq_socket[3] for a description of the exact 36 | semantics involved when connecting or binding a socket to multiple endpoints. 37 | 38 | 39 | RETURN VALUE 40 | ------------ 41 | The _zmq_bind()_ function shall return zero if successful. Otherwise it shall 42 | return `-1` and set 'errno' to one of the values defined below. 43 | 44 | 45 | ERRORS 46 | ------ 47 | *EINVAL*:: 48 | The endpoint supplied is invalid. 49 | *EPROTONOSUPPORT*:: 50 | The requested 'transport' protocol is not supported. 51 | *ENOCOMPATPROTO*:: 52 | The requested 'transport' protocol is not compatible with the socket type. 53 | *EADDRINUSE*:: 54 | The requested 'address' is already in use. 55 | *EADDRNOTAVAIL*:: 56 | The requested 'address' was not local. 57 | *ENODEV*:: 58 | The requested 'address' specifies a nonexistent interface. 59 | *ETERM*:: 60 | The 0MQ 'context' associated with the specified 'socket' was terminated. 61 | *ENOTSOCK*:: 62 | The provided 'socket' was invalid. 63 | *EMTHREAD*:: 64 | No I/O thread is available to accomplish the task. 65 | 66 | 67 | EXAMPLE 68 | ------- 69 | .Binding a publisher socket to an in-process and a TCP transport 70 | ---- 71 | /* Create a ZMQ_PUB socket */ 72 | void *socket = zmq_socket (context, ZMQ_PUB); 73 | assert (socket); 74 | /* Bind it to a in-process transport with the address 'my_publisher' */ 75 | int rc = zmq_bind (socket, "inproc://my_publisher"); 76 | assert (rc == 0); 77 | /* Bind it to a TCP transport on port 5555 of the 'eth0' interface */ 78 | rc = zmq_bind (socket, "tcp://eth0:5555"); 79 | assert (rc == 0); 80 | ---- 81 | 82 | 83 | SEE ALSO 84 | -------- 85 | linkzmq:zmq_connect[3] 86 | linkzmq:zmq_socket[3] 87 | linkzmq:zmq[7] 88 | 89 | 90 | AUTHORS 91 | ------- 92 | This manual page was written by the 0MQ community. 93 | -------------------------------------------------------------------------------- /src/named_session.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "named_session.hpp" 22 | #include "socket_base.hpp" 23 | 24 | zmq::named_session_t::named_session_t (class io_thread_t *io_thread_, 25 | socket_base_t *socket_, const options_t &options_, 26 | const blob_t &name_) : 27 | session_t (io_thread_, socket_, options_), 28 | name (name_) 29 | { 30 | // Make double sure that the session has valid name. 31 | zmq_assert (!name.empty ()); 32 | zmq_assert (name [0] != 0); 33 | 34 | if (!socket_->register_session (name, this)) { 35 | 36 | // TODO: There's already a session with the specified 37 | // identity. We should log the error and drop the 38 | // session. 39 | zmq_assert (false); 40 | } 41 | } 42 | 43 | zmq::named_session_t::~named_session_t () 44 | { 45 | // Unregister the session from the global list of named sessions. 46 | if (!name.empty ()) 47 | unregister_session (name); 48 | } 49 | 50 | void zmq::named_session_t::attached (const blob_t &peer_identity_) 51 | { 52 | if (!name.empty ()) { 53 | 54 | // If both IDs are temporary, no checking is needed. 55 | // TODO: Old ID should be reused in this case... 56 | if (name.empty () || name [0] != 0 || 57 | peer_identity_.empty () || peer_identity_ [0] != 0) { 58 | 59 | // If we already know the peer name do nothing, just check whether 60 | // it haven't changed. 61 | zmq_assert (name == peer_identity_); 62 | } 63 | } 64 | else if (!peer_identity_.empty ()) { 65 | 66 | // Store the peer identity. 67 | name = peer_identity_; 68 | 69 | // Register the session using the peer name. 70 | if (!register_session (name, this)) { 71 | 72 | // TODO: There's already a session with the specified 73 | // identity. We should presumably syslog it and drop the 74 | // session. 75 | zmq_assert (false); 76 | } 77 | } 78 | } 79 | 80 | void zmq::named_session_t::detached () 81 | { 82 | // Do nothing. Named sessions are never destroyed because of disconnection, 83 | // neither they have to actively reconnect. 84 | } 85 | 86 | -------------------------------------------------------------------------------- /doc/zmq_connect.txt: -------------------------------------------------------------------------------- 1 | zmq_connect(3) 2 | ============== 3 | 4 | 5 | NAME 6 | ---- 7 | zmq_connect - connect a socket 8 | 9 | 10 | SYNOPSIS 11 | -------- 12 | *int zmq_connect (void '*socket', const char '*endpoint');* 13 | 14 | 15 | DESCRIPTION 16 | ----------- 17 | The _zmq_connect()_ function shall connect the socket referenced by the 18 | 'socket' argument to the endpoint specified by the 'endpoint' argument. 19 | 20 | The 'endpoint' argument is a string consisting of two parts as follows: 21 | 'transport'`://`'address'. The 'transport' part specifies the underlying 22 | transport protocol to use. The meaning of the 'address' part is specific to 23 | the underlying transport protocol selected. 24 | 25 | The following transports are defined: 26 | 27 | 'inproc':: local in-process (inter-thread) communication transport, see linkzmq:zmq_inproc[7] 28 | 'ipc':: local inter-process communication transport, see linkzmq:zmq_ipc[7] 29 | 'tcp':: unicast transport using TCP, see linkzmq:zmq_tcp[7] 30 | 'pgm', 'epgm':: reliable multicast transport using PGM, see linkzmq:zmq_pgm[7] 31 | 32 | With the exception of 'ZMQ_PAIR' sockets, a single socket may be connected to 33 | multiple endpoints using _zmq_connect()_, while simultaneously accepting 34 | incoming connections from multiple endpoints bound to the socket using 35 | _zmq_bind()_. Refer to linkzmq:zmq_socket[3] for a description of the exact 36 | semantics involved when connecting or binding a socket to multiple endpoints. 37 | 38 | NOTE: The connection will not be performed immediately but as needed by 0MQ. 39 | Thus a successful invocation of _zmq_connect()_ does not indicate that a 40 | physical connection was or can actually be established. 41 | 42 | 43 | RETURN VALUE 44 | ------------ 45 | The _zmq_connect()_ function shall return zero if successful. Otherwise it 46 | shall return `-1` and set 'errno' to one of the values defined below. 47 | 48 | 49 | ERRORS 50 | ------ 51 | *EINVAL*:: 52 | The endpoint supplied is invalid. 53 | *EPROTONOSUPPORT*:: 54 | The requested 'transport' protocol is not supported. 55 | *ENOCOMPATPROTO*:: 56 | The requested 'transport' protocol is not compatible with the socket type. 57 | *ETERM*:: 58 | The 0MQ 'context' associated with the specified 'socket' was terminated. 59 | *ENOTSOCK*:: 60 | The provided 'socket' was invalid. 61 | *EMTHREAD*:: 62 | No I/O thread is available to accomplish the task. 63 | 64 | 65 | EXAMPLE 66 | ------- 67 | .Connecting a subscriber socket to an in-process and a TCP transport 68 | ---- 69 | /* Create a ZMQ_SUB socket */ 70 | void *socket = zmq_socket (context, ZMQ_SUB); 71 | assert (socket); 72 | /* Connect it to an in-process transport with the address 'my_publisher' */ 73 | int rc = zmq_connect (socket, "inproc://my_publisher"); 74 | assert (rc == 0); 75 | /* Connect it to the host server001, port 5555 using a TCP transport */ 76 | rc = zmq_connect (socket, "tcp://server001:5555"); 77 | assert (rc == 0); 78 | ---- 79 | 80 | 81 | SEE ALSO 82 | -------- 83 | linkzmq:zmq_bind[3] 84 | linkzmq:zmq_socket[3] 85 | linkzmq:zmq[7] 86 | 87 | 88 | AUTHORS 89 | ------- 90 | This manual page was written by the 0MQ community. 91 | -------------------------------------------------------------------------------- /src/zmq_init.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_ZMQ_INIT_HPP_INCLUDED__ 22 | #define __ZMQ_ZMQ_INIT_HPP_INCLUDED__ 23 | 24 | #include "i_inout.hpp" 25 | #include "i_engine.hpp" 26 | #include "own.hpp" 27 | #include "fd.hpp" 28 | #include "stdint.hpp" 29 | #include "stdint.hpp" 30 | #include "blob.hpp" 31 | 32 | namespace zmq 33 | { 34 | 35 | // The class handles initialisation phase of 0MQ wire-level protocol. 36 | 37 | class zmq_init_t : public own_t, public i_inout 38 | { 39 | public: 40 | 41 | zmq_init_t (class io_thread_t *io_thread_, class socket_base_t *socket_, 42 | class session_t *session_, fd_t fd_, const options_t &options_); 43 | ~zmq_init_t (); 44 | 45 | private: 46 | 47 | void finalise_initialisation (); 48 | void dispatch_engine (); 49 | 50 | // i_inout interface implementation. 51 | bool read (::zmq_msg_t *msg_); 52 | bool write (::zmq_msg_t *msg_); 53 | void flush (); 54 | void detach (); 55 | 56 | // Handlers for incoming commands. 57 | void process_plug (); 58 | void process_unplug (); 59 | 60 | // Associated wire-protocol engine. 61 | i_engine *engine; 62 | 63 | // Detached transient engine. 64 | i_engine *ephemeral_engine; 65 | 66 | // True if our own identity was already sent to the peer. 67 | bool sent; 68 | 69 | // True if peer's identity was already received. 70 | bool received; 71 | 72 | // Socket the object belongs to. 73 | class socket_base_t *socket; 74 | 75 | // Reference to the session the init object belongs to. 76 | // If the associated session is unknown and should be found 77 | // depending on peer identity this value is NULL. 78 | class session_t *session; 79 | 80 | // Identity of the peer socket. 81 | blob_t peer_identity; 82 | 83 | // I/O thread the object is living in. It will be used to plug 84 | // the engine into the same I/O thread. 85 | class io_thread_t *io_thread; 86 | 87 | zmq_init_t (const zmq_init_t&); 88 | const zmq_init_t &operator = (const zmq_init_t&); 89 | }; 90 | 91 | } 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /src/options.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_OPTIONS_HPP_INCLUDED__ 22 | #define __ZMQ_OPTIONS_HPP_INCLUDED__ 23 | 24 | #include "stddef.h" 25 | #include "stdint.hpp" 26 | #include "blob.hpp" 27 | 28 | namespace zmq 29 | { 30 | 31 | struct options_t 32 | { 33 | options_t (); 34 | 35 | int setsockopt (int option_, const void *optval_, size_t optvallen_); 36 | int getsockopt (int option_, void *optval_, size_t *optvallen_); 37 | 38 | uint64_t hwm; 39 | int64_t swap; 40 | uint64_t affinity; 41 | blob_t identity; 42 | 43 | // Maximum tranfer rate [kb/s]. Default 100kb/s. 44 | uint32_t rate; 45 | 46 | // Reliability time interval [s]. Default 10s. 47 | uint32_t recovery_ivl; 48 | // Reliability time interval [ms]. Default -1 = not used. 49 | int32_t recovery_ivl_msec; 50 | 51 | // Enable multicast loopback. Default disabled (false). 52 | bool use_multicast_loop; 53 | 54 | uint64_t sndbuf; 55 | uint64_t rcvbuf; 56 | 57 | // Socket type. 58 | int type; 59 | 60 | // Linger time, in milliseconds. 61 | int linger; 62 | 63 | // Minimum interval between attempts to reconnect, in milliseconds. 64 | // Default 100ms 65 | int reconnect_ivl; 66 | // Maximum interval between attempts to reconnect, in milliseconds. 67 | // Default 0 (unused) 68 | int reconnect_ivl_max; 69 | 70 | // Maximum backlog for pending connections. 71 | int backlog; 72 | 73 | // These options are never set by the user directly. Instead they are 74 | // provided by the specific socket type. 75 | bool requires_in; 76 | bool requires_out; 77 | 78 | // If true, when connecting, pipes are created immediately without 79 | // waiting for the connection to be established. That way the socket 80 | // is not aware of the peer's identity, however, it is able to send 81 | // messages straight away. 82 | bool immediate_connect; 83 | 84 | // The timeout for send/recv operations for this socket. 85 | int rcvtimeo; 86 | int sndtimeo; 87 | }; 88 | 89 | } 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/io_thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include "../include/zmq.h" 24 | 25 | #include "io_thread.hpp" 26 | #include "platform.hpp" 27 | #include "err.hpp" 28 | #include "ctx.hpp" 29 | 30 | zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) : 31 | object_t (ctx_, tid_) 32 | { 33 | poller = new (std::nothrow) poller_t; 34 | alloc_assert (poller); 35 | 36 | mailbox_handle = poller->add_fd (mailbox.get_fd (), this); 37 | poller->set_pollin (mailbox_handle); 38 | } 39 | 40 | zmq::io_thread_t::~io_thread_t () 41 | { 42 | delete poller; 43 | } 44 | 45 | void zmq::io_thread_t::start () 46 | { 47 | // Start the underlying I/O thread. 48 | poller->start (); 49 | } 50 | 51 | void zmq::io_thread_t::stop () 52 | { 53 | send_stop (); 54 | } 55 | 56 | zmq::mailbox_t *zmq::io_thread_t::get_mailbox () 57 | { 58 | return &mailbox; 59 | } 60 | 61 | int zmq::io_thread_t::get_load () 62 | { 63 | return poller->get_load (); 64 | } 65 | 66 | void zmq::io_thread_t::in_event () 67 | { 68 | // TODO: Do we want to limit number of commands I/O thread can 69 | // process in a single go? 70 | 71 | while (true) { 72 | 73 | // Get the next command. If there is none, exit. 74 | command_t cmd; 75 | int rc = mailbox.recv (&cmd, 0); 76 | if (rc != 0 && errno == EINTR) 77 | continue; 78 | if (rc != 0 && errno == EAGAIN) 79 | break; 80 | errno_assert (rc == 0); 81 | 82 | // Process the command. 83 | cmd.destination->process_command (cmd); 84 | } 85 | } 86 | 87 | void zmq::io_thread_t::out_event () 88 | { 89 | // We are never polling for POLLOUT here. This function is never called. 90 | zmq_assert (false); 91 | } 92 | 93 | void zmq::io_thread_t::timer_event (int id_) 94 | { 95 | // No timers here. This function is never called. 96 | zmq_assert (false); 97 | } 98 | 99 | zmq::poller_t *zmq::io_thread_t::get_poller () 100 | { 101 | zmq_assert (poller); 102 | return poller; 103 | } 104 | 105 | void zmq::io_thread_t::process_stop () 106 | { 107 | poller->rm_fd (mailbox_handle); 108 | poller->stop (); 109 | } 110 | -------------------------------------------------------------------------------- /src/devpoll.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_DEVPOLL_HPP_INCLUDED__ 22 | #define __ZMQ_DEVPOLL_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #if defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_HPUX 27 | 28 | #include 29 | 30 | #include "fd.hpp" 31 | #include "thread.hpp" 32 | #include "poller_base.hpp" 33 | 34 | namespace zmq 35 | { 36 | 37 | // Implements socket polling mechanism using the "/dev/poll" interface. 38 | 39 | class devpoll_t : public poller_base_t 40 | { 41 | public: 42 | 43 | typedef fd_t handle_t; 44 | 45 | devpoll_t (); 46 | ~devpoll_t (); 47 | 48 | // "poller" concept. 49 | handle_t add_fd (fd_t fd_, struct i_poll_events *events_); 50 | void rm_fd (handle_t handle_); 51 | void set_pollin (handle_t handle_); 52 | void reset_pollin (handle_t handle_); 53 | void set_pollout (handle_t handle_); 54 | void reset_pollout (handle_t handle_); 55 | void start (); 56 | void stop (); 57 | 58 | private: 59 | 60 | // Main worker thread routine. 61 | static void worker_routine (void *arg_); 62 | 63 | // Main event loop. 64 | void loop (); 65 | 66 | // File descriptor referring to "/dev/poll" pseudo-device. 67 | fd_t devpoll_fd; 68 | 69 | struct fd_entry_t 70 | { 71 | short events; 72 | struct i_poll_events *reactor; 73 | bool valid; 74 | bool accepted; 75 | }; 76 | 77 | typedef std::vector fd_table_t; 78 | fd_table_t fd_table; 79 | 80 | typedef std::vector pending_list_t; 81 | pending_list_t pending_list; 82 | 83 | // Pollset manipulation function. 84 | void devpoll_ctl (fd_t fd_, short events_); 85 | 86 | // If true, thread is in the process of shutting down. 87 | bool stopping; 88 | 89 | // Handle of the physical thread doing the I/O work. 90 | thread_t worker; 91 | 92 | devpoll_t (const devpoll_t&); 93 | const devpoll_t &operator = (const devpoll_t&); 94 | }; 95 | 96 | } 97 | 98 | #endif 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /src/encoder.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "encoder.hpp" 22 | #include "i_inout.hpp" 23 | #include "wire.hpp" 24 | 25 | zmq::encoder_t::encoder_t (size_t bufsize_) : 26 | encoder_base_t (bufsize_), 27 | source (NULL) 28 | { 29 | zmq_msg_init (&in_progress); 30 | 31 | // Write 0 bytes to the batch and go to message_ready state. 32 | next_step (NULL, 0, &encoder_t::message_ready, true); 33 | } 34 | 35 | zmq::encoder_t::~encoder_t () 36 | { 37 | zmq_msg_close (&in_progress); 38 | } 39 | 40 | void zmq::encoder_t::set_inout (i_inout *source_) 41 | { 42 | source = source_; 43 | } 44 | 45 | bool zmq::encoder_t::size_ready () 46 | { 47 | // Write message body into the buffer. 48 | next_step (zmq_msg_data (&in_progress), zmq_msg_size (&in_progress), 49 | &encoder_t::message_ready, false); 50 | return true; 51 | } 52 | 53 | bool zmq::encoder_t::message_ready () 54 | { 55 | // Destroy content of the old message. 56 | zmq_msg_close (&in_progress); 57 | 58 | // Read new message. If there is none, return false. 59 | // Note that new state is set only if write is successful. That way 60 | // unsuccessful write will cause retry on the next state machine 61 | // invocation. 62 | if (!source || !source->read (&in_progress)) { 63 | zmq_msg_init (&in_progress); 64 | return false; 65 | } 66 | 67 | // Get the message size. 68 | size_t size = zmq_msg_size (&in_progress); 69 | 70 | // Account for the 'flags' byte. 71 | size++; 72 | 73 | // For messages less than 255 bytes long, write one byte of message size. 74 | // For longer messages write 0xff escape character followed by 8-byte 75 | // message size. In both cases 'flags' field follows. 76 | if (size < 255) { 77 | tmpbuf [0] = (unsigned char) size; 78 | tmpbuf [1] = (in_progress.flags & ZMQ_MSG_MORE); 79 | next_step (tmpbuf, 2, &encoder_t::size_ready, 80 | !(in_progress.flags & ZMQ_MSG_MORE)); 81 | } 82 | else { 83 | tmpbuf [0] = 0xff; 84 | put_uint64 (tmpbuf + 1, size); 85 | tmpbuf [9] = (in_progress.flags & ZMQ_MSG_MORE); 86 | next_step (tmpbuf, 10, &encoder_t::size_ready, 87 | !(in_progress.flags & ZMQ_MSG_MORE)); 88 | } 89 | return true; 90 | } 91 | -------------------------------------------------------------------------------- /src/dist.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_DIST_HPP_INCLUDED__ 22 | #define __ZMQ_DIST_HPP_INCLUDED__ 23 | 24 | #include 25 | 26 | #include "array.hpp" 27 | #include "pipe.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | // Class manages a set of outbound pipes. It sends each messages to 33 | // each of them. 34 | class dist_t : public i_writer_events 35 | { 36 | public: 37 | 38 | dist_t (class own_t *sink_); 39 | ~dist_t (); 40 | 41 | void attach (writer_t *pipe_); 42 | void terminate (); 43 | int send (zmq_msg_t *msg_, int flags_); 44 | bool has_out (); 45 | 46 | // i_writer_events interface implementation. 47 | void activated (writer_t *pipe_); 48 | void terminated (writer_t *pipe_); 49 | 50 | private: 51 | 52 | // Write the message to the pipe. Make the pipe inactive if writing 53 | // fails. In such a case false is returned. 54 | bool write (class writer_t *pipe_, zmq_msg_t *msg_); 55 | 56 | // Put the message to all active pipes. 57 | void distribute (zmq_msg_t *msg_, int flags_); 58 | 59 | // List of outbound pipes. 60 | typedef array_t pipes_t; 61 | pipes_t pipes; 62 | 63 | // Number of active pipes. All the active pipes are located at the 64 | // beginning of the pipes array. These are the pipes the messages 65 | // can be sent to at the moment. 66 | pipes_t::size_type active; 67 | 68 | // Number of pipes eligible for sending messages to. This includes all 69 | // the active pipes plus all the pipes that we can in theory send 70 | // messages to (the HWM is not yet reached), but sending a message 71 | // to them would result in partial message being delivered, ie. message 72 | // with initial parts missing. 73 | pipes_t::size_type eligible; 74 | 75 | // True if last we are in the middle of a multipart message. 76 | bool more; 77 | 78 | // Object to send events to. 79 | class own_t *sink; 80 | 81 | // If true, termination process is already underway. 82 | bool terminating; 83 | 84 | dist_t (const dist_t&); 85 | const dist_t &operator = (const dist_t&); 86 | }; 87 | 88 | } 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/mutex.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_MUTEX_HPP_INCLUDED__ 22 | #define __ZMQ_MUTEX_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | #include "err.hpp" 26 | 27 | // Mutex class encapsulates OS mutex in a platform-independent way. 28 | 29 | #ifdef ZMQ_HAVE_WINDOWS 30 | 31 | #include "windows.hpp" 32 | 33 | namespace zmq 34 | { 35 | 36 | class mutex_t 37 | { 38 | public: 39 | inline mutex_t () 40 | { 41 | InitializeCriticalSection (&cs); 42 | } 43 | 44 | inline ~mutex_t () 45 | { 46 | DeleteCriticalSection (&cs); 47 | } 48 | 49 | inline void lock () 50 | { 51 | EnterCriticalSection (&cs); 52 | } 53 | 54 | inline void unlock () 55 | { 56 | LeaveCriticalSection (&cs); 57 | } 58 | 59 | private: 60 | 61 | CRITICAL_SECTION cs; 62 | 63 | // Disable copy construction and assignment. 64 | mutex_t (const mutex_t&); 65 | void operator = (const mutex_t&); 66 | }; 67 | 68 | } 69 | 70 | #else 71 | 72 | #include 73 | 74 | namespace zmq 75 | { 76 | 77 | class mutex_t 78 | { 79 | public: 80 | inline mutex_t () 81 | { 82 | int rc = pthread_mutex_init (&mutex, NULL); 83 | if (rc) 84 | posix_assert (rc); 85 | } 86 | 87 | inline ~mutex_t () 88 | { 89 | int rc = pthread_mutex_destroy (&mutex); 90 | if (rc) 91 | posix_assert (rc); 92 | } 93 | 94 | inline void lock () 95 | { 96 | int rc = pthread_mutex_lock (&mutex); 97 | if (rc) 98 | posix_assert (rc); 99 | } 100 | 101 | inline void unlock () 102 | { 103 | int rc = pthread_mutex_unlock (&mutex); 104 | if (rc) 105 | posix_assert (rc); 106 | } 107 | 108 | private: 109 | 110 | pthread_mutex_t mutex; 111 | 112 | // Disable copy construction and assignment. 113 | mutex_t (const mutex_t&); 114 | const mutex_t &operator = (const mutex_t&); 115 | }; 116 | 117 | } 118 | 119 | #endif 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /src/zmq_connecter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_ZMQ_CONNECTER_HPP_INCLUDED__ 22 | #define __ZMQ_ZMQ_CONNECTER_HPP_INCLUDED__ 23 | 24 | #include "own.hpp" 25 | #include "io_object.hpp" 26 | #include "tcp_connecter.hpp" 27 | #include "stdint.hpp" 28 | 29 | namespace zmq 30 | { 31 | 32 | class zmq_connecter_t : public own_t, public io_object_t 33 | { 34 | public: 35 | 36 | // If 'wait' is true connecter first waits for a while, then starts 37 | // connection process. 38 | zmq_connecter_t (class io_thread_t *io_thread_, 39 | class session_t *session_, const options_t &options_, 40 | const char *protocol_, const char *address_, bool delay_); 41 | ~zmq_connecter_t (); 42 | 43 | private: 44 | 45 | // ID of the timer used to delay the reconnection. 46 | enum {reconnect_timer_id = 1}; 47 | 48 | // Handlers for incoming commands. 49 | void process_plug (); 50 | 51 | // Handlers for I/O events. 52 | void in_event (); 53 | void out_event (); 54 | void timer_event (int id_); 55 | 56 | // Internal function to start the actual connection establishment. 57 | void start_connecting (); 58 | 59 | // Internal function to add a reconnect timer 60 | void add_reconnect_timer(); 61 | 62 | // Internal function to return a reconnect backoff delay. 63 | // Will modify the current_reconnect_ivl used for next call 64 | // Returns the currently used interval 65 | int get_new_reconnect_ivl (); 66 | 67 | // Actual connecting socket. 68 | tcp_connecter_t tcp_connecter; 69 | 70 | // Handle corresponding to the listening socket. 71 | handle_t handle; 72 | 73 | // If true file descriptor is registered with the poller and 'handle' 74 | // contains valid value. 75 | bool handle_valid; 76 | 77 | // If true, connecter is waiting a while before trying to connect. 78 | bool wait; 79 | 80 | // Reference to the session we belong to. 81 | class session_t *session; 82 | 83 | // Current reconnect ivl, updated for backoff strategy 84 | int current_reconnect_ivl; 85 | 86 | zmq_connecter_t (const zmq_connecter_t&); 87 | const zmq_connecter_t &operator = (const zmq_connecter_t&); 88 | }; 89 | 90 | } 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /src/kqueue.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_KQUEUE_HPP_INCLUDED__ 22 | #define __ZMQ_KQUEUE_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #if defined ZMQ_HAVE_FREEBSD || defined ZMQ_HAVE_OPENBSD ||\ 27 | defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_NETBSD 28 | 29 | #include 30 | 31 | #include "fd.hpp" 32 | #include "thread.hpp" 33 | #include "poller_base.hpp" 34 | 35 | namespace zmq 36 | { 37 | 38 | // Implements socket polling mechanism using the BSD-specific 39 | // kqueue interface. 40 | 41 | class kqueue_t : public poller_base_t 42 | { 43 | public: 44 | 45 | typedef void* handle_t; 46 | 47 | kqueue_t (); 48 | ~kqueue_t (); 49 | 50 | // "poller" concept. 51 | handle_t add_fd (fd_t fd_, struct i_poll_events *events_); 52 | void rm_fd (handle_t handle_); 53 | void set_pollin (handle_t handle_); 54 | void reset_pollin (handle_t handle_); 55 | void set_pollout (handle_t handle_); 56 | void reset_pollout (handle_t handle_); 57 | void start (); 58 | void stop (); 59 | 60 | private: 61 | 62 | // Main worker thread routine. 63 | static void worker_routine (void *arg_); 64 | 65 | // Main event loop. 66 | void loop (); 67 | 68 | // File descriptor referring to the kernel event queue. 69 | fd_t kqueue_fd; 70 | 71 | // Adds the event to the kqueue. 72 | void kevent_add (fd_t fd_, short filter_, void *udata_); 73 | 74 | // Deletes the event from the kqueue. 75 | void kevent_delete (fd_t fd_, short filter_); 76 | 77 | struct poll_entry_t 78 | { 79 | fd_t fd; 80 | bool flag_pollin; 81 | bool flag_pollout; 82 | i_poll_events *reactor; 83 | }; 84 | 85 | // List of retired event sources. 86 | typedef std::vector retired_t; 87 | retired_t retired; 88 | 89 | // If true, thread is in the process of shutting down. 90 | bool stopping; 91 | 92 | // Handle of the physical thread doing the I/O work. 93 | thread_t worker; 94 | 95 | kqueue_t (const kqueue_t&); 96 | const kqueue_t &operator = (const kqueue_t&); 97 | }; 98 | 99 | } 100 | 101 | #endif 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /src/pgm_sender.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_PGM_SENDER_HPP_INCLUDED__ 22 | #define __ZMQ_PGM_SENDER_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #if defined ZMQ_HAVE_OPENPGM 27 | 28 | #ifdef ZMQ_HAVE_WINDOWS 29 | #include "windows.hpp" 30 | #endif 31 | 32 | #include "stdint.hpp" 33 | #include "io_object.hpp" 34 | #include "i_engine.hpp" 35 | #include "options.hpp" 36 | #include "pgm_socket.hpp" 37 | #include "encoder.hpp" 38 | 39 | namespace zmq 40 | { 41 | 42 | class pgm_sender_t : public io_object_t, public i_engine 43 | { 44 | 45 | public: 46 | 47 | pgm_sender_t (class io_thread_t *parent_, const options_t &options_); 48 | ~pgm_sender_t (); 49 | 50 | int init (bool udp_encapsulation_, const char *network_); 51 | 52 | // i_engine interface implementation. 53 | void plug (class io_thread_t *io_thread_, struct i_inout *inout_); 54 | void unplug (); 55 | void terminate (); 56 | void activate_in (); 57 | void activate_out (); 58 | 59 | // i_poll_events interface implementation. 60 | void in_event (); 61 | void out_event (); 62 | void timer_event (int token); 63 | 64 | private: 65 | 66 | // TX and RX timeout timer ID's. 67 | enum {tx_timer_id = 0xa0, rx_timer_id = 0xa1}; 68 | 69 | // Timers are running. 70 | bool has_tx_timer; 71 | bool has_rx_timer; 72 | 73 | // Message encoder. 74 | encoder_t encoder; 75 | 76 | // PGM socket. 77 | pgm_socket_t pgm_socket; 78 | 79 | // Socket options. 80 | options_t options; 81 | 82 | // Poll handle associated with PGM socket. 83 | handle_t handle; 84 | handle_t uplink_handle; 85 | handle_t rdata_notify_handle; 86 | handle_t pending_notify_handle; 87 | 88 | // Output buffer from pgm_socket. 89 | unsigned char *out_buffer; 90 | 91 | // Output buffer size. 92 | size_t out_buffer_size; 93 | 94 | // Number of bytes in the buffer to be written to the socket. 95 | // If zero, there are no data to be sent. 96 | size_t write_size; 97 | 98 | pgm_sender_t (const pgm_sender_t&); 99 | const pgm_sender_t &operator = (const pgm_sender_t&); 100 | }; 101 | 102 | } 103 | #endif 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/poll.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __ZMQ_POLL_HPP_INCLUDED__ 22 | #define __ZMQ_POLL_HPP_INCLUDED__ 23 | 24 | #include "platform.hpp" 25 | 26 | #if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\ 27 | defined ZMQ_HAVE_OPENBSD || defined ZMQ_HAVE_SOLARIS ||\ 28 | defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_QNXNTO ||\ 29 | defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_AIX ||\ 30 | defined ZMQ_HAVE_NETBSD 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #include "fd.hpp" 37 | #include "thread.hpp" 38 | #include "poller_base.hpp" 39 | 40 | namespace zmq 41 | { 42 | 43 | // Implements socket polling mechanism using the POSIX.1-2001 44 | // poll() system call. 45 | 46 | class poll_t : public poller_base_t 47 | { 48 | public: 49 | 50 | typedef fd_t handle_t; 51 | 52 | poll_t (); 53 | ~poll_t (); 54 | 55 | // "poller" concept. 56 | handle_t add_fd (fd_t fd_, struct i_poll_events *events_); 57 | void rm_fd (handle_t handle_); 58 | void set_pollin (handle_t handle_); 59 | void reset_pollin (handle_t handle_); 60 | void set_pollout (handle_t handle_); 61 | void reset_pollout (handle_t handle_); 62 | void start (); 63 | void stop (); 64 | 65 | private: 66 | 67 | // Main worker thread routine. 68 | static void worker_routine (void *arg_); 69 | 70 | // Main event loop. 71 | void loop (); 72 | 73 | struct fd_entry_t 74 | { 75 | fd_t index; 76 | struct i_poll_events *events; 77 | }; 78 | 79 | // This table stores data for registered descriptors. 80 | typedef std::vector fd_table_t; 81 | fd_table_t fd_table; 82 | 83 | // Pollset to pass to the poll function. 84 | typedef std::vector pollset_t; 85 | pollset_t pollset; 86 | 87 | // If true, there's at least one retired event source. 88 | bool retired; 89 | 90 | // If true, thread is in the process of shutting down. 91 | bool stopping; 92 | 93 | // Handle of the physical thread doing the I/O work. 94 | thread_t worker; 95 | 96 | poll_t (const poll_t&); 97 | const poll_t &operator = (const poll_t&); 98 | }; 99 | 100 | } 101 | 102 | #endif 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /src/poller_base.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "poller_base.hpp" 22 | #include "i_poll_events.hpp" 23 | #include "err.hpp" 24 | 25 | zmq::poller_base_t::poller_base_t () 26 | { 27 | } 28 | 29 | zmq::poller_base_t::~poller_base_t () 30 | { 31 | // Make sure there is no more load on the shutdown. 32 | zmq_assert (get_load () == 0); 33 | } 34 | 35 | int zmq::poller_base_t::get_load () 36 | { 37 | return load.get (); 38 | } 39 | 40 | void zmq::poller_base_t::adjust_load (int amount_) 41 | { 42 | if (amount_ > 0) 43 | load.add (amount_); 44 | else if (amount_ < 0) 45 | load.sub (-amount_); 46 | } 47 | 48 | void zmq::poller_base_t::add_timer (int timeout_, i_poll_events *sink_, int id_) 49 | { 50 | uint64_t expiration = clock.now_ms () + timeout_; 51 | timer_info_t info = {sink_, id_}; 52 | timers.insert (timers_t::value_type (expiration, info)); 53 | } 54 | 55 | void zmq::poller_base_t::cancel_timer (i_poll_events *sink_, int id_) 56 | { 57 | // Complexity of this operation is O(n). We assume it is rarely used. 58 | for (timers_t::iterator it = timers.begin (); it != timers.end (); ++it) 59 | if (it->second.sink == sink_ && it->second.id == id_) { 60 | timers.erase (it); 61 | return; 62 | } 63 | 64 | // Timer not found. 65 | zmq_assert (false); 66 | } 67 | 68 | uint64_t zmq::poller_base_t::execute_timers () 69 | { 70 | // Fast track. 71 | if (timers.empty ()) 72 | return 0; 73 | 74 | // Get the current time. 75 | uint64_t current = clock.now_ms (); 76 | 77 | // Execute the timers that are already due. 78 | timers_t::iterator it = timers.begin (); 79 | while (it != timers.end ()) { 80 | 81 | // If we have to wait to execute the item, same will be true about 82 | // all the following items (multimap is sorted). Thus we can stop 83 | // checking the subsequent timers and return the time to wait for 84 | // the next timer (at least 1ms). 85 | if (it->first > current) 86 | return it->first - current; 87 | 88 | // Trigger the timer. 89 | it->second.sink->timer_event (it->second.id); 90 | 91 | // Remove it from the list of active timers. 92 | timers_t::iterator o = it; 93 | ++it; 94 | timers.erase (o); 95 | } 96 | 97 | // There are no more timers. 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /perf/remote_thr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | #include "../include/zmq_utils.h" 23 | #include 24 | #include 25 | #include 26 | 27 | int main (int argc, char *argv []) 28 | { 29 | const char *connect_to; 30 | int message_count; 31 | int message_size; 32 | void *ctx; 33 | void *s; 34 | int rc; 35 | int i; 36 | zmq_msg_t msg; 37 | 38 | if (argc != 4) { 39 | printf ("usage: remote_thr " 40 | "\n"); 41 | return 1; 42 | } 43 | connect_to = argv [1]; 44 | message_size = atoi (argv [2]); 45 | message_count = atoi (argv [3]); 46 | 47 | ctx = zmq_init (1); 48 | if (!ctx) { 49 | printf ("error in zmq_init: %s\n", zmq_strerror (errno)); 50 | return -1; 51 | } 52 | 53 | s = zmq_socket (ctx, ZMQ_PUB); 54 | if (!s) { 55 | printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); 56 | return -1; 57 | } 58 | 59 | // Add your socket options here. 60 | // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. 61 | 62 | rc = zmq_connect (s, connect_to); 63 | if (rc != 0) { 64 | printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); 65 | return -1; 66 | } 67 | 68 | for (i = 0; i != message_count; i++) { 69 | 70 | rc = zmq_msg_init_size (&msg, message_size); 71 | if (rc != 0) { 72 | printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); 73 | return -1; 74 | } 75 | #if defined ZMQ_MAKE_VALGRIND_HAPPY 76 | memset (zmq_msg_data (&msg), 0, message_size); 77 | #endif 78 | 79 | rc = zmq_send (s, &msg, 0); 80 | if (rc != 0) { 81 | printf ("error in zmq_send: %s\n", zmq_strerror (errno)); 82 | return -1; 83 | } 84 | rc = zmq_msg_close (&msg); 85 | if (rc != 0) { 86 | printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); 87 | return -1; 88 | } 89 | } 90 | 91 | rc = zmq_close (s); 92 | if (rc != 0) { 93 | printf ("error in zmq_close: %s\n", zmq_strerror (errno)); 94 | return -1; 95 | } 96 | 97 | rc = zmq_term (ctx); 98 | if (rc != 0) { 99 | printf ("error in zmq_term: %s\n", zmq_strerror (errno)); 100 | return -1; 101 | } 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /perf/local_lat.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2007-2011 iMatix Corporation 3 | Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 4 | 5 | This file is part of 0MQ. 6 | 7 | 0MQ is free software; you can redistribute it and/or modify it under 8 | the terms of the GNU Lesser General Public License as published by 9 | the Free Software Foundation; either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | 0MQ is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public License 18 | along with this program. If not, see . 19 | */ 20 | 21 | #include "../include/zmq.h" 22 | #include "../include/zmq_utils.h" 23 | #include 24 | #include 25 | 26 | int main (int argc, char *argv []) 27 | { 28 | const char *bind_to; 29 | int roundtrip_count; 30 | size_t message_size; 31 | void *ctx; 32 | void *s; 33 | int rc; 34 | int i; 35 | zmq_msg_t msg; 36 | 37 | if (argc != 4) { 38 | printf ("usage: local_lat " 39 | "\n"); 40 | return 1; 41 | } 42 | bind_to = argv [1]; 43 | message_size = atoi (argv [2]); 44 | roundtrip_count = atoi (argv [3]); 45 | 46 | ctx = zmq_init (1); 47 | if (!ctx) { 48 | printf ("error in zmq_init: %s\n", zmq_strerror (errno)); 49 | return -1; 50 | } 51 | 52 | s = zmq_socket (ctx, ZMQ_REP); 53 | if (!s) { 54 | printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); 55 | return -1; 56 | } 57 | 58 | rc = zmq_bind (s, bind_to); 59 | if (rc != 0) { 60 | printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); 61 | return -1; 62 | } 63 | 64 | rc = zmq_msg_init (&msg); 65 | if (rc != 0) { 66 | printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno)); 67 | return -1; 68 | } 69 | 70 | for (i = 0; i != roundtrip_count; i++) { 71 | rc = zmq_recv (s, &msg, 0); 72 | if (rc != 0) { 73 | printf ("error in zmq_recv: %s\n", zmq_strerror (errno)); 74 | return -1; 75 | } 76 | if (zmq_msg_size (&msg) != message_size) { 77 | printf ("message of incorrect size received\n"); 78 | return -1; 79 | } 80 | rc = zmq_send (s, &msg, 0); 81 | if (rc != 0) { 82 | printf ("error in zmq_send: %s\n", zmq_strerror (errno)); 83 | return -1; 84 | } 85 | } 86 | 87 | rc = zmq_msg_close (&msg); 88 | if (rc != 0) { 89 | printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); 90 | return -1; 91 | } 92 | 93 | zmq_sleep (1); 94 | 95 | rc = zmq_close (s); 96 | if (rc != 0) { 97 | printf ("error in zmq_close: %s\n", zmq_strerror (errno)); 98 | return -1; 99 | } 100 | 101 | rc = zmq_term (ctx); 102 | if (rc != 0) { 103 | printf ("error in zmq_term: %s\n", zmq_strerror (errno)); 104 | return -1; 105 | } 106 | 107 | return 0; 108 | } 109 | --------------------------------------------------------------------------------