问题:Boost.Asio在Linux下封装epoll这种同步接口是如何做到异步IO的。看了源代码,有async_read,但没有看出其中的奥妙,请指点,谢谢
2 个解决方案
#1
看io_service类里的成员impl_, 贴几段代码你应该就明白了,
detail/impl/epoll_reactor.ipp 里有很多epoll的封装
// io_service.hpp
typedef class task_io_service io_service_impl;
...
class io_service: private noncopyable
{
private:
typedef detail::io_service_impl impl_type;
...
impl_type& impl_;
};
// detail/task_io_service.hpp
#include <boost/asio/detail/reactor_fwd.hpp>
...
class task_io_service
: public boost::asio::detail::service_base<task_io_service>
{
...
reactor* task_;
...
};
// detail/reactor_fwd.hpp
typedef class epoll_reactor reactor;
// detail/epoll_reactor.hpp
class epoll_reactor
: public boost::asio::detail::service_base<epoll_reactor>
{
...
private:
...
BOOST_ASIO_DECL static int do_epoll_create();
...
};
// detail/impl/epoll_reactor.ipp
int epoll_reactor::do_epoll_create()
{
...
if (fd == -1 && (errno == EINVAL || errno == ENOSYS))
{
fd = epoll_create(epoll_size);
if (fd != -1)
::fcntl(fd, F_SETFD, FD_CLOEXEC);
}
...
return fd;
}
detail/impl/epoll_reactor.ipp 里有很多epoll的封装
#2
多谢回复,我好好整理一下
#1
看io_service类里的成员impl_, 贴几段代码你应该就明白了,
detail/impl/epoll_reactor.ipp 里有很多epoll的封装
// io_service.hpp
typedef class task_io_service io_service_impl;
...
class io_service: private noncopyable
{
private:
typedef detail::io_service_impl impl_type;
...
impl_type& impl_;
};
// detail/task_io_service.hpp
#include <boost/asio/detail/reactor_fwd.hpp>
...
class task_io_service
: public boost::asio::detail::service_base<task_io_service>
{
...
reactor* task_;
...
};
// detail/reactor_fwd.hpp
typedef class epoll_reactor reactor;
// detail/epoll_reactor.hpp
class epoll_reactor
: public boost::asio::detail::service_base<epoll_reactor>
{
...
private:
...
BOOST_ASIO_DECL static int do_epoll_create();
...
};
// detail/impl/epoll_reactor.ipp
int epoll_reactor::do_epoll_create()
{
...
if (fd == -1 && (errno == EINVAL || errno == ENOSYS))
{
fd = epoll_create(epoll_size);
if (fd != -1)
::fcntl(fd, F_SETFD, FD_CLOEXEC);
}
...
return fd;
}
detail/impl/epoll_reactor.ipp 里有很多epoll的封装
#2
多谢回复,我好好整理一下