proxygen
TestAsyncTransport.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-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. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <deque>
13 #include <folly/SocketAddress.h>
14 #include <folly/io/IOBufQueue.h>
18 
20  private folly::AsyncTimeout {
21  public:
22  class WriteEvent {
23  public:
24  static std::shared_ptr<WriteEvent> newEvent(const struct iovec* vec,
25  size_t count);
26 
28  return time_;
29  }
30  const struct iovec* getIoVec() const {
31  return vec_;
32  }
33  size_t getCount() const {
34  return count_;
35  }
36 
37  private:
38  static void destroyEvent(WriteEvent* event);
39 
40  WriteEvent(proxygen::TimePoint time, size_t count);
41  ~WriteEvent();
42 
44  size_t count_;
45  struct iovec vec_[];
46  };
47 
48  explicit TestAsyncTransport(folly::EventBase* eventBase);
49 
50  // AsyncTransport methods
51  void setReadCB(AsyncTransportWrapper::ReadCallback* callback) override;
52  ReadCallback* getReadCallback() const override;
53  void write(AsyncTransportWrapper::WriteCallback* callback,
54  const void* buf, size_t bytes,
56  folly::WriteFlags::NONE) override;
57  void writev(AsyncTransportWrapper::WriteCallback* callback,
58  const struct iovec* vec, size_t count,
60  folly::WriteFlags::NONE) override;
61  void writeChain(AsyncTransportWrapper::WriteCallback* callback,
62  std::unique_ptr<folly::IOBuf>&& iob,
64  folly::WriteFlags::NONE) override;
65  void close() override;
66  void closeNow() override;
67  void shutdownWrite() override;
68  void shutdownWriteNow() override;
70  const override;
72  const override;
73  bool good() const override;
74  bool readable() const override;
75  bool connecting() const override;
76  bool error() const override;
77  void attachEventBase(folly::EventBase* eventBase) override;
78  void detachEventBase() override;
79  bool isDetachable() const override;
80  folly::EventBase* getEventBase() const override;
81  void setSendTimeout(uint32_t milliseconds) override;
82  uint32_t getSendTimeout() const override;
83 
84  // Methods to control read events
85  void addReadEvent(const void* buf, size_t buflen,
86  std::chrono::milliseconds delayFromPrevious);
87  void addReadEvent(folly::IOBufQueue& chain,
88  std::chrono::milliseconds delayFromPrevious);
89  void addReadEvent(const char* buf,
90  std::chrono::milliseconds delayFromPrevious=
91  std::chrono::milliseconds(0));
92  void addReadEOF(std::chrono::milliseconds delayFromPrevious);
94  std::chrono::milliseconds delayFromPrevious);
95  void startReadEvents();
96 
97  void pauseWrites();
98  void resumeWrites();
99 
100  // Methods to get the data written to this transport
101  std::deque< std::shared_ptr<WriteEvent> >* getWriteEvents() {
102  return &writeEvents_;
103  }
104 
106  return eorCount_;
107  }
108 
110  return corkCount_;
111  }
112 
113  size_t getAppBytesWritten() const override { return 0; }
114  size_t getRawBytesWritten() const override { return 0; }
115  size_t getAppBytesReceived() const override { return 0; }
116  size_t getRawBytesReceived() const override { return 0; }
117  bool isEorTrackingEnabled() const override { return false; }
118  void setEorTracking(bool) override { return; }
119 
120  private:
121  enum StateEnum {
126  };
127 
128  class ReadEvent;
129 
130  bool writesAllowed() const { return writeState_ == kStateOpen ||
132 
133  // Forbidden copy constructor and assignment opererator
136 
137  void addReadEvent(const std::shared_ptr<ReadEvent>& event);
139  void fireNextReadEvent();
140  void fireOneReadEvent();
141  void failPendingWrites();
142 
143  // AsyncTimeout methods
144  void timeoutExpired() noexcept override;
145 
149 
154  std::deque< std::shared_ptr<ReadEvent> > readEvents_;
155  std::deque< std::shared_ptr<WriteEvent> > writeEvents_;
156  std::deque< std::pair<std::shared_ptr<WriteEvent>, AsyncTransportWrapper::WriteCallback*>>
158 
161 };
void attachEventBase(folly::EventBase *eventBase) override
std::deque< std::shared_ptr< WriteEvent > > writeEvents_
void shutdownWrite() override
flags
Definition: http_parser.h:127
void detachEventBase() override
size_t getAppBytesWritten() const override
folly::EventBase * getEventBase() const override
folly::EventBase * eventBase_
void addReadError(const folly::AsyncSocketException &ex, std::chrono::milliseconds delayFromPrevious)
proxygen::TimePoint getTime() const
uint32_t getSendTimeout() const override
WriteEvent(proxygen::TimePoint time, size_t count)
void setSendTimeout(uint32_t milliseconds) override
std::chrono::steady_clock::time_point now()
ReadCallback * getReadCallback() const override
void writeChain(AsyncTransportWrapper::WriteCallback *callback, std::unique_ptr< folly::IOBuf > &&iob, folly::WriteFlags flags=folly::WriteFlags::NONE) override
std::deque< std::shared_ptr< WriteEvent > > * getWriteEvents()
requires E e noexcept(noexcept(s.error(std::move(e))))
void setEorTracking(bool) override
bool error() const override
void addReadEvent(const void *buf, size_t buflen, std::chrono::milliseconds delayFromPrevious)
static std::shared_ptr< WriteEvent > newEvent(const struct iovec *vec, size_t count)
void shutdownWriteNow() override
bool readable() const override
void timeoutExpired() noexceptoverride
const struct iovec * getIoVec() const
SocketAddress getPeerAddress() const
TestAsyncTransport & operator=(TestAsyncTransport const &)
bool connecting() const override
bool writesAllowed() const
size_t getRawBytesReceived() const override
std::deque< std::pair< std::shared_ptr< WriteEvent >, AsyncTransportWrapper::WriteCallback * > > pendingWriteEvents_
Definition: Traits.h:588
proxygen::TimePoint nextReadEventTime_
bool isDetachable() const override
size_t getRawBytesWritten() const override
size_t getAppBytesReceived() const override
SocketAddress getLocalAddress() const
int * count
SteadyClock::time_point TimePoint
Definition: Time.h:25
void setReadCB(AsyncTransportWrapper::ReadCallback *callback) override
bool good() const override
proxygen::TimePoint prevReadEventTime_
std::deque< std::shared_ptr< ReadEvent > > readEvents_
void addReadEOF(std::chrono::milliseconds delayFromPrevious)
void writev(AsyncTransportWrapper::WriteCallback *callback, const struct iovec *vec, size_t count, folly::WriteFlags flags=folly::WriteFlags::NONE) override
folly::AsyncTransportWrapper::ReadCallback * readCallback_
bool isEorTrackingEnabled() const override
static void destroyEvent(WriteEvent *event)
void closeNow() override
void write(AsyncTransportWrapper::WriteCallback *callback, const void *buf, size_t bytes, folly::WriteFlags flags=folly::WriteFlags::NONE) override
ThreadPoolListHook * addr
TestAsyncTransport(folly::EventBase *eventBase)
std::chrono::nanoseconds time()
void scheduleNextReadEvent(proxygen::TimePoint now)