Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just started using <a href="https://developers.google.com/protocol-buffers" rel="nofollow noreferrer">Google Protocol Buffers (protobuf)</a> and also had problems sending (and receiving) messages over a computer network.</p> <p>In contrast to the Java API, the C++ API does not have a <a href="https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite" rel="nofollow noreferrer"><code>writeDelimitedTo</code></a> method to send a protobuf message with a delimiter. Without a delimiter we also have to send the size of the message, to be able to de-serialize it at the receive endpoint.</p> <p>The C++ API offers the class <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.coded_stream" rel="nofollow noreferrer"><code>::google::protobuf::io::CodedOutputStream</code></a>, defined in the header file <code>google/protobuf/io/coded_stream.h</code>.</p> <p>The following source code demonstrates how-to send a delimited protobuf message via <a href="http://boost.org/doc/libs/1_55_0/doc/html/boost_asio.html" rel="nofollow noreferrer">Boost.Asio</a> over the wire. The example uses UDP. Since I haven't found a working example on the WWW, I share it here.</p> <pre><code>#include "boost/asio.hpp" #include "google/protobuf/io/coded_stream.h" #include "google/protobuf/io/zero_copy_stream_impl.h" using ::boost::asio::ip::udp; int main() { PlayerInfo message; message.set_name("Player 1"); // ... const boost::asio::ip::address_v4 kIpAddress = boost::asio::ip::address_v4::loopback(); const unsigned short kPortNumber = 65535; try { boost::asio::io_service io_service; udp::socket socket(io_service, boost::asio::ip::udp::v4()); udp::endpoint endpoint(kIpAddress, kPortNumber); boost::system::error_code error; boost::asio::streambuf stream_buffer; std::ostream output_stream(&amp;stream_buffer); { ::google::protobuf::io::OstreamOutputStream raw_output_stream(&amp;output_stream); ::google::protobuf::io::CodedOutputStream coded_output_stream(&amp;raw_output_stream); coded_output_stream.WriteVarint32(message.ByteSize()); message.SerializeToCodedStream(&amp;coded_output_stream); // IMPORTANT: In order to flush a CodedOutputStream it has to be deleted, // otherwise a 0 bytes package is send over the wire. } } size_t len = socket.send_to(stream_buffer.data(), endpoint, 0, error); if (error &amp;&amp; error != boost::asio::error::message_size) { throw boost::system::system_error(error); } std::cout &lt;&lt; "Sent " &lt;&lt; len &lt;&lt; " bytes data to " &lt;&lt; kIpAddress.to_string() &lt;&lt; "." &lt;&lt; std::endl; } catch (const std::exception&amp; ex) { std::cerr &lt;&lt; ex.what() &lt;&lt; std::endl; } </code></pre> <p>While writing this article, I've also discovered the following two questions:</p> <ul> <li><a href="https://stackoverflow.com/questions/8269452/google-protocol-buffers-parsedelimitedfrom-and-writedelimitedto-for-c">Google Protocol Buffers: parseDelimitedFrom and writeDelimitedTo for C++</a></li> <li><a href="https://stackoverflow.com/questions/5679764/boostasiostreambuf-empty">boost::asio::streambuf empty?</a></li> </ul> <p>Both are related to this question and also contain (partial) answers. I hope my answer may be useful anyway.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload