libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_stream_empty.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 int main (void) {
34  void *ctx = zmq_ctx_new ();
35  assert (ctx);
36 
37  void *stream = zmq_socket (ctx, ZMQ_STREAM);
38  assert (stream);
39  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
40  assert (dealer);
41 
42  int rc = zmq_bind (stream, "tcp://127.0.0.1:5555");
43  assert (rc >= 0);
44  rc = zmq_connect (dealer, "tcp://127.0.0.1:5555");
45  assert (rc >= 0);
46  zmq_send (dealer, "", 0, 0);
47 
48  zmq_msg_t ident, empty;
49  zmq_msg_init (&ident);
50  rc = zmq_msg_recv (&ident, stream, 0);
51  assert (rc >= 0);
52  rc = zmq_msg_init_data (&empty, (void *) "", 0, NULL, NULL);
53  assert (rc >= 0);
54 
55  rc = zmq_msg_send (&ident, stream, ZMQ_SNDMORE);
56  assert (rc >= 0);
57  rc = zmq_msg_close (&ident);
58  assert (rc >= 0);
59 
60  rc = zmq_msg_send (&empty, stream, 0);
61  assert (rc >= 0);
62 
63  // This close used to fail with Bad Address
64  rc = zmq_msg_close (&empty);
65  assert (rc >= 0);
66 
67  close_zero_linger (dealer);
68  close_zero_linger (stream);
69  zmq_ctx_term (ctx);
70 }
#define ZMQ_STREAM
Definition: zmq.h:257
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
ZMQ_EXPORT int zmq_msg_init_data(zmq_msg_t *msg, void *data, size_t size, zmq_free_fn *ffn, void *hint)
Definition: zmq.cpp:623
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
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_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
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
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
int main(void)
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613