本文整理汇总了C++中zmq_msg_recv函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_msg_recv函数的具体用法?C++ zmq_msg_recv怎么用?C++ zmq_msg_recv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_msg_recv函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
virtual void run()
{
zmq_msg_t msg;
int rc = zmq_msg_init(&msg);
assert(rc == 0);
rc = zmq_msg_recv(&msg, socket_, ZMQ_DONTWAIT);
assert(rc != -1);
if (rc > 0) {
std::cout << id_ << ": Persisting data from member TODO." << std::endl;
}
}
示例2: receiveStringFrame
/**
* Receive a single frame from a socket and store the results in a string.
* The frame is not expected to contain a terminating NUL character.
* @param socket The socket to receive from
* @param str A reference to a string that will be set to the received value
* @return -1 on error (--> str value is unchanged). 0 else
*/
static inline int receiveStringFrame(void* socket, std::string& str) {
zmq_msg_t msg;
zmq_msg_init(&msg);
int rc = zmq_msg_recv(&msg, socket, 0);
if (rc == -1) {
return -1;
}
str = std::string((char*) zmq_msg_data(&msg), zmq_msg_size(&msg));
zmq_msg_close(&msg);
return 0;
}
示例3: main
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to receive messages on
void *receiver = zmq_socket (context, ZMQ_PULL);
zmq_connect (receiver, "tcp://localhost:5557");
// Socket to send messages to
void *sender = zmq_socket (context, ZMQ_PUSH);
zmq_connect (sender, "tcp://localhost:5558");
// Socket for control input
void *controller = zmq_socket (context, ZMQ_SUB);
zmq_connect (controller, "tcp://localhost:5559");
zmq_setsockopt (controller, ZMQ_SUBSCRIBE, "", 0);
// Process messages from receiver and controller
zmq_pollitem_t items [] = {
{ receiver, 0, ZMQ_POLLIN, 0 },
{ controller, 0, ZMQ_POLLIN, 0 }
};
// Process messages from both sockets
while (true) {
zmq_msg_t message;
zmq_poll (items, 2, -1);
if (items [0].revents & ZMQ_POLLIN) {
zmq_msg_init (&message);
zmq_msg_recv (&message, receiver, 0);
// Do the work
s_sleep (atoi ((char *) zmq_msg_data (&message)));
// Send results to sink
zmq_msg_init (&message);
zmq_msg_send (&message, sender, 0);
// Simple progress indicator for the viewer
printf (".");
fflush (stdout);
zmq_msg_close (&message);
}
// Any waiting controller command acts as 'KILL'
if (items [1].revents & ZMQ_POLLIN)
break; // Exit loop
}
// Finished
zmq_close (receiver);
zmq_close (sender);
zmq_close (controller);
zmq_ctx_destroy (context);
return 0;
}
示例4: process
void process( zeq::detail::Socket& socket )
{
zmq_msg_t msg;
zmq_msg_init( &msg );
zmq_msg_recv( &msg, socket.socket, 0 );
const std::string address( (const char*)zmq_msg_data( &msg ),
zmq_msg_size( &msg ));
_receiver.addConnection( std::string( "tcp://" ) + address );
zmq_msg_send( &msg, socket.socket, 0 );
zmq_msg_close( &msg );
}
示例5: safe_recv_from_server
static char* safe_recv_from_server (void *socket, int *size) {
zmq_msg_t message;
zmq_msg_init (&message);
*size = zmq_msg_recv (&message, socket, 0);
if (*size == -1)
return NULL;
char *string = malloc (*size + 1);
memcpy (string, zmq_msg_data (&message), *size);
zmq_msg_close (&message);
string [*size] = 0;
return (string);
}
示例6: zmq_socket
static void *Log_internal_thread(void *spec)
{
zmq_msg_t msg;
int rc = 0;
LogConfig *config = spec;
void *socket = zmq_socket(ZMQ_CTX, ZMQ_SUB);
check(socket, "Could not bind the logging subscribe socket.");
// warning: could cause threading problems if more than one of these
rc = zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0);
check(rc == 0, "Could not subscribe to the logger.");
#ifdef ZMQ_LINGER
int opt = 0;
rc = zmq_setsockopt(socket, ZMQ_LINGER, &opt, sizeof(opt));
check(rc == 0, "Could not set the linger option.");
#endif
rc = zmq_connect(socket, bdata(config->log_spec));
check(rc == 0, "Could connect to logging endpoint: %s", bdata(config->log_spec));
while(1) {
rc = zmq_msg_init(&msg);
check(rc == 0, "Failed to initialize message.");
rc = zmq_msg_recv(&msg, socket, 0);
if(rc == -1 && errno == ETERM) {
// The ZMQ context has been terminated, must be shutting down.
break;
}
check(rc >= 0, "Failed to receive from the zeromq logging socket");
check(zmq_msg_size(&msg) > 0, "Received poison pill, log thread exiting.");
fprintf(config->log_file, "%.*s", (int)zmq_msg_size(&msg), (char *)zmq_msg_data(&msg));
rc = zmq_msg_close(&msg);
check(rc == 0, "Message close failed.");
}
rc = zmq_close(socket);
check(rc == 0, "Could not close socket");
LogConfig_destroy(config);
return NULL;
error:
LogConfig_destroy(config);
// could leak the socket and the msg but not much we can do
return NULL;
}
示例7: recv_data
inline int recv_data(void * socket, function<int(char * data, size_t len)> process_input){
zmq_msg_t msg;
int rc = zmq_msg_init(&msg);
assert(rc == 0);
rc = zmq_msg_recv(&msg, socket, 0);
assert(rc >= 0);
rc = process_input(reinterpret_cast<char*>(zmq_msg_data(&msg)), zmq_msg_size(&msg));
zmq_msg_close(&msg);
return rc;
}
示例8: test_multiple_connects
void test_multiple_connects ()
{
const unsigned int no_of_connects = 10;
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
void *connectSocket [no_of_connects];
// Connect first
for (unsigned int i = 0; i < no_of_connects; ++i)
{
connectSocket [i] = zmq_socket (ctx, ZMQ_PUSH);
assert (connectSocket [i]);
rc = zmq_connect (connectSocket [i], "inproc://multiple");
assert (rc == 0);
// Queue up some data
rc = zmq_send_const (connectSocket [i], "foobar", 6, 0);
assert (rc == 6);
}
// Now bind
void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
assert (bindSocket);
rc = zmq_bind (bindSocket, "inproc://multiple");
assert (rc == 0);
for (unsigned int i = 0; i < no_of_connects; ++i)
{
// Read pending message
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_msg_recv (&msg, bindSocket, 0);
assert (rc == 6);
void *data = zmq_msg_data (&msg);
assert (memcmp ("foobar", data, 6) == 0);
}
// Cleanup
for (unsigned int i = 0; i < no_of_connects; ++i)
{
rc = zmq_close (connectSocket [i]);
assert (rc == 0);
}
rc = zmq_close (bindSocket);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}
示例9: main
int main(void)
{
void *context = zmq_ctx_new();
void *receiver = zmq_socket(context, ZMQ_PULL);
zmq_connect(receiver, "tcp://localhost:5557");
void *subscriber = zmq_socket(context, ZMQ_SUB);
zmq_connect(subscriber, "tcp://localhost:5556");
zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "10001 ", 6);
zmq_pollitem_t items[] = {
{receiver, 0, ZMQ_POLLIN, 0},
{subscriber, 0, ZMQ_POLLIN, 0},
};
while(1) {
zmq_msg_t message;
zmq_poll(items, 2, -1);
if (items[0].revents & ZMQ_POLLIN) {
zmq_msg_init(&message);
zmq_msg_recv(&message, receiver, 0);
printf("receiver: %s\n", (char *)zmq_msg_data(&message));
zmq_msg_close(&message);
}
if (items[1].revents & ZMQ_POLLIN) {
zmq_msg_init(&message);
zmq_msg_recv(&message, subscriber, 0);
printf("subscribe: %s\n", (char *)zmq_msg_data(&message));
zmq_msg_close(&message);
}
}
zmq_close(receiver);
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
示例10: switch_msg_from_to
static inline void switch_msg_from_to( void *from, void *to ) {
zmq_msg_t msg;
int more = 1;
while( more ) {
zmq_msg_init( &msg );
int ret = zmq_msg_recv( &msg, from, 0 );
assert( ret != -1 );
more = zmq_msg_more( &msg );
ret = zmq_msg_send( &msg, to, more ? ZMQ_SNDMORE : 0 );
assert( ret != -1 );
}
}
示例11: _zmq2agent_receive_acks
static void
_zmq2agent_receive_acks (struct _zmq2agent_ctx_s *ctx)
{
int rc;
zmq_msg_t msg;
do {
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, ctx->zagent, ZMQ_DONTWAIT);
if (rc > 0)
_zmq2agent_manage_ack (ctx, &msg);
zmq_msg_close (&msg);
} while (rc >= 0);
}
示例12: s_recv
// Receive 0MQ string from socket and convert into C string
static char *
s_recv (void *socket) {
zmq_msg_t message;
zmq_msg_init (&message);
int size = zmq_msg_recv (&message, socket, 0);
if (size == -1)
return NULL;
char *string = malloc (size + 1);
memcpy (string, zmq_msg_data (&message), size);
zmq_msg_close (&message);
string [size] = 0;
return (string);
}
示例13: zmq_socket
void ConnectedApp::initConnection(string name_in) {
int rc;
name_client = zmq_socket(context, ZMQ_REQ);
rc = zmq_connect(name_client, "ipc:///tmp/name");
// send a name request
Message m("name");
m.send(name_client);
cout << "ConnectedApp: send name request " << endl << flush;
// poll for a few seconds
zmq_pollitem_t items [] = {
{ name_client, 0, ZMQ_POLLIN, 0 }
};
zmq_poll(items, 1, 2 * 1000);
if (items[0].revents & ZMQ_POLLIN) {
// use the received name
zmq_msg_t msg;
zmq_msg_init(&msg);
int r = zmq_msg_recv(&msg, name_client, 0);
cout << "ConnectedApp: got name reply " << r << endl << flush;
Message in_m(&msg);
messages.push_back(in_m.message_string);
zmq_msg_close(&msg);
cout << "got name " << in_m.name << endl;
this->name = in_m.name;
address.str = "ipc:///tmp/" + name;
server = zmq_socket(context, ZMQ_REP);
rc = zmq_bind(server, address.str.c_str());
} else {
// if did not get a reply, use the tcp socket
address.str = "tcp://*:" + ofToString(port_number);
server = zmq_socket(context, ZMQ_REP);
rc = zmq_bind(server, address.str.c_str());
cout << "ConnectedApp: no name reply" << endl << flush;
}
}
示例14: client_loop_frontend
static void
client_loop_frontend (client_state* state, void *frontend)
{
zmq_msg_t zmessage;
while (1) {
// Process all parts of the message
//printf("RECEIVED \n");
zmq_msg_init (&zmessage);
zmq_msg_recv (&zmessage, frontend, 0);
int more = zmq_msg_more (&zmessage);
if(more)
{
zmq_msg_close (&zmessage);
continue;
}
zchat_message* message = zchat_message_deserialize_from_zmq_msg(&zmessage);
if(message == NULL)
{
zchat_log("Message deserialisation error");
zmq_msg_close (&zmessage);
return;
}
if(message->type() == zchat_message_message_type_PONG)
{
//ECHO_2_STR("PONG", message->ShortDebugString().c_str());;
process_pong_message(state, message);
}
if(message->type() == zchat_message_message_type_PING)
{
//ECHO_2_STR("PING", message->ShortDebugString().c_str());
client_state_reset_heartbeat(state);
client_state_set_heartbeat_time(state);
add_pong_message(state);
}
client_state_reset_heartbeat(state);
client_state_set_heartbeat_time(state);
ECHO_2_STR("RECEIVED", message->ShortDebugString().c_str());
zchat_message_destroy(message);
zmq_msg_close (&zmessage);
break; // Last message part
}
}
示例15: worker_routine
static void * worker_routine (void *context) {
char *query_string, *datos_jugador;
query_string = NULL;
query_string = (char *) malloc(10 * sizeof(char));
datos_jugador = NULL;
datos_jugador = (char *) malloc(10 * sizeof(char));
// Socket to talk to dispatcher
void *receiver = zmq_socket (context, ZMQ_REP);
zmq_connect (receiver, "inproc://example");
while (1) {
zmq_msg_t request;
zmq_msg_init(&request);
zmq_msg_recv(&request,receiver,0);
query_string = (char *)zmq_msg_data(&request);
zmq_msg_close(&request);
printf("%s\n", query_string);
if(strcmp(query_string,"%") == 0){
datos_jugador = "1|1,2,3,4,5,6,7,8,9|0";
}else{
datos_jugador = query_string;
}
printf("%s\n", datos_jugador);
sleep (1); // Do some 'work'
//Envio de respuesta al cliente
zmq_msg_t reply;
zmq_msg_init_size(&reply, 23);
memcpy(zmq_msg_data(&reply), datos_jugador, 23);
zmq_msg_send(&reply,receiver,0);
zmq_msg_close(&reply);
char *string = s_recv (receiver);
printf ("Received request: [%s]\n", string);
free (string);
// Do some 'work'
sleep (1);
// Send reply back to client
s_send (receiver, "World");
}
zmq_close (receiver);
return NULL;
}