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 #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_ | 5 #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_ |
6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_ | 6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // This class provides facilities for basic binary value packing | 21 // This class provides facilities for basic binary value packing |
22 // into Spdy frames. | 22 // into Spdy frames. |
23 // | 23 // |
24 // The SpdyFrameBuilder supports appending primitive values (int, string, etc) | 24 // The SpdyFrameBuilder supports appending primitive values (int, string, etc) |
25 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer | 25 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer |
26 // dynamically to hold the sequence of primitive values. The internal memory | 26 // dynamically to hold the sequence of primitive values. The internal memory |
27 // buffer is exposed as the "data" of the SpdyFrameBuilder. | 27 // buffer is exposed as the "data" of the SpdyFrameBuilder. |
28 class NET_EXPORT_PRIVATE SpdyFrameBuilder { | 28 class NET_EXPORT_PRIVATE SpdyFrameBuilder { |
29 public: | 29 public: |
30 // Initializes a SpdyFrameBuilder with a buffer of given size | 30 // Initializes a SpdyFrameBuilder with a buffer of given size |
31 explicit SpdyFrameBuilder(size_t size); | 31 SpdyFrameBuilder(size_t size, SpdyMajorVersion version); |
32 | 32 |
33 ~SpdyFrameBuilder(); | 33 ~SpdyFrameBuilder(); |
34 | 34 |
35 // Returns the size of the SpdyFrameBuilder's data. | 35 // Returns the total size of the SpdyFrameBuilder's data, which may include |
36 size_t length() const { return length_; } | 36 // multiple frames. |
| 37 size_t length() const { return offset_ + length_; } |
37 | 38 |
38 // Returns a writeable buffer of given size in bytes, to be appended to the | 39 // Returns a writeable buffer of given size in bytes, to be appended to the |
39 // currently written frame. Does bounds checking on length but does not | 40 // currently written frame. Does bounds checking on length but does not |
40 // increment the underlying iterator. To do so, consumers should subsequently | 41 // increment the underlying iterator. To do so, consumers should subsequently |
41 // call Seek(). | 42 // call Seek(). |
42 // In general, consumers should use Write*() calls instead of this. | 43 // In general, consumers should use Write*() calls instead of this. |
43 // Returns NULL on failure. | 44 // Returns NULL on failure. |
44 char* GetWritableBuffer(size_t length); | 45 char* GetWritableBuffer(size_t length); |
45 | 46 |
46 // Seeks forward by the given number of bytes. Useful in conjunction with | 47 // Seeks forward by the given number of bytes. Useful in conjunction with |
(...skipping 11 matching lines...) Expand all Loading... |
58 // Populates this frame with a SPDY data frame header using version-specific | 59 // Populates this frame with a SPDY data frame header using version-specific |
59 // information from the |framer| and length information from capacity_. | 60 // information from the |framer| and length information from capacity_. |
60 bool WriteDataFrameHeader(const SpdyFramer& framer, | 61 bool WriteDataFrameHeader(const SpdyFramer& framer, |
61 SpdyStreamId stream_id, | 62 SpdyStreamId stream_id, |
62 uint8 flags); | 63 uint8 flags); |
63 | 64 |
64 // Populates this frame with a SPDY4/HTTP2 frame prefix using | 65 // Populates this frame with a SPDY4/HTTP2 frame prefix using |
65 // version-specific information from the |framer| and length information from | 66 // version-specific information from the |framer| and length information from |
66 // capacity_. The given type must be a control frame type. | 67 // capacity_. The given type must be a control frame type. |
67 // Used only for SPDY versions >=4. | 68 // Used only for SPDY versions >=4. |
68 bool WriteFramePrefix(const SpdyFramer& framer, | 69 bool BeginNewFrame(const SpdyFramer& framer, |
69 SpdyFrameType type, | 70 SpdyFrameType type, |
70 uint8 flags, | 71 uint8 flags, |
71 SpdyStreamId stream_id); | 72 SpdyStreamId stream_id); |
72 | 73 |
73 // Takes the buffer from the SpdyFrameBuilder. | 74 // Takes the buffer from the SpdyFrameBuilder. |
74 SpdyFrame* take() { | 75 SpdyFrame* take() { |
75 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length_, true); | 76 if (version_ > SPDY3) { |
| 77 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) |
| 78 << "Frame length " << length_ |
| 79 << " is longer than the maximum allowed length."; |
| 80 } |
| 81 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length(), true); |
76 capacity_ = 0; | 82 capacity_ = 0; |
77 length_ = 0; | 83 length_ = 0; |
| 84 offset_ = 0; |
78 return rv; | 85 return rv; |
79 } | 86 } |
80 | 87 |
81 // Methods for adding to the payload. These values are appended to the end | 88 // Methods for adding to the payload. These values are appended to the end |
82 // of the SpdyFrameBuilder payload. Note - binary integers are converted from | 89 // of the SpdyFrameBuilder payload. Note - binary integers are converted from |
83 // host to network form. | 90 // host to network form. |
84 bool WriteUInt8(uint8 value) { | 91 bool WriteUInt8(uint8 value) { |
85 return WriteBytes(&value, sizeof(value)); | 92 return WriteBytes(&value, sizeof(value)); |
86 } | 93 } |
87 bool WriteUInt16(uint16 value) { | 94 bool WriteUInt16(uint16 value) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // Used only for SPDY versions >=4. | 129 // Used only for SPDY versions >=4. |
123 bool OverwriteFlags(const SpdyFramer& framer, uint8 flags); | 130 bool OverwriteFlags(const SpdyFramer& framer, uint8 flags); |
124 | 131 |
125 private: | 132 private: |
126 // Checks to make sure that there is an appropriate amount of space for a | 133 // Checks to make sure that there is an appropriate amount of space for a |
127 // write of given size, in bytes. | 134 // write of given size, in bytes. |
128 bool CanWrite(size_t length) const; | 135 bool CanWrite(size_t length) const; |
129 | 136 |
130 scoped_ptr<char[]> buffer_; | 137 scoped_ptr<char[]> buffer_; |
131 size_t capacity_; // Allocation size of payload, set by constructor. | 138 size_t capacity_; // Allocation size of payload, set by constructor. |
132 size_t length_; // Current length of the buffer. | 139 size_t length_; // Length of the latest frame in the buffer. |
| 140 size_t offset_; // Position at which the latest frame begins. |
| 141 |
| 142 const SpdyMajorVersion version_; |
133 }; | 143 }; |
134 | 144 |
135 } // namespace net | 145 } // namespace net |
136 | 146 |
137 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ | 147 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ |
OLD | NEW |