使用boost:asio和select?阻止TCP输入或文件更新

using boost:asio with select? blocking on TCP input OR file update

本文关键字:输入 TCP 文件 更新 阻止 boost asio select 使用      更新时间:2023-10-16

我本来打算在程序中有一个线程,它将等待两个文件描述符,一个用于套接字,另一个用于描述文件系统的FD(特别是等待是否将新文件添加到目录中)。由于我希望很少看到添加的新文件或传入的新TCP消息,我希望有一个线程等待输入,并在发生输入时处理检测到的任何输入,而不是用单独的线程来处理。

然后(终于!)我得到了"老板"的许可,可以使用boost。所以现在我想用boost:asio替换基本的插座。只是我遇到了一个小问题。它似乎实现了它自己的select版本,而不是提供一个我可以直接与select一起使用的FD。这让我无法确定如何在新文件和TCP输入这两个条件下进行阻止,同时一个条件只能使用select,另一个条件似乎不支持使用select。我错过的这件事有什么简单的解决办法吗?

ASIO最好异步使用(这就是它的含义):您可以为TCP读取和文件描述符活动设置处理程序,并且会为您调用处理程序。

下面是一个演示示例(为支持inotify的Linux编写):

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <sys/inotify.h>
namespace asio = boost::asio;
void start_notify_handler();
void start_accept_handler();
// this stuff goes into your class, only global for the simplistic demo
asio::streambuf buf(1024);
asio::io_service io_svc;
asio::posix::stream_descriptor stream_desc(io_svc);
asio::ip::tcp::socket sock(io_svc);
asio::ip::tcp::endpoint end(asio::ip::tcp::v4(), 1234);
asio::ip::tcp::acceptor acceptor(io_svc, end);
// this gets called on file system activity
void notify_handler(const boost::system::error_code&,
                    std::size_t transferred)
{
    size_t processed = 0;
    while(transferred - processed >= sizeof(inotify_event))
    {
        const char* cdata = processed
                            + asio::buffer_cast<const char*>(buf.data());
        const inotify_event* ievent =
                                 reinterpret_cast<const inotify_event*>(cdata);
        processed += sizeof(inotify_event) + ievent->len;
        if(ievent->len > 0 && ievent->mask & IN_OPEN)
            std::cout << "Someone opened " << ievent->name << 'n';
    }
    start_notify_handler();
}
// this gets called when nsomeone connects to you on TCP port 1234
void accept_handler(const boost::system::error_code&)
{
    std::cout << "Someone connected from " 
              << sock.remote_endpoint().address() << 'n';
    sock.close(); // dropping connection: this is just a demo
    start_accept_handler();
}
void start_notify_handler()
{
    stream_desc.async_read_some( buf.prepare(buf.max_size()),
        boost::bind(&notify_handler, asio::placeholders::error,
                    asio::placeholders::bytes_transferred));
}
void start_accept_handler()
{
    acceptor.async_accept(sock,
        boost::bind(&accept_handler, asio::placeholders::error));
}
int main()
{
    int raw_fd = inotify_init(); // error handling ignored
    stream_desc.assign(raw_fd);
    inotify_add_watch(raw_fd, ".", IN_OPEN);
    start_notify_handler();
    start_accept_handler();
    io_svc.run();
}