Revert of Revert of Add a WorkerScheduler and a WebThreadImplForWorker (patchset...
[chromium-blink-merge.git] / net / quic / quic_data_reader.h
blob3384e209801b23074d40966fcdf8b57e3d78d4fd
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_QUIC_QUIC_DATA_READER_H_
6 #define NET_QUIC_QUIC_DATA_READER_H_
8 #include "base/basictypes.h"
9 #include "base/strings/string_piece.h"
10 #include "net/base/int128.h"
11 #include "net/base/net_export.h"
13 namespace net {
15 // Used for reading QUIC data. Though there isn't really anything terribly
16 // QUIC-specific here, it's a helper class that's useful when doing QUIC
17 // framing.
19 // To use, simply construct a QuicDataReader 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.
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 QuicDataReader 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 QuicDataReader {
30 public:
31 // Caller must provide an underlying buffer to work on.
32 QuicDataReader(const char* data, const size_t len);
34 // Empty destructor.
35 ~QuicDataReader() {}
37 // Reads a 16-bit unsigned integer into the given output parameter.
38 // Forwards the internal iterator on success.
39 // Returns true on success, false otherwise.
40 bool ReadUInt16(uint16* result);
42 // Reads a 32-bit unsigned integer into the given output parameter.
43 // Forwards the internal iterator on success.
44 // Returns true on success, false otherwise.
45 bool ReadUInt32(uint32* result);
47 // Reads a 64-bit unsigned integer into the given output parameter.
48 // Forwards the internal iterator on success.
49 // Returns true on success, false otherwise.
50 bool ReadUInt64(uint64* result);
52 // Reads a 16-bit unsigned float into the given output parameter.
53 // Forwards the internal iterator on success.
54 // Returns true on success, false otherwise.
55 bool ReadUFloat16(uint64* result);
57 // Reads a string prefixed with 16-bit length into the given output parameter.
59 // NOTE: Does not copy but rather references strings in the underlying buffer.
60 // This should be kept in mind when handling memory management!
62 // Forwards the internal iterator on success.
63 // Returns true on success, false otherwise.
64 bool ReadStringPiece16(base::StringPiece* result);
66 // Reads a given number of bytes into the given buffer. The buffer
67 // must be of adequate size.
68 // Forwards the internal iterator on success.
69 // Returns true on success, false otherwise.
70 bool ReadStringPiece(base::StringPiece* result, size_t len);
72 // Returns the remaining payload as a StringPiece.
74 // NOTE: Does not copy but rather references strings in the underlying buffer.
75 // This should be kept in mind when handling memory management!
77 // Forwards the internal iterator.
78 base::StringPiece ReadRemainingPayload();
80 // Returns the remaining payload as a StringPiece.
82 // NOTE: Does not copy but rather references strings in the underlying buffer.
83 // This should be kept in mind when handling memory management!
85 // DOES NOT forward the internal iterator.
86 base::StringPiece PeekRemainingPayload();
88 // Reads a given number of bytes into the given buffer. The buffer
89 // must be of adequate size.
90 // Forwards the internal iterator on success.
91 // Returns true on success, false otherwise.
92 bool ReadBytes(void* result, size_t size);
94 // Returns true if the entirety of the underlying buffer has been read via
95 // Read*() calls.
96 bool IsDoneReading() const;
98 // Returns the number of bytes remaining to be read.
99 size_t BytesRemaining() const;
101 private:
102 // Returns true if the underlying buffer has enough room to read the given
103 // amount of bytes.
104 bool CanRead(size_t bytes) const;
106 // To be called when a read fails for any reason.
107 void OnFailure();
109 // The data buffer that we're reading from.
110 const char* data_;
112 // The length of the data buffer that we're reading from.
113 const size_t len_;
115 // The location of the next read from our data buffer.
116 size_t pos_;
118 DISALLOW_COPY_AND_ASSIGN(QuicDataReader);
121 } // namespace net
123 #endif // NET_QUIC_QUIC_DATA_READER_H_