int main (void) { setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); void *server = zmq_socket (ctx, ZMQ_SERVER); void *client = zmq_socket (ctx, ZMQ_CLIENT); int rc; rc = zmq_bind (server, "tcp://127.0.0.1:5560"); assert (rc == 0); rc = zmq_connect (client, "tcp://127.0.0.1:5560"); assert (rc == 0); zmq_msg_t msg; rc = zmq_msg_init_size(&msg,1); assert (rc == 0); char * data = (char *)zmq_msg_data(&msg); data[0] = 1; rc = zmq_msg_send(&msg, client, 0); assert (rc == 1); rc = zmq_msg_recv(&msg, server, 0); assert (rc == 1); uint32_t routing_id = zmq_msg_get_routing_id(&msg); assert(routing_id != 0); rc = zmq_msg_close(&msg); assert (rc == 0); rc = zmq_msg_init_size (&msg, 1); assert (rc == 0); data = (char *)zmq_msg_data(&msg); data[0] = 2; rc = zmq_msg_set_routing_id(&msg, routing_id); assert (rc == 0); rc = zmq_msg_send(&msg, server, 0); assert (rc == 1); rc = zmq_msg_recv(&msg, client, 0); assert (rc == 1); rc = zmq_close (server); assert (rc == 0); rc = zmq_close (client); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); return 0 ; }
void test_client_server () { void *ctx = zmq_ctx_new (); assert (ctx); void *sb = zmq_socket (ctx, ZMQ_SERVER); assert (sb); pre_allocate_sock(sb, "127.0.0.1", "5560"); int rc = zmq_bind (sb, "tcp://127.0.0.1:5560"); assert (rc == 0); void *sc = zmq_socket (ctx, ZMQ_CLIENT); assert (sc); rc = zmq_connect (sc, "tcp://127.0.0.1:5560"); assert (rc == 0); zmq_msg_t msg; rc = zmq_msg_init_size (&msg, 1); assert (rc == 0); char *data = (char *) zmq_msg_data (&msg); data [0] = 1; rc = zmq_msg_send (&msg, sc, ZMQ_SNDMORE); assert (rc == -1); rc = zmq_msg_send (&msg, sc, 0); assert (rc == 1); rc = zmq_msg_init (&msg); assert (rc == 0); rc = zmq_msg_recv (&msg, sb, 0); assert (rc == 1); uint32_t routing_id = zmq_msg_routing_id (&msg); assert (routing_id != 0); rc = zmq_msg_close (&msg); assert (rc == 0); rc = zmq_msg_init_size (&msg, 1); assert (rc == 0); data = (char *)zmq_msg_data (&msg); data[0] = 2; rc = zmq_msg_set_routing_id (&msg, routing_id); assert (rc == 0); rc = zmq_msg_send (&msg, sb, ZMQ_SNDMORE); assert (rc == -1); rc = zmq_msg_send (&msg, sb, 0); assert (rc == 1); rc = zmq_msg_recv (&msg, sc, 0); assert (rc == 1); routing_id = zmq_msg_routing_id (&msg); assert (routing_id == 0); rc = zmq_msg_close (&msg); assert (rc == 0); rc = zmq_close (sc); assert (rc == 0); rc = zmq_close (sb); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); }
inline void set_routing_id(uint32_t routing_id) { int rc = zmq_msg_set_routing_id(&msg, routing_id); if (rc != 0) throw error_t(); }