Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / spdy / spdy_frame_reader.h
blobd30ae93c1bab814e2f8052a08dcc981d9089ef78
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.
5 #ifndef NET_SPDY_SPDY_FRAME_READER_H_
6 #define NET_SPDY_SPDY_FRAME_READER_H_
8 #include "base/basictypes.h"
9 #include "base/strings/string_piece.h"
10 #include "net/base/net_export.h"
12 namespace net {
14 // Used for reading SPDY frames. Though there isn't really anything terribly
15 // SPDY-specific here, it's a helper class that's useful when doing SPDY
16 // framing.
18 // To use, simply construct a SpdyFramerReader using the underlying buffer that
19 // you'd like to read fields from, then call one of the Read*() methods to
20 // actually do some reading.
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
24 // on success. On failure, internal state of the SpdyFrameReader should not be
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
27 // called after failure, as they will also fail immediately.
28 class NET_EXPORT_PRIVATE SpdyFrameReader {
29 public:
30 // Caller must provide an underlying buffer to work on.
31 SpdyFrameReader(const char* data, const size_t len);
33 // Empty destructor.
34 ~SpdyFrameReader() {}
36 // Reads an 8-bit unsigned integer into the given output parameter.
37 // Forwards the internal iterator on success.
38 // Returns true on success, false otherwise.
39 bool ReadUInt8(uint8* result);
41 // Reads a 16-bit unsigned integer into the given output parameter.
42 // Forwards the internal iterator on success.
43 // Returns true on success, false otherwise.
44 bool ReadUInt16(uint16* result);
46 // Reads a 32-bit unsigned integer into the given output parameter.
47 // Forwards the internal iterator on success.
48 // Returns true on success, false otherwise.
49 bool ReadUInt32(uint32* result);
51 // Reads a 64-bit unsigned integer into the given output parameter.
52 // Forwards the internal iterator on success.
53 // Returns true on success, false otherwise.
54 bool ReadUInt64(uint64* result);
56 // Reads a 31-bit unsigned integer into the given output parameter. This is
57 // equivalent to ReadUInt32() above except that the highest-order bit is
58 // discarded.
59 // Forwards the internal iterator (by 4B) on success.
60 // Returns true on success, false otherwise.
61 bool ReadUInt31(uint32* result);
63 // Reads a 24-bit unsigned integer into the given output parameter.
64 // Forwards the internal iterator (by 3B) on success.
65 // Returns true on success, false otherwise.
66 bool ReadUInt24(uint32* result);
68 // Reads a string prefixed with 16-bit length into the given output parameter.
70 // NOTE: Does not copy but rather references strings in the underlying buffer.
71 // This should be kept in mind when handling memory management!
73 // Forwards the internal iterator on success.
74 // Returns true on success, false otherwise.
75 bool ReadStringPiece16(base::StringPiece* result);
77 // Reads a string prefixed with 32-bit length into the given output parameter.
79 // NOTE: Does not copy but rather references strings in the underlying buffer.
80 // This should be kept in mind when handling memory management!
82 // Forwards the internal iterator on success.
83 // Returns true on success, false otherwise.
84 bool ReadStringPiece32(base::StringPiece* result);
86 // Reads a given number of bytes into the given buffer. The buffer
87 // must be of adequate size.
88 // Forwards the internal iterator on success.
89 // Returns true on success, false otherwise.
90 bool ReadBytes(void* result, size_t size);
92 // Seeks a given number of bytes into the buffer from the current offset.
93 // Equivelant to an empty read.
94 // Forwards the internal iterator.
95 // Returns true on success, false otherwise.
96 bool Seek(size_t size);
98 // Rewinds this reader to the beginning of the frame.
99 void Rewind() { ofs_ = 0; }
101 // Returns true if the entirety of the underlying buffer has been read via
102 // Read*() calls.
103 bool IsDoneReading() const;
105 // Returns the number of bytes that have been consumed by the reader so far.
106 size_t GetBytesConsumed() const { return ofs_; }
108 private:
109 // Returns true if the underlying buffer has enough room to read the given
110 // amount of bytes.
111 bool CanRead(size_t bytes) const;
113 // To be called when a read fails for any reason.
114 void OnFailure();
116 // The data buffer that we're reading from.
117 const char* data_;
119 // The length of the data buffer that we're reading from.
120 const size_t len_;
122 // The location of the next read from our data buffer.
123 size_t ofs_;
126 } // namespace net
128 #endif // NET_SPDY_SPDY_FRAME_READER_H_