Boost :: Process管道链

时间:2018-04-27 16:08:21

标签: c++ asynchronous boost

使用这个问题simultaneous read and write to child's stdio using boost.process的结果,我试图修改代码,以便读取文件,通过gzip传送,通过bzip2传送gzip的输出,最后将bzip2的输出写入a文件。

我的第一次尝试是

/*
 * ProcessPipe.cpp
 *
 *  Created on: Apr 17, 2018
 *      Author: dbetz
 */

//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING 1

#include <boost/asio.hpp>
#include <boost/asio/high_resolution_timer.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
#include <fstream>
#include <functional>

namespace bp = boost::process;
using boost::system::error_code;
using namespace std::chrono_literals;

using Loop = std::function<void()>;
using Buffer = std::array<char, 500>;

int main() {
    boost::asio::io_service svc;    
    auto gzip_exit = [](int code, std::error_code ec) {
            std::cout << "gzip exited " << code << " (" << ec.message() << ")\n";
        };

    auto bzip2_exit = [](int code, std::error_code ec) {
            std::cout << "bzip2 exited " << code << " (" << ec.message() << ")\n";
        };

    bp::async_pipe file_to_gzip_pipe{svc}, gzip_to_bzip_pipe{svc}, bzip_to_file_pipe{svc};
    bp::child process_gzip("/usr/bin/gzip", "-c", bp::std_in < file_to_gzip_pipe, bp::std_out > gzip_to_bzip_pipe, svc, bp::on_exit(gzip_exit));
    bp::child process_bzip2("/usr/bin/bzip2", "-c", bp::std_in < gzip_to_bzip_pipe, bp::std_out > bzip_to_file_pipe, svc, bp::on_exit(bzip2_exit));

    std::ifstream ifs("src/ProcessPipe2.cpp");

    Buffer file_to_gzip_buffer;
    Loop file_to_gzip_loop;
    file_to_gzip_loop = [&] {
        if (!ifs.good())
        {
            error_code ec;
            file_to_gzip_pipe.close(ec);
            std::cout << "Read file, write gzip: closed stdin (" << ec.message() << ")\n";
            return;
        }

        ifs.read(file_to_gzip_buffer.data(), file_to_gzip_buffer.size());

        boost::asio::async_write(file_to_gzip_pipe, boost::asio::buffer(file_to_gzip_buffer.data(), ifs.gcount()),
            [&](error_code ec, size_t transferred) {
                std::cout << "Read file, write gzip: " << ec.message() << " sent " << transferred << " bytes\n";
                if (!ec) {
                    file_to_gzip_loop(); // continue writing
                }
            });
    };

    Buffer gzip_to_bzip_buffer;
    Loop gzip_to_bzip_loop;

    gzip_to_bzip_loop=[&] {
    gzip_to_bzip_pipe.async_read_some(boost::asio::buffer(gzip_to_bzip_buffer),
            [&](error_code ec, size_t transferred){
                // duplicate buffer
                std::cout << "Read gzip, write bzip: " << ec.message() << " got " << transferred  << " bytes\n";
                if (!ec)
                    gzip_to_bzip_loop();
                else
                    gzip_to_bzip_pipe.close();
            }
        );
    };


    std::ofstream ofs("src/ProcessPipe2.gz");

    Buffer bzip_to_file_buffer;
    Loop bzip_to_file_loop;
    bzip_to_file_loop = [&] {
    bzip_to_file_pipe.async_read_some(boost::asio::buffer(bzip_to_file_buffer),
            [&](error_code ec, size_t transferred) {
                std::cout << "Read bzip, write file: " << ec.message() << " got " << transferred << " bytes\n";
                ofs << std::string(bzip_to_file_buffer.data(),transferred);
                if (!ec)
                    bzip_to_file_loop(); // continue reading
            });
    };

    file_to_gzip_loop(); // async
    gzip_to_bzip_loop();
    bzip_to_file_loop(); // async

    svc.run(); // Await all async operations
}

但这会出错:

  

读取gzip,写入bzip:错误的文件描述符得到0字节

问题似乎是gzip_to_bzip_pipe被gzip写入并通过bzip读取。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我写的代码就像:

#include <boost/process.hpp>
#include <iostream>

namespace bp = boost::process;

int main() {
    bp::pipe intermediate;

    bp::child process_gzip("/bin/gzip",   "-c", bp::std_in<"src/ProcessPipe2.cpp",   bp::std_out> intermediate);
    bp::child process_bzip2("/bin/bzip2", "-c", bp::std_in<intermediate, bp::std_out> "src/ProcessPipe2.gz.bz2");

    process_bzip2.wait();
    process_bzip2.wait();
}

奖金

您可以完全不使用子流程,只需使用boost::iostreams::copy

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <iostream>
#include <fstream>

namespace io = boost::iostreams;

int main() {
    std::ifstream ifs("src/ProcessPipe2.cpp");

    io::filtering_stream<io::output> os;
    os.push(io::gzip_compressor());
    os.push(io::bzip2_compressor());

    std::ofstream ofs("src/ProcessPipe2.gz.bz2");
    os.push(ofs);

    io::copy(ifs, os);
}