proxygen
UDPClient Class Reference
Inheritance diagram for UDPClient:
folly::AsyncTimeout folly::AsyncTimeout ConnectedWriteUDPClient

Public Member Functions

 UDPClient (EventBase *evb, TestData &testData)
 
void start (const folly::SocketAddress &server)
 
void connect ()
 
void shutdown ()
 
void sendPing ()
 
virtual void writePing (std::unique_ptr< folly::IOBuf > buf, int gso)
 
void getReadBuffer (void **buf, size_t *len) noexceptoverride
 
void onDataAvailable (const folly::SocketAddress &, size_t len, bool) noexceptoverride
 
void onReadError (const folly::AsyncSocketException &ex) noexceptoverride
 
void onReadClosed () noexceptoverride
 
void timeoutExpired () noexceptoverride
 
AsyncUDPSocketgetSocket ()
 
void setShouldConnect (const folly::SocketAddress &connectAddr)
 
 UDPClient (EventBase *evb)
 
void start (const folly::SocketAddress &server, int n)
 
void connect ()
 
void shutdown ()
 
void sendPing ()
 
virtual void writePing (std::unique_ptr< folly::IOBuf > buf)
 
void getReadBuffer (void **buf, size_t *len) noexceptoverride
 
void onDataAvailable (const folly::SocketAddress &client, size_t len, bool truncated) noexceptoverride
 
void onReadError (const folly::AsyncSocketException &ex) noexceptoverride
 
void onReadClosed () noexceptoverride
 
void timeoutExpired () noexceptoverride
 
int pongRecvd () const
 
AsyncUDPSocketgetSocket ()
 
void setShouldConnect (const folly::SocketAddress &connectAddr)
 

Protected Attributes

folly::Optional< folly::SocketAddressconnectAddr_
 
EventBase *const evb_ {nullptr}
 
folly::SocketAddress server_
 
std::unique_ptr< AsyncUDPSocketsocket_
 

Private Attributes

char buf_ [2048]
 
TestDatatestData_
 
int pongRecvd_ {0}
 
int n_ {0}
 

Additional Inherited Members

- Private Types inherited from folly::AsyncTimeout
typedef TimeoutManager::InternalEnum InternalEnum
 
- Private Member Functions inherited from folly::AsyncTimeout
 AsyncTimeout (TimeoutManager *timeoutManager)
 
 AsyncTimeout (EventBase *eventBase)
 
 AsyncTimeout (TimeoutManager *timeoutManager, InternalEnum internal)
 
 AsyncTimeout (EventBase *eventBase, InternalEnum internal)
 
 AsyncTimeout ()
 
virtual ~AsyncTimeout ()
 
bool scheduleTimeout (uint32_t milliseconds)
 
bool scheduleTimeout (TimeoutManager::timeout_type timeout)
 
void cancelTimeout ()
 
bool isScheduled () const
 
void attachTimeoutManager (TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
 
void attachEventBase (EventBase *eventBase, InternalEnum internal=InternalEnum::NORMAL)
 
void detachTimeoutManager ()
 
void detachEventBase ()
 
const TimeoutManagergetTimeoutManager ()
 
struct event * getEvent ()
 
- Static Private Member Functions inherited from folly::AsyncTimeout
template<typename TCallback >
static std::unique_ptr< AsyncTimeoutmake (TimeoutManager &manager, TCallback &&callback)
 
template<typename TCallback >
static std::unique_ptr< AsyncTimeoutschedule (TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback)
 

Detailed Description

Definition at line 188 of file AsyncUDPSocketGSOTest.cpp.

Constructor & Destructor Documentation

UDPClient::UDPClient ( EventBase evb,
TestData testData 
)
inlineexplicit

Definition at line 190 of file AsyncUDPSocketGSOTest.cpp.

191  : AsyncTimeout(evb), evb_(evb), testData_(testData) {}
EventBase *const evb_
UDPClient::UDPClient ( EventBase evb)
inlineexplicit

Definition at line 170 of file AsyncUDPSocketTest.cpp.

170 : AsyncTimeout(evb), evb_(evb) {}
EventBase *const evb_

Member Function Documentation

void UDPClient::connect ( )
inline

Definition at line 195 of file AsyncUDPSocketTest.cpp.

References folly::AsyncSocketException::NOT_OPEN, and socket_.

195  {
196  int ret = socket_->connect(*connectAddr_);
197  if (ret != 0) {
199  folly::AsyncSocketException::NOT_OPEN, "ConnectFail", errno);
200  }
201  VLOG(2) << "Client connected to address=" << *connectAddr_;
202  }
std::unique_ptr< AsyncUDPSocket > socket_
folly::Optional< folly::SocketAddress > connectAddr_
void UDPClient::connect ( )
inline

