| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // This class provides facilities for basic binary value packing | 24 // This class provides facilities for basic binary value packing |
| 25 // into Spdy frames. | 25 // into Spdy frames. |
| 26 // | 26 // |
| 27 // The SpdyFrameBuilder supports appending primitive values (int, string, etc) | 27 // The SpdyFrameBuilder supports appending primitive values (int, string, etc) |
| 28 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer | 28 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer |
| 29 // dynamically to hold the sequence of primitive values. The internal memory | 29 // dynamically to hold the sequence of primitive values. The internal memory |
| 30 // buffer is exposed as the "data" of the SpdyFrameBuilder. | 30 // buffer is exposed as the "data" of the SpdyFrameBuilder. |
| 31 class NET_EXPORT_PRIVATE SpdyFrameBuilder { | 31 class NET_EXPORT_PRIVATE SpdyFrameBuilder { |
| 32 public: | 32 public: |
| 33 // Initializes a SpdyFrameBuilder with a buffer of given size | 33 // Initializes a SpdyFrameBuilder with a buffer of given size |
| 34 SpdyFrameBuilder(size_t size, SpdyMajorVersion version); | 34 explicit SpdyFrameBuilder(size_t size); |
| 35 | 35 |
| 36 ~SpdyFrameBuilder(); | 36 ~SpdyFrameBuilder(); |
| 37 | 37 |
| 38 // Returns the total size of the SpdyFrameBuilder's data, which may include | 38 // Returns the total size of the SpdyFrameBuilder's data, which may include |
| 39 // multiple frames. | 39 // multiple frames. |
| 40 size_t length() const { return offset_ + length_; } | 40 size_t length() const { return offset_ + length_; } |
| 41 | 41 |
| 42 // Returns a writeable buffer of given size in bytes, to be appended to the | 42 // Returns a writeable buffer of given size in bytes, to be appended to the |
| 43 // currently written frame. Does bounds checking on length but does not | 43 // currently written frame. Does bounds checking on length but does not |
| 44 // increment the underlying iterator. To do so, consumers should subsequently | 44 // increment the underlying iterator. To do so, consumers should subsequently |
| 45 // call Seek(). | 45 // call Seek(). |
| 46 // In general, consumers should use Write*() calls instead of this. | 46 // In general, consumers should use Write*() calls instead of this. |
| 47 // Returns NULL on failure. | 47 // Returns NULL on failure. |
| 48 char* GetWritableBuffer(size_t length); | 48 char* GetWritableBuffer(size_t length); |
| 49 | 49 |
| 50 // Seeks forward by the given number of bytes. Useful in conjunction with | 50 // Seeks forward by the given number of bytes. Useful in conjunction with |
| 51 // GetWriteableBuffer() above. | 51 // GetWriteableBuffer() above. |
| 52 bool Seek(size_t length); | 52 bool Seek(size_t length); |
| 53 | 53 |
| 54 // Populates this frame with a HTTP2 frame prefix using version-specific | 54 // Populates this frame with a HTTP2 frame prefix using length information |
| 55 // information from the |framer| and length information from |capacity_|. The | 55 // from |capacity_|. The given type must be a control frame type. |
| 56 // given type must be a control frame type. | |
| 57 // Used only for HTTP2. | |
| 58 bool BeginNewFrame(const SpdyFramer& framer, | 56 bool BeginNewFrame(const SpdyFramer& framer, |
| 59 SpdyFrameType type, | 57 SpdyFrameType type, |
| 60 uint8_t flags, | 58 uint8_t flags, |
| 61 SpdyStreamId stream_id); | 59 SpdyStreamId stream_id); |
| 62 | 60 |
| 63 // Takes the buffer from the SpdyFrameBuilder. | 61 // Takes the buffer from the SpdyFrameBuilder. |
| 64 SpdySerializedFrame take() { | 62 SpdySerializedFrame take() { |
| 65 if (version_ == HTTP2) { | 63 SPDY_BUG_IF(SpdyConstants::kMaxFrameSizeLimit < length_) |
| 66 SPDY_BUG_IF(SpdyConstants::GetMaxFrameSizeLimit(version_) < length_) | 64 << "Frame length " << length_ |
| 67 << "Frame length " << length_ | 65 << " is longer than the maximum possible allowed length."; |
| 68 << " is longer than the maximum possible allowed length."; | |
| 69 } | |
| 70 SpdySerializedFrame rv(buffer_.release(), length(), true); | 66 SpdySerializedFrame rv(buffer_.release(), length(), true); |
| 71 capacity_ = 0; | 67 capacity_ = 0; |
| 72 length_ = 0; | 68 length_ = 0; |
| 73 offset_ = 0; | 69 offset_ = 0; |
| 74 return rv; | 70 return rv; |
| 75 } | 71 } |
| 76 | 72 |
| 77 // Methods for adding to the payload. These values are appended to the end | 73 // Methods for adding to the payload. These values are appended to the end |
| 78 // of the SpdyFrameBuilder payload. Note - binary integers are converted from | 74 // of the SpdyFrameBuilder payload. Note - binary integers are converted from |
| 79 // host to network form. | 75 // host to network form. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 110 | 106 |
| 111 // Update (in-place) the length field in the frame being built to reflect the | 107 // Update (in-place) the length field in the frame being built to reflect the |
| 112 // given length. | 108 // given length. |
| 113 // The framer parameter is used to determine version-specific location and | 109 // The framer parameter is used to determine version-specific location and |
| 114 // size information of the length field to be written, and must be initialized | 110 // size information of the length field to be written, and must be initialized |
| 115 // with the correct version for the frame being written. | 111 // with the correct version for the frame being written. |
| 116 bool OverwriteLength(const SpdyFramer& framer, size_t length); | 112 bool OverwriteLength(const SpdyFramer& framer, size_t length); |
| 117 | 113 |
| 118 // Update (in-place) the flags field in the frame being built to reflect the | 114 // Update (in-place) the flags field in the frame being built to reflect the |
| 119 // given flags value. | 115 // given flags value. |
| 120 // Used only for SPDY versions >=4. | |
| 121 bool OverwriteFlags(const SpdyFramer& framer, uint8_t flags); | 116 bool OverwriteFlags(const SpdyFramer& framer, uint8_t flags); |
| 122 | 117 |
| 123 private: | 118 private: |
| 124 // Checks to make sure that there is an appropriate amount of space for a | 119 // Checks to make sure that there is an appropriate amount of space for a |
| 125 // write of given size, in bytes. | 120 // write of given size, in bytes. |
| 126 bool CanWrite(size_t length) const; | 121 bool CanWrite(size_t length) const; |
| 127 | 122 |
| 128 std::unique_ptr<char[]> buffer_; | 123 std::unique_ptr<char[]> buffer_; |
| 129 size_t capacity_; // Allocation size of payload, set by constructor. | 124 size_t capacity_; // Allocation size of payload, set by constructor. |
| 130 size_t length_; // Length of the latest frame in the buffer. | 125 size_t length_; // Length of the latest frame in the buffer. |
| 131 size_t offset_; // Position at which the latest frame begins. | 126 size_t offset_; // Position at which the latest frame begins. |
| 132 | |
| 133 const SpdyMajorVersion version_; | |
| 134 }; | 127 }; |
| 135 | 128 |
| 136 } // namespace net | 129 } // namespace net |
| 137 | 130 |
| 138 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ | 131 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ |
| OLD | NEW |