proxygen
SocketClient.cpp File Reference

Go to the source code of this file.

Functions

 DEFINE_string (host,"localhost","Host")
 
 DEFINE_int32 (port, 0,"port")
 
 DEFINE_bool (tfo, false,"enable tfo")
 
 DEFINE_string (msg,"","Message to send")
 
 DEFINE_bool (ssl, false,"use ssl")
 
 DEFINE_int32 (timeout_ms, 0,"timeout")
 
 DEFINE_int32 (sendtimeout_ms, 0,"send timeout")
 
 DEFINE_int32 (num_writes, 1,"number of writes")
 
int main (int argc, char **argv)
 

Function Documentation

DEFINE_bool ( tfo  ,
false  ,
"enable tfo"   
)
DEFINE_bool ( ssl  ,
false  ,
"use ssl"   
)
DEFINE_int32 ( port  ,
,
"port"   
)
DEFINE_int32 ( timeout_ms  ,
,
"timeout"   
)
DEFINE_int32 ( sendtimeout_ms  ,
,
"send timeout"   
)
DEFINE_int32 ( num_writes  ,
,
"number of writes"   
)
DEFINE_string ( host  ,
"localhost"  ,
"Host"   
)
DEFINE_string ( msg  ,
""  ,
"Message to send"   
)
int main ( int  argc,
char **  argv 
)

Definition at line 34 of file SocketClient.cpp.

References addr, BlockingSocket::close(), folly::SocketAddress::getAddressStr(), i, folly::INFO, int32_t, folly::gen::move, BlockingSocket::open(), BlockingSocket::read(), BlockingSocket::setAddress(), folly::SocketAddress::setFromHostPort(), folly::netops::socket(), string, folly::detail::timeout, uint8_t, and BlockingSocket::write().

34  {
35  gflags::ParseCommandLineFlags(&argc, &argv, true);
36 
37  if (FLAGS_port == 0) {
38  LOG(ERROR) << "Must specify port";
39  exit(EXIT_FAILURE);
40  }
41 
42  // Prep the socket
43  EventBase evb;
45  if (FLAGS_ssl) {
46  auto sslContext = std::make_shared<SSLContext>();
47  socket = AsyncSocket::UniquePtr(new AsyncSSLSocket(sslContext, &evb));
48  } else {
49  socket = AsyncSocket::UniquePtr(new AsyncSocket(&evb));
50  }
51  socket->detachEventBase();
52 
53  if (FLAGS_tfo) {
54 #if FOLLY_ALLOW_TFO
55  socket->enableTFO();
56 #endif
57  }
58 
59  if (FLAGS_sendtimeout_ms != 0) {
60  socket->setSendTimeout(FLAGS_sendtimeout_ms);
61  }
62 
63  // Keep this around
64  auto sockAddr = socket.get();
65 
66  BlockingSocket sock(std::move(socket));
68  addr.setFromHostPort(FLAGS_host, FLAGS_port);
69  sock.setAddress(addr);
70  std::chrono::milliseconds timeout(FLAGS_timeout_ms);
71  sock.open(timeout);
72  LOG(INFO) << "connected to " << addr.getAddressStr();
73 
74  for (int32_t i = 0; i < FLAGS_num_writes; ++i) {
75  sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size());
76  }
77 
78  LOG(INFO) << "TFO attempted: " << sockAddr->getTFOAttempted();
79  LOG(INFO) << "TFO finished: " << sockAddr->getTFOFinished();
80  LOG(INFO) << "TFO success: " << sockAddr->getTFOSucceded();
81 
82  std::array<char, 1024> buf;
83  int32_t bytesRead = 0;
84  while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) {
85  std::cout << std::string(buf.data(), bytesRead);
86  }
87 
88  sock.close();
89  return 0;
90 }
void setFromHostPort(const char *host, uint16_t port)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
char ** argv
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
const char * string
Definition: Conv.cpp:212
ThreadPoolListHook * addr
std::unique_ptr< AsyncSocket, Destructor > UniquePtr
Definition: AsyncSocket.h:83
std::string getAddressStr() const