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