BOOST.ASIO源码剖析(四) ---- ASIO中的泛型概念(CONCEPTS)

Protocol(通信协议)

      Protocol,是asio在网络编程方面最重要的一个concept。在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protocol来确定一些细节。

Protocol的约束摘要如下:

复制代码

1 class protocol
 2 {
 3 public:
 4   /// Obtain an identifier for the type of the protocol.
 5   int type() const;
 6 
 7   /// Obtain an identifier for the protocol.
 8   int protocol() const;
 9 
10   /// Obtain an identifier for the protocol family.
11   int family() const;
12 
13   typedef ... endpoint;
14   typedef ... socket; 
15 };

复制代码

      符合Protocol约束的类需要提供type/protocol/family三个接口,分别返回协议类型/协议枚举/协议组枚举;还需要提供两个类型定义endpoint/socket,分别表示通信协议一方的地址/继承于asio::basic_socket的类型。

      目前,asio中符合Protocol约束的类有:stream_protocol,datagram_protocol,raw_protocol,seq_packet_protocol;
      既符合Protocol约束,同时又符合InternetProtocol约束的类有:tcp(TCP协议),udp(UDP协议),icmp(ICMP协议)。


* InternetProtocol(网络通信协议)

      InternetProtocol,是Protocol的约束超集,在Protocol约束的基础上添加了几个新的约束。

InternetProtocol的约束摘要如下:

复制代码

1 class InternetProtocol
 2 {
 3 public:
 4   /// Construct to represent the IPv4 internet protocol.
 5   static InternetProtocol v4();
 6 
 7   /// Construct to represent the IPv6 internet protocol.
 8   static InternetProtocol v6();
 9 
10   /// Obtain an identifier for the type of the protocol.
11   int type() const;
12 
13   /// Obtain an identifier for the protocol.
14   int protocol() const;
15 
16   /// Obtain an identifier for the protocol family.
17   int family() const;
18 
19   typedef ... endpoint;
20   typedef ... socket; 
21   typedef ... resolver;
22 };

复制代码

      其中,type/protocol/family接口和endpoint/socket类型定义都是属于Protocol约束的部分,在此不再赘述。InternetProtocol相对于Protocol新增的约束有:v4/v6两个静态接口,分别返回IPv4/IPv6版本的网络通信协议对象;类型定义resolver,表示继承于basic_resolver的类型。


* ConstBuffer(不可变缓冲区),ConstBufferSequence(不可变缓冲区序列),MutableBuffer(可变缓冲区),MutableBufferSequence(可变缓冲区序列)

      ConstBuffer和MutableBuffer是asio中各种组件通用的缓冲区适配器concept,在asio中以const_buffer和mutable_buffer两个类实现。

ConstBuffer和MutableBuffer的约束摘要如下:

复制代码

1 class ConstBuffer
 2 {
 3 private:
 4   friend void const* boost::asio::detail::buffer_cast_helper(const ConstBuffer& b);
 5   friend std::size_t boost::asio::detail::buffer_size_helper(const ConstBuffer& b);
 6 };
 7 
 8 class MutableBuffer
 9 {
10 private:
11   friend void* boost::asio::detail::buffer_cast_helper(const MutableBuffer& b);
12   friend std::size_t boost::asio::detail::buffer_size_helper(const MutableBuffer& b);
13 };

复制代码

      只需能通过buffer_cast_helper和buffer_size_helper这两个自由函数获取缓冲区首地址指针和缓冲区长度即可。这两个concept没有什么扩展的必要,因此asio中并未显式地提及,在后文中我们直接以他们当前的实现const_buffer和mutable_buffer这两个类替代。

ConstBufferSequence和MutableBufferSequence是const_buffer和mutable_buffer的容器约束。它们的约束摘要如下:

复制代码

1 class ConstBufferSequence
 2 {
 3 public:
 4     typedef const_buffer value_type;
 5     typedef ... const_iterator;
 6 
 7     const_iterator begin() const;
 8     const_iterator end() const;
 9 };
