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