libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_use_fd_ipc.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 #if !defined (ZMQ_HAVE_WINDOWS)
33 # include <sys/socket.h>
34 # include <sys/un.h>
35 
36 void pre_allocate_sock (void *zmq_socket, const char *path)
37 {
38  struct sockaddr_un addr;
39  addr.sun_family = AF_UNIX;
40  strcpy (addr.sun_path, path);
41 
42  unlink (path);
43 
44  int s_pre = socket (AF_UNIX, SOCK_STREAM, 0);
45  assert (s_pre != -1);
46 
47  int rc = bind (s_pre, (struct sockaddr *) &addr,
48  sizeof (struct sockaddr_un));
49  assert (rc == 0);
50 
51  rc = listen (s_pre, SOMAXCONN);
52  assert (rc == 0);
53 
54  rc = zmq_setsockopt (zmq_socket, ZMQ_USE_FD, &s_pre,
55  sizeof (s_pre));
56  assert(rc == 0);
57 }
58 
59 void test_req_rep ()
60 {
61  void *ctx = zmq_ctx_new ();
62  assert (ctx);
63 
64  void *sb = zmq_socket (ctx, ZMQ_REP);
65  assert (sb);
66 
67  pre_allocate_sock(sb, "/tmp/tester");
68 
69  int rc = zmq_bind (sb, "ipc:///tmp/tester");
70  assert (rc == 0);
71 
72  void *sc = zmq_socket (ctx, ZMQ_REQ);
73  assert (sc);
74  rc = zmq_connect (sc, "ipc:///tmp/tester");
75  assert (rc == 0);
76 
77  bounce (sb, sc);
78 
79  rc = zmq_close (sc);
80  assert (rc == 0);
81 
82  rc = zmq_close (sb);
83  assert (rc == 0);
84 
85  rc = zmq_ctx_term (ctx);
86  assert (rc == 0);
87 
88  rc = unlink ("/tmp/tester");
89  assert (rc == 0);
90 }
91 
92 void test_pair ()
93 {
94  void *ctx = zmq_ctx_new ();
95  assert (ctx);
96 
97  void *sb = zmq_socket (ctx, ZMQ_PAIR);
98  assert (sb);
99 
100  pre_allocate_sock(sb, "/tmp/tester");
101 
102  int rc = zmq_bind (sb, "ipc:///tmp/tester");
103  assert (rc == 0);
104 
105  void *sc = zmq_socket (ctx, ZMQ_PAIR);
106  assert (sc);
107  rc = zmq_connect (sc, "ipc:///tmp/tester");
108  assert (rc == 0);
109 
110  bounce (sb, sc);
111 
112  rc = zmq_close (sc);
113  assert (rc == 0);
114 
115  rc = zmq_close (sb);
116  assert (rc == 0);
117 
118  rc = zmq_ctx_term (ctx);
119  assert (rc == 0);
120 
121  rc = unlink ("/tmp/tester");
122  assert (rc == 0);
123 }
124 
126 {
127 #if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
128  void *ctx = zmq_ctx_new ();
129  assert (ctx);
130 
131  void *sb = zmq_socket (ctx, ZMQ_SERVER);
132  assert (sb);
133 
134  pre_allocate_sock(sb, "/tmp/tester");
135 
136  int rc = zmq_bind (sb, "ipc:///tmp/tester");
137  assert (rc == 0);
138 
139  void *sc = zmq_socket (ctx, ZMQ_CLIENT);
140  assert (sc);
141  rc = zmq_connect (sc, "ipc:///tmp/tester");
142  assert (rc == 0);
143 
144  zmq_msg_t msg;
145  rc = zmq_msg_init_size (&msg, 1);
146  assert (rc == 0);
147 
148  char *data = (char *) zmq_msg_data (&msg);
149  data [0] = 1;
150 
151  rc = zmq_msg_send (&msg, sc, ZMQ_SNDMORE);
152  assert (rc == -1);
153 
154  rc = zmq_msg_send (&msg, sc, 0);
155  assert (rc == 1);
156 
157  rc = zmq_msg_init (&msg);
158  assert (rc == 0);
159 
160  rc = zmq_msg_recv (&msg, sb, 0);
161  assert (rc == 1);
162 
163  uint32_t routing_id = zmq_msg_routing_id (&msg);
164  assert (routing_id != 0);
165 
166  rc = zmq_msg_close (&msg);
167  assert (rc == 0);
168 
169  rc = zmq_msg_init_size (&msg, 1);
170  assert (rc == 0);
171 
172  data = (char *)zmq_msg_data (&msg);
173  data[0] = 2;
174 
175  rc = zmq_msg_set_routing_id (&msg, routing_id);
176  assert (rc == 0);
177 
178  rc = zmq_msg_send (&msg, sb, ZMQ_SNDMORE);
179  assert (rc == -1);
180 
181  rc = zmq_msg_send (&msg, sb, 0);
182  assert (rc == 1);
183 
184  rc = zmq_msg_recv (&msg, sc, 0);
185  assert (rc == 1);
186 
187  routing_id = zmq_msg_routing_id (&msg);
188  assert (routing_id == 0);
189 
190  rc = zmq_msg_close (&msg);
191  assert (rc == 0);
192 
193  rc = zmq_close (sc);
194  assert (rc == 0);
195 
196  rc = zmq_close (sb);
197  assert (rc == 0);
198 
199  rc = zmq_ctx_term (ctx);
200  assert (rc == 0);
201 
202  rc = unlink ("/tmp/tester");
203  assert (rc == 0);
204 #endif
205 }
206 
207 int main (void)
208 {
210 
211  test_req_rep();
212  test_pair();
214 
215  return 0 ;
216 }
217 
218 #else
219 int main (void)
220 {
221  return 0 ;
222 }
223 #endif
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
uint32_t zmq_msg_routing_id(zmq_msg_t *msg)
Definition: zmq.cpp:715
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_REP
Definition: zmq.h:250
int main(void)
Definition: command.hpp:84
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT void * zmq_msg_data(zmq_msg_t *msg)
Definition: zmq.cpp:666
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
void test_req_rep()
#define ZMQ_REQ
Definition: zmq.h:249
void test_client_server()
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:629
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
void test_pair()
#define ZMQ_USE_FD
Definition: zmq.h:338
Definition: zmq.h:221
#define ZMQ_SERVER
void bounce(void *server, void *client)
Definition: testutil.hpp:73
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
void pre_allocate_sock(void *zmq_socket, const char *path)
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
#define ZMQ_PAIR
Definition: zmq.h:246
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT int zmq_msg_init_size(zmq_msg_t *msg, size_t size)
Definition: zmq.cpp:618
int zmq_msg_set_routing_id(zmq_msg_t *msg, uint32_t routing_id)
Definition: zmq.cpp:710
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
#define ZMQ_CLIENT