Definition at line 228 of file AsyncUDPSocketGSOTest.cpp.

References folly::AsyncSocketException::NOT_OPEN, and socket_.

228  {
229  int ret = socket_->connect(*connectAddr_);
230  if (ret != 0) {
232  folly::AsyncSocketException::NOT_OPEN, "ConnectFail", errno);
233  }
234  VLOG(2) << "Client connected to address=" << *connectAddr_;
235  }
std::unique_ptr< AsyncUDPSocket > socket_
folly::Optional< folly::SocketAddress > connectAddr_
void UDPClient::getReadBuffer ( void **  buf,
size_t *  len 
)
inlineoverridenoexcept

Definition at line 227 of file AsyncUDPSocketTest.cpp.

227  {
228  *buf = buf_;
229  *len = 1024;
230  }
void UDPClient::getReadBuffer ( void **  buf,
size_t *  len 
)
inlineoverridenoexcept

Definition at line 255 of file AsyncUDPSocketGSOTest.cpp.

255  {
256  *buf = buf_;
257  *len = sizeof(buf_);
258  }
AsyncUDPSocket& UDPClient::getSocket ( )
inline

Definition at line 265 of file AsyncUDPSocketTest.cpp.

References socket_.

265  {
266  return *socket_;
267  }
std::unique_ptr< AsyncUDPSocket > socket_
AsyncUDPSocket& UDPClient::getSocket ( )
inline

Definition at line 286 of file AsyncUDPSocketGSOTest.cpp.

References socket_.

286  {
287  return *socket_;
288  }
std::unique_ptr< AsyncUDPSocket > socket_
void UDPClient::onDataAvailable ( const folly::SocketAddress client,
size_t  len,
bool  truncated 
)
inlineoverridenoexcept

Definition at line 232 of file AsyncUDPSocketTest.cpp.

References string.

235  {
236  VLOG(4) << "Read " << len << " bytes (trun:" << truncated << ") from "
237  << client.describe() << " - " << std::string(buf_, len);
238  VLOG(4) << n_ << " left";
239 
240  ++pongRecvd_;
241 
242  sendPing();
243  }
std::string describe() const
const char * string
Definition: Conv.cpp:212
void UDPClient::onDataAvailable ( const folly::SocketAddress ,
size_t  len,
bool   
)
inlineoverridenoexcept

Definition at line 260 of file AsyncUDPSocketGSOTest.cpp.

References folly::shutdown().

263  {
264  VLOG(0) << "Got " << len << " bytes";
265  if (testData_.appendOut(len)) {
266  shutdown();
267  }
268  }
void UDPClient::onReadClosed ( )
inlineoverridenoexcept

Definition at line 252 of file AsyncUDPSocketTest.cpp.

252  {
253  CHECK(false) << "We unregister reads before closing";
254  }
void UDPClient::onReadClosed ( )
inlineoverridenoexcept

Definition at line 277 of file AsyncUDPSocketGSOTest.cpp.

277  {
278  CHECK(false) << "We unregister reads before closing";
279  }
void UDPClient::onReadError ( const folly::AsyncSocketException ex)
inlineoverridenoexcept

Definition at line 245 of file AsyncUDPSocketTest.cpp.

References socket_.

245  {
246  VLOG(4) << ex.what();
247 
248  // Start listening for next PONG
249  socket_->resumeRead(this);
250  }
std::unique_ptr< AsyncUDPSocket > socket_
void UDPClient::onReadError ( const folly::AsyncSocketException ex)
inlineoverridenoexcept

Definition at line 270 of file AsyncUDPSocketGSOTest.cpp.

References socket_.

270  {
271  VLOG(4) << ex.what();
272 
273  // Start listening for next PONG
274  socket_->resumeRead(this);
275  }
std::unique_ptr< AsyncUDPSocket > socket_
int UDPClient::pongRecvd ( ) const
inline

