[zeromq-dev] How to send/receive a custom C++ object via zeromq sockets?

Michael Haberler mail17 at mah.priv.at
Wed Sep 18 08:51:26 CEST 2013

Am 18.09.2013 um 04:53 schrieb Michael Scofield <bayinamine at gmail.com>:

> I strongly recommend Google's Protocol Buffer as the serialization/deserialization tool. It's fast, convenient and robust. It directly transform an object to the bytes formation(and vice-versa), so can be send/recv through zeromq sockets. We are using it in our online game, and it works well so far.

This also reflects my experience. There is no point to be inventive on data formats of this kind, use protobuf and be done with it - learn once, never go back.


A heads-up for those looking at protobuf versus JSON (which was the other contender for my current project):

It turns out protobuf messages can be fully automatically translated to and from JSON without any format-specific code, including full type checking against the protobuf schema, using protobuf reflection

Pavel Shramov did the conversion code in C++ and Python which I'm using: https://github.com/shramov/json2pb

together with libwebsockets this gives me the capability to send/receive JSON objects from/to a browser and have it automatically interfaced as protobuf objects into my zmq zoo. It is super-convenient to edit a proto file, hit make, and everything else follows (except the Javascipt coding)

in my case that even includes protobuf en/decoders which run in a realtime Linux setting, worst case as kernel modules; those are interfaced by a lock-free ringbuffer, also done by Pavel: https://github.com/shramov/ring which hooks into zmq on the userland side 

- Michael

> On 2013年09月18日 00:27, Riskybiz wrote:
>> So, I’ve got a working implementation of a zeromq REQ-REP socket pattern and am using it to pass data in the form of character strings between a server and several clients, like so:
>> Sample comma separated data strings sent as a multipart message (of several thousand parts), each line is a complete instruction for the client to draw something on screen:
>> 414,2095, at ESU13,upper,1,1,41528.600694,41528.602778,1679.75000,1679.75000,16776960,3,0,1
>> 415,2095, at ESU13,lower,1,1,41528.600694,41528.602778,1679.50000,1679.50000,16776960,3,0,1
>> 416,2095, at ESU13,marker,1,1,41528.602778,41528.602778,1679.75000,1680.50000,16776960,3,0,0
>> 417,2095, at ESU13,rectangle,1,1,41528.600694,41528.603472,1679.75000,1679.50000,65535,0,0,1
>> 418,2095, at ESU13,label,1,1,41528.600694,1679.50000,ID:2095 0.0149% 5Bars 100D 3.00IR,Arial,5.00000,0,0
>> My issue is that each of the data strings has to be composed at the server and then decoded at the client end to extract the data; this works but is not very elegant, I’m sure there is a better and more efficient way………
>> I have the notion that I’d like to try encapsulating each line of the data to be sent in a custom class object object instance; then serialize each object, pass it through the REQ-REP sockets and deserialise to reconstruct the object at the client end.  The simple class would be in the format like so; only comprising data members.
>> class Instruction
>> {
>> public:
>> //Constructor
>> Instruction(const unsigned int &intData, const double &dblData, const std::string &strData): IntData(intData), DblData(dblData), StrData(strData)
>> {
>> }
>> //Data Members
>> const unsigned int IntData;
>> const double DblData;
>> const std::string StrData;
>> };//class
>> Trouble is I don’t know how to implement this practically.  Is anyone able to provide a simple example of how to accomplish serialising and deserialising a custom object and sending it though a REQ-REP socket pair?  Or does anyone know of a good online tutorial on this subject? I’m working with Visual Studio 2012 Express in C++.
>> With very many thanks,
>> Riskybiz.
>> _______________________________________________
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev

More information about the zeromq-dev mailing list