proxygen
LocalTransport.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
12 
13 namespace fizz {
14 namespace test {
15 
17  public:
18  using UniquePtr = std::unique_ptr<LocalTransport, Destructor>;
19 
27  void setTrickle(bool trickle, folly::Function<void()> callback = nullptr) {
28  trickle_ = trickle;
29  trickleCallback_ = std::move(callback);
30  }
31 
32  void setPeer(LocalTransport* peer) {
33  peer_ = peer;
34  }
35 
36  void receiveData(std::unique_ptr<folly::IOBuf> buf) {
39  deliverData();
40  }
41 
42  ReadCallback* getReadCallback() const override {
43  return callback_;
44  }
45 
46  void setReadCB(ReadCallback* callback) override {
47  callback_ = callback;
48  if (callback_) {
49  CHECK(callback->isBufferMovable());
50  deliverData();
51  }
52  }
53 
54  void write(
55  WriteCallback* /*callback*/,
56  const void* /*buf*/,
57  size_t /*bytes*/,
58  folly::WriteFlags /*flags*/ = folly::WriteFlags::NONE) override {
59  LOG(FATAL) << "only writeChain() supported";
60  }
61 
62  void writeChain(
63  WriteCallback* callback,
64  std::unique_ptr<folly::IOBuf>&& buf,
65  folly::WriteFlags /*flags*/ = folly::WriteFlags::NONE) override {
66  written_ += buf->computeChainDataLength();
68  if (callback) {
69  callback->writeSuccess();
70  }
71  }
72 
73  void writev(
74  WriteCallback* /*callback*/,
75  const iovec* /*vec*/,
76  size_t /*bytes*/,
77  folly::WriteFlags /*flags*/ = folly::WriteFlags::NONE) override {
78  LOG(FATAL) << "only writeChain() supported";
79  }
80 
81  folly::EventBase* getEventBase() const override {
82  return evb_;
83  }
84 
85  void attachEventBase(folly::EventBase* eventBase) override {
86  evb_ = eventBase;
87  }
88 
89  void close() override {}
90 
91  void closeNow() override {}
92 
93  void closeWithReset() override {}
94 
95  bool connecting() const override {
96  return false;
97  }
98 
99  void detachEventBase() override {
100  evb_ = nullptr;
101  }
102 
103  bool error() const override {
104  return false;
105  }
106 
107  size_t getAppBytesReceived() const override {
108  return received_;
109  }
110 
111  size_t getAppBytesWritten() const override {
112  return written_;
113  }
114 
115  void getLocalAddress(folly::SocketAddress*) const override {}
116 
117  void getPeerAddress(folly::SocketAddress*) const override {}
118 
119  size_t getRawBytesReceived() const override {
120  return received_;
121  }
122 
123  size_t getRawBytesWritten() const override {
124  return written_;
125  }
126 
127  uint32_t getSendTimeout() const override {
128  return 0;
129  }
130 
131  bool good() const override {
132  return true;
133  }
134 
135  bool isDetachable() const override {
136  return true;
137  }
138 
139  bool isEorTrackingEnabled() const override {
140  return false;
141  }
142 
143  bool readable() const override {
144  return true;
145  }
146 
147  void setEorTracking(bool) override {}
148 
149  void setSendTimeout(uint32_t) override {}
150 
151  void shutdownWrite() override {}
152 
153  void shutdownWriteNow() override {}
154 
155  private:
156  void deliverData() {
157  while (callback_ && !readBuf_.empty()) {
158  if (!trickle_) {
160  } else {
161  if (trickleCallback_) {
163  }
165  }
166  }
167  }
168 
171  size_t received_{0};
172  size_t written_{0};
175 
176  bool trickle_{false};
178 };
179 } // namespace test
180 } // namespace fizz
virtual void readBufferAvailable(std::unique_ptr< IOBuf >) noexcept
std::unique_ptr< folly::IOBuf > split(size_t n)
Definition: IOBufQueue.h:420
void shutdownWriteNow() override
void getLocalAddress(folly::SocketAddress *) const override
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
folly::IOBufQueue readBuf_
void write(WriteCallback *, const void *, size_t, folly::WriteFlags=folly::WriteFlags::NONE) override
size_t getRawBytesWritten() const override
bool connecting() const override
void setSendTimeout(uint32_t) override
void writeChain(WriteCallback *callback, std::unique_ptr< folly::IOBuf > &&buf, folly::WriteFlags=folly::WriteFlags::NONE) override
ReadCallback * getReadCallback() const override
bool empty() const
Definition: IOBufQueue.h:503
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
size_t getAppBytesWritten() const override
virtual bool isBufferMovable() noexcept
void getPeerAddress(folly::SocketAddress *) const override
std::unique_ptr< folly::IOBuf > move()
Definition: IOBufQueue.h:459
void closeWithReset() override
uint32_t getSendTimeout() const override
folly::EventBase * getEventBase() const override
void receiveData(std::unique_ptr< folly::IOBuf > buf)
folly::EventBase * evb_
void attachEventBase(folly::EventBase *eventBase) override
static Options cacheChainLength()
Definition: IOBufQueue.h:83
bool good() const override
bool isDetachable() const override
Definition: Actions.h:16
void detachEventBase() override
folly::Function< void()> trickleCallback_
bool isEorTrackingEnabled() const override
void shutdownWrite() override
void setPeer(LocalTransport *peer)
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
bool error() const override
std::unique_ptr< LocalTransport, Destructor > UniquePtr
void setEorTracking(bool) override
void setReadCB(ReadCallback *callback) override
virtual void writeSuccess() noexcept=0
void writev(WriteCallback *, const iovec *, size_t, folly::WriteFlags=folly::WriteFlags::NONE) override
void setTrickle(bool trickle, folly::Function< void()> callback=nullptr)
size_t getRawBytesReceived() const override
bool readable() const override
size_t getAppBytesReceived() const override