Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / net / spdy / spdy_frame_builder.cc
blobba7071f9b2de266c7c8c44cc13c65c28a8223946
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.
5 #include <limits>
7 #include "net/spdy/spdy_frame_builder.h"
8 #include "net/spdy/spdy_protocol.h"
10 namespace net {
12 namespace {
14 // Creates a FlagsAndLength.
15 FlagsAndLength CreateFlagsAndLength(SpdyControlFlags flags, size_t length) {
16 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask));
17 FlagsAndLength flags_length;
18 flags_length.length_ = htonl(static_cast<uint32>(length));
19 DCHECK_EQ(0, flags & ~kControlFlagsMask);
20 flags_length.flags_[0] = flags;
21 return flags_length;
24 } // namespace
26 SpdyFrameBuilder::SpdyFrameBuilder(size_t size)
27 : buffer_(new char[size]),
28 capacity_(size),
29 length_(0) {
32 SpdyFrameBuilder::SpdyFrameBuilder(SpdyControlType type,
33 SpdyControlFlags flags,
34 int spdy_version,
35 size_t size)
36 : buffer_(new char[size]),
37 capacity_(size),
38 length_(0) {
39 FlagsAndLength flags_length = CreateFlagsAndLength(
40 flags, size - SpdyFrame::kHeaderSize);
41 WriteUInt16(kControlFlagMask | spdy_version);
42 WriteUInt16(type);
43 WriteBytes(&flags_length, sizeof(flags_length));
46 SpdyFrameBuilder::SpdyFrameBuilder(SpdyStreamId stream_id,
47 SpdyDataFlags flags,
48 size_t size)
49 : buffer_(new char[size]),
50 capacity_(size),
51 length_(0) {
52 DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
53 WriteUInt32(stream_id);
54 size_t length = size - SpdyFrame::kHeaderSize;
55 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask));
56 FlagsAndLength flags_length;
57 flags_length.length_ = htonl(length);
58 DCHECK_EQ(0, flags & ~kDataFlagsMask);
59 flags_length.flags_[0] = flags;
60 WriteBytes(&flags_length, sizeof(flags_length));
63 SpdyFrameBuilder::~SpdyFrameBuilder() {
66 char* SpdyFrameBuilder::BeginWrite(size_t length) {
67 size_t offset = length_;
68 size_t needed_size = length_ + length;
69 if (needed_size > capacity_)
70 return NULL;
72 #ifdef ARCH_CPU_64_BITS
73 DCHECK_LE(length, std::numeric_limits<uint32>::max());
74 #endif
76 return buffer_.get() + offset;
79 void SpdyFrameBuilder::EndWrite(char* dest, int length) {
82 bool SpdyFrameBuilder::WriteBytes(const void* data, uint32 data_len) {
83 if (data_len > kLengthMask) {
84 return false;
87 char* dest = BeginWrite(data_len);
88 if (!dest)
89 return false;
91 memcpy(dest, data, data_len);
93 EndWrite(dest, data_len);
94 length_ += data_len;
95 return true;
98 bool SpdyFrameBuilder::WriteString(const std::string& value) {
99 if (value.size() > 0xffff)
100 return false;
102 if (!WriteUInt16(static_cast<int>(value.size())))
103 return false;
105 return WriteBytes(value.data(), static_cast<uint16>(value.size()));
108 bool SpdyFrameBuilder::WriteStringPiece32(const base::StringPiece& value) {
109 if (!WriteUInt32(value.size())) {
110 return false;
113 return WriteBytes(value.data(), value.size());
116 } // namespace net