This is the new send() function:
bool send(int value, int flags =0) { zmq::message_t msg(sizeof(int)); memcpy(msg.data(), &value, sizeof(int)); return socket_t::send(msg, flags); }A new specialized function to receive a message and format it as an int:
int recvAsInt(int flags =0) { zmq::message_t message; if(!socket_t::recv(&message, flags)) throw error_t(); return *(static_cast<int*>(message.data())); }Even the mild EAGAIN error leads to an exception. Admittedly, this is not good.
Here is how to use this functions:
// ... skRequest.send(42); // ... int value = skReply.recvAsInt();We can also have a multipart message containing ints:
skRequest.send(42, ZMQ_SNDMORE); skRequest.send(12); // ... int val1 = skReply.recvAsInt(ZMQ_RCVMORE); int val2 = skReply.recvAsInt();There is no type check on the data, so we can write weird code like this:
// ... skRequest.send("hello"); // ... int value = skReply.recvAsInt();That runs fine, but probably gives a surprising result to the user.
The zmq::Socket new version is on github.
No comments:
Post a Comment