MCPcopy Create free account
hub / github.com/arun11299/cpp-subprocess / communicate

Method communicate

cpp-subprocess/subprocess.hpp:2027–2090  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2025 }
2026
2027 inline std::pair<OutBuffer, ErrBuffer>
2028 Communication::communicate(const char* msg, size_t length)
2029 {
2030 // Optimization from subprocess.py
2031 // If we are using one pipe, or no pipe
2032 // at all, using select() or threads is unnecessary.
2033 auto hndls = {stream_->input(), stream_->output(), stream_->error()};
2034 int count = std::count(std::begin(hndls), std::end(hndls), nullptr);
2035 const int len_conv = length;
2036
2037 if (count >= 2) {
2038 OutBuffer obuf;
2039 ErrBuffer ebuf;
2040 if (stream_->input()) {
2041 if (msg) {
2042 int wbytes = std::fwrite(msg, sizeof(char), length, stream_->input());
2043 if (wbytes < len_conv) {
2044 if (errno != EPIPE && errno != EINVAL) {
2045 throw OSError("fwrite error", errno);
2046 }
2047 }
2048 }
2049 // Close the input stream
2050 stream_->input_.reset();
2051 } else if (stream_->output()) {
2052 // Read till EOF
2053 // ATTN: This could be blocking, if the process
2054 // at the other end screws up, we get screwed as well
2055 obuf.add_cap(out_buf_cap_);
2056
2057 int rbytes = util::read_all(
2058 stream_->output(),
2059 obuf.buf);
2060
2061 if (rbytes == -1) {
2062 throw OSError("read to obuf failed", errno);
2063 }
2064
2065 obuf.length = rbytes;
2066 // Close the output stream
2067 stream_->output_.reset();
2068
2069 } else if (stream_->error()) {
2070 // Same screwness applies here as well
2071 ebuf.add_cap(err_buf_cap_);
2072
2073 int rbytes = util::read_atmost_n(
2074 stream_->error(),
2075 ebuf.buf.data(),
2076 ebuf.buf.size());
2077
2078 if (rbytes == -1) {
2079 throw OSError("read to ebuf failed", errno);
2080 }
2081
2082 ebuf.length = rbytes;
2083 // Close the error stream
2084 stream_->error_.reset();

Callers 10

test_ret_code_commFunction · 0.45
test_double_quotesFunction · 0.45
test_inputFunction · 0.45
test_pipingFunction · 0.45
test_simple_cmdFunction · 0.45
communicateMethod · 0.45
communicateMethod · 0.45
check_output_implFunction · 0.45
pipelineFunction · 0.45

Calls 7

OSErrorClass · 0.85
read_allFunction · 0.85
read_atmost_nFunction · 0.85
add_capMethod · 0.80
inputMethod · 0.45
outputMethod · 0.45
errorMethod · 0.45

Tested by 6

test_ret_code_commFunction · 0.36
test_double_quotesFunction · 0.36
test_inputFunction · 0.36
test_pipingFunction · 0.36
test_simple_cmdFunction · 0.36