After a lot of wasted time due to not knowing c++ very well and after a venture into different solutions like gRPC... I have finally found a working solution.
The problem was in the c++ side where some character was appended when i was trying to send the message.c_str() directly via the socket.
working solution for sending a simple protobuf message in c++:
std::string data;
SampleHello hello;
hello.set_name("Hello");
hello.set_num(12);
hello.SerializeToString(&data);
char messageToBeSent[data.length()];
sprintf(messageToBeSent, "%s", data);
//simply sends via the socket using write(mSocket, msgToBeSent, msgSize);
session->send(messageToBeSent,sizeof(messageToBeSent));
In case you re like me and instead of using the sane approach of using boost
network streams, you implemented your own circular buffer without using
ostream.
This is how you can receive a message
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
typedef google::protobuf::io::ArrayInputStream arrayIStream;
void HandlePacket(int packetSize)
{
char* packet = newchar[packetSize]; //allocate some space for the message
rcvBuffer.Read(packet,packetSize); //the buffer where our received messages are
arrayIStream arr(rcvBuffer.GetBuffer(), packetSize); //pass the received message to a zero coded input object so we can use the delimited functions
HeaderProto header; //the header as described in the .proto file
if (readDelimitedFrom(&arr,&header)) //readDelimitedFrom is the c++ solution as provided from the author of the c++ lib (look below for link)
{
//Do stuff with your message
}
delete packet;
}
readDelimitedFrom solution