int main(int argc, char** argv) { gflags::ParseCommandLineFlags(&argc, &argv, true); if (FLAGS_port == 0) { LOG(ERROR) << "Must specify port"; exit(EXIT_FAILURE); } // Prep the socket EventBase evb; AsyncSocket::UniquePtr socket; if (FLAGS_ssl) { auto sslContext = std::make_shared<SSLContext>(); socket = AsyncSocket::UniquePtr(new AsyncSSLSocket(sslContext, &evb)); } else { socket = AsyncSocket::UniquePtr(new AsyncSocket(&evb)); } socket->detachEventBase(); if (FLAGS_tfo) { #if FOLLY_ALLOW_TFO socket->enableTFO(); #endif } if (FLAGS_sendtimeout_ms != 0) { socket->setSendTimeout(FLAGS_sendtimeout_ms); } // Keep this around auto sockAddr = socket.get(); BlockingSocket sock(std::move(socket)); SocketAddress addr; addr.setFromHostPort(FLAGS_host, FLAGS_port); sock.setAddress(addr); std::chrono::milliseconds timeout(FLAGS_timeout_ms); sock.open(timeout); LOG(INFO) << "connected to " << addr.getAddressStr(); for (int32_t i = 0; i < FLAGS_num_writes; ++i) { sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size()); } LOG(INFO) << "TFO attempted: " << sockAddr->getTFOAttempted(); LOG(INFO) << "TFO finished: " << sockAddr->getTFOFinished(); LOG(INFO) << "TFO success: " << sockAddr->getTFOSucceded(); std::array<char, 1024> buf; int32_t bytesRead = 0; while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) { std::cout << std::string(buf.data(), bytesRead); } sock.close(); return 0; }
void Acceptor::connectionReady( AsyncSocket::UniquePtr sock, const SocketAddress& clientAddr, const string& nextProtocolName, TransportInfo& tinfo) { // Limit the number of reads from the socket per poll loop iteration, // both to keep memory usage under control and to prevent one fast- // writing client from starving other connections. sock->setMaxReadsPerEvent(16); tinfo.initWithSocket(sock.get()); onNewConnection(std::move(sock), &clientAddr, nextProtocolName, tinfo); }