OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_CORE_QUIC_DATA_WRITER_H_ | 5 #ifndef NET_QUIC_CORE_QUIC_DATA_WRITER_H_ |
6 #define NET_QUIC_CORE_QUIC_DATA_WRITER_H_ | 6 #define NET_QUIC_CORE_QUIC_DATA_WRITER_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <cstdint> | 9 #include <cstdint> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "net/base/int128.h" | 13 #include "net/base/int128.h" |
14 #include "net/quic/core/quic_packets.h" | 14 #include "net/quic/core/quic_packets.h" |
| 15 #include "net/quic/platform/api/quic_endian.h" |
15 #include "net/quic/platform/api/quic_export.h" | 16 #include "net/quic/platform/api/quic_export.h" |
16 #include "net/quic/platform/api/quic_string_piece.h" | 17 #include "net/quic/platform/api/quic_string_piece.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 // This class provides facilities for packing QUIC data. | 21 // This class provides facilities for packing QUIC data. |
21 // | 22 // |
22 // The QuicDataWriter supports appending primitive values (int, string, etc) | 23 // The QuicDataWriter supports appending primitive values (int, string, etc) |
23 // to a frame instance. The internal memory buffer is exposed as the "data" | 24 // to a frame instance. The internal memory buffer is exposed as the "data" |
24 // of the QuicDataWriter. | 25 // of the QuicDataWriter. |
25 class QUIC_EXPORT_PRIVATE QuicDataWriter { | 26 class QUIC_EXPORT_PRIVATE QuicDataWriter { |
26 public: | 27 public: |
27 // Creates a QuicDataWriter where |buffer| is not owned. | 28 // Creates a QuicDataWriter where |buffer| is not owned. |
28 QuicDataWriter(size_t size, char* buffer, Perspective perspective); | 29 QuicDataWriter(size_t size, |
| 30 char* buffer, |
| 31 Perspective perspective, |
| 32 Endianness endianness); |
29 | 33 |
30 ~QuicDataWriter(); | 34 ~QuicDataWriter(); |
31 | 35 |
32 // Returns the size of the QuicDataWriter's data. | 36 // Returns the size of the QuicDataWriter's data. |
33 size_t length() const { return length_; } | 37 size_t length() const { return length_; } |
34 | 38 |
35 // Retrieves the buffer from the QuicDataWriter without changing ownership. | 39 // Retrieves the buffer from the QuicDataWriter without changing ownership. |
36 char* data(); | 40 char* data(); |
37 | 41 |
38 // Methods for adding to the payload. These values are appended to the end | 42 // Methods for adding to the payload. These values are appended to the end |
39 // of the QuicDataWriter payload. Note - binary integers are written in | 43 // of the QuicDataWriter payload. |
40 // host byte order (little endian) not network byte order (big endian). | 44 |
| 45 // Writes 8/16/32/64-bit unsigned integers. |
41 bool WriteUInt8(uint8_t value); | 46 bool WriteUInt8(uint8_t value); |
42 bool WriteUInt16(uint16_t value); | 47 bool WriteUInt16(uint16_t value); |
43 bool WriteUInt32(uint32_t value); | 48 bool WriteUInt32(uint32_t value); |
44 bool WriteUInt48(uint64_t value); | |
45 bool WriteUInt64(uint64_t value); | 49 bool WriteUInt64(uint64_t value); |
| 50 |
| 51 // Writes least significant |num_bytes| of a 64-bit unsigned integer in the |
| 52 // correct byte order. |
| 53 bool WriteBytesToUInt64(size_t num_bytes, uint64_t value); |
| 54 |
46 // Write unsigned floating point corresponding to the value. Large values are | 55 // Write unsigned floating point corresponding to the value. Large values are |
47 // clamped to the maximum representable (kUFloat16MaxValue). Values that can | 56 // clamped to the maximum representable (kUFloat16MaxValue). Values that can |
48 // not be represented directly are rounded down. | 57 // not be represented directly are rounded down. |
49 bool WriteUFloat16(uint64_t value); | 58 bool WriteUFloat16(uint64_t value); |
50 bool WriteStringPiece16(QuicStringPiece val); | 59 bool WriteStringPiece16(QuicStringPiece val); |
51 bool WriteBytes(const void* data, size_t data_len); | 60 bool WriteBytes(const void* data, size_t data_len); |
52 bool WriteRepeatedByte(uint8_t byte, size_t count); | 61 bool WriteRepeatedByte(uint8_t byte, size_t count); |
53 // Fills the remaining buffer with null characters. | 62 // Fills the remaining buffer with null characters. |
54 void WritePadding(); | 63 void WritePadding(); |
55 // Write padding of |count| bytes. | 64 // Write padding of |count| bytes. |
(...skipping 21 matching lines...) Expand all Loading... |
77 | 86 |
78 char* buffer_; | 87 char* buffer_; |
79 size_t capacity_; // Allocation size of payload (or -1 if buffer is const). | 88 size_t capacity_; // Allocation size of payload (or -1 if buffer is const). |
80 size_t length_; // Current length of the buffer. | 89 size_t length_; // Current length of the buffer. |
81 | 90 |
82 // Perspective of this data writer. Please note, although client and server | 91 // Perspective of this data writer. Please note, although client and server |
83 // may have different in-memory representation of the same field, the on wire | 92 // may have different in-memory representation of the same field, the on wire |
84 // representation must be consistent. | 93 // representation must be consistent. |
85 Perspective perspective_; | 94 Perspective perspective_; |
86 | 95 |
| 96 // The endianness to write integers and floating numbers. |
| 97 Endianness endianness_; |
| 98 |
87 DISALLOW_COPY_AND_ASSIGN(QuicDataWriter); | 99 DISALLOW_COPY_AND_ASSIGN(QuicDataWriter); |
88 }; | 100 }; |
89 | 101 |
90 } // namespace net | 102 } // namespace net |
91 | 103 |
92 #endif // NET_QUIC_CORE_QUIC_DATA_WRITER_H_ | 104 #endif // NET_QUIC_CORE_QUIC_DATA_WRITER_H_ |
OLD | NEW |