После многих потраченных впустую времени из-за того, что не очень хорошо знал c ++, и после того, как занялся различными решениями, такими как gRPC ... Я наконец нашел рабочее решение.
Проблема была в стороне c ++, где некоторые символы были добавлены, когда я пытался отправить message.c_str () напрямую через сокет.
рабочее решение для отправки простого сообщения protobuf на 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));
В случае, если я вам нравлюсь, а не вменяемый подход, использующий повышение
сетевые потоки, вы реализовали свой собственный круговой буфер без
ostream.
Вот как вы можете получить сообщение
#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 решения