libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_use_fd_tcp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 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 <netdb.h>
34 
35 void pre_allocate_sock (void *zmq_socket, const char *address,
36  const char *port)
37 {
38  struct addrinfo *addr, hint;
39  hint.ai_flags=0;
40  hint.ai_family=AF_INET;
41  hint.ai_socktype=SOCK_STREAM;
42  hint.ai_protocol=IPPROTO_TCP;
43  hint.ai_addrlen=0;
44  hint.ai_canonname=NULL;
45  hint.ai_addr=NULL;
46  hint.ai_next=NULL;
47 
48  int rc = getaddrinfo (address, port, &hint, &addr);
49  assert (rc == 0);
50 
51  int s_pre = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
52  assert (s_pre != -1);
53 
54  int flag = 1;
55  rc = setsockopt (s_pre, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
56  assert (rc == 0);
57 
58  rc = bind (s_pre, addr->ai_addr, addr->ai_addrlen);
59  assert (rc == 0);
60 
61  rc = listen (s_pre, SOMAXCONN);
62  assert (rc == 0);
63 
64  rc = zmq_setsockopt (zmq_socket, ZMQ_USE_FD, &s_pre,
65  sizeof (s_pre));
66  assert(rc == 0);
67 
68  freeaddrinfo(addr);
69 }
70 
71 void test_req_rep ()
72 {
73  void *ctx = zmq_ctx_new ();
74  assert (ctx);
75 
76  void *sb = zmq_socket (ctx, ZMQ_REP);
77  assert (sb);
78 
79  pre_allocate_sock(sb, "127.0.0.1", "5560");
80 
81  int rc = zmq_bind (sb, "tcp://127.0.0.1:5560");
82  assert (rc == 0);
83 
84  void *sc = zmq_socket (ctx, ZMQ_REQ);
85  assert (sc);
86  rc = zmq_connect (sc, "tcp://127.0.0.1:5560");
87  assert (rc == 0);
88 
89  bounce (sb, sc);
90 
91  rc = zmq_close (sc);
92  assert (rc == 0);
93 
94  rc = zmq_close (sb);
95  assert (rc == 0);
96 
97  rc = zmq_ctx_term (ctx);
98  assert (rc == 0);
99 }
100 
101 void test_pair ()
102 {
103  void *ctx = zmq_ctx_new ();
104  assert (ctx);
105 
106  void *sb = zmq_socket (ctx, ZMQ_PAIR);
107  assert (sb);
108 
109  pre_allocate_sock(sb, "127.0.0.1", "5560");
110 
111  int rc = zmq_bind (sb, "tcp://127.0.0.1:5560");
112  assert (rc == 0);
113 
114  void *sc = zmq_socket (ctx, ZMQ_PAIR);
115  assert (sc);
116  rc = zmq_connect (sc, "tcp://127.0.0.1:5560");
117  assert (rc == 0);
118 
119  bounce (sb, sc);
120 
121  rc = zmq_close (sc);
122  assert (rc == 0);
123 
124  rc = zmq_close (sb);
125  assert (rc == 0);
126 
127  rc = zmq_ctx_term (ctx);
128  assert (rc == 0);
129 }
130 
132 {
133 #if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
134  void *ctx = zmq_ctx_new ();
135  assert (ctx);
136 
137  void *sb = zmq_socket (ctx, ZMQ_SERVER);
138  assert (sb);
139 
140  pre_allocate_sock(sb, "127.0.0.1", "5560");
141 
142  int rc = zmq_bind (sb, "tcp://127.0.0.1:5560");
143  assert (rc == 0);
144 
145  void *sc = zmq_socket (ctx, ZMQ_CLIENT);
146  assert (sc);
147  rc = zmq_connect (sc, "tcp://127.0.0.1:5560");
148  assert (rc == 0);
149 
150  zmq_msg_t msg;
151  rc = zmq_msg_init_size (&msg, 1);
152  assert (rc == 0);
153 
154  char *data = (char *) zmq_msg_data (&msg);
155  data [0] = 1;
156 
157  rc = zmq_msg_send (&msg, sc, ZMQ_SNDMORE);
158  assert (rc == -1);
159 
160  rc = zmq_msg_send (&msg, sc, 0);
161  assert (rc == 1);
162 
163  rc = zmq_msg_init (&msg);
164  assert (rc == 0);
165 
166  rc = zmq_msg_recv (&msg, sb, 0);
167  assert (rc == 1);
168 
169  uint32_t routing_id = zmq_msg_routing_id (&msg);
170  assert (routing_id != 0);
171 
172  rc = zmq_msg_close (&msg);
173  assert (rc == 0);
174 
175  rc = zmq_msg_init_size (&msg, 1);
176  assert (rc == 0);
177 
178  data = (char *)zmq_msg_data (&msg);
179  data[0] = 2;
180 
181  rc = zmq_msg_set_routing_id (&msg, routing_id);
182  assert (rc == 0);
183 
184  rc = zmq_msg_send (&msg, sb, ZMQ_SNDMORE);
185  assert (rc == -1);
186 
187  rc = zmq_msg_send (&msg, sb, 0);
188  assert (rc == 1);
189 
190  rc = zmq_msg_recv (&msg, sc, 0);
191  assert (rc == 1);
192 
193  routing_id = zmq_msg_routing_id (&msg);
194  assert (routing_id == 0);
195 
196  rc = zmq_msg_close (&msg);
197  assert (rc == 0);
198 
199  rc = zmq_close (sc);
200  assert (rc == 0);
201 
202  rc = zmq_close (sb);
203  assert (rc == 0);
204 
205  rc = zmq_ctx_term (ctx);
206  assert (rc == 0);
207 #endif
208 }
209 
210 int main (void)
211 {
213 
214  test_req_rep();
215  test_pair();
217 
218  return 0 ;
219 }
220 #else
221 int main (void)
222 {
223  return 0 ;
224 }
225 #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
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()
int main(void)
#define ZMQ_REQ
Definition: zmq.h:249
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
#define ZMQ_USE_FD
Definition: zmq.h:338
Definition: zmq.h:221
#define ZMQ_SERVER
void test_client_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
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
const char * address
Definition: test_fork.cpp:32
int zmq_msg_set_routing_id(zmq_msg_t *msg, uint32_t routing_id)
Definition: zmq.cpp:710
void pre_allocate_sock(void *zmq_socket, const char *address, const char *port)
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
void test_pair()
#define ZMQ_CLIENT