summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_data_writer.h
diff options
context:
space:
mode:
authorrch@chromium.org <rch@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-18 21:53:49 +0000
committerrch@chromium.org <rch@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-18 21:53:49 +0000
commit8b37a0928ef852ec0c0215dac3f1d89be348bad8 (patch)
treebfdb4917839e4c86cb1f3e5fce455c0e2fb536f6 /net/quic/quic_data_writer.h
parent54440b09794e847b6bad6cee33846b1750d0d869 (diff)
downloadchromium_src-8b37a0928ef852ec0c0215dac3f1d89be348bad8.zip
chromium_src-8b37a0928ef852ec0c0215dac3f1d89be348bad8.tar.gz
chromium_src-8b37a0928ef852ec0c0215dac3f1d89be348bad8.tar.bz2
Trying yet again to add QuicFramer and friends.
Includes a second attempt to fix the constants. This time, I was able to set up a 32bit chroot to test the fix on since none of the bots seemed to do the trick. 162618 - Reverted 162606 162606 - Relanding with second fix 162468 - Reverted 162462 162462 - Relanding w/ fix 162263 - Reverted 162259 162259 - Add QuicFramer and friends. Review URL: https://codereview.chromium.org/11193036 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162805 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/quic/quic_data_writer.h')
-rw-r--r--net/quic/quic_data_writer.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/net/quic/quic_data_writer.h b/net/quic/quic_data_writer.h
new file mode 100644
index 0000000..fa0e05a
--- /dev/null
+++ b/net/quic/quic_data_writer.h
@@ -0,0 +1,95 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_QUIC_QUIC_DATA_WRITER_H_
+#define NET_QUIC_QUIC_DATA_WRITER_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+#include "base/port.h"
+#include "base/string_piece.h"
+#include "net/base/net_export.h"
+#include "net/quic/quic_protocol.h"
+#include "net/quic/uint128.h"
+
+namespace net {
+
+// This class provides facilities for packing QUIC data.
+//
+// The QuicDataWriter supports appending primitive values (int, string, etc)
+// to a frame instance. The QuicDataWriter grows its internal memory buffer
+// dynamically to hold the sequence of primitive values. The internal memory
+// buffer is exposed as the "data" of the QuicDataWriter.
+class NET_EXPORT_PRIVATE QuicDataWriter {
+ public:
+ explicit QuicDataWriter(size_t length);
+ ~QuicDataWriter();
+
+ // Returns the size of the QuicDataWriter's data.
+ size_t length() const { return length_; }
+
+ // Takes the buffer from the QuicDataWriter.
+ // TODO(rch): move non-trivial methods into .cc file.
+ char* take() {
+ char* rv = buffer_;
+ buffer_ = NULL;
+ capacity_ = 0;
+ length_ = 0;
+ return rv;
+ }
+
+ // Methods for adding to the payload. These values are appended to the end
+ // of the QuicDataWriter payload. Note - binary integers are written in
+ // host byte order (little endian) not network byte order (big endian).
+ bool WriteUInt8(uint8 value) {
+ return WriteBytes(&value, sizeof(value));
+ }
+ bool WriteUInt16(uint16 value) {
+ return WriteBytes(&value, sizeof(value));
+ }
+ bool WriteUInt32(uint32 value) {
+ return WriteBytes(&value, sizeof(value));
+ }
+ bool WriteUInt48(uint64 value) {
+ uint32 hi = value >> 32;
+ uint32 lo = value & GG_UINT64_C(0x00000000FFFFFFFF);
+ return WriteUInt32(lo) && WriteUInt16(hi);
+ }
+ bool WriteUInt64(uint64 value) {
+ return WriteBytes(&value, sizeof(value));
+ }
+ bool WriteUInt128(uint128 value) {
+ return WriteUInt64(value.lo) && WriteUInt64(value.hi);
+ }
+
+ bool AdvancePointer(uint32 len);
+
+ bool WriteBytes(const void* data, uint32 data_len);
+
+ static void WriteUint64ToBuffer(uint64 value, char* buffer);
+ static void WriteUint128ToBuffer(uint128 value, char* buffer);
+
+ size_t capacity() const {
+ return capacity_;
+ }
+
+ protected:
+ const char* end_of_payload() const { return buffer_ + length_; }
+
+ private:
+ // Returns the location that the data should be written at, or NULL if there
+ // is not enough room. Call EndWrite with the returned offset and the given
+ // length to pad out for the next write.
+ char* BeginWrite(size_t length);
+
+ char* buffer_;
+ size_t capacity_; // Allocation size of payload (or -1 if buffer is const).
+ size_t length_; // Current length of the buffer.
+};
+
+} // namespace net
+
+#endif // NET_QUIC_QUIC_DATA_WRITER_H_