Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_FRAME_READER_H_ | 5 #ifndef NET_QUIC_QUIC_DATA_READER_H_ |
| 6 #define NET_SPDY_SPDY_FRAME_READER_H_ | 6 #define NET_QUIC_QUIC_DATA_READER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "net/quic/uint128.h" | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: alphabetize
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 9 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
| 10 #include "net/base/net_export.h" | |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 // Used for reading SPDY frames. Though there isn't really anything terribly | 14 // Used for reading QUIC data. Though there isn't really anything terribly |
| 15 // SPDY-specific here, it's a helper class that's useful when doing SPDY | 15 // QUIC-specific here, it's a helper class that's useful when doing QUIC |
| 16 // framing. | 16 // framing. |
| 17 // | 17 // |
| 18 // To use, simply construct a SpdyFramerReader using the underlying buffer that | 18 // To use, simply construct a QuicDataReader using the underlying buffer that |
| 19 // you'd like to read fields from, then call one of the Read*() methods to | 19 // you'd like to read fields from, then call one of the Read*() methods to |
| 20 // actually do some reading. | 20 // actually do some reading. |
| 21 // | 21 // |
| 22 // This class keeps an internal iterator to keep track of what's already been | 22 // This class keeps an internal iterator to keep track of what's already been |
| 23 // read and each successive Read*() call automatically increments said iterator | 23 // read and each successive Read*() call automatically increments said iterator |
| 24 // on success. On failure, internal state of the SpdyFrameReader should not be | 24 // on success. On failure, internal state of the QuicDataReader should not be |
| 25 // trusted and it is up to the caller to throw away the failed instance and | 25 // trusted and it is up to the caller to throw away the failed instance and |
| 26 // handle the error as appropriate. None of the Read*() methods should ever be | 26 // handle the error as appropriate. None of the Read*() methods should ever be |
| 27 // called after failure, as they will also fail immediately. | 27 // called after failure, as they will also fail immediately. |
| 28 class NET_EXPORT_PRIVATE SpdyFrameReader { | 28 class QuicDataReader { |
| 29 public: | 29 public: |
| 30 // Caller must provide an underlying buffer to work on. | 30 // Caller must provide an underlying buffer to work on. |
| 31 SpdyFrameReader(const char* data, const size_t len); | 31 QuicDataReader(const char* data, const size_t len); |
| 32 | 32 |
| 33 // Empty destructor. | 33 // Empty destructor. |
| 34 ~SpdyFrameReader() {} | 34 ~QuicDataReader() {} |
| 35 | 35 |
| 36 // Reads a 16-bit unsigned integer into the given output parameter. | 36 // Reads a 16-bit unsigned integer into the given output parameter. |
| 37 // Forwards the internal iterater on success. | 37 // Forwards the internal iterater on success. |
| 38 // Returns true on success, false otherwise. | 38 // Returns true on success, false otherwise. |
| 39 bool ReadUInt16(uint16* result); | 39 bool ReadUInt16(uint16* result); |
| 40 | 40 |
| 41 // Reads a 32-bit unsigned integer into the given output parameter. | 41 // Reads a 32-bit unsigned integer into the given output parameter. |
| 42 // Forwards the internal iterater on success. | 42 // Forwards the internal iterater on success. |
| 43 // Returns true on success, false otherwise. | 43 // Returns true on success, false otherwise. |
| 44 bool ReadUInt32(uint32* result); | 44 bool ReadUInt32(uint32* result); |
| 45 | 45 |
| 46 // Reads a 48-bit unsigned integer into the given output parameter. | |
| 47 // Forwards the internal iterater on success. | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: spelling: iterater--> iterator
This was inhe
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 48 // Returns true on success, false otherwise. | |
| 49 bool ReadUInt48(uint64* result); | |
| 50 | |
| 51 // Reads a 64-bit unsigned integer into the given output parameter. | |
| 52 // Forwards the internal iterater on success. | |
| 53 // Returns true on success, false otherwise. | |
| 54 bool ReadUInt64(uint64* result); | |
| 55 | |
| 56 // Reads a 128-bit unsigned integer nito the given output parameter. | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: nito-->into
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 57 // Forwards the internal iterater on success. | |
| 58 // Returns true on success, false otherwise. | |
| 59 bool ReadUInt128(uint128* result); | |
| 46 // Reads a string prefixed with 16-bit length into the given output parameter. | 60 // Reads a string prefixed with 16-bit length into the given output parameter. |
| 47 // | 61 // |
| 48 // NOTE: Does not copy but rather references strings in the underlying buffer. | 62 // NOTE: Does not copy but rather references strings in the underlying buffer. |
| 49 // This should be kept in mind when handling memory management! | 63 // This should be kept in mind when handling memory management! |
| 50 // | 64 // |
| 51 // Forwards the internal iterater on success. | 65 // Forwards the internal iterater on success. |
| 52 // Returns true on success, false otherwise. | 66 // Returns true on success, false otherwise. |
| 53 bool ReadStringPiece16(base::StringPiece* result); | 67 bool ReadStringPiece16(base::StringPiece* result); |
| 54 | 68 |
| 55 // Reads a string prefixed with 32-bit length into the given output parameter. | 69 // Reads a given number of bytes into the given buffer. The buffer |
| 70 // must be of adequate size. | |
| 71 // Forwards the internal iterater on success. | |
| 72 // Returns true on success, false otherwise. | |
| 73 bool ReadStringPiece(base::StringPiece* result, size_t len); | |
| 74 | |
| 75 // Returns the remaining payload as a StringPiece. | |
| 56 // | 76 // |
| 57 // NOTE: Does not copy but rather references strings in the underlying buffer. | 77 // NOTE: Does not copy but rather references strings in the underlying buffer. |
| 58 // This should be kept in mind when handling memory management! | 78 // This should be kept in mind when handling memory management! |
| 59 // | 79 // |
| 60 // Forwards the internal iterater on success. | 80 // Forwards the internal iterater. |
| 61 // Returns true on success, false otherwise. | 81 base::StringPiece ReadRemainingPayload(); |
| 62 bool ReadStringPiece32(base::StringPiece* result); | 82 |
| 83 // Returns the remaining payload as a StringPiece. | |
| 84 // | |
| 85 // NOTE: Does not copy but rather references strings in the underlying buffer. | |
| 86 // This should be kept in mind when handling memory management! | |
| 87 // | |
| 88 // DOES NOT forward the internal iterater. | |
| 89 base::StringPiece PeekRemainingPayload(); | |
| 63 | 90 |
| 64 // Reads a given number of bytes into the given buffer. The buffer | 91 // Reads a given number of bytes into the given buffer. The buffer |
| 65 // must be of adequate size. | 92 // must be of adequate size. |
| 66 // Forwards the internal iterater on success. | 93 // Forwards the internal iterater on success. |
| 67 // Returns true on success, false otherwise. | 94 // Returns true on success, false otherwise. |
| 68 bool ReadBytes(void* result, size_t size); | 95 bool ReadBytes(void* result, size_t size); |
| 69 | 96 |
| 70 // Returns true if the entirety of the underlying buffer has been read via | 97 // Returns true if the entirety of the underlying buffer has been read via |
| 71 // Read*() calls. | 98 // Read*() calls. |
| 72 bool IsDoneReading() const; | 99 bool IsDoneReading() const; |
| 73 | 100 |
| 101 // Returns the number of bytes remaining to be read. | |
| 102 size_t BytesRemaining() const; | |
| 103 | |
| 74 private: | 104 private: |
| 75 // Returns true if the underlying buffer has enough room to read the given | 105 // Returns true if the underlying buffer has enough room to read the given |
| 76 // amount of bytes. | 106 // amount of bytes. |
| 77 bool CanRead(size_t bytes) const; | 107 bool CanRead(size_t bytes) const; |
| 78 | 108 |
| 79 // To be called when a read fails for any reason. | 109 // To be called when a read fails for any reason. |
| 80 void OnFailure(); | 110 void OnFailure(); |
| 81 | 111 |
| 82 // The data buffer that we're reading from. | 112 // The data buffer that we're reading from. |
| 83 const char* data_; | 113 const char* data_; |
| 84 | 114 |
| 85 // The length of the data buffer that we're reading from. | 115 // The length of the data buffer that we're reading from. |
| 86 const size_t len_; | 116 const size_t len_; |
| 87 | 117 |
| 88 // The location of the next read from our data buffer. | 118 // The location of the next read from our data buffer. |
| 89 size_t ofs_; | 119 size_t pos_; |
| 90 }; | 120 }; |
| 91 | 121 |
| 92 } // namespace net | 122 } // namespace net |
| 93 | 123 |
| 94 #endif // NET_SPDY_SPDY_FRAME_READER_H_ | 124 #endif // NET_QUIC_QUIC_DATA_READER_H_ |
| OLD | NEW |