2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/io/async/test/BlockingSocket.h>
18 #include <folly/ExceptionWrapper.h>
19 #include <gflags/gflags.h>
21 using namespace folly;
23 DEFINE_string(host, "localhost", "Host");
24 DEFINE_int32(port, 0, "port");
25 DEFINE_bool(tfo, false, "enable tfo");
26 DEFINE_string(msg, "", "Message to send");
27 DEFINE_bool(ssl, false, "use ssl");
29 int main(int argc, char** argv) {
30 gflags::ParseCommandLineFlags(&argc, &argv, true);
32 if (FLAGS_port == 0) {
33 LOG(ERROR) << "Must specify port";
39 AsyncSocket::UniquePtr socket;
41 auto sslContext = std::make_shared<SSLContext>();
42 socket = AsyncSocket::UniquePtr(new AsyncSSLSocket(sslContext, &evb));
44 socket = AsyncSocket::UniquePtr(new AsyncSocket(&evb));
46 socket->detachEventBase();
55 auto sockAddr = socket.get();
57 BlockingSocket sock(std::move(socket));
59 addr.setFromHostPort(FLAGS_host, FLAGS_port);
60 sock.setAddress(addr);
62 LOG(INFO) << "connected to " << addr.getAddressStr();
64 sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size());
66 LOG(ERROR) << "TFO attempted: " << sockAddr->getTFOAttempted();
67 LOG(ERROR) << "TFO finished: " << sockAddr->getTFOFinished();
69 std::array<char, 1024> buf;
70 int32_t bytesRead = 0;
71 while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) {
72 std::cout << std::string(buf.data(), bytesRead);