zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.97k stars 757 forks source link

Examples would be nice #254

Open Slesa opened 6 years ago

Slesa commented 6 years ago

Hi all, this lib seems to be great when trying to use zeromq in C++. The first problem I ran into: how to use it?

kurdybacha commented 6 years ago

Hi @Slesa, thanks for the feedback. By how to use it, do you mean: 1) how to integrated with your project 2) how to use socket_t, message_t, ... 3) all above :smile:

Slesa commented 6 years ago

Well - I ended up this way:

NetMessaging::NetMessaging() 
: m_zmqContext(1)
{ }

bool NetMessaging::getOsVersion(const string& address, NetMsgOsVersion& version) {
    zmq::socket_t requester(m_zmqContext, ZMQ_REQ);
    requester.connect(address);
    try {
        s_send(requester, "getosversion");
        auto reply = s_recv(requester);
        /* zmq::message_t env1("getosversion", strlen("getosversion"));
        if (!requester.send(env1)) {
        return;
        } */

        //string osreply;
        /* zmq::message_t msg1;
        bool moreData;
        do {
        if (!requester.recv(&msg1)) {
        return;
        }
        //osreply += (char*)msg1.data();
        moreData = msg1.more();
        //OutputDebugStringA(msg1.gets("version"));
        } while (moreData);
        auto osreply = (char*)msg1.data(); */
        return true;
    }
    catch (zmq::error_t err)
    {
        //OutputDebugStringA(err.what);
        return false;
    }
}

Which means in detail:

What I wanted to do:

The server uses NetMQ, so tried to use

NetMQMessage message = new NetMQMessage();
message.Append("getosversion");
message.Append...

which couldn't be read in the c++ client.

Current mood: it runs somehow - do not touch ;)

kurdybacha commented 6 years ago

it is hard to read your code because of formatting. You can use socket_t::recv and socket_t::send only. We plan to add some demo to the project but for now I would suggest you take a look at our current tests e.g. client_server test from tests/active_poller.cpp where socket_t::recv and socket_t::send is used.

Slesa commented 6 years ago

Thanks. I think that helps to start with messages.

vpaladino778 commented 4 years ago

Some examples would be super helpful. I can't figure out of cppzmq automatically adds the empty delimiter on a send or if i need to add it myself.

I also cant figure out the proper way to set the ID of a message.

        std::string msg= "ExampleMessage";

        // Send JobRequest to camera to run
        zmq::message_t request (msg.length());
        memcpy (request.data (), msg.data(), msg.length());

        // Set the address, then the empty delimiter and then the request itself
        socket.send("ID", ZMQ_SNDMORE);
        socket.send("", ZMQ_SNDMORE);
        socket.send(request);
sinaaghli commented 4 years ago

Any Update on more examples for different capabilities of cppzmq?

brettviren commented 4 years ago

Developing a set of ZeroMQ Guide examples with cppzmq would be, I think, a good way to focus this kind of effort. I guess that would best be best added as part of:

https://github.com/booksbyus/zguide/tree/master/examples

Probably a new "language" name of cppzmq needs to be chosen and some work to hook this into the Guide's language links itself would ultimately be desired. But, simply populating a subdirectory with example code could could happen in parallel with that web work.

BTW, I have developed the Majordomo examples to work with cppzmq, however they are also extended to allow swapping between ROUTER/DEALER and SERVER/CLIENT so as-is are not so good as basic examples. They could maybe be stripped down to go back to just the MDP related examples. Repo is here:

https://github.com/brettviren/generaldomo

I see I've not yet picked an explicit license but I'm happy to set an OSS license to best assist in their use.

@vpaladino778 I have not seen that cppzmq does any of that message packing automatically. The packing is dependent on the underlying libzmq socket type. Even though the packing scheme is "standard" (RFC'ed) it's kind of an application-level duty to assure (one could in principle violate it if needed) so personally I think such a policy is best not implemented in cppzmq.

In general, I often find reading zmq.hpp to see what it's really doing in translating between C++ and libzmq C idioms is useful, essentially required, and not that hard.

Examples, of course do help. generaldomo has an example although it uses cppzmq multipart_t support from zmq_addon.hpp instead of explicit zmq::send_flags::sndmore.

https://github.com/brettviren/generaldomo/blob/master/src/util.cpp#L101

There are also some snippets of examples at:

https://brettviren.github.io/cppzmq-tour/