Pages

New version for my 0MQ socket wrapper

Instead of using zmq::socket_t class provided by the official C++ wrapper, I have written a subclass with some extended capabilities that I find useful in making the code easier to write and maintain. I have just released a new version on github.

The change is in the way zmq::Socket manages the multipart messages to be sent and received. My original idea was stripping the separators from the vector of strings representing the complete message. You can see the code as it was originally thought in the github history.

Now I realized that this supposed simplification was putting too much pressure on the user code, that could be confused by the behavior of this class.

The zmq::Socket code is cleaner. For instance here is the new main loop in Socket::blockingRecv():
do {
    zmq::message_t message;
    if(!socket_t::recv(&message, 0))
        throw error_t();

    const char* base = static_cast(message.data());
    frames.push_back(std::string(base, base + message.size()));
} while(sockopt_rcvmore());
We receive on the socket till the flag on the socket itself is set to show that there is something more to be read. Each message, without caring of any detail, is pushed in the string vector that is going to be returned to the caller.

As comparison, this was the previous code:
int currentFrame = 1;
do {
    zmq::message_t message;
    if(!socket_t::recv(&message, 0))
        throw error_t();

    if(!(currentFrame++ % 2))
    { // skipping separators
        if(message.size())
            throw error_t();
    }
    else
    {
        const char* base = static_cast(message.data());
        frames.push_back(std::string(base, base + message.size()));
    }
} while(sockopt_rcvmore());
Separators was assumed to be in even positions, and simply discarded.

Alternatively, I should have had to provide a way to let the user code to decide if it wanted to send/receive multipart messages with or without separator. And it looked to me fishing for troubles.

No comments:

Post a Comment