0

SO helpful folks:

This is another NEWBIE question. I am new at C++, Google Protocol Buffers, and serializing messages over HTTP without SOAP (even with SOAP for that matter). I am trying to send image data over http. I was told that Google Protocol Buffers was the way to go. So I eagerly started going through all of the documentation and used CocoaPods to install and include Google Protocol Buffer classes to my project. I created my .protoc files and generated the classes. Now I'm populating the data using the generated classes. Now what???

I cannot find any information on how to send the data. I have found a few other questions on SO that contained what might be meaningful information if I had a clue. This is what I have so far:

void message::myMessage::transmit(const uint32_t ipaddress, uint32_t port, message::MatMessage* rawImage)
{
    message::HostMessage *transmitter;
    transmitter->set_ipaddress(ipaddress);
    transmitter->set_port(port);

    //open socket with ip and port

    //send unsing socket

}

HostMessage (*transmitter) is generated C++ class and header from a .protoc file that contains only the ipAddress and port number. OK, so I have this transmitter with an ipaddress and a port. Now what? How do I open a socket with it?

Maybe once I open a socket the other answers will make more sense to me. Can someone please help unconfuse me?

Thank you.

Patricia
  • 5,019
  • 14
  • 72
  • 152
  • 2
    You need to write the socket code yourself, protocol buffers doesn't offer much help there. Read up on sockets for your platform, create/connect the endpoints, serialize the protocol buffer messages (e.g use ArrayInputStream and CodedInputStream or SerializeToString), write the packet length and then the data, at the receiving end, read the packet length, then data, and deserialize the data (ParseFromCodedStream). With a little bit of searching, you'll find some examples here and there going through the serializing/deserializing process. – imbtfab Apr 24 '14 at 21:59

1 Answers1

4

Protocol Buffers is just a serialization tool. It will convert your message object into bytes, and will convert bytes back into a message object. The library does not directly implement a way to transport those bytes.

If you have a raw socket, you could wrap it in FileInputStream/FileOutputStream to read/write protobufs from/to it*. Since protobufs are not self-delimiting, you need to write the size as a prefix, followed by the data, and interpret these correctly on the receiving end. See my answer to this other question for code to do that: Are there C++ equivalents for the Protocol Buffers delimited I/O functions in Java?

Another alternative is to use a higher-level transport library like ZeroMQ, which implements sending and receiving of byte-blob messages. Use protobufs to encode/decode the byte blobs, then hand them off to ZeroMQ for transport.

* This works on Unix, where sockets are file descriptors. On Windows, they aren't, so you'll need to implement the ZeroCopyInputStream/ZeroCopyOutputStream directly in terms of send()/recv(), which is not too hard if you use CopyingInputStreamAdapter/CopyingOutputStreamAdaptor.

Community
  • 1
  • 1
Kenton Varda
  • 41,353
  • 8
  • 121
  • 105