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