Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer.h
bloba77de753ab97c18ab8ecc88a1066c833567e4417
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_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/socket/next_proto.h"
15 #include "net/spdy/spdy_framer.h"
16 #include "net/spdy/spdy_header_block.h"
17 #include "net/spdy/spdy_protocol.h"
19 namespace net {
21 // Returns the SPDY major version corresponding to the given NextProto
22 // value, which must represent a SPDY-like protocol.
23 NET_EXPORT_PRIVATE SpdyMajorVersion NextProtoToSpdyMajorVersion(
24 NextProto next_proto);
26 class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface {
27 public:
28 BufferedSpdyFramerVisitorInterface() {}
30 // Called if an error is detected in the SpdyFrame protocol.
31 virtual void OnError(SpdyFramer::SpdyError error_code) = 0;
33 // Called if an error is detected in a SPDY stream.
34 virtual void OnStreamError(SpdyStreamId stream_id,
35 const std::string& description) = 0;
37 // Called after all the header data for SYN_STREAM control frame is received.
38 virtual void OnSynStream(SpdyStreamId stream_id,
39 SpdyStreamId associated_stream_id,
40 SpdyPriority priority,
41 bool fin,
42 bool unidirectional,
43 const SpdyHeaderBlock& headers) = 0;
45 // Called after all the header data for SYN_REPLY control frame is received.
46 virtual void OnSynReply(SpdyStreamId stream_id,
47 bool fin,
48 const SpdyHeaderBlock& headers) = 0;
50 // Called after all the header data for HEADERS control frame is received.
51 virtual void OnHeaders(SpdyStreamId stream_id,
52 bool fin,
53 const SpdyHeaderBlock& headers) = 0;
55 // Called when a data frame header is received.
56 virtual void OnDataFrameHeader(SpdyStreamId stream_id,
57 size_t length,
58 bool fin) = 0;
60 // Called when data is received.
61 // |stream_id| The stream receiving data.
62 // |data| A buffer containing the data received.
63 // |len| The length of the data buffer (at most 2^24 - 1 for SPDY/3,
64 // but 2^16 - 1 - 8 for SPDY/4).
65 // When the other side has finished sending data on this stream,
66 // this method will be called with a zero-length buffer.
67 virtual void OnStreamFrameData(SpdyStreamId stream_id,
68 const char* data,
69 size_t len,
70 bool fin) = 0;
72 // Called when a SETTINGS frame is received.
73 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
74 virtual void OnSettings(bool clear_persisted) = 0;
76 // Called when an individual setting within a SETTINGS frame has been parsed
77 // and validated.
78 virtual void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) = 0;
80 // Called when a SETTINGS frame is received with the ACK flag set.
81 virtual void OnSettingsAck() {}
83 // Called at the completion of parsing SETTINGS id and value tuples.
84 virtual void OnSettingsEnd() {};
86 // Called when a PING frame has been parsed.
87 virtual void OnPing(SpdyPingId unique_id, bool is_ack) = 0;
89 // Called when a RST_STREAM frame has been parsed.
90 virtual void OnRstStream(SpdyStreamId stream_id,
91 SpdyRstStreamStatus status) = 0;
93 // Called when a GOAWAY frame has been parsed.
94 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
95 SpdyGoAwayStatus status) = 0;
97 // Called when a WINDOW_UPDATE frame has been parsed.
98 virtual void OnWindowUpdate(SpdyStreamId stream_id,
99 uint32 delta_window_size) = 0;
101 // Called when a PUSH_PROMISE frame has been parsed.
102 virtual void OnPushPromise(SpdyStreamId stream_id,
103 SpdyStreamId promised_stream_id) = 0;
105 protected:
106 virtual ~BufferedSpdyFramerVisitorInterface() {}
108 private:
109 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramerVisitorInterface);
112 class NET_EXPORT_PRIVATE BufferedSpdyFramer
113 : public SpdyFramerVisitorInterface {
114 public:
115 BufferedSpdyFramer(SpdyMajorVersion version,
116 bool enable_compression);
117 virtual ~BufferedSpdyFramer();
119 // Sets callbacks to be called from the buffered spdy framer. A visitor must
120 // be set, or else the framer will likely crash. It is acceptable for the
121 // visitor to do nothing. If this is called multiple times, only the last
122 // visitor will be used.
123 void set_visitor(BufferedSpdyFramerVisitorInterface* visitor);
125 // Set debug callbacks to be called from the framer. The debug visitor is
126 // completely optional and need not be set in order for normal operation.
127 // If this is called multiple times, only the last visitor will be used.
128 void set_debug_visitor(SpdyFramerDebugVisitorInterface* debug_visitor);
130 // SpdyFramerVisitorInterface
131 virtual void OnError(SpdyFramer* spdy_framer) OVERRIDE;
132 virtual void OnSynStream(SpdyStreamId stream_id,
133 SpdyStreamId associated_stream_id,
134 SpdyPriority priority,
135 bool fin,
136 bool unidirectional) OVERRIDE;
137 virtual void OnSynReply(SpdyStreamId stream_id, bool fin) OVERRIDE;
138 virtual void OnHeaders(SpdyStreamId stream_id, bool fin, bool end) OVERRIDE;
139 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id,
140 const char* header_data,
141 size_t len) OVERRIDE;
142 virtual void OnStreamFrameData(SpdyStreamId stream_id,
143 const char* data,
144 size_t len,
145 bool fin) OVERRIDE;
146 virtual void OnSettings(bool clear_persisted) OVERRIDE;
147 virtual void OnSetting(
148 SpdySettingsIds id, uint8 flags, uint32 value) OVERRIDE;
149 virtual void OnSettingsAck() OVERRIDE;
150 virtual void OnSettingsEnd() OVERRIDE;
151 virtual void OnPing(SpdyPingId unique_id, bool is_ack) OVERRIDE;
152 virtual void OnRstStream(SpdyStreamId stream_id,
153 SpdyRstStreamStatus status) OVERRIDE;
154 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
155 SpdyGoAwayStatus status) OVERRIDE;
156 virtual void OnWindowUpdate(SpdyStreamId stream_id,
157 uint32 delta_window_size) OVERRIDE;
158 virtual void OnPushPromise(SpdyStreamId stream_id,
159 SpdyStreamId promised_stream_id,
160 bool end) OVERRIDE;
161 virtual void OnDataFrameHeader(SpdyStreamId stream_id,
162 size_t length,
163 bool fin) OVERRIDE;
164 virtual void OnContinuation(SpdyStreamId stream_id, bool end) OVERRIDE;
166 // SpdyFramer methods.
167 size_t ProcessInput(const char* data, size_t len);
168 SpdyMajorVersion protocol_version();
169 void Reset();
170 SpdyFramer::SpdyError error_code() const;
171 SpdyFramer::SpdyState state() const;
172 bool MessageFullyRead();
173 bool HasError();
174 SpdyFrame* CreateSynStream(SpdyStreamId stream_id,
175 SpdyStreamId associated_stream_id,
176 SpdyPriority priority,
177 SpdyControlFlags flags,
178 const SpdyHeaderBlock* headers);
179 SpdyFrame* CreateSynReply(SpdyStreamId stream_id,
180 SpdyControlFlags flags,
181 const SpdyHeaderBlock* headers);
182 SpdyFrame* CreateRstStream(SpdyStreamId stream_id,
183 SpdyRstStreamStatus status) const;
184 SpdyFrame* CreateSettings(const SettingsMap& values) const;
185 SpdyFrame* CreatePingFrame(uint32 unique_id, bool is_ack) const;
186 SpdyFrame* CreateGoAway(
187 SpdyStreamId last_accepted_stream_id,
188 SpdyGoAwayStatus status) const;
189 SpdyFrame* CreateHeaders(SpdyStreamId stream_id,
190 SpdyControlFlags flags,
191 const SpdyHeaderBlock* headers);
192 SpdyFrame* CreateWindowUpdate(
193 SpdyStreamId stream_id,
194 uint32 delta_window_size) const;
195 SpdyFrame* CreateDataFrame(SpdyStreamId stream_id,
196 const char* data,
197 uint32 len,
198 SpdyDataFlags flags);
200 // Serialize a frame of unknown type.
201 SpdySerializedFrame* SerializeFrame(const SpdyFrameIR& frame) {
202 return spdy_framer_.SerializeFrame(frame);
205 SpdyPriority GetHighestPriority() const;
207 size_t GetDataFrameMinimumSize() const {
208 return spdy_framer_.GetDataFrameMinimumSize();
211 size_t GetControlFrameHeaderSize() const {
212 return spdy_framer_.GetControlFrameHeaderSize();
215 size_t GetSynStreamMinimumSize() const {
216 return spdy_framer_.GetSynStreamMinimumSize();
219 size_t GetFrameMinimumSize() const {
220 return spdy_framer_.GetFrameMinimumSize();
223 size_t GetFrameMaximumSize() const {
224 return spdy_framer_.GetFrameMaximumSize();
227 size_t GetDataFrameMaximumPayload() const {
228 return spdy_framer_.GetDataFrameMaximumPayload();
231 int frames_received() const { return frames_received_; }
233 private:
234 // The size of the header_buffer_.
235 enum { kHeaderBufferSize = 32 * 1024 };
237 void InitHeaderStreaming(SpdyStreamId stream_id);
239 SpdyFramer spdy_framer_;
240 BufferedSpdyFramerVisitorInterface* visitor_;
242 // Header block streaming state:
243 char header_buffer_[kHeaderBufferSize];
244 size_t header_buffer_used_;
245 bool header_buffer_valid_;
246 SpdyStreamId header_stream_id_;
247 int frames_received_;
249 // Collection of fields from control frames that we need to
250 // buffer up from the spdy framer.
251 struct ControlFrameFields {
252 SpdyFrameType type;
253 SpdyStreamId stream_id;
254 SpdyStreamId associated_stream_id;
255 SpdyPriority priority;
256 uint8 credential_slot;
257 bool fin;
258 bool unidirectional;
260 scoped_ptr<ControlFrameFields> control_frame_fields_;
262 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramer);
265 } // namespace net
267 #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_