| OLD | NEW |
| 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" |
| 11 #include "net/spdy/spdy_protocol.h" | 11 #include "net/spdy/spdy_protocol.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // A special structure for the 8 bit flags and 24 bit length fields. | 17 // A special structure for the 8 bit flags and 24 bit length fields. |
| 18 union FlagsAndLength { | 18 union FlagsAndLength { |
| 19 uint8 flags[4]; // 8 bits | 19 uint8 flags[4]; // 8 bits |
| 20 uint32 length; // 24 bits | 20 uint32 length; // 24 bits |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 // Creates a FlagsAndLength. | 23 // Creates a FlagsAndLength. |
| 24 FlagsAndLength CreateFlagsAndLength(uint8 flags, size_t length) { | 24 FlagsAndLength CreateFlagsAndLength(uint8 flags, size_t length) { |
| 25 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask)); | 25 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask)); |
| 26 FlagsAndLength flags_length; | 26 FlagsAndLength flags_length; |
| 27 flags_length.length = htonl(static_cast<uint32>(length)); | 27 flags_length.length = base::HostToNet32(static_cast<uint32>(length)); |
| 28 DCHECK_EQ(0, flags & ~kControlFlagsMask); | 28 DCHECK_EQ(0, flags & ~kControlFlagsMask); |
| 29 flags_length.flags[0] = flags; | 29 flags_length.flags[0] = flags; |
| 30 return flags_length; | 30 return flags_length; |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 SpdyFrameBuilder::SpdyFrameBuilder(size_t size, SpdyMajorVersion version) | 35 SpdyFrameBuilder::SpdyFrameBuilder(size_t size, SpdyMajorVersion version) |
| 36 : buffer_(new char[size]), | 36 : buffer_(new char[size]), |
| 37 capacity_(size), | 37 capacity_(size), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 uint8 flags) { | 82 uint8 flags) { |
| 83 if (version_ > SPDY3) { | 83 if (version_ > SPDY3) { |
| 84 return BeginNewFrame(framer, DATA, flags, stream_id); | 84 return BeginNewFrame(framer, DATA, flags, stream_id); |
| 85 } | 85 } |
| 86 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 86 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
| 87 bool success = true; | 87 bool success = true; |
| 88 success &= WriteUInt32(stream_id); | 88 success &= WriteUInt32(stream_id); |
| 89 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); | 89 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); |
| 90 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); | 90 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); |
| 91 FlagsAndLength flags_length; | 91 FlagsAndLength flags_length; |
| 92 flags_length.length = htonl(length_field); | 92 flags_length.length = base::HostToNet32(static_cast<uint32>(length_field)); |
| 93 DCHECK_EQ(0, flags & ~kDataFlagsMask); | 93 DCHECK_EQ(0, flags & ~kDataFlagsMask); |
| 94 flags_length.flags[0] = flags; | 94 flags_length.flags[0] = flags; |
| 95 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 95 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
| 96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
| 97 return success; | 97 return success; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, | 100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, |
| 101 SpdyFrameType type, | 101 SpdyFrameType type, |
| 102 uint8 flags, | 102 uint8 flags, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 217 |
| 218 if (offset_ + length_ + length > capacity_) { | 218 if (offset_ + length_ + length > capacity_) { |
| 219 DCHECK(false); | 219 DCHECK(false); |
| 220 return false; | 220 return false; |
| 221 } | 221 } |
| 222 | 222 |
| 223 return true; | 223 return true; |
| 224 } | 224 } |
| 225 | 225 |
| 226 } // namespace net | 226 } // namespace net |
| OLD | NEW |