来自Asio文档:
The Asio library includes a low-level socket interface based on the BSD socket
API, which is widely implemented and supported by extensive literature. It is
also used as the basis for networking APIs in other languages, like Java. This
low-level interface is designed to support the development of efficient and
scalable applications.
那么Asio库是否重新实现了低级套接字接口?还是Asio库套接字接口是具有许多功能的BSD套接字API / Winsock的包装器?如果是重新实现的话,我希望看到很多内核代码。
答案 0 :(得分:1)
实际上,套接字实现抽象了底层的OS实现。 WinInet和linux套接字都类似于BSD套接字。
“低级” API的原因在于asio::ip::basic_socket<...>::read_some
和...::write_some
与BSD级API处于同一级别。
实际上,服务 implementations 中继到基础调用,抽象出阻塞IO和非阻塞IO的(特定于实现的)详细信息,并等待就绪的套接字(不需要使用线程)。 / p>
您仍可以通过在服务对象(例如,https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/reference/basic_socket/native_handle.html)内部使用native_handle()
成员函数,将原始API与原始套接字句柄一起使用。
但是请注意不要破坏实现不变式。例如。在这样的句柄上调用
::close
或更改阻止模式会引起虚假的错误和死锁。