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 #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_
6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "base/sys_byteorder.h"
14 #include "net/base/net_export.h"
15 #include "net/spdy/spdy_protocol.h"
21 // This class provides facilities for basic binary value packing
24 // The SpdyFrameBuilder supports appending primitive values (int, string, etc)
25 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer
26 // dynamically to hold the sequence of primitive values. The internal memory
27 // buffer is exposed as the "data" of the SpdyFrameBuilder.
28 class NET_EXPORT_PRIVATE SpdyFrameBuilder {
30 // Initializes a SpdyFrameBuilder with a buffer of given size
31 explicit SpdyFrameBuilder(size_t size);
35 // Returns the size of the SpdyFrameBuilder's data.
36 size_t length() const { return length_; }
38 // 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 // increment the underlying iterator. To do so, consumers should subsequently
42 // In general, consumers should use Write*() calls instead of this.
43 // Returns NULL on failure.
44 char* GetWritableBuffer(size_t length);
46 // Seeks forward by the given number of bytes. Useful in conjunction with
47 // GetWriteableBuffer() above.
48 bool Seek(size_t length);
50 // Populates this frame with a SPDY control frame header using
51 // version-specific information from the |framer| and length information from
52 // capacity_. The given type must be a control frame type.
53 // Used only for SPDY versions <4.
54 bool WriteControlFrameHeader(const SpdyFramer& framer,
58 // Populates this frame with a SPDY data frame header using version-specific
59 // information from the |framer| and length information from capacity_.
60 bool WriteDataFrameHeader(const SpdyFramer& framer,
61 SpdyStreamId stream_id,
64 // Populates this frame with a SPDY4/HTTP2 frame prefix using
65 // version-specific information from the |framer| and length information from
66 // capacity_. The given type must be a control frame type.
67 // Used only for SPDY versions >=4.
68 bool WriteFramePrefix(const SpdyFramer& framer,
71 SpdyStreamId stream_id);
73 // Takes the buffer from the SpdyFrameBuilder.
75 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length_, true);
81 // Methods for adding to the payload. These values are appended to the end
82 // of the SpdyFrameBuilder payload. Note - binary integers are converted from
83 // host to network form.
84 bool WriteUInt8(uint8 value) {
85 return WriteBytes(&value, sizeof(value));
87 bool WriteUInt16(uint16 value) {
89 return WriteBytes(&value, sizeof(value));
91 bool WriteUInt32(uint32 value) {
93 return WriteBytes(&value, sizeof(value));
95 // TODO(hkhalil) Rename to WriteStringPiece16().
96 bool WriteString(const std::string& value);
97 bool WriteStringPiece32(const base::StringPiece& value);
98 bool WriteBytes(const void* data, uint32 data_len);
100 // Update (in-place) the length field in the frame being built to reflect the
101 // current actual length of bytes written to said frame through this builder.
102 // The framer parameter is used to determine version-specific location and
103 // size information of the length field to be written, and must be initialized
104 // with the correct version for the frame being written.
105 bool RewriteLength(const SpdyFramer& framer);
107 // Update (in-place) the length field in the frame being built to reflect the
109 // The framer parameter is used to determine version-specific location and
110 // size information of the length field to be written, and must be initialized
111 // with the correct version for the frame being written.
112 bool OverwriteLength(const SpdyFramer& framer, size_t length);
115 // Checks to make sure that there is an appropriate amount of space for a
116 // write of given size, in bytes.
117 bool CanWrite(size_t length) const;
119 scoped_ptr<char[]> buffer_;
120 size_t capacity_; // Allocation size of payload, set by constructor.
121 size_t length_; // Current length of the buffer.
126 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_