Definition at line 261 of file AsyncUDPSocketTest.cpp.

261  {
262  return pongRecvd_;
263  }
void UDPClient::sendPing ( )
inline

Definition at line 212 of file AsyncUDPSocketTest.cpp.

References folly::IOBuf::copyBuffer(), folly::shutdown(), and proxygen::http2::writePing().

212  {
213  if (n_ == 0) {
214  shutdown();
215  return;
216  }
217 
218  --n_;
219  scheduleTimeout(5);
220  writePing(folly::IOBuf::copyBuffer(folly::to<std::string>("PING ", n_)));
221  }
virtual void writePing(std::unique_ptr< folly::IOBuf > buf, int gso)
bool scheduleTimeout(uint32_t milliseconds)
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
void UDPClient::sendPing ( )
inline

Definition at line 245 of file AsyncUDPSocketGSOTest.cpp.

References proxygen::http2::writePing().

245  {
246  scheduleTimeout(5);
247  writePing(
248  testData_.getInBuf(), testData_.useSocketGSO_ ? -1 : testData_.gso_);
249  }
virtual void writePing(std::unique_ptr< folly::IOBuf > buf, int gso)
bool scheduleTimeout(uint32_t milliseconds)
void UDPClient::setShouldConnect ( const folly::SocketAddress connectAddr)
inline

Definition at line 269 of file AsyncUDPSocketTest.cpp.

References evb_, and socket_.

269  {
270  connectAddr_ = connectAddr;
271  }
folly::Optional< folly::SocketAddress > connectAddr_
void UDPClient::setShouldConnect ( const folly::SocketAddress connectAddr)
inline

Definition at line 290 of file AsyncUDPSocketGSOTest.cpp.

290  {
291  connectAddr_ = connectAddr;
292  }
folly::Optional< folly::SocketAddress > connectAddr_
void UDPClient::shutdown ( )
inline

Definition at line 204 of file AsyncUDPSocketTest.cpp.

References evb_, folly::EventBase::isInEventBaseThread(), socket_, and folly::EventBase::terminateLoopSoon().

204  {
205  CHECK(evb_->isInEventBaseThread());
206  socket_->pauseRead();
207  socket_->close();
208  socket_.reset();
210  }
std::unique_ptr< AsyncUDPSocket > socket_
bool isInEventBaseThread() const
Definition: EventBase.h:504
void terminateLoopSoon()
Definition: EventBase.cpp:493
EventBase *const evb_
void UDPClient::shutdown ( )
inline

Definition at line 237 of file AsyncUDPSocketGSOTest.cpp.

References evb_, folly::EventBase::isInEventBaseThread(), socket_, and folly::EventBase::terminateLoopSoon().

237  {
238  CHECK(evb_->isInEventBaseThread());
239  socket_->pauseRead();
240  socket_->close();
241  socket_.reset();
243  }
std::unique_ptr< AsyncUDPSocket > socket_
bool isInEventBaseThread() const
Definition: EventBase.h:504
void terminateLoopSoon()
Definition: EventBase.cpp:493
EventBase *const evb_
void UDPClient::start ( const folly::SocketAddress server,
int  n 
)
inline

Definition at line 172 of file AsyncUDPSocketTest.cpp.

References folly::netops::connect(), evb_, folly::EventBase::isInEventBaseThread(), and socket_.

172  {
173  CHECK(evb_->isInEventBaseThread());
174  server_ = server;
175  socket_ = std::make_unique<AsyncUDPSocket>(evb_);
176 
177  try {
178  socket_->bind(folly::SocketAddress("127.0.0.1", 0));
179  if (connectAddr_) {
180  connect();
181  }
182  VLOG(2) << "Client bound to " << socket_->address().describe();
183  } catch (const std::exception& ex) {
184  LOG(FATAL) << ex.what();
185  }
186 
187  socket_->resumeRead(this);
188 
189  n_ = n;
190 
191  // Start playing ping pong
192  sendPing();
193  }
folly::SocketAddress server_
std::unique_ptr< AsyncUDPSocket > socket_
folly::Optional< folly::SocketAddress > connectAddr_
bool isInEventBaseThread() const
Definition: EventBase.h:504
EventBase *const evb_
void UDPClient::start ( const folly::SocketAddress server)
inline

