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 "base/string_piece.h" |
| 13 #include "net/base/net_export.h" |
| 14 #include "net/quic/quic_protocol.h" |
| 15 #include "net/quic/uint128.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 // This class provides facilities for packing QUIC data. |
| 20 // |
| 21 // The QuicDataWriter supports appending primitive values (int, string, etc) |
| 22 // to a frame instance. The QuicDataWriter grows its internal memory buffer |
| 23 // dynamically to hold the sequence of primitive values. The internal memory |
| 24 // buffer is exposed as the "data" of the QuicDataWriter. |
| 25 class NET_EXPORT_PRIVATE QuicDataWriter { |
| 26 public: |
| 27 explicit QuicDataWriter(size_t length); |
| 28 ~QuicDataWriter(); |
| 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 // TODO(rch): move non-trivial methods into .cc file. |
| 35 char* take() { |
| 36 char* rv = buffer_; |
| 37 buffer_ = NULL; |
| 38 capacity_ = 0; |
| 39 length_ = 0; |
| 40 return rv; |
| 41 } |
| 42 |
| 43 // Methods for adding to the payload. These values are appended to the end |
| 44 // of the QuicDataWriter payload. Note - binary integers are written in |
| 45 // host byte order (little endian) not network byte order (big endian). |
| 46 bool WriteUInt8(uint8 value) { |
| 47 return WriteBytes(&value, sizeof(value)); |
| 48 } |
| 49 bool WriteUInt16(uint16 value) { |
| 50 return WriteBytes(&value, sizeof(value)); |
| 51 } |
| 52 bool WriteUInt32(uint32 value) { |
| 53 return WriteBytes(&value, sizeof(value)); |
| 54 } |
| 55 bool WriteUInt48(uint64 value) { |
| 56 uint32 hi = value >> 32; |
| 57 uint32 lo = value & 0x00000000FFFFFFFF; |
| 58 return WriteUInt32(lo) && WriteUInt16(hi); |
| 59 } |
| 60 bool WriteUInt64(uint64 value) { |
| 61 return WriteBytes(&value, sizeof(value)); |
| 62 } |
| 63 bool WriteUInt128(uint128 value) { |
| 64 return WriteUInt64(value.lo) && WriteUInt64(value.hi); |
| 65 } |
| 66 |
| 67 bool AdvancePointer(uint32 len); |
| 68 |
| 69 bool WriteBytes(const void* data, uint32 data_len); |
| 70 |
| 71 static void WriteUint64ToBuffer(uint64 value, char* buffer); |
| 72 static void WriteUint128ToBuffer(uint128 value, char* buffer); |
| 73 |
| 74 size_t capacity() const { |
| 75 return capacity_; |
| 76 } |
| 77 |
| 78 protected: |
| 79 const char* end_of_payload() const { return buffer_ + length_; } |
| 80 |
| 81 private: |
| 82 // Returns the location that the data should be written at, or NULL if there |
| 83 // is not enough room. Call EndWrite with the returned offset and the given |
| 84 // length to pad out for the next write. |
| 85 char* BeginWrite(size_t length); |
| 86 |
| 87 char* buffer_; |
| 88 size_t capacity_; // Allocation size of payload (or -1 if buffer is const). |
| 89 size_t length_; // Current length of the buffer. |
| 90 }; |
| 91 |
| 92 } // namespace net |
| 93 |
| 94 #endif // NET_QUIC_QUIC_DATA_WRITER_H_ |
OLD | NEW |