Add logs for TFO succeded
[folly.git] / folly / io / async / test / SocketClient.cpp
1 /*
2  * Copyright 2016 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 #include <folly/io/async/test/BlockingSocket.h>
17
18 #include <iostream>
19
20 #include <folly/ExceptionWrapper.h>
21 #include <gflags/gflags.h>
22
23 using namespace folly;
24
25 DEFINE_string(host, "localhost", "Host");
26 DEFINE_int32(port, 0, "port");
27 DEFINE_bool(tfo, false, "enable tfo");
28 DEFINE_string(msg, "", "Message to send");
29 DEFINE_bool(ssl, false, "use ssl");
30
31 int main(int argc, char** argv) {
32   gflags::ParseCommandLineFlags(&argc, &argv, true);
33
34   if (FLAGS_port == 0) {
35     LOG(ERROR) << "Must specify port";
36     exit(EXIT_FAILURE);
37   }
38
39   // Prep the socket
40   EventBase evb;
41   AsyncSocket::UniquePtr socket;
42   if (FLAGS_ssl) {
43     auto sslContext = std::make_shared<SSLContext>();
44     socket = AsyncSocket::UniquePtr(new AsyncSSLSocket(sslContext, &evb));
45   } else {
46     socket = AsyncSocket::UniquePtr(new AsyncSocket(&evb));
47   }
48   socket->detachEventBase();
49
50   if (FLAGS_tfo) {
51 #if FOLLY_ALLOW_TFO
52     socket->enableTFO();
53 #endif
54   }
55
56   // Keep this around
57   auto sockAddr = socket.get();
58
59   BlockingSocket sock(std::move(socket));
60   SocketAddress addr;
61   addr.setFromHostPort(FLAGS_host, FLAGS_port);
62   sock.setAddress(addr);
63   sock.open();
64   LOG(INFO) << "connected to " << addr.getAddressStr();
65
66   sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size());
67
68   LOG(INFO) << "TFO attempted: " << sockAddr->getTFOAttempted();
69   LOG(INFO) << "TFO finished: " << sockAddr->getTFOFinished();
70   LOG(INFO) << "TFO success: " << sockAddr->getTFOSucceded();
71
72   std::array<char, 1024> buf;
73   int32_t bytesRead = 0;
74   while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) {
75     std::cout << std::string(buf.data(), bytesRead);
76   }
77
78   sock.close();
79   return 0;
80 }