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_BUFFERED_SPDY_FRAMER_H_
6 #define NET_SPDY_BUFFERED_SPDY_FRAMER_H_
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/net_export.h"
16 #include "net/spdy/spdy_framer.h"
17 #include "net/spdy/spdy_protocol.h"
21 class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface
{
23 BufferedSpdyFramerVisitorInterface() {}
24 virtual ~BufferedSpdyFramerVisitorInterface() {}
26 // Called if an error is detected in the SpdyFrame protocol.
27 virtual void OnError(int error_code
) = 0;
29 // Called if an error is detected in a SPDY stream.
30 virtual void OnStreamError(SpdyStreamId stream_id
,
31 const std::string
& description
) = 0;
33 // Called after all the header data for SYN_STREAM control frame is received.
34 virtual void OnSynStream(const SpdySynStreamControlFrame
& frame
,
35 const linked_ptr
<SpdyHeaderBlock
>& headers
) = 0;
37 // Called after all the header data for SYN_REPLY control frame is received.
38 virtual void OnSynReply(const SpdySynReplyControlFrame
& frame
,
39 const linked_ptr
<SpdyHeaderBlock
>& headers
) = 0;
41 // Called after all the header data for HEADERS control frame is received.
42 virtual void OnHeaders(const SpdyHeadersControlFrame
& frame
,
43 const linked_ptr
<SpdyHeaderBlock
>& headers
) = 0;
45 // Called after a RST_STREAM frame is received.
46 virtual void OnRstStream(const SpdyRstStreamControlFrame
& frame
) = 0;
48 // Called after a GOAWAY frame is received.
49 virtual void OnGoAway(const SpdyGoAwayControlFrame
& frame
) = 0;
51 // Called after a PING frame is received.
52 virtual void OnPing(const SpdyPingControlFrame
& frame
) = 0;
54 // Called after a WINDOW_UPDATE frame is received.
55 virtual void OnWindowUpdate(
56 const SpdyWindowUpdateControlFrame
& frame
) = 0;
58 // Called when data is received.
59 // |stream_id| The stream receiving data.
60 // |data| A buffer containing the data received.
61 // |len| The length of the data buffer.
62 // When the other side has finished sending data on this stream,
63 // this method will be called with a zero-length buffer.
64 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
68 // Called when an individual setting within a SETTINGS frame has been parsed
70 virtual void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) = 0;
73 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramerVisitorInterface
);
76 class NET_EXPORT_PRIVATE BufferedSpdyFramer
77 : public SpdyFramerVisitorInterface
{
79 explicit BufferedSpdyFramer(int version
);
80 virtual ~BufferedSpdyFramer();
82 // Sets callbacks to be called from the buffered spdy framer. A visitor must
83 // be set, or else the framer will likely crash. It is acceptable for the
84 // visitor to do nothing. If this is called multiple times, only the last
85 // visitor will be used.
86 void set_visitor(BufferedSpdyFramerVisitorInterface
* visitor
);
88 // SpdyFramerVisitorInterface
89 virtual void OnError(SpdyFramer
* spdy_framer
) OVERRIDE
;
90 virtual void OnControl(const SpdyControlFrame
* frame
) OVERRIDE
;
91 virtual bool OnCredentialFrameData(const char* frame_data
,
93 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
94 const char* header_data
,
96 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
99 virtual void OnSetting(
100 SpdySettingsIds id
, uint8 flags
, uint32 value
) OVERRIDE
;
101 virtual void OnDataFrameHeader(const SpdyDataFrame
* frame
) OVERRIDE
;
103 // SpdyFramer methods.
104 size_t ProcessInput(const char* data
, size_t len
);
105 int protocol_version();
107 SpdyFramer::SpdyError
error_code() const;
108 SpdyFramer::SpdyState
state() const;
109 bool MessageFullyRead();
111 SpdySynStreamControlFrame
* CreateSynStream(SpdyStreamId stream_id
,
112 SpdyStreamId associated_stream_id
,
113 SpdyPriority priority
,
114 uint8 credential_slot
,
115 SpdyControlFlags flags
,
117 const SpdyHeaderBlock
* headers
);
118 SpdySynReplyControlFrame
* CreateSynReply(SpdyStreamId stream_id
,
119 SpdyControlFlags flags
,
121 const SpdyHeaderBlock
* headers
);
122 SpdyRstStreamControlFrame
* CreateRstStream(SpdyStreamId stream_id
,
123 SpdyStatusCodes status
) const;
124 SpdySettingsControlFrame
* CreateSettings(const SettingsMap
& values
) const;
125 SpdyPingControlFrame
* CreatePingFrame(uint32 unique_id
) const;
126 SpdyGoAwayControlFrame
* CreateGoAway(
127 SpdyStreamId last_accepted_stream_id
,
128 SpdyGoAwayStatus status
) const;
129 SpdyHeadersControlFrame
* CreateHeaders(SpdyStreamId stream_id
,
130 SpdyControlFlags flags
,
132 const SpdyHeaderBlock
* headers
);
133 SpdyWindowUpdateControlFrame
* CreateWindowUpdate(
134 SpdyStreamId stream_id
,
135 uint32 delta_window_size
) const;
136 SpdyCredentialControlFrame
* CreateCredentialFrame(
137 const SpdyCredential
& credential
) const;
138 SpdyDataFrame
* CreateDataFrame(SpdyStreamId stream_id
,
141 SpdyDataFlags flags
);
142 SpdyPriority
GetHighestPriority() const;
143 bool IsCompressible(const SpdyFrame
& frame
) const;
144 SpdyControlFrame
* CompressControlFrame(const SpdyControlFrame
& frame
);
145 // Specify if newly created SpdySessions should have compression enabled.
146 static void set_enable_compression_default(bool value
);
148 int frames_received() const { return frames_received_
; }
151 // The size of the header_buffer_.
152 enum { kHeaderBufferSize
= 32 * 1024 };
154 void InitHeaderStreaming(const SpdyControlFrame
* frame
);
156 SpdyFramer spdy_framer_
;
157 BufferedSpdyFramerVisitorInterface
* visitor_
;
159 // Header block streaming state:
160 char header_buffer_
[kHeaderBufferSize
];
161 size_t header_buffer_used_
;
162 bool header_buffer_valid_
;
163 SpdyStreamId header_stream_id_
;
164 scoped_ptr
<SpdyFrame
> control_frame_
;
165 int frames_received_
;
167 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramer
);
172 #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_