1

为了通过套接字传输数据,我使用下面的“sendData”方法。由于数据量可能会变得非常大(~ 250000 - 500000 字节),我想使用 GZipOutputStream 来压缩 protobuf 消息。我面临的问题是,我无法获得压缩消息的大小来添加它。

我如何将 GZipOutputStream 与 CodedOutputStream 结合使用?

void CommIF::sendData(FVMsg message, vector<int> clients)
{   
  google::protobuf::uint32 message_length = message.ByteSize();
  int prefix_length = sizeof(message_length);
  int buffer_length = prefix_length + message_length;
  google::protobuf::uint8 buffer[buffer_length];

  google::protobuf::io::ArrayOutputStream array_output(buffer, buffer_length);
  google::protobuf::io::CodedOutputStream coded_output(&array_output);

  coded_output.WriteLittleEndian32(message_length);
  message.SerializeToCodedStream(&coded_output);

  for (vector<int>::iterator it = clients.begin(); it!=clients.end(); ++it) {
    int client = *it;
    int sent_bytes = write(client, buffer, buffer_length);
    if (sent_bytes != buffer_length) {
      ERROR_LOG("problems sending . Only sent " << sent_bytes << " bytes");
    }
    else
    {
      DBG_LOG("Successfully sent " << sent_bytes << " bytes");
    }
  }
}
4

0 回答 0