OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_FRAMER_H_ | 5 #ifndef NET_SPDY_SPDY_FRAMER_H_ |
6 #define NET_SPDY_SPDY_FRAMER_H_ | 6 #define NET_SPDY_SPDY_FRAMER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #ifdef _WIN32 | 9 #ifdef _WIN32 |
10 #include <winsock2.h> | 10 #include <winsock2.h> |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 | 67 |
68 // Called when data is received. | 68 // Called when data is received. |
69 // |stream_id| The stream receiving data. | 69 // |stream_id| The stream receiving data. |
70 // |data| A buffer containing the data received. | 70 // |data| A buffer containing the data received. |
71 // |len| The length of the data buffer. | 71 // |len| The length of the data buffer. |
72 // When the other side has finished sending data on this stream, | 72 // When the other side has finished sending data on this stream, |
73 // this method will be called with a zero-length buffer. | 73 // this method will be called with a zero-length buffer. |
74 virtual void OnStreamFrameData(SpdyStreamId stream_id, | 74 virtual void OnStreamFrameData(SpdyStreamId stream_id, |
75 const char* data, | 75 const char* data, |
76 size_t len) = 0; | 76 size_t len) = 0; |
77 | |
78 // Returns true if the underlying socket's transport protocol is SCTP. | |
79 virtual bool using_sctp() { return false; } | |
Mike Belshe
2011/04/06 18:32:53
const functions
| |
80 | |
81 // Returns true if the underlying socker transport protocol if SCTP and SPDY | |
82 // control frames are send on SCTP stream 0. | |
83 virtual bool using_sctp_control_stream() { return false; } | |
Mike Belshe
2011/04/06 18:32:53
const
| |
84 | |
85 // Maps a SPDY stream number to an SCTP stream ID. | |
86 virtual uint16 MapSpdyToSctp(uint32 stream_id) { return 0; } | |
Mike Belshe
2011/04/06 18:32:53
const
| |
77 }; | 87 }; |
78 | 88 |
79 class SpdyFramer { | 89 class SpdyFramer { |
80 public: | 90 public: |
81 // SPDY states. | 91 // SPDY states. |
82 // TODO(mbelshe): Can we move these into the implementation | 92 // TODO(mbelshe): Can we move these into the implementation |
83 // and avoid exposing through the header. (Needed for test) | 93 // and avoid exposing through the header. (Needed for test) |
84 enum SpdyState { | 94 enum SpdyState { |
85 SPDY_ERROR, | 95 SPDY_ERROR, |
86 SPDY_DONE, | 96 SPDY_DONE, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 // On failure, returns NULL. | 249 // On failure, returns NULL. |
240 SpdyFrame* DecompressFrame(const SpdyFrame& frame); | 250 SpdyFrame* DecompressFrame(const SpdyFrame& frame); |
241 | 251 |
242 // Create a copy of a frame. | 252 // Create a copy of a frame. |
243 // Returned frame must be freed with "delete". | 253 // Returned frame must be freed with "delete". |
244 SpdyFrame* DuplicateFrame(const SpdyFrame& frame); | 254 SpdyFrame* DuplicateFrame(const SpdyFrame& frame); |
245 | 255 |
246 // Returns true if a frame could be compressed. | 256 // Returns true if a frame could be compressed. |
247 bool IsCompressible(const SpdyFrame& frame) const; | 257 bool IsCompressible(const SpdyFrame& frame) const; |
248 | 258 |
259 // Returns true if underlying socket transport protocol is SCTP. | |
260 bool using_sctp() { | |
261 if (visitor_) | |
262 return visitor_->using_sctp(); | |
263 else | |
264 // TODO(jtl): return sctp_enabled() here? | |
265 return false; | |
266 } | |
267 | |
268 // Returns true if the underlying socker transport protocol if SCTP and SPDY | |
269 // control frames are send on SCTP stream 0. | |
270 bool using_sctp_control_stream() { | |
271 if (visitor_) | |
272 return visitor_->using_sctp_control_stream(); | |
273 else | |
274 // TODO(jtl): return sctp_enabled() here? | |
275 return false; | |
276 } | |
277 | |
278 // Maps a SPDY stream number of an SCTP stream ID. | |
279 uint16 MapSpdyToSctp(uint32 stream_id) { | |
280 return visitor_->MapSpdyToSctp(stream_id); | |
281 } | |
282 | |
249 // For debugging. | 283 // For debugging. |
250 static const char* StateToString(int state); | 284 static const char* StateToString(int state); |
251 static const char* ErrorCodeToString(int error_code); | 285 static const char* ErrorCodeToString(int error_code); |
252 static void set_protocol_version(int version) { spdy_version_= version; } | 286 static void set_protocol_version(int version) { spdy_version_= version; } |
253 static int protocol_version() { return spdy_version_; } | 287 static int protocol_version() { return spdy_version_; } |
254 | 288 |
255 // Export the compression dictionary | 289 // Export the compression dictionary |
256 static const char kDictionary[]; | 290 static const char kDictionary[]; |
257 static const int kDictionarySize; | 291 static const int kDictionarySize; |
258 | 292 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 typedef std::map<SpdyStreamId, z_stream*> CompressorMap; | 327 typedef std::map<SpdyStreamId, z_stream*> CompressorMap; |
294 | 328 |
295 // Internal breakout from ProcessInput. Returns the number of bytes | 329 // Internal breakout from ProcessInput. Returns the number of bytes |
296 // consumed from the data. | 330 // consumed from the data. |
297 size_t ProcessCommonHeader(const char* data, size_t len); | 331 size_t ProcessCommonHeader(const char* data, size_t len); |
298 void ProcessControlFrameHeader(); | 332 void ProcessControlFrameHeader(); |
299 size_t ProcessControlFramePayload(const char* data, size_t len); | 333 size_t ProcessControlFramePayload(const char* data, size_t len); |
300 size_t ProcessDataFramePayload(const char* data, size_t len); | 334 size_t ProcessDataFramePayload(const char* data, size_t len); |
301 | 335 |
302 // Get (and lazily initialize) the ZLib state. | 336 // Get (and lazily initialize) the ZLib state. |
303 z_stream* GetHeaderCompressor(); | 337 z_stream* GetHeaderCompressor(int index); |
304 z_stream* GetHeaderDecompressor(); | 338 z_stream* GetHeaderDecompressor(int index); |
305 z_stream* GetStreamCompressor(SpdyStreamId id); | 339 z_stream* GetStreamCompressor(SpdyStreamId id); |
306 z_stream* GetStreamDecompressor(SpdyStreamId id); | 340 z_stream* GetStreamDecompressor(SpdyStreamId id); |
307 | 341 |
342 uint16 GetDictionaryIndex(const SpdyControlFrame& control_frame); | |
343 | |
308 // Compression helpers | 344 // Compression helpers |
309 SpdyControlFrame* CompressControlFrame(const SpdyControlFrame& frame); | 345 SpdyControlFrame* CompressControlFrame(const SpdyControlFrame& frame); |
310 SpdyDataFrame* CompressDataFrame(const SpdyDataFrame& frame); | 346 SpdyDataFrame* CompressDataFrame(const SpdyDataFrame& frame); |
311 SpdyControlFrame* DecompressControlFrame(const SpdyControlFrame& frame); | 347 SpdyControlFrame* DecompressControlFrame(const SpdyControlFrame& frame); |
312 SpdyDataFrame* DecompressDataFrame(const SpdyDataFrame& frame); | 348 SpdyDataFrame* DecompressDataFrame(const SpdyDataFrame& frame); |
313 SpdyFrame* CompressFrameWithZStream(const SpdyFrame& frame, | 349 SpdyFrame* CompressFrameWithZStream(const SpdyFrame& frame, |
314 z_stream* compressor); | 350 z_stream* compressor); |
315 SpdyFrame* DecompressFrameWithZStream(const SpdyFrame& frame, | 351 SpdyFrame* DecompressFrameWithZStream(const SpdyFrame& frame, |
316 z_stream* decompressor); | 352 z_stream* decompressor); |
317 void CleanupCompressorForStream(SpdyStreamId id); | 353 void CleanupCompressorForStream(SpdyStreamId id); |
318 void CleanupDecompressorForStream(SpdyStreamId id); | 354 void CleanupDecompressorForStream(SpdyStreamId id); |
319 void CleanupStreamCompressorsAndDecompressors(); | 355 void CleanupStreamCompressorsAndDecompressors(); |
356 void CleanupHeaderCompressorsAndDecompressors(); | |
320 | 357 |
321 // Not used (yet) | 358 // Not used (yet) |
322 size_t BytesSafeToRead() const; | 359 size_t BytesSafeToRead() const; |
323 | 360 |
324 // Set the error code and moves the framer into the error state. | 361 // Set the error code and moves the framer into the error state. |
325 void set_error(SpdyError error); | 362 void set_error(SpdyError error); |
326 | 363 |
327 // Expands the control frame buffer to accomodate a particular payload size. | 364 // Expands the control frame buffer to accomodate a particular payload size. |
328 void ExpandControlFrameBuffer(size_t size); | 365 void ExpandControlFrameBuffer(size_t size); |
329 | 366 |
330 // Given a frame, breakdown the variable payload length, the static header | 367 // Given a frame, breakdown the variable payload length, the static header |
331 // header length, and variable payload pointer. | 368 // header length, and variable payload pointer. |
332 bool GetFrameBoundaries(const SpdyFrame& frame, int* payload_length, | 369 bool GetFrameBoundaries(const SpdyFrame& frame, int* payload_length, |
333 int* header_length, const char** payload) const; | 370 int* header_length, const char** payload) const; |
334 | 371 |
335 int num_stream_compressors() const { return stream_compressors_.size(); } | 372 int num_stream_compressors() const { return stream_compressors_.size(); } |
336 int num_stream_decompressors() const { return stream_decompressors_.size(); } | 373 int num_stream_decompressors() const { return stream_decompressors_.size(); } |
337 | 374 |
338 SpdyState state_; | 375 SpdyState state_; |
339 SpdyError error_code_; | 376 SpdyError error_code_; |
340 size_t remaining_payload_; | 377 size_t remaining_payload_; |
341 size_t remaining_control_payload_; | 378 size_t remaining_control_payload_; |
342 | 379 |
343 char* current_frame_buffer_; | 380 char* current_frame_buffer_; |
344 size_t current_frame_len_; // Number of bytes read into the current_frame_. | 381 size_t current_frame_len_; // Number of bytes read into the current_frame_. |
345 size_t current_frame_capacity_; | 382 size_t current_frame_capacity_; |
346 | 383 |
347 bool enable_compression_; // Controls all compression | 384 bool enable_compression_; // Controls all compression |
348 // SPDY header compressors. | 385 |
349 scoped_ptr<z_stream> header_compressor_; | 386 // SPDY header compressors (possbily per-stream when using SCTP). |
350 scoped_ptr<z_stream> header_decompressor_; | 387 CompressorMap header_compressors_; |
388 CompressorMap header_decompressors_; | |
351 | 389 |
352 // Per-stream data compressors. | 390 // Per-stream data compressors. |
353 CompressorMap stream_compressors_; | 391 CompressorMap stream_compressors_; |
354 CompressorMap stream_decompressors_; | 392 CompressorMap stream_decompressors_; |
355 | 393 |
356 SpdyFramerVisitorInterface* visitor_; | 394 SpdyFramerVisitorInterface* visitor_; |
357 | 395 |
358 static bool compression_default_; | 396 static bool compression_default_; |
359 static int spdy_version_; | 397 static int spdy_version_; |
360 }; | 398 }; |
361 | 399 |
362 } // namespace spdy | 400 } // namespace spdy |
363 | 401 |
364 #endif // NET_SPDY_SPDY_FRAMER_H_ | 402 #endif // NET_SPDY_SPDY_FRAMER_H_ |
OLD | NEW |