OLD | NEW |
(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 #ifndef BLIMP_NET_COMPRESSED_PACKET_WRITER_H_ |
| 6 #define BLIMP_NET_COMPRESSED_PACKET_WRITER_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "blimp/net/blimp_net_export.h" |
| 10 #include "blimp/net/packet_writer.h" |
| 11 #include "net/base/completion_callback.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 #include "third_party/zlib/zlib.h" |
| 14 |
| 15 namespace blimp { |
| 16 |
| 17 // Filter object which wraps a PacketWriter, adding a DEFLATE compression step |
| 18 // to outgoing packets. |
| 19 // |
| 20 // Each packet is appended as a DEFLATE block to a long-lived compression |
| 21 // stream. |
| 22 // Blocks are explicitly flushed at each packet boundary, which prevents zlib's |
| 23 // internal buffers from introducing unwanted output latency. |
| 24 // Gzip headers and CRC checks are omitted from the stream output. |
| 25 class BLIMP_NET_EXPORT CompressedPacketWriter : public PacketWriter { |
| 26 public: |
| 27 // |source|: The PacketWriter which will receive compressed packets. |
| 28 explicit CompressedPacketWriter(scoped_ptr<PacketWriter> sink); |
| 29 ~CompressedPacketWriter() override; |
| 30 |
| 31 // PacketWriter implementation. |
| 32 void WritePacket(const scoped_refptr<net::DrainableIOBuffer>& buf, |
| 33 const net::CompletionCallback& cb) override; |
| 34 |
| 35 private: |
| 36 // Compresses |src_buf| into |dest_buf|. |
| 37 // On success, returns a >= 0 value indicating the length of the |
| 38 // compressed payload. |
| 39 // On failure, returns a negative value indicating the error code |
| 40 // (see net_errors.h). |
| 41 int Compress(const scoped_refptr<net::DrainableIOBuffer>& src_buf, |
| 42 const scoped_refptr<net::GrowableIOBuffer>& dest_buf); |
| 43 |
| 44 z_stream zlib_stream_{}; |
| 45 scoped_ptr<PacketWriter> sink_; |
| 46 scoped_refptr<net::GrowableIOBuffer> compressed_buf_; |
| 47 size_t uncompressed_size_total_ = 0u; |
| 48 size_t compressed_size_total_ = 0u; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(CompressedPacketWriter); |
| 51 }; |
| 52 |
| 53 } // namespace blimp |
| 54 |
| 55 #endif // BLIMP_NET_COMPRESSED_PACKET_WRITER_H_ |
OLD | NEW |