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");
28 int main(int argc, char** argv) {
29 gflags::ParseCommandLineFlags(&argc, &argv, true);
31 if (FLAGS_port == 0) {
32 LOG(ERROR) << "Must specify port";
38 AsyncSocket::UniquePtr socket(new AsyncSocket(&evb));
39 socket->detachEventBase();
48 auto sockAddr = socket.get();
50 BlockingSocket sock(std::move(socket));
52 addr.setFromHostPort(FLAGS_host, FLAGS_port);
53 sock.setAddress(addr);
55 LOG(INFO) << "connected to " << addr.getAddressStr();
57 sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size());
59 LOG(ERROR) << "TFO attempted: " << sockAddr->getTFOAttempted();
60 LOG(ERROR) << "TFO finished: " << sockAddr->getTFOFinished();
62 std::array<char, 1024> buf;
63 int32_t bytesRead = 0;
64 while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) {
65 std::cout << std::string(buf.data(), bytesRead);