Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / net / spdy / spdy_frame_reader.h
blob3d456b77f29ac807871cb4731e846fd7585c3f2e
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/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 a 16-bit unsigned integer into the given output parameter.
37 // Forwards the internal iterater on success.
38 // Returns true on success, false otherwise.
39 bool ReadUInt16(uint16* result);
41 // Reads a 32-bit unsigned integer into the given output parameter.
42 // Forwards the internal iterater on success.
43 // Returns true on success, false otherwise.
44 bool ReadUInt32(uint32* result);
46 // Reads a string prefixed with 16-bit length into the given output parameter.
48 // NOTE: Does not copy but rather references strings in the underlying buffer.
49 // This should be kept in mind when handling memory management!
51 // Forwards the internal iterater on success.
52 // Returns true on success, false otherwise.
53 bool ReadStringPiece16(base::StringPiece* result);
55 // Reads a string prefixed with 32-bit length into the given output parameter.
57 // NOTE: Does not copy but rather references strings in the underlying buffer.
58 // This should be kept in mind when handling memory management!
60 // Forwards the internal iterater on success.
61 // Returns true on success, false otherwise.
62 bool ReadStringPiece32(base::StringPiece* result);
64 // Reads a given number of bytes into the given buffer. The buffer
65 // must be of adequate size.
66 // Forwards the internal iterater on success.
67 // Returns true on success, false otherwise.
68 bool ReadBytes(void* result, size_t size);
70 // Returns true if the entirety of the underlying buffer has been read via
71 // Read*() calls.
72 bool IsDoneReading() const;
74 // Returns the number of bytes that have been consumed by the reader so far.
75 size_t GetBytesConsumed() const { return ofs_; }
77 private:
78 // Returns true if the underlying buffer has enough room to read the given
79 // amount of bytes.
80 bool CanRead(size_t bytes) const;
82 // To be called when a read fails for any reason.
83 void OnFailure();
85 // The data buffer that we're reading from.
86 const char* data_;
88 // The length of the data buffer that we're reading from.
89 const size_t len_;
91 // The location of the next read from our data buffer.
92 size_t ofs_;
95 } // namespace net
97 #endif // NET_SPDY_SPDY_FRAME_READER_H_