proxygen
fizz::tool::TerminalInputHandler Class Reference

#include <FizzCommandCommon.h>

Inheritance diagram for fizz::tool::TerminalInputHandler:
folly::EventHandler

Public Member Functions

 TerminalInputHandler (folly::EventBase *evb, InputHandlerCallback *cb)
 
void handlerReady (uint16_t events) noexceptoverride
 
- Public Member Functions inherited from folly::EventHandler
 EventHandler (EventBase *eventBase, int fd)
 
 EventHandler (EventBase *eventBase=nullptr, NetworkSocket fd=NetworkSocket())
 
virtual ~EventHandler ()
 
bool registerHandler (uint16_t events)
 
void unregisterHandler ()
 
bool isHandlerRegistered () const
 
void attachEventBase (EventBase *eventBase)
 
void detachEventBase ()
 
void changeHandlerFD (int fd)
 
void changeHandlerFD (NetworkSocket fd)
 
void initHandler (EventBase *eventBase, int fd)
 
void initHandler (EventBase *eventBase, NetworkSocket fd)
 
uint16_t getRegisteredEvents () const
 
bool registerInternalHandler (uint16_t events)
 
bool isPending () const
 

Private Member Functions

void hitEOF ()
 

Private Attributes

InputHandlerCallbackcb_
 
folly::EventBaseevb_
 

Additional Inherited Members

- Public Types inherited from folly::EventHandler
enum  EventFlags {
  NONE = 0, READ = EV_READ, WRITE = EV_WRITE, READ_WRITE = (READ | WRITE),
  PERSIST = EV_PERSIST
}
 

Detailed Description

Definition at line 70 of file FizzCommandCommon.h.

Constructor & Destructor Documentation

fizz::tool::TerminalInputHandler::TerminalInputHandler ( folly::EventBase evb,
InputHandlerCallback cb 
)
explicit

Definition at line 49 of file FizzCommandCommon.cpp.

References folly::EventHandler::registerHandler().

52  : EventHandler(evb, 0), cb_(cb), evb_(evb) {
53  registerHandler(EventHandler::READ | EventHandler::PERSIST);
54 }
EventHandler(EventBase *eventBase, int fd)
Definition: EventHandler.h:65
bool registerHandler(uint16_t events)
Definition: EventHandler.h:100

Member Function Documentation

void fizz::tool::TerminalInputHandler::handlerReady ( uint16_t  events)
overridevirtualnoexcept

handlerReady() is invoked when the handler is ready.

Parameters
eventsA bitset indicating the events that are ready.

Implements folly::EventHandler.

Definition at line 56 of file FizzCommandCommon.cpp.

References cb_, fizz::tool::InputHandlerCallback::connected(), folly::errnoStr(), hitEOF(), fizz::detail::read(), and fizz::tool::InputHandlerCallback::write().

56  {
57  // Handle read ready on stdin, but only once we're connected.
58  if (events & EventHandler::READ && cb_->connected()) {
59  std::array<char, 512> buf;
60  int result = read(0, buf.data(), buf.size());
61 
62  if (result > 0) {
63  cb_->write(IOBuf::wrapBuffer(buf.data(), result));
64  } else {
65  if (result < 0) {
66  LOG(ERROR) << "Error on terminal read: " << folly::errnoStr(errno);
67  }
68  hitEOF();
69  }
70  }
71 }
virtual bool connected() const =0
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
virtual void write(std::unique_ptr< folly::IOBuf > msg)=0
fbstring errnoStr(int err)
Definition: String.cpp:463
void fizz::tool::TerminalInputHandler::hitEOF ( )
private

Definition at line 73 of file FizzCommandCommon.cpp.

References cb_, fizz::tool::InputHandlerCallback::close(), evb_, and folly::EventBase::runInLoop().

Referenced by handlerReady().

73  {
74  evb_->runInLoop([cb_ = cb_]() { cb_->close(); });
75 }
void runInLoop(LoopCallback *callback, bool thisIteration=false)
Definition: EventBase.cpp:520

Member Data Documentation

InputHandlerCallback* fizz::tool::TerminalInputHandler::cb_
private

Definition at line 80 of file FizzCommandCommon.h.

Referenced by handlerReady(), and hitEOF().

folly::EventBase* fizz::tool::TerminalInputHandler::evb_
private

Definition at line 81 of file FizzCommandCommon.h.

Referenced by hitEOF().


The documentation for this class was generated from the following files: