proxygen
folly::Subprocess::ReadLinesCallback< Callback > Class Template Reference

#include <Subprocess.h>

Classes

struct  StreamSplitterCallback
 

Public Member Functions

 ReadLinesCallback (Callback &&fdLineCb, uint64_t maxLineLength=0, char delimiter= '\n', uint64_t bufSize=1024)
 
bool operator() (int pfd, int cfd)
 

Private Types

typedef gen::StreamSplitter< StreamSplitterCallbackLineSplitter
 

Private Attributes

Callback fdLineCb_
 
const uint64_t maxLineLength_
 
const char delimiter_
 
const uint64_t bufSize_
 
std::unordered_map< int, LineSplitterfdToSplitter_
 

Detailed Description

template<class Callback>
class folly::Subprocess::ReadLinesCallback< Callback >

A readCallback for Subprocess::communicate() that helps you consume lines (or other delimited pieces) from your subprocess's file descriptors. Use the readLinesCallback() helper to get template deduction. For example:

subprocess.communicate( Subprocess::readLinesCallback( [](int fd, folly::StringPiece s) { std::cout << fd << " said: " << s; return false; // Keep reading from the child } ), [](int pdf, int cfd){ return true; } // Don't write to the child );

If a file line exceeds maxLineLength, your callback will get some initial chunks of maxLineLength with no trailing delimiters. The final chunk of a line is delimiter-terminated iff the delimiter was present in the input. In particular, the last line in a file always lacks a delimiter – so if a file ends on a delimiter, the final line is empty.

Like a regular communicate() callback, your fdLineCb() normally returns false. It may return true to tell Subprocess to close the underlying file descriptor. The child process may then receive SIGPIPE or get EPIPE errors on writes.

Definition at line 749 of file Subprocess.h.

Member Typedef Documentation

template<class Callback >
typedef gen::StreamSplitter<StreamSplitterCallback> folly::Subprocess::ReadLinesCallback< Callback >::LineSplitter
private

Definition at line 761 of file Subprocess.h.

Constructor & Destructor Documentation

template<class Callback >
folly::Subprocess::ReadLinesCallback< Callback >::ReadLinesCallback ( Callback &&  fdLineCb,
uint64_t  maxLineLength = 0,
char  delimiter = '\n',
uint64_t  bufSize = 1024 
)
inlineexplicit

Definition at line 764 of file Subprocess.h.

769  : fdLineCb_(std::forward<Callback>(fdLineCb)),
770  maxLineLength_(maxLineLength),
771  delimiter_(delimiter),
772  bufSize_(bufSize) {}

Member Function Documentation

template<class Callback >
bool folly::Subprocess::ReadLinesCallback< Callback >::operator() ( int  pfd,
int  cfd 
)
inline

Definition at line 774 of file Subprocess.h.

References folly::checkUnixError(), and folly::readNoInt().

774  {
775  // Make a splitter for this cfd if it doesn't already exist
776  auto it = fdToSplitter_.find(cfd);
777  auto& splitter = (it != fdToSplitter_.end())
778  ? it->second
779  : fdToSplitter_
780  .emplace(
781  cfd,
782  LineSplitter(
783  delimiter_,
784  StreamSplitterCallback(fdLineCb_, cfd),
786  .first->second;
787  // Read as much as we can from this FD
788  char buf[bufSize_];
789  while (true) {
790  ssize_t ret = readNoInt(pfd, buf, bufSize_);
791  if (ret == -1 && errno == EAGAIN) { // No more data for now
792  return false;
793  }
794  checkUnixError(ret, "read");
795  if (ret == 0) { // Reached end-of-file
796  splitter.flush(); // Ignore return since the file is over anyway
797  return true;
798  }
799  if (!splitter(StringPiece(buf, ret))) {
800  return true; // The callback told us to stop
801  }
802  }
803  }
std::unordered_map< int, LineSplitter > fdToSplitter_
Definition: Subprocess.h:811
gen::StreamSplitter< StreamSplitterCallback > LineSplitter
Definition: Subprocess.h:761
ssize_t readNoInt(int fd, void *buf, size_t count)
Definition: FileUtil.cpp:102
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
Range< const char * > StringPiece

Member Data Documentation

template<class Callback >
const uint64_t folly::Subprocess::ReadLinesCallback< Callback >::bufSize_
private

Definition at line 809 of file Subprocess.h.

template<class Callback >
const char folly::Subprocess::ReadLinesCallback< Callback >::delimiter_
private

Definition at line 808 of file Subprocess.h.

template<class Callback >
Callback folly::Subprocess::ReadLinesCallback< Callback >::fdLineCb_
private

Definition at line 806 of file Subprocess.h.

template<class Callback >
std::unordered_map<int, LineSplitter> folly::Subprocess::ReadLinesCallback< Callback >::fdToSplitter_
private

Definition at line 811 of file Subprocess.h.

template<class Callback >
const uint64_t folly::Subprocess::ReadLinesCallback< Callback >::maxLineLength_
private

Definition at line 807 of file Subprocess.h.


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