Boost Asio庫的學習與探究(三)
到這裡已經是第三篇文章了,這一章我會來進一步介紹tcp.hpp.上一章我們談到,tcp必須顯示構造且建構函式私有,那麼怎樣來構建tcp的物件呢,這裡使用的是靜態成員函式。這是對類的進一步封裝。
1、
static tcp v4()
{
return tcp(BOOST_ASIO_OS_DEF(AF_INET));
}
/// Construct to represent the IPv6 TCP protocol.
static tcp v6()
{
return tcp(BOOST_ASIO_OS_DEF(AF_INET6));
}
這裡給出的是ipv4和ipv6協議的tcp實現。
接下來的幾個運算子過載判斷使用的地址族是否相同,還有一個返回地址族的成員函式這裡就不再列舉了。
2、其中有兩個成員函式是這樣的:
/// Obtain an identifier for the type of the protocol.
int type() const
{
return BOOST_ASIO_OS_DEF(SOCK_STREAM);
}
/// Obtain an identifier for the protocol.
int protocol() const
{
return BOOST_ASIO_OS_DEF(IPPROTO_TCP);
}
看註釋是分別返回協議的型別和協議的標誌。
tcp.hpp中的大部分內容介紹完了,這裡還剩下幾個別名沒有介紹:
/// The TCP socket type.
typedef basic_stream_socket<tcp> socket;
/// The TCP acceptor type.
typedef basic_socket_acceptor<tcp> acceptor;
/// The TCP resolver type.
typedef basic_resolver<tcp> resolver;
一個是socket,一個是acceptor,一個是resolver.
Ok,下面我們來一一介紹:
首先是socket,這是對於socket的進一步封裝。
template <typename Protocol,
typename StreamSocketService = stream_socket_service<Protocol> >
class basic_stream_socket
: public basic_socket<Protocol, StreamSocketService>;
從形式可以看出這是一個模板類,其父類也是一個模板類。我們先來看一下它的父類,這裡可能又要開始我們的遞迴之旅了,希望有人看完以後還轉得回來:
template <typename Protocol, typename SocketService>
class basic_socket
: public basic_io_object<SocketService>,
public socket_base
果然,又要遞迴下去,先來看一下它上一個模板類傳遞過來的第二個引數 stream_socket_service;替換一下就是stream_socket_service;那麼這是一個什麼東西呢?看類的宣告:
template <typename Protocol>
class stream_socket_service
#if defined(GENERATING_DOCUMENTATION)
: public boost::asio::io_service::service
#else
: public boost::asio::detail::service_base<stream_socket_service<Protocol> >
#endif
如果定義了GENERATING_DOCUMENTATION,那麼就從io_service::service繼承否則就從boost::asio::detail::service_base
template <typename Type>
class service_base
: public boost::asio::io_service::service
它也是從boost::asio::io_service::service中繼承過來的。io_service是一個類,service是io_service的內部類。我們來看它的建構函式:
io_service::service::service(boost::asio::io_service& owner)
: owner_(owner),
next_(0)
{
}
然後它給傳遞過來的引數起了個別名:
/// The protocol type.
typedef Protocol protocol_type;
/// The endpoint type.
typedef typename Protocol::endpoint endpoint_type;
下面我來看下它的成員變數:
// The platform-specific implementation.
service_impl_type service_impl_;
那麼 service_impl_type是什麼型別呢?
#if defined(BOOST_ASIO_WINDOWS_RUNTIME)
typedef detail::winrt_ssocket_service<Protocol> service_impl_type;
#elif defined(BOOST_ASIO_HAS_IOCP)
typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
#else
typedef detail::reactive_socket_service<Protocol> service_impl_type;
#endif
我們來看一 下win_iocp_socket_service
class win_iocp_socket_service : public win_iocp_socket_service_base
進一步地看win_iocp_socket_service_base:
看一下它的成員變數:
// The io_service used to obtain the reactor, if required.
boost::asio::io_service& io_service_;
// The IOCP service used for running asynchronous operations and dispatching
// handlers.
win_iocp_io_service& iocp_service_;
// The reactor used for performing connect operations. This object is created
// only if needed.
// Mutex to protect access to the linked list of implementations.
boost::asio::detail::mutex mutex_;
// The head of a linked list of all implementations.
base_implementation_type* impl_list_;
這裡的東西有點多,我們將在下一節做進一步的整理。