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/string_piece.h"
13 #include "base/sys_byteorder.h"
14 #include "net/base/net_export.h"
15 #include "net/spdy/spdy_protocol.h"
19 // This class provides facilities for basic binary value packing
22 // The SpdyFrameBuilder supports appending primitive values (int, string, etc)
23 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer
24 // dynamically to hold the sequence of primitive values. The internal memory
25 // buffer is exposed as the "data" of the SpdyFrameBuilder.
26 class NET_EXPORT_PRIVATE SpdyFrameBuilder
{
28 // Initializes a SpdyFrameBuilder with a buffer of given size
29 explicit SpdyFrameBuilder(size_t size
);
31 // Initializes a SpdyFrameBuilder with a buffer of given size,
32 // populate with a SPDY control frame header based on
33 // |type|, |flags|, and |spdy_version|.
34 SpdyFrameBuilder(SpdyControlType type
, SpdyControlFlags flags
,
35 int spdy_version
, size_t size
);
37 // Initiailizes a SpdyFrameBuilder with a buffer of given size,
38 // populated with a SPDY data frame header based on
39 // |stream_id| and |flags|.
40 SpdyFrameBuilder(SpdyStreamId stream_id
, SpdyDataFlags flags
, size_t size
);
44 // Returns the size of the SpdyFrameBuilder's data.
45 size_t length() const { return length_
; }
47 // Takes the buffer from the SpdyFrameBuilder.
49 SpdyFrame
* rv
= new SpdyFrame(buffer_
.release(), true);
55 // Methods for adding to the payload. These values are appended to the end
56 // of the SpdyFrameBuilder payload. Note - binary integers are converted from
57 // host to network form.
58 bool WriteUInt8(uint8 value
) {
59 return WriteBytes(&value
, sizeof(value
));
61 bool WriteUInt16(uint16 value
) {
63 return WriteBytes(&value
, sizeof(value
));
65 bool WriteUInt32(uint32 value
) {
67 return WriteBytes(&value
, sizeof(value
));
69 // TODO(hkhalil) Rename to WriteStringPiece16().
70 bool WriteString(const std::string
& value
);
71 bool WriteStringPiece32(const base::StringPiece
& value
);
72 bool WriteBytes(const void* data
, uint32 data_len
);
74 // Write an integer to a particular offset in the data buffer.
75 bool WriteUInt32ToOffset(int offset
, uint32 value
) {
77 return WriteBytesToOffset(offset
, &value
, sizeof(value
));
80 // Write to a particular offset in the data buffer.
81 bool WriteBytesToOffset(int offset
, const void* data
, uint32 data_len
) {
82 if (offset
+ data_len
> length_
)
84 char *ptr
= buffer_
.get() + offset
;
85 memcpy(ptr
, data
, data_len
);
90 const char* end_of_payload() const { return buffer_
.get() + length_
; }
92 // Completes the write operation by padding the data with NULL bytes until it
93 // is padded. Should be paired with BeginWrite, but it does not necessarily
94 // have to be called after the data is written.
95 void EndWrite(char* dest
, int length
);
98 // Returns the location that the data should be written at, or NULL if there
99 // is not enough room. Call EndWrite with the returned offset and the given
100 // length to pad out for the next write.
101 char* BeginWrite(size_t length
);
103 scoped_ptr
<char[]> buffer_
;
104 size_t capacity_
; // Allocation size of payload (or -1 if buffer is const).
105 size_t length_
; // current length of the buffer
110 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_