Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: blimp/net/compressed_packet_writer.cc

Issue 1825263003: Blimp: add packet-level DEFLATE compression using zlib. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: wez feedback. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "blimp/net/compressed_packet_writer.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "base/sys_byteorder.h"
11 #include "blimp/net/common.h"
12 #include "net/base/io_buffer.h"
13 #include "third_party/zlib/zlib.h"
14
15 namespace blimp {
16 namespace {
17
18 // Allocate the maxmimum amount of memory to deflate (512KB) for higher
19 // compression. (See zconf.h for details on memLevel semantics.)
20 const int kZlibMemoryLevel = 9;
21
22 } // namespace
23
24 CompressedPacketWriter::CompressedPacketWriter(scoped_ptr<PacketWriter> sink)
25 : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) {
26 DCHECK(sink_);
27
28 // MAX_WBITS means we are using the maximal window size for decompression;
29 // negating it means that we are ignoring headers and CRC checks.
30 int err = deflateInit2(&zlib_stream_, Z_BEST_COMPRESSION, Z_DEFLATED,
Wez 2016/03/29 21:38:38 nit: zlib_error or init_error?
Kevin M 2016/03/29 23:48:24 Done.
31 -MAX_WBITS, // Negative value = no headers or CRC.
32 kZlibMemoryLevel, Z_DEFAULT_STRATEGY);
33 DCHECK_EQ(err, Z_OK);
34 }
35
36 CompressedPacketWriter::~CompressedPacketWriter() {
37 deflateEnd(&zlib_stream_);
Wez 2016/03/29 21:38:38 You're calling deflateEnd() here but only DCHECK()
Kevin M 2016/03/29 23:48:24 Good point, done
38 }
39
40 void CompressedPacketWriter::WritePacket(
41 const scoped_refptr<net::DrainableIOBuffer>& buf,
Wez 2016/03/29 21:38:38 nit: buf & cb should have style-guide-compliant na
Kevin M 2016/03/29 23:48:23 Done.
Kevin M 2016/03/29 23:48:24 Done.
42 const net::CompletionCallback& cb) {
43 DCHECK(buf);
44 DCHECK(!cb.is_null());
45 size_t uncompressed_size = static_cast<size_t>(buf->BytesRemaining());
Wez 2016/03/29 21:38:38 Use a checked cast here (see the header in base/).
Kevin M 2016/03/29 23:48:24 Done.
46
47 if (uncompressed_size == 0) {
48 // Zero-length input => zero-length output.
Wez 2016/03/29 21:38:38 nit: Move this outside the if block, and add a com
Kevin M 2016/03/29 23:48:24 Done.
Kevin M 2016/03/29 23:48:24 Done.
49 sink_->WritePacket(buf, cb);
50 return;
51 }
52 if (uncompressed_size > kMaxPacketPayloadSizeBytes) {
53 cb.Run(net::ERR_FILE_TOO_BIG);
54 return;
55 }
56
57 int compress_result = Compress(buf, compressed_buf_);
58 if (compress_result < 0) {
59 cb.Run(compress_result);
60 return;
61 }
62 uncompressed_size_total_ += uncompressed_size;
63 compressed_size_total_ += compress_result;
Wez 2016/03/29 21:38:38 Do we log these in non-DVLOG enabled builds? If no
Kevin M 2016/03/29 23:48:24 Done.
Kevin M 2016/03/29 23:48:24 Done.
64
65 scoped_refptr<net::DrainableIOBuffer> compressed_outbuf(
66 new net::DrainableIOBuffer(compressed_buf_.get(), compress_result));
67 sink_->WritePacket(compressed_outbuf, cb);
68 DVLOG(4) << "deflate packet: " << uncompressed_size << " in, "
69 << compress_result << " out.";
70 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, "
71 << compressed_size_total_ << " out.";
72 }
73
74 int CompressedPacketWriter::Compress(
75 const scoped_refptr<net::DrainableIOBuffer>& src_buf,
76 const scoped_refptr<net::GrowableIOBuffer>& dest_buf) {
77 DCHECK_EQ(dest_buf->offset(), 0);
Wez 2016/03/29 21:38:38 nit: Prefer DCHECK_EQ(expected, actual) nit: Foll
Kevin M 2016/03/29 23:48:24 Done.
78 const int zlib_output_ubound =
79 deflateBound(&zlib_stream_, src_buf->BytesRemaining());
80 if (dest_buf->capacity() < zlib_output_ubound) {
81 dest_buf->SetCapacity(zlib_output_ubound);
82 }
83
84 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data());
85 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining());
86 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(dest_buf->data());
87 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound);
88 int err = deflate(&zlib_stream_, Z_SYNC_FLUSH);
Wez 2016/03/29 21:38:38 See request above re naming.
Kevin M 2016/03/29 23:48:23 Done.
89
90 if (err != Z_OK) {
91 DLOG(FATAL) << "Unexpected deflate() return value: " << err;
92 return net::ERR_UNEXPECTED;
93 }
94 if (zlib_stream_.avail_in > 0) {
95 DLOG(ERROR) << "deflate() did not consume all data, remainder: "
96 << zlib_stream_.avail_in << " bytes.";
97 return net::ERR_UNEXPECTED;
98 }
99
100 return zlib_output_ubound - zlib_stream_.avail_out;
101 }
102
103 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698