OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef NET_SPDY_FRAME_READER_H_ |
| 6 #define NET_SPDY_FRAME_READER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/string_piece.h" |
| 11 #include "net/base/net_export.h" |
| 12 |
| 13 namespace spdy { |
| 14 |
| 15 // Used for reading SPDY frames. Though there isn't really anything terribly |
| 16 // SPDY-specific here, it's a helper class that's useful when doing SPDY |
| 17 // framing. |
| 18 // |
| 19 // To use, simply construct a SpdyFramerReader using the underlying buffer that |
| 20 // you'd like to read fields from, then call one of the Read*() methods to |
| 21 // actually do some reading. |
| 22 // |
| 23 // This class keeps an internal iterator to keep track of what's already been |
| 24 // read and each successive Read*() call automatically increments said iterator |
| 25 // on success. On failure, internal state of the SpdyFrameReader should not be |
| 26 // trusted and it is up to the caller to throw away the failed instance and |
| 27 // handle the error as appropriate. None of the Read*() methods should ever be |
| 28 // called after failure, as they will also fail immediately. |
| 29 class NET_EXPORT_PRIVATE SpdyFrameReader { |
| 30 public: |
| 31 // Caller must provide an underlying buffer to work on. |
| 32 SpdyFrameReader(const char* data, const size_t len); |
| 33 |
| 34 // Empty destructor. |
| 35 ~SpdyFrameReader() {} |
| 36 |
| 37 // Reads a 16-bit unsigned integer into the given output parameter. |
| 38 // Forwards the internal iterater on success. |
| 39 // Returns true on success, false otherwise. |
| 40 bool ReadUInt16(uint16* result); |
| 41 |
| 42 // Reads a 32-bit unsigned integer into the given output parameter. |
| 43 // Forwards the internal iterater on success. |
| 44 // Returns true on success, false otherwise. |
| 45 bool ReadUInt32(uint32* result); |
| 46 |
| 47 // Reads a string prefixed with 16-bit length into the given output parameter. |
| 48 // |
| 49 // NOTE: Does not copy but rather references strings in the underlying buffer. |
| 50 // This should be kept in mind when handling memory management! |
| 51 // |
| 52 // Forwards the internal iterater on success. |
| 53 // Returns true on success, false otherwise. |
| 54 bool ReadStringPiece16(base::StringPiece* result); |
| 55 |
| 56 // Reads a string prefixed with 32-bit length into the given output parameter. |
| 57 // |
| 58 // NOTE: Does not copy but rather references strings in the underlying buffer. |
| 59 // This should be kept in mind when handling memory management! |
| 60 // |
| 61 // Forwards the internal iterater on success. |
| 62 // Returns true on success, false otherwise. |
| 63 bool ReadStringPiece32(base::StringPiece* result); |
| 64 |
| 65 // Reads a given number of bytes into the given buffer. The buffer |
| 66 // must be of adequate size. |
| 67 // Forwards the internal iterater on success. |
| 68 // Returns true on success, false otherwise. |
| 69 bool ReadBytes(void* result, size_t size); |
| 70 |
| 71 // Returns true if the entirety of the underlying buffer has been read via |
| 72 // Read*() calls. |
| 73 bool IsDoneReading() const; |
| 74 |
| 75 private: |
| 76 // Returns true if the underlying buffer has enough room to read the given |
| 77 // amount of bytes. |
| 78 bool CanRead(size_t bytes) const; |
| 79 |
| 80 // To be called when a read fails for any reason. |
| 81 void OnFailure(); |
| 82 |
| 83 // The data buffer that we're reading from. |
| 84 const char* data_; |
| 85 |
| 86 // The length of the data buffer that we're reading from. |
| 87 const size_t len_; |
| 88 |
| 89 // The location of the next read from our data buffer. |
| 90 size_t ofs_; |
| 91 }; |
| 92 |
| 93 } // namespace spdy |
| 94 |
| 95 #endif // NET_SPDY_FRAME_READER_H_ |
| 96 |
OLD | NEW |