proxygen
FizzCommandCommon.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <folly/io/IOBuf.h>
14 
15 #include <cstdint>
16 #include <functional>
17 #include <iostream>
18 #include <limits>
19 #include <map>
20 #include <stdexcept>
21 #include <string>
22 #include <vector>
23 
24 namespace fizz {
25 namespace tool {
26 
27 inline uint16_t portFromString(const std::string& portStr, bool serverSide) {
28  unsigned long converted = 0;
29  try {
30  converted = std::stoul(portStr);
31  } catch (const std::exception&) {
32  throw std::runtime_error(
33  "Couldn't convert " + portStr + " to port number.");
34  }
35  if (converted <= std::numeric_limits<uint16_t>::max()) {
36  if (converted == 0 && !serverSide) {
37  throw std::runtime_error("Port 0 is not valid for client ports.");
38  }
39  return static_cast<uint16_t>(converted);
40  } else {
41  throw std::runtime_error(
42  "Couldn't convert " + portStr + " to port number.");
43  }
44 }
45 
46 // Argument handler function
47 
48 typedef std::function<void(const std::string&)> FizzCommandArgHandler;
51  FizzCommandArgHandler handler;
52 };
53 typedef std::map<std::string, FizzCommandArgHandlerInfo> FizzArgHandlerMap;
54 
55 int parseArguments(
56  std::vector<std::string> argv,
57  FizzArgHandlerMap handlers,
58  std::function<void()> usageFunc);
59 
60 // Echo client/server classes
61 
63  public:
64  virtual ~InputHandlerCallback() = default;
65  virtual void write(std::unique_ptr<folly::IOBuf> msg) = 0;
66  virtual void close() = 0;
67  virtual bool connected() const = 0;
68 };
69 
71  public:
72  explicit TerminalInputHandler(
73  folly::EventBase* evb,
75  void handlerReady(uint16_t events) noexcept override;
76 
77  private:
78  void hitEOF();
79 
82 };
83 
84 } // namespace tool
85 } // namespace fizz
std::function< void(const std::string &)> FizzCommandArgHandler
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
LogLevel max
Definition: LogLevel.cpp:31
requires E e noexcept(noexcept(s.error(std::move(e))))
int parseArguments(std::vector< std::string > argv, FizzArgHandlerMap handlers, std::function< void()> usageFunc)
char ** argv
uint16_t portFromString(const std::string &portStr, bool serverSide)
Definition: Actions.h:16
const char * string
Definition: Conv.cpp:212
std::map< std::string, FizzCommandArgHandlerInfo > FizzArgHandlerMap
int close(NetworkSocket s)
Definition: NetOps.cpp:90