linux - Parsing protocol buffers in c++ -


i wanted write protocol buffers socket , read client side. things didn't work wrote decoding part in server right after encoded it. can please take @ code below , tell me doing wrong ?

(i had use arraystream , coded stream can write delimiter)

int bytes_written = tdata.bytesize() + sizeof(google::protobuf::uint32); google::protobuf::uint8 buffer[bytes_written]; memset(buffer, '\0', bytes_written); google::protobuf::io::arrayoutputstream aos(buffer,bytes_written); google::protobuf::io::codedoutputstream *coded_output = new google::protobuf::io::codedoutputstream(&aos); google::protobuf::uint32 size_  = tdata.bytesize(); coded_output->writevarint32(size_);  tdata.serializetocodedstream(coded_output);  int sent_bytes = 0; std::cout << buffer << std::endl; if ( (sent_bytes = send(liveconnections.at(i), buffer, bytes_written, msg_nosignal)) == -1 )     liveconnections.erase(liveconnections.begin() + i); else     std::cout << "sent "  << sent_bytes << " bytes " << << std::endl;  delete coded_output;    ////////////////   google::protobuf::uint8 __buffer[sizeof(google::protobuf::uint32)]; memset(__buffer, '\0', sizeof(google::protobuf::uint32)); memcpy (__buffer, buffer, sizeof(google::protobuf::uint32));  google::protobuf::uint32 __size = 0;  google::protobuf::io::arrayinputstream ais(__buffer,sizeof(google::protobuf::uint32)); google::protobuf::io::codedinputstream coded_input(&ais); coded_input. readvarint32(&__size); std::cout <<" size of payload "<<__size << std::endl;  google::protobuf::uint8 databuffer[__size]; memset(databuffer, '\0', __size); memcpy (databuffer, buffer+sizeof(google::protobuf::uint32), __size);      std::cout << "databuffs " << "size " << __size << "  "<< databuffer << std::endl; google::protobuf::io::arrayinputstream array_input(databuffer,__size); google::protobuf::io::codedinputstream _coded_input(&array_input); data_model::terminal_data* tdata = new data_model::terminal_data(); if (!tdata->parsefromcodedstream(&_coded_input)) {     std::cout << "data not parsed" << std::endl;      } else {     std::cout <<" symbol --" << tdata->symbol_name() << std::endl; } delete tdata; 

out put of program:

size of payload 55 databuffs size 55  c109"056*    banknifty0���20140915@�j    145406340 data not parsed c109"056*   banknifty0���20140915@�j    145406340 

writevarint32 doesn't write 4 bytes, , readvarint32 doesn't read 4 bytes. "var" stands "variable", in "variable length encoding".

when encoding, write size (which can take little 1 byte), proto. when decoding, read size, advance 4 bytes, read proto. so, parsing starting wrong offset.

use currentposition() after readvarint32 figure out how many bytes size indicator consumed. advance number of bytes.


Comments

Popular posts from this blog

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -

java - How to specify maven bin in eclipse maven plugin? -