Hi,
Could someone please help me with serialization/deserialization classes defined in .proto (protobuf). here is an exp that I am trying to build:
file.proto
message Data{
required string x1 = 1;
required uint32 x2 = 2;
required float x3 = 3;
}
message DataExge {
repeated Data data = 1;
}
client.cpp
...
void serialize(const DataExge &data_snd){
try {
ofstream ofs("DataExge");
data_snd.SerializeToOstream(&ofs);
} catch(exception &e) {
cerr << "serialize/exception: " << e.what() << endl;
exit(1);
}
}
void deserialize(DataExge &data_rec){
try {
ifstream ifs("DataExge");
data_rec.ParseFromIstream(&ifs);
} catch(exception& e) {
cerr << "deserialize/exception: " << e.what() << endl;
exit(1);
}
}
int main(){
...
DataExge dataexge;
Data *dat = dataexge.add_data();
char *y1 = "operation1";
uint32_t y2 = 123 ;
float y3 = 3.14;
// assigning data to send()
dat->set_set_x1(y1);
dat->set_set_x2(y2);
dat->set_set_x3(y3);
//sending data to the client
serialize(dataexge);
if (send(socket, &dataexge, sizeof(dataexge), 0) < 0) {
cerr << "send() failed" ;
exit(1);
}
//receiving data from the server
deserialize(dataexge);
if (recv(socket, &dataexge, sizeof(dataexge), 0) < 0) {
cerr << "recv() failed";
exit(1);
}
//printing received data
cout << dat->x1() << "\n";
cout << dat->x2() << "\n";
cout << dat->x3() << "\n";
...
}
server.cpp
...
void serialize(const DataExge &data_snd){
try {
ofstream ofs("DataExge");
data_snd.SerializeToOstream(&ofs);
} catch(exception &e) {
cerr << "serialize/exception: " << e.what() << endl;
exit(1);
}
}
void deserialize(DataExge &data_rec){
try {
ifstream ifs("DataExge");
data_rec.ParseFromIstream(&ifs);
} catch(exception& e) {
cerr << "deserialize/exception: " << e.what() << endl;
exit(1);
}
}
int main(){
...
DataExge dataexge;
Data *dat = dataexge.add_data();
//receiving data from the client
deserialize(dataexge);
if (recv(socket, &dataexge, sizeof(dataexge), 0) < 0) {
cerr << "recv() failed";
exit(1);
}
//printing received data
cout << dat->x1() << "\n";
cout << dat->x2() << "\n";
cout << dat->x3() << "\n";
// assigning data to send()
dat->set_set_x1("operation2");
dat->set_set_x2(dat->x2() + 1);
dat->set_set_x3(dat->x3() + 1.1);
//sending data to the client
serialize(dataexge); //error// I am getting error at this line ...
if (send(socket, &dataexge, sizeof(dataexge), 0) < 0) {
cerr << "send() failed" ;
exit(1);
}
...
}
Thanks for your help and replies -