Definition at line 193 of file AsyncUDPSocketGSOTest.cpp.

References folly::netops::connect(), evb_, folly::EventBase::isInEventBaseThread(), folly::shutdown(), and socket_.

193  {
194  CHECK(evb_->isInEventBaseThread());
195  server_ = server;
196  socket_ = std::make_unique<AsyncUDPSocket>(evb_);
197 
198  try {
199  socket_->bind(folly::SocketAddress("127.0.0.1", 0));
200  if (connectAddr_) {
201  connect();
202  }
203  VLOG(2) << "Client bound to " << socket_->address().describe();
204  } catch (const std::exception& ex) {
205  LOG(FATAL) << ex.what();
206  }
207 
208  // succeed if GSO not available
209  if (socket_->getGSO() < 0) {
210  LOG(INFO) << "GSO not supported";
211  testData_.out_ = testData_.expected_;
212  shutdown();
213  return;
214  }
215 
216  if (testData_.useSocketGSO_) {
217  socket_->setGSO(testData_.gso_);
218  } else {
219  socket_->setGSO(0);
220  }
221 
222  socket_->resumeRead(this);
223 
224  // Start playing ping pong
225  sendPing();
226  }
folly::SocketAddress server_
std::unique_ptr< AsyncUDPSocket > socket_
folly::Optional< folly::SocketAddress > connectAddr_
bool isInEventBaseThread() const
Definition: EventBase.h:504
EventBase *const evb_
void UDPClient::timeoutExpired ( )
inlineoverridevirtualnoexcept

timeoutExpired() is invoked when the timeout period has expired.

Implements folly::AsyncTimeout.

Definition at line 256 of file AsyncUDPSocketTest.cpp.

256  {
257  VLOG(4) << "Timeout expired";
258  sendPing();
259  }
void UDPClient::timeoutExpired ( )
inlineoverridevirtualnoexcept

timeoutExpired() is invoked when the timeout period has expired.

Implements folly::AsyncTimeout.

Definition at line 281 of file AsyncUDPSocketGSOTest.cpp.

References folly::shutdown().

281  {
282  VLOG(4) << "Timeout expired";
283  shutdown();
284  }
virtual void UDPClient::writePing ( std::unique_ptr< folly::IOBuf buf)
inlinevirtual

Reimplemented in ConnectedWriteUDPClient.

Definition at line 223 of file AsyncUDPSocketTest.cpp.

References folly::gen::move, and socket_.

223  {
224  socket_->write(server_, std::move(buf));
225  }
folly::SocketAddress server_
std::unique_ptr< AsyncUDPSocket > socket_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual void UDPClient::writePing ( std::unique_ptr< folly::IOBuf buf,
int  gso 
)
inlinevirtual

Definition at line 251 of file AsyncUDPSocketGSOTest.cpp.

References folly::gen::move, and socket_.

251  {
252  socket_->writeGSO(server_, std::move(buf), gso);
253  }
folly::SocketAddress server_
std::unique_ptr< AsyncUDPSocket > socket_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Data Documentation

char UDPClient::buf_
private

Definition at line 302 of file AsyncUDPSocketGSOTest.cpp.

folly::Optional< folly::SocketAddress > UDPClient::connectAddr_
protected

Definition at line 295 of file AsyncUDPSocketGSOTest.cpp.

EventBase *const UDPClient::evb_ {nullptr}
protected

Definition at line 296 of file AsyncUDPSocketGSOTest.cpp.

int UDPClient::n_ {0}
private

Definition at line 283 of file AsyncUDPSocketTest.cpp.

int UDPClient::pongRecvd_ {0}
private

Definition at line 281 of file AsyncUDPSocketTest.cpp.

folly::SocketAddress UDPClient::server_
protected

Definition at line 298 of file AsyncUDPSocketGSOTest.cpp.

std::unique_ptr< AsyncUDPSocket > UDPClient::socket_
protected

Definition at line 299 of file AsyncUDPSocketGSOTest.cpp.

TestData& UDPClient::testData_
private

Definition at line 303 of file AsyncUDPSocketGSOTest.cpp.


The documentation for this class was generated from the following files: