Chromium Code Reviews| 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 <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "net/quic/quic_protocol.h" | |
| 13 #include "net/quic/uint128.h" | |
| 14 #include "base/string_piece.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 // This class provides facilities for packing QUIC data. | |
| 19 // | |
| 20 // The QuicDataWriter supports appending primitive values (int, string, etc) | |
| 21 // to a frame instance. The QuicDataWriter grows its internal memory buffer | |
| 22 // dynamically to hold the sequence of primitive values. The internal memory | |
| 23 // buffer is exposed as the "data" of the QuicDataWriter. | |
| 24 class QuicDataWriter { | |
| 25 public: | |
| 26 ~QuicDataWriter(); | |
| 27 | |
| 28 explicit QuicDataWriter(size_t length); | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: constructors then destructors.
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 29 | |
| 30 // Returns the size of the QuicDataWriter's data. | |
| 31 size_t length() const { return length_; } | |
| 32 | |
| 33 // Takes the buffer from the QuicDataWriter. | |
| 34 char* take() { | |
| 35 char* rv = buffer_; | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: Usually we try not to put much code in the he
Ryan Hamilton
2012/10/15 21:22:08
This code mostly came from the SPDY equivalent and
| |
| 36 buffer_ = NULL; | |
| 37 capacity_ = 0; | |
| 38 length_ = 0; | |
| 39 return rv; | |
| 40 } | |
| 41 | |
| 42 // Methods for adding to the payload. These values are appended to the end | |
| 43 // of the QuicDataWriter payload. Note - binary integers are written in | |
| 44 // host byte order (little endian) not network byte order (big endian). | |
| 45 bool WriteUInt8(uint8 value) { | |
| 46 return WriteBytes(&value, sizeof(value)); | |
| 47 } | |
| 48 bool WriteUInt16(uint16 value) { | |
| 49 return WriteBytes(&value, sizeof(value)); | |
| 50 } | |
| 51 bool WriteUInt32(uint32 value) { | |
| 52 return WriteBytes(&value, sizeof(value)); | |
| 53 } | |
| 54 bool WriteUInt48(uint64 value) { | |
| 55 uint32 hi = value >> 32; | |
| 56 uint32 lo = value & 0x00000000FFFFFFFF; | |
| 57 return WriteUInt32(lo) && WriteUInt16(hi); | |
| 58 } | |
| 59 bool WriteUInt64(uint64 value) { | |
| 60 return WriteBytes(&value, sizeof(value)); | |
| 61 } | |
| 62 bool WriteUInt128(uint128 value) { | |
| 63 return WriteUInt64(value.lo) && WriteUInt64(value.hi); | |
| 64 } | |
| 65 | |
| 66 bool AdvancePointer(uint32 len); | |
| 67 | |
| 68 bool WriteBytes(const void* data, uint32 data_len); | |
| 69 | |
| 70 static void WriteUint64ToBuffer(uint64 value, char* buffer); | |
| 71 static void WriteUint128ToBuffer(uint128 value, char* buffer); | |
|
jar (doing other things)
2012/10/14 23:04:38
This API is a bit scary (no way to check). Is thi
Ryan Hamilton
2012/10/15 21:22:08
I believe these methods are used for tweaking fiel
| |
| 72 | |
| 73 size_t capacity() const { | |
| 74 return capacity_; | |
| 75 } | |
| 76 | |
| 77 protected: | |
| 78 const char* end_of_payload() const { return buffer_ + length_; } | |
| 79 | |
| 80 private: | |
| 81 // Returns the location that the data should be written at, or NULL if there | |
| 82 // is not enough room. Call EndWrite with the returned offset and the given | |
| 83 // length to pad out for the next write. | |
| 84 char* BeginWrite(size_t length); | |
| 85 | |
| 86 char* buffer_; | |
| 87 size_t capacity_; // Allocation size of payload (or -1 if buffer is const). | |
| 88 size_t length_; // current length of the buffer | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: Capital and period.
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 89 }; | |
| 90 | |
| 91 } // namespace net | |
| 92 | |
| 93 #endif // NET_QUIC_QUIC_DATA_WRITER_H_ | |
| OLD | NEW |