File: spdy_buffer.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SPDY_SPDY_BUFFER_H_
#define NET_SPDY_SPDY_BUFFER_H_

#include <cstddef>
#include <memory>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "net/base/net_export.h"

namespace spdy {
class SpdySerializedFrame;
}  // namespace spdy

namespace net {

class IOBuffer;

// SpdyBuffer is a class to hold data read from or to be written to a
// SPDY connection. It is similar to a DrainableIOBuffer but is not
// ref-counted and will include a way to get notified when Consume()
// is called.
//
// NOTE(akalin): This explicitly does not inherit from IOBuffer to
// avoid the needless ref-counting and to avoid working around the
// fact that IOBuffer member functions are not virtual.
class NET_EXPORT_PRIVATE SpdyBuffer {
 public:
  // The source of a call to a ConsumeCallback.
  enum ConsumeSource {
    // Called via a call to Consume().
    CONSUME,
    // Called via the SpdyBuffer being destroyed.
    DISCARD
  };

  // A Callback that gets called when bytes are consumed with the
  // (non-zero) number of bytes consumed and the source of the
  // consume. May be called any number of times with CONSUME as the
  // source followed by at most one call with DISCARD as the
  // source. The sum of the number of bytes consumed equals the total
  // size of the buffer.
  typedef base::RepeatingCallback<void(size_t, ConsumeSource)> ConsumeCallback;

  // Construct with the data in the given frame. Assumes that data is
  // owned by |frame| or outlives it.
  explicit SpdyBuffer(std::unique_ptr<spdy::SpdySerializedFrame> frame);

  // Construct with a copy of the given raw data. |data| must be
  // non-NULL and |size| must be non-zero.
  SpdyBuffer(const char* data, size_t size);

  SpdyBuffer(const SpdyBuffer&) = delete;
  SpdyBuffer& operator=(const SpdyBuffer&) = delete;

  // If there are bytes remaining in the buffer, triggers a call to
  // any consume callbacks with a DISCARD source.
  ~SpdyBuffer();

  // Returns the remaining (unconsumed) data.
  const char* GetRemainingData() const;

  // Returns the number of remaining (unconsumed) bytes.
  size_t GetRemainingSize() const;

  // Add a callback to be called when bytes are consumed. The
  // ConsumeCallback should not do anything complicated; ideally it
  // should only update a counter. In particular, it must *not* cause
  // the SpdyBuffer itself to be destroyed.
  void AddConsumeCallback(const ConsumeCallback& consume_callback);

  // Consume the given number of bytes, which must be positive but not
  // greater than GetRemainingSize().
  void Consume(size_t consume_size);

  // Returns an IOBuffer pointing to the data starting at
  // GetRemainingData(). Use with care; the returned IOBuffer is not
  // updated when Consume() is called. However, it may still be used
  // past the lifetime of this object.
  //
  // This is used with Socket::Write(), which takes an IOBuffer* that
  // may be written to even after the socket itself is destroyed. (See
  // http://crbug.com/249725 .)
  scoped_refptr<IOBuffer> GetIOBufferForRemainingData();

 private:
  void ConsumeHelper(size_t consume_size, ConsumeSource consume_source);

  // Ref-count the passed-in spdy::SpdySerializedFrame to support the semantics
  // of |GetIOBufferForRemainingData()|.
  typedef base::RefCountedData<std::unique_ptr<spdy::SpdySerializedFrame>>
      SharedFrame;

  class SharedFrameIOBuffer;

  const scoped_refptr<SharedFrame> shared_frame_;
  std::vector<ConsumeCallback> consume_callbacks_;
  size_t offset_ = 0;
};

}  // namespace net

#endif  // NET_SPDY_SPDY_BUFFER_H_