10 
11 class MutableBufferSequence
12 {
13 public:
14     typedef mutable_buffer value_type;
15     typedef ... const_iterator;
16 
17     const_iterator begin() const;
18     const_iterator end() const;
19 };

复制代码

      ConstBufferSequence和MutableBufferSequence只需提供begin/end两个接口,返回相应的迭代器即可。

      asio中,提供了const_buffer_1和mutable_buffer_1两个类,可以方便地将单个的const_buffer和mutable_buffer封装为容器外观,使其符合ConstBufferSequence和MutableBufferSequence约束。


* Stream(流),AsyncReadStream(支持异步读操作的流),AsyncWriteStream(支持异步写操作的流),SyncReadStream(支持同步写操作的流),SyncWriteStream(支持同步写操作的流)

      Stream,就是大家耳熟能详的“流”。AsyncReadStream,AsyncWriteStream,SyncReadStream,SyncWriteStream四种concept是Stream的子集,在流的基础上添加一些接口。

Stream的约束摘要如下:

1 class Stream
2 {
3 public:
4     void close();
5     boost::system::error_code close(boost::system::error_code& ec);
6 };

      Stream的约束非常简单,只需要两个用于关闭流的close接口。

AsyncReadStream的约束摘要如下:

复制代码

1 class AsyncReadStream
 2 {
 3 public:
 4     template <typename MutableBufferSequence, typename ReadHandler>
 5     void async_read_some(const MutableBufferSequence& buffers,
 6                 BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
 7 
 8     void close();
 9     boost::system::error_code close(boost::system::error_code& ec);
10 };

复制代码

      AsyncReadStream在Stream的基础上增加了一个异步读数据的接口async_read_some,第一个参数buffers是一个符合MutableBufferSequence约束的对象,第二个参数是异步操作的回调函数。

AsyncWriteStream的约束摘要如下:

复制代码

1 class AsyncWriteStream
 2 {
 3 public:
 4     template <typename ConstBufferSequence, typename WriteHandler>
 5     void async_write_some(const ConstBufferSequence& buffers,
 6                 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
 7 
 8     void close();
 9     boost::system::error_code close(boost::system::error_code& ec);
10 };

复制代码

      AsyncWriteStream在Stream的基础上增加了一个异步写数据的接口async_write_some,第一个参数buffers是一个符合ConstBufferSequence约束的对象,第二个参数是异步操作的回调函数。

SyncReadStream的约束摘要如下:

复制代码

1 class SyncReadStream
 2 {
 3 public:
 4     template <typename MutableBufferSequence>
 5     void read_some(const MutableBufferSequence& buffers);
 6 
 7     template <typename MutableBufferSequence>
 8     boost::system::error_code read_some(const MutableBufferSequence& buffers, boost::system::error_code& ec);
 9 
10     void close();
11     boost::system::error_code close(boost::system::error_code& ec);
12 };

复制代码

      SyncReadStream在Stream的基础上增加了一个异步读数据的接口read_some,第一个参数buffers是一个符合MutableBufferSequence约束的对象。

SyncWriteStream的约束摘要如下:

复制代码

1 class SyncWriteStream
 2 {
 3 public:
 4     template <typename ConstBufferSequence>
 5     void write_some(const ConstBufferSequence& buffers);
 6 
 7     template <typename ConstBufferSequence>
 8     boost::system::error_code write_some(const ConstBufferSequence& buffers, boost::system::error_code& ec);
 9 
10     void close();
11     boost::system::error_code close(boost::system::error_code& ec);
12 };

复制代码

      SyncWriteStream在Stream的基础上增加了一个同步写数据的接口write_some,第一个参数buffers是一个符合ConstBufferSequence约束的对象。

猜你喜欢

转载自my.oschina.net/u/1426828/blog/1793942