dso*_*len 5 c++ boost boost-asio
我本来打算在我的程序中的线程这将等待两个文件描述符,一个用于插座,第二个为FD描述文件系统(特别是等着看是否有新的文件被添加到一个目录).因为我希望很少看到任何新文件添加或在我即将到来的新的TCP消息想有一个线程等待任一输入和处理取其输入时,它occures而不是有独立的线程困扰检测.
然后我(终于!)获得了"老板"的许可,使用了boost.所以现在我想用boost:asio替换基本套接字.只是我遇到了一个小问题.似乎asio对它自己的select版本进行了修改,而不是提供一个我可以直接使用select的FD.这让我不确定我怎么能在这两个条件,新的文件和TCP输入块,在同一时间,当一个只选择工作和其他似乎并不支持使用选择.有一个简单的工作,我错过了吗?
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(¬ify_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();
}
Run Code Online (Sandbox Code Playgroud)