Analysis of BOOST.ASIO source code (4) ---- The concept of generics in ASIO (CONCEPTS)

Protocol (communication protocol)

      Protocol is the most important concept of asio in network programming. As you can see in the levelX class diagram in Chapter 1, all services and I/O objects that provide network-related functionality require a Protocol to determine some details.

A summary of the constraints of the Protocol is as follows:

copy code

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 };

copy code

      Classes that conform to the Protocol constraints need to provide three interfaces of type/protocol/family, which respectively return the protocol type/protocol enumeration/protocol group enumeration; they also need to provide two type definitions endpoint/socket, which respectively represent the address/inheritance of one party of the communication protocol. The type of asio::basic_socket.

      At present, the classes that conform to the Protocol constraints in asio are: stream_protocol, datagram_protocol, raw_protocol, seq_packet_protocol; the classes that conform
      to both the Protocol constraints and the Internet Protocol constraints are: tcp (TCP protocol), udp (UDP protocol), icmp (ICMP protocol) .


* InternetProtocol (Internet communication protocol)

      InternetProtocol, a constraint superset of Protocol, adds several new constraints on the basis of Protocol constraints.

A summary of the constraints of the InternetProtocol is as follows:

copy code

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 };

copy code

      Among them, the type/protocol/family interface and the endpoint/socket type definition are all part of the Protocol constraint, and will not be repeated here. The new constraints of InternetProtocol relative to Protocol are: two static interfaces v4/v6, which respectively return the network communication protocol object of IPv4/IPv6 version; type definition resolver, indicating the type inherited from basic_resolver.


* ConstBuffer (immutable buffer), ConstBufferSequence (immutable buffer sequence), MutableBuffer (variable buffer), MutableBufferSequence (mutable buffer sequence)

      ConstBuffer and MutableBuffer are buffer adapter concepts common to various components in asio, which are implemented in asio with two classes of const_buffer and mutable_buffer.

A summary of constraints for ConstBuffer and MutableBuffer is as follows:

copy code

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 };

copy code

      It is only necessary to obtain the buffer first address pointer and buffer length through the two free functions buffer_cast_helper and buffer_size_helper. These two concepts do not need to be extended, so they are not explicitly mentioned in asio. In the following, we directly replace them with their current implementations of const_buffer and mutable_buffer.

ConstBufferSequence and MutableBufferSequence are container constraints for const_buffer and mutable_buffer. A summary of their constraints is as follows:

copy code

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 };

copy code

      ConstBufferSequence and MutableBufferSequence only need to provide two interfaces begin/end and return the corresponding iterator.

      In asio, two classes, const_buffer_1 and mutable_buffer_1, are provided, which can easily encapsulate a single const_buffer and mutable_buffer as a container appearance, making it conform to the constraints of ConstBufferSequence and MutableBufferSequence.


* Stream (stream), AsyncReadStream (stream that supports asynchronous read operation), AsyncWriteStream (stream that supports asynchronous write operation), SyncReadStream (stream that supports synchronous write operation), SyncWriteStream (stream that supports synchronous write operation)

      Stream is the "stream" that everyone is familiar with. The four concepts of AsyncReadStream, AsyncWriteStream, SyncReadStream, and SyncWriteStream are subsets of Stream, and some interfaces are added on the basis of stream.

A summary of the constraints for Stream is as follows:

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

      The constraints of Stream are very simple, only two close interfaces are needed to close the stream.

 

A summary of the constraints for AsyncReadStream is as follows:

copy code

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 };

copy code

      AsyncReadStream adds an interface async_read_some to read data asynchronously on the basis of Stream. The first parameter buffers is an object that conforms to the constraints of MutableBufferSequence, and the second parameter is the callback function of asynchronous operation.

 

A summary of the constraints of AsyncWriteStream is as follows:

copy code

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 };

copy code

      AsyncWriteStream adds an interface async_write_some to asynchronously write data on the basis of Stream. The first parameter buffers is an object that conforms to the constraint of ConstBufferSequence, and the second parameter is the callback function of asynchronous operation.

 

 

A summary of the constraints for SyncReadStream is as follows:

copy code

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 };

copy code

      SyncReadStream adds an interface read_some to asynchronously read data on the basis of Stream. The first parameter buffers is an object that conforms to the constraints of MutableBufferSequence.

 

A summary of the constraints for SyncWriteStream is as follows:

copy code

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 };

copy code

      SyncWriteStream adds an interface write_some to synchronously write data on the basis of Stream. The first parameter buffers is an object that conforms to the constraints of ConstBufferSequence.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446136&siteId=291194637