Use protobuf’s Gzip{Input,Output}Stream wrapper around zlib

This removes our direct zlib dependency (although of course protobuf
still uses it internally), removes a fixed 4 MiB buffer and its
corresponding limit on the terminal size, reduces some string copying,
and deletes some code.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

(Closes #230.)
This commit is contained in:
Anders Kaseorg
2012-04-17 02:50:29 -04:00
committed by Keith Winstein
parent 76f5b593d9
commit b6736eb0a5
7 changed files with 30 additions and 71 deletions
+28 -6
View File
@@ -17,13 +17,15 @@
*/
#include <assert.h>
#include <google/protobuf/io/gzip_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include "byteorder.h"
#include "transportfragment.h"
#include "transportinstruction.pb.h"
#include "compressor.h"
#include "fatal_assert.h"
using namespace google::protobuf::io;
using namespace Network;
using namespace TransportBuffers;
@@ -115,15 +117,24 @@ Instruction FragmentAssembly::get_assembly( void )
{
assert( fragments_arrived == fragments_total );
string encoded;
ZeroCopyInputStream **streams = new ZeroCopyInputStream *[fragments_total];
for ( int i = 0; i < fragments_total; i++ ) {
assert( fragments.at( i ).initialized );
encoded += fragments.at( i ).contents;
streams[i] = new ArrayInputStream( fragments.at( i ).contents.data(),
fragments.at( i ).contents.size() );
}
Instruction ret;
fatal_assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );
{
ConcatenatingInputStream stream( streams, fragments_total );
GzipInputStream gzip_stream( &stream, GzipInputStream::ZLIB );
fatal_assert( ret.ParseFromZeroCopyStream( &gzip_stream ) );
}
for ( int i = 0; i < fragments_total; i++ ) {
delete streams[i];
}
delete[] streams;
fragments.clear();
fragments_arrived = 0;
@@ -158,7 +169,18 @@ vector<Fragment> Fragmenter::make_fragments( const Instruction &inst, int MTU )
last_instruction = inst;
last_MTU = MTU;
string payload = get_compressor().compress_str( inst.SerializeAsString() );
string payload;
{
StringOutputStream stream( &payload );
GzipOutputStream::Options gzip_options;
gzip_options.format = GzipOutputStream::ZLIB;
GzipOutputStream gzip_stream( &stream, gzip_options );
fatal_assert( inst.SerializeToZeroCopyStream( &gzip_stream ) );
if ( !gzip_stream.Close() ) {
throw std::string( "zlib error: " ) + gzip_stream.ZlibErrorMessage();
}
}
uint16_t fragment_num = 0;
vector<Fragment> ret;