proxygen
AsyncSocketTest2.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <deque>
19 #include <exception>
20 #include <functional>
21 #include <string>
22 
24 
26 
27 namespace folly {
28 namespace test {
29 
36  public:
38  const int /* socket */,
39  const SocketAddress& /* addr */) noexcept override {
42  }
43 
44  void onConnectionAcceptError(const int /* err */) noexcept override {
47  }
48 
50  const int /* socket */,
51  const SocketAddress& /* addr */) noexcept override {
54  }
55 
57  const int /* socket */,
58  const SocketAddress& /* addr */) noexcept override {
61  }
62 
64  const int /* socket */,
65  const SocketAddress& /* addr */) noexcept override {
68  }
69 
70  void onBackoffStarted() noexcept override {
73  }
74 
75  void onBackoffEnded() noexcept override {
77  backoffEnded_++;
78  }
79 
80  void onBackoffError() noexcept override {
82  backoffError_++;
83  }
84 
85  unsigned int getConnectionAccepted() const {
87  return connectionAccepted_;
88  }
89 
90  unsigned int getConnectionAcceptedError() const {
93  }
94 
95  unsigned int getConnectionDropped() const {
97  return connectionDropped_;
98  }
99 
103  }
104 
108  }
109 
110  unsigned int getBackoffStarted() const {
112  return backoffStarted_;
113  }
114 
115  unsigned int getBackoffEnded() const {
117  return backoffEnded_;
118  }
119 
120  unsigned int getBackoffError() const {
122  return backoffError_;
123  }
124 
125  private:
127  unsigned int connectionAccepted_{0};
128  unsigned int connectionAcceptedError_{0};
129  unsigned int connectionDropped_{0};
132  unsigned int backoffStarted_{0};
133  unsigned int backoffEnded_{0};
134  unsigned int backoffError_{0};
135 };
136 
143  public:
144  enum EventType { TYPE_START, TYPE_ACCEPT, TYPE_ERROR, TYPE_STOP };
145  struct EventInfo {
147  : type(TYPE_ACCEPT), fd(fd_), address(addr), errorMsg() {}
148  explicit EventInfo(const std::string& msg)
149  : type(TYPE_ERROR), fd(-1), address(), errorMsg(msg) {}
150  explicit EventInfo(EventType et)
151  : type(et), fd(-1), address(), errorMsg() {}
152 
154  int fd; // valid for TYPE_ACCEPT
155  folly::SocketAddress address; // valid for TYPE_ACCEPT
156  std::string errorMsg; // valid for TYPE_ERROR
157  };
158  typedef std::deque<EventInfo> EventList;
159 
161  : connectionAcceptedFn_(),
162  acceptErrorFn_(),
163  acceptStoppedFn_(),
164  events_() {}
165 
166  std::deque<EventInfo>* getEvents() {
167  return &events_;
168  }
169 
171  const std::function<void(int, const folly::SocketAddress&)>& fn) {
172  connectionAcceptedFn_ = fn;
173  }
174  void setAcceptErrorFn(const std::function<void(const std::exception&)>& fn) {
175  acceptErrorFn_ = fn;
176  }
177  void setAcceptStartedFn(const std::function<void()>& fn) {
178  acceptStartedFn_ = fn;
179  }
180  void setAcceptStoppedFn(const std::function<void()>& fn) {
181  acceptStoppedFn_ = fn;
182  }
183 
185  int fd,
186  const folly::SocketAddress& clientAddr) noexcept override {
187  events_.emplace_back(fd, clientAddr);
188 
189  if (connectionAcceptedFn_) {
190  connectionAcceptedFn_(fd, clientAddr);
191  }
192  }
193  void acceptError(const std::exception& ex) noexcept override {
194  events_.emplace_back(ex.what());
195 
196  if (acceptErrorFn_) {
197  acceptErrorFn_(ex);
198  }
199  }
200  void acceptStarted() noexcept override {
201  events_.emplace_back(TYPE_START);
202 
203  if (acceptStartedFn_) {
204  acceptStartedFn_();
205  }
206  }
207  void acceptStopped() noexcept override {
208  events_.emplace_back(TYPE_STOP);
209 
210  if (acceptStoppedFn_) {
211  acceptStoppedFn_();
212  }
213  }
214 
215  private:
216  std::function<void(int, const folly::SocketAddress&)> connectionAcceptedFn_;
217  std::function<void(const std::exception&)> acceptErrorFn_;
218  std::function<void()> acceptStartedFn_;
219  std::function<void()> acceptStoppedFn_;
220 
221  std::deque<EventInfo> events_;
222 };
223 } // namespace test
224 } // namespace folly
void setConnectionAcceptedFn(const std::function< void(int, const folly::SocketAddress &)> &fn)
void onConnectionDropped(const int, const SocketAddress &) noexceptoverride
PskType type
std::deque< EventInfo > * getEvents()
unsigned int getConnectionAcceptedError() const
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void onConnectionEnqueuedForAcceptorCallback(const int, const SocketAddress &) noexceptoverride
void acceptStarted() noexceptoverride
std::function< void(const std::exception &)> acceptErrorFn_
void setAcceptStartedFn(const std::function< void()> &fn)
void onConnectionAccepted(const int, const SocketAddress &) noexceptoverride
std::function< void()> acceptStoppedFn_
void connectionAccepted(int fd, const folly::SocketAddress &clientAddr) noexceptoverride
std::function< void(int, const folly::SocketAddress &)> connectionAcceptedFn_
std::deque< EventInfo > EventList
const char * string
Definition: Conv.cpp:212
void onConnectionDequeuedByAcceptorCallback(const int, const SocketAddress &) noexceptoverride
std::deque< EventInfo > events_
unsigned int getConnectionEnqueuedForAcceptCallback() const
void acceptError(const std::exception &ex) noexceptoverride
std::function< void()> acceptStartedFn_
EventInfo(int fd_, const folly::SocketAddress &addr)
void setAcceptErrorFn(const std::function< void(const std::exception &)> &fn)
ThreadPoolListHook * addr
void acceptStopped() noexceptoverride
void setAcceptStoppedFn(const std::function< void()> &fn)
unsigned int getConnectionDequeuedByAcceptCallback() const
void onConnectionAcceptError(const int) noexceptoverride