proxygen
folly::test::Server Class Reference

Public Member Functions

 Server ()
 
void stop (bool abortive)
 
void join ()
 
int port () const
 
int closeClients (bool abortive)
 

Private Types

enum  StopMode { NO_STOP, ORDERLY, ABORTIVE }
 

Private Attributes

int acceptSocket_
 
int port_
 
std::atomic< StopModestop_
 
std::thread serverThread_
 
std::vector< int > fds_
 

Detailed Description

Definition at line 36 of file ShutdownSocketSetTest.cpp.

Member Enumeration Documentation

Constructor & Destructor Documentation

folly::test::Server::Server ( )

Definition at line 56 of file ShutdownSocketSetTest.cpp.

References ABORTIVE, folly::netops::accept(), acceptSocket_, folly::ShutdownSocketSet::add(), addr, folly::netops::bind(), folly::ShutdownSocketSet::close(), closeClients(), fds_, folly::netops::getsockname(), folly::netops::listen(), NO_STOP, port_, serverThread_, folly::netops::socket(), and stop_.

56  : acceptSocket_(-1), port_(0), stop_(NO_STOP) {
57  acceptSocket_ = fsp::socket(PF_INET, SOCK_STREAM, 0);
58  CHECK_ERR(acceptSocket_);
60 
61  sockaddr_in addr;
62  addr.sin_family = AF_INET;
63  addr.sin_port = 0;
64  addr.sin_addr.s_addr = INADDR_ANY;
65  CHECK_ERR(bind(
66  acceptSocket_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)));
67 
68  CHECK_ERR(listen(acceptSocket_, 10));
69 
70  socklen_t addrLen = sizeof(addr);
71  CHECK_ERR(
72  getsockname(acceptSocket_, reinterpret_cast<sockaddr*>(&addr), &addrLen));
73 
74  port_ = ntohs(addr.sin_port);
75 
76  serverThread_ = std::thread([this] {
77  while (stop_ == NO_STOP) {
78  sockaddr_in peer;
79  socklen_t peerLen = sizeof(peer);
80  int fd =
81  accept(acceptSocket_, reinterpret_cast<sockaddr*>(&peer), &peerLen);
82  if (fd == -1) {
83  if (errno == EINTR) {
84  continue;
85  }
86  if (errno == EINVAL || errno == ENOTSOCK) { // socket broken
87  break;
88  }
89  }
90  CHECK_ERR(fd);
92  fds_.push_back(fd);
93  }
94 
95  if (stop_ != NO_STOP) {
97  }
98 
100  acceptSocket_ = -1;
101  port_ = 0;
102  });
103 }
std::atomic< StopMode > stop_
int closeClients(bool abortive)
int getsockname(NetworkSocket s, sockaddr *name, socklen_t *namelen)
Definition: NetOps.cpp:108
ShutdownSocketSet shutdownSocketSet
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
int listen(NetworkSocket s, int backlog)
Definition: NetOps.cpp:137
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
ThreadPoolListHook * addr
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71

Member Function Documentation

int folly::test::Server::closeClients ( bool  abortive)

Definition at line 105 of file ShutdownSocketSetTest.cpp.

References folly::ShutdownSocketSet::close(), fds_, and folly::netops::setsockopt().

Referenced by port(), folly::test::runCloseTest(), folly::test::runKillTest(), and Server().

105  {
106  for (int fd : fds_) {
107  if (abortive) {
108  struct linger l = {1, 0};
109  CHECK_ERR(setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)));
110  }
112  }
113  int n = fds_.size();
114  fds_.clear();
115  return n;
116 }
int setsockopt(NetworkSocket s, int level, int optname, const void *optval, socklen_t optlen)
Definition: NetOps.cpp:384
ShutdownSocketSet shutdownSocketSet
void folly::test::Server::join ( )

Definition at line 123 of file ShutdownSocketSetTest.cpp.

References serverThread_.

Referenced by folly::test::runCloseTest(), and folly::test::runKillTest().

123  {
124  serverThread_.join();
125 }
int folly::test::Server::port ( ) const
inline

Definition at line 42 of file ShutdownSocketSetTest.cpp.

References closeClients(), and port_.

Referenced by folly::test::runCloseTest(), and folly::test::runKillTest().

42  {
43  return port_;
44  }
void folly::test::Server::stop ( bool  abortive)

Member Data Documentation

int folly::test::Server::acceptSocket_
private

Definition at line 48 of file ShutdownSocketSetTest.cpp.

Referenced by Server(), and stop().

std::vector<int> folly::test::Server::fds_
private

Definition at line 53 of file ShutdownSocketSetTest.cpp.

Referenced by closeClients(), and Server().

int folly::test::Server::port_
private

Definition at line 49 of file ShutdownSocketSetTest.cpp.

Referenced by port(), and Server().

std::thread folly::test::Server::serverThread_
private

Definition at line 52 of file ShutdownSocketSetTest.cpp.

Referenced by join(), and Server().

std::atomic<StopMode> folly::test::Server::stop_
private

Definition at line 51 of file ShutdownSocketSetTest.cpp.

Referenced by Server(), and stop().


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