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的類型。socket
目前,asio中符合Protocol約束的類有:stream_protocol,datagram_protocol,raw_protocol,seq_packet_protocol;
既符合Protocol約束,同時又符合InternetProtocol約束的類有:tcp(TCP協議),udp(UDP協議),icmp(ICMP協議)。async
* InternetProtocol(網絡通訊協議)tcp
InternetProtocol,是Protocol的約束超集,在Protocol約束的基礎上添加了幾個新的約束。ide
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(可變緩衝區序列)code
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約束的對象。