// // Created by miti on 21/07/19. // #include "protobufReadWrite.h" namespace protobufReadWrite { int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite &message) { ZeroCopyInputStream *raw_input; CodedInputStream *coded_input; uint32_t size; CodedInputStream::Limit limit; raw_input = new FileInputStream(accept_fd); coded_input = new CodedInputStream(raw_input); if (!coded_input->ReadVarint32(&size)) { printf("Error in reading size of msg"); fflush(stdout); return -1; } //printf("size of msg was read to be %" PRIu32 " \n", size); fflush(stdout); limit = coded_input->PushLimit(size); if (!message.ParseFromCodedStream(coded_input)) { printf("Error in parsing msg"); fflush(stdout); return -1; } coded_input->PopLimit(limit); delete raw_input; delete coded_input; return 0; } int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite &message) { ZeroCopyOutputStream *raw_output = new FileOutputStream(accept_fd); CodedOutputStream *coded_output = new CodedOutputStream(raw_output); coded_output->WriteVarint32(message.ByteSize()); if (!message.SerializeToCodedStream(coded_output)) { printf("SerializeToCodedStream failed"); fflush(stdout); return -1; } // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1 // TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually) delete coded_output; delete raw_output; fflush(stdout); return 0; } };