| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 NET_QUIC_QUIC_DATA_WRITER_H_ | |
| 6 #define NET_QUIC_QUIC_DATA_WRITER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <cstddef> | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/strings/string_piece.h" | |
| 17 #include "net/base/int128.h" | |
| 18 #include "net/base/net_export.h" | |
| 19 #include "net/quic/quic_protocol.h" | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 // This class provides facilities for packing QUIC data. | |
| 24 // | |
| 25 // The QuicDataWriter supports appending primitive values (int, string, etc) | |
| 26 // to a frame instance. The internal memory buffer is exposed as the "data" | |
| 27 // of the QuicDataWriter. | |
| 28 class NET_EXPORT_PRIVATE QuicDataWriter { | |
| 29 public: | |
| 30 // Creates a QuicDataWriter where |buffer| is not owned. | |
| 31 QuicDataWriter(size_t size, char* buffer); | |
| 32 | |
| 33 ~QuicDataWriter(); | |
| 34 | |
| 35 // Returns the size of the QuicDataWriter's data. | |
| 36 size_t length() const { return length_; } | |
| 37 | |
| 38 // Retrieves the buffer from the QuicDataWriter without changing ownership. | |
| 39 char* data(); | |
| 40 | |
| 41 // Methods for adding to the payload. These values are appended to the end | |
| 42 // of the QuicDataWriter payload. Note - binary integers are written in | |
| 43 // host byte order (little endian) not network byte order (big endian). | |
| 44 bool WriteUInt8(uint8_t value); | |
| 45 bool WriteUInt16(uint16_t value); | |
| 46 bool WriteUInt32(uint32_t value); | |
| 47 bool WriteUInt48(uint64_t value); | |
| 48 bool WriteUInt64(uint64_t value); | |
| 49 // Write unsigned floating point corresponding to the value. Large values are | |
| 50 // clamped to the maximum representable (kUFloat16MaxValue). Values that can | |
| 51 // not be represented directly are rounded down. | |
| 52 bool WriteUFloat16(uint64_t value); | |
| 53 bool WriteStringPiece16(base::StringPiece val); | |
| 54 bool WriteBytes(const void* data, size_t data_len); | |
| 55 bool WriteRepeatedByte(uint8_t byte, size_t count); | |
| 56 // Fills the remaining buffer with null characters. | |
| 57 void WritePadding(); | |
| 58 | |
| 59 size_t capacity() const { return capacity_; } | |
| 60 | |
| 61 private: | |
| 62 // Returns the location that the data should be written at, or nullptr if | |
| 63 // there is not enough room. Call EndWrite with the returned offset and the | |
| 64 // given length to pad out for the next write. | |
| 65 char* BeginWrite(size_t length); | |
| 66 | |
| 67 char* buffer_; | |
| 68 size_t capacity_; // Allocation size of payload (or -1 if buffer is const). | |
| 69 size_t length_; // Current length of the buffer. | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(QuicDataWriter); | |
| 72 }; | |
| 73 | |
| 74 } // namespace net | |
| 75 | |
| 76 #endif // NET_QUIC_QUIC_DATA_WRITER_H_ | |
| OLD | NEW |