Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: net/spdy/spdy_frame_builder.cc

Issue 247243003: SPDY: Use SpdyMajorVersion rather than ints for SPDY version number. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Also include cr/64899106 Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/spdy/spdy_frame_builder_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/spdy/spdy_frame_builder.h" 5 #include "net/spdy/spdy_frame_builder.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "net/spdy/spdy_framer.h" 10 #include "net/spdy/spdy_framer.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 return false; 55 return false;
56 } 56 }
57 57
58 length_ += length; 58 length_ += length;
59 return true; 59 return true;
60 } 60 }
61 61
62 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer, 62 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer,
63 SpdyFrameType type, 63 SpdyFrameType type,
64 uint8 flags) { 64 uint8 flags) {
65 DCHECK_GT(4, framer.protocol_version()); 65 DCHECK_GE(SPDY3, version_);
66 DCHECK_NE(-1, 66 DCHECK_NE(-1,
67 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); 67 SpdyConstants::SerializeFrameType(version_, type));
68 bool success = true; 68 bool success = true;
69 FlagsAndLength flags_length = CreateFlagsAndLength( 69 FlagsAndLength flags_length = CreateFlagsAndLength(
70 flags, capacity_ - framer.GetControlFrameHeaderSize()); 70 flags, capacity_ - framer.GetControlFrameHeaderSize());
71 success &= WriteUInt16(kControlFlagMask | framer.protocol_version()); 71 success &= WriteUInt16(kControlFlagMask |
72 SpdyConstants::SerializeMajorVersion(version_));
72 success &= WriteUInt16( 73 success &= WriteUInt16(
73 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); 74 SpdyConstants::SerializeFrameType(framer.protocol_version(), type));
74 success &= WriteBytes(&flags_length, sizeof(flags_length)); 75 success &= WriteBytes(&flags_length, sizeof(flags_length));
75 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); 76 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length());
76 return success; 77 return success;
77 } 78 }
78 79
79 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer, 80 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer,
80 SpdyStreamId stream_id, 81 SpdyStreamId stream_id,
81 uint8 flags) { 82 uint8 flags) {
82 if (framer.protocol_version() >= 4) { 83 if (version_ > SPDY3) {
83 return BeginNewFrame(framer, DATA, flags, stream_id); 84 return BeginNewFrame(framer, DATA, flags, stream_id);
84 } 85 }
85 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); 86 DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
86 bool success = true; 87 bool success = true;
87 success &= WriteUInt32(stream_id); 88 success &= WriteUInt32(stream_id);
88 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); 89 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize();
89 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); 90 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask));
90 FlagsAndLength flags_length; 91 FlagsAndLength flags_length;
91 flags_length.length_ = htonl(length_field); 92 flags_length.length_ = htonl(length_field);
92 DCHECK_EQ(0, flags & ~kDataFlagsMask); 93 DCHECK_EQ(0, flags & ~kDataFlagsMask);
93 flags_length.flags_[0] = flags; 94 flags_length.flags_[0] = flags;
94 success &= WriteBytes(&flags_length, sizeof(flags_length)); 95 success &= WriteBytes(&flags_length, sizeof(flags_length));
95 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); 96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length());
96 return success; 97 return success;
97 } 98 }
98 99
99 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, 100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer,
100 SpdyFrameType type, 101 SpdyFrameType type,
101 uint8 flags, 102 uint8 flags,
102 SpdyStreamId stream_id) { 103 SpdyStreamId stream_id) {
103 DCHECK(SpdyConstants::IsValidFrameType(version_, 104 DCHECK(SpdyConstants::IsValidFrameType(version_,
104 SpdyConstants::SerializeFrameType(version_, type))); 105 SpdyConstants::SerializeFrameType(version_, type)));
105 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); 106 DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
106 DCHECK_LE(4, version_); 107 DCHECK_LT(SPDY3, framer.protocol_version());
107
108 bool success = true; 108 bool success = true;
109 if (length_ > 0) { 109 if (length_ > 0) {
110 // Update length field for previous frame. 110 // Update length field for previous frame.
111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); 111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type));
112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) 112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_)
113 << "Frame length " << length_ 113 << "Frame length " << length_
114 << " is longer than the maximum allowed length."; 114 << " is longer than the maximum allowed length.";
115 } 115 }
116 116
117 offset_ += length_; 117 offset_ += length_;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 return true; 161 return true;
162 } 162 }
163 163
164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { 164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) {
165 return OverwriteLength(framer, 165 return OverwriteLength(framer,
166 length_ - framer.GetControlFrameHeaderSize()); 166 length_ - framer.GetControlFrameHeaderSize());
167 } 167 }
168 168
169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, 169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer,
170 size_t length) { 170 size_t length) {
171 if (version_ < 4) { 171 if (version_ <= SPDY3) {
172 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) - 172 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) -
173 framer.GetFrameMinimumSize(), 173 framer.GetFrameMinimumSize(),
174 length); 174 length);
175 } else { 175 } else {
176 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length); 176 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length);
177 } 177 }
178 bool success = false; 178 bool success = false;
179 const size_t old_length = length_; 179 const size_t old_length = length_;
180 180
181 if (version_ < 4) { 181 if (version_ <= SPDY3) {
182 FlagsAndLength flags_length = CreateFlagsAndLength( 182 FlagsAndLength flags_length = CreateFlagsAndLength(
183 0, // We're not writing over the flags value anyway. 183 0, // We're not writing over the flags value anyway.
184 length); 184 length);
185 185
186 // Write into the correct location by temporarily faking the offset. 186 // Write into the correct location by temporarily faking the offset.
187 length_ = 5; // Offset at which the length field occurs. 187 length_ = 5; // Offset at which the length field occurs.
188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, 188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1,
189 sizeof(flags_length) - 1); 189 sizeof(flags_length) - 1);
190 } else { 190 } else {
191 length_ = 0; 191 length_ = 0;
192 success = WriteUInt16(length); 192 success = WriteUInt16(length);
193 } 193 }
194 194
195 length_ = old_length; 195 length_ = old_length;
196 return success; 196 return success;
197 } 197 }
198 198
199 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, 199 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer,
200 uint8 flags) { 200 uint8 flags) {
201 DCHECK_LE(SPDY4, framer.protocol_version()); 201 DCHECK_LT(SPDY3, framer.protocol_version());
202 bool success = false; 202 bool success = false;
203 const size_t old_length = length_; 203 const size_t old_length = length_;
204 // Flags are the fourth octet in the frame prefix. 204 // Flags are the fourth octet in the frame prefix.
205 length_ = 3; 205 length_ = 3;
206 success = WriteUInt8(flags); 206 success = WriteUInt8(flags);
207 length_ = old_length; 207 length_ = old_length;
208 return success; 208 return success;
209 } 209 }
210 210
211 bool SpdyFrameBuilder::CanWrite(size_t length) const { 211 bool SpdyFrameBuilder::CanWrite(size_t length) const {
212 if (length > kLengthMask) { 212 if (length > kLengthMask) {
213 DCHECK(false); 213 DCHECK(false);
214 return false; 214 return false;
215 } 215 }
216 216
217 if (offset_ + length_ + length > capacity_) { 217 if (offset_ + length_ + length > capacity_) {
218 DCHECK(false); 218 DCHECK(false);
219 return false; 219 return false;
220 } 220 }
221 221
222 return true; 222 return true;
223 } 223 }
224 224
225 } // namespace net 225 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/spdy/spdy_frame_builder_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698