BOOST. Asio, boost

Source: Internet
Author: User

BOOST. Asio, boost

======================================Copyright Notice======================================

Copyright statement: the original article declined to repost what it said, and despised those crawlers who ignored the copyright to capture blog posts at will. I wish you a very happy early time.

Please contact me through "contact email (wlsandwho@foxmail.com)" in the announcement on the right

Do not use academic references.

Do not use for commercial publishing, commercial printing, commercial reference, or other commercial purposes.

 

This article is occasionally revised and improved.

Link: http://www.cnblogs.com/wlsandwho/p/5050318.html

Shame wall: http://www.cnblogs.com/wlsandwho/p/4206472.html

========================================================== ====================================

I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

========================================================== ====================================

BOOST is almost a clever stack, and it comes from the malicious nature of the gods. I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

This library is not intended for beginners as they use the C ++ standard library. I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

I think it is best to know the design mode of the four-person group before using it. I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

========================================================== ====================================

This article describes the content in Overview under Boost. Asio.

========================================================== ====================================

The model given in the overview of the official document, taking socket as an example, briefly describes what has been done by both the synchronous model and the asynchronous model, and I will slightly modify it. I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

 

========================================================== ====================================

Knowledge reserve: Proactor and Reactor have nothing to say. They despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you an early boost.

Comparison of two high-performance I/O design modes (Reactor/Proactor)

Reactor mode, or Reactor Mode

========================================================== ====================================

Boost. Asio supports asynchronous operations (in Proactor mode) and synchronous operations. I have nothing to do with it. I despise crawlers who ignore copyrights and randomly crawl blog posts. I wish you a very happy time.

The Proactor mode can achieve concurrency without multithreading.

========================================================== ====================================

What about Boost. Asio and multithreading?

I think it is better to use "thread reentrant" here.

In short, io_service: run () is used to execute the completion routine, and io_service: post () is used to deliver tasks to the thread pool.

 

I don't want to use Boost on platforms other than Linux and Windows. In fact, I only want to write code on Windows. Ignore this part.

========================================================== ====================================

When a noun does not know how to translate it, do not translate it.

========================================================== ====================================

Strands

1 calls io_service: run () in only one thread, which is implicit.

2. There is only one asynchronous operation chain in a connection like HTTP, which is implicit.

3. explicitly calling io_service: strand is displayed.

(I don't understand what I said. It feels like Strands puts the items to be executed in parallel in a queue protected by access locks. Like a hemp rope, many strands are screwed into one .)

========================================================== ====================================

Buffers

Boost: asio: basic_streambuf

Use data () to obtain the input order. (In this way, the input data can be obtained in a loop .)

Use prepare () to obtain the output sequence. (In this way, the output data can be obtained in a loop .)

Use commit () to append the output data to the end of the input data. (This document does not show usage, but it looks like it is a copy of the cache .)

Use consume () to remove the start part of the input data. (It seems that you can forcibly identify a piece of data as dirty data and then discard it .)

 

Buffer by byte Replication

1 boost: asio: streambuf sb; 2 3 std: size_t n = boost: asio: read_until (sock, sb, '\ n '); // obtain data from the Socket 4 5 boost: asio: streambuf: const_buffers_type bufs = sb. data (); // copy to the constant buffer 6 7 std: string line (boost: asio: buffers_begin (bufs), boost: asio: buffers_begin (bufs) + n); // copy to string

========================================================== ====================================

Streams, Short Reads and Short Writes

Stream-based I/O model

1. Use read_some () of SyncReadStream to implement synchronous read.

3. Use the write_some () of SyncWriteStream to implement synchronous write.

2. Use async_read_some () of AsyncReadStream to Implement Asynchronous read.

4. Use AsyncWriteStream's async_write_some () to Implement Asynchronous write.

 

Transmit precise Bytes:

Read ()

Async_read ()

Write ()

Async_write ()

========================================================== ====================================

Reactor-Style Operations

It seems that the Asio Library provides the null_buffer type for read/write operations when third-party components that implement the communication function need to be integrated into the Asio library.

Before the I/O object is ready, the nullbuffer operation will not return.

In this example, the async_read_some function will return after the read_handler handler completes the operation.

========================================================== ====================================

Line-Based Operations

 

Read "\ r \ n" by row"

Read_until

Async_read_until

 

Read data until there is a space

typedef boost::asio::buffers_iterator<boost::asio::streambuf::const_buffers_type> iterator;std::pair<iterator, bool>match_whitespace(iterator begin, iterator end){  iterator i = begin;  while (i != end)    if (std::isspace(*i++))      return std::make_pair(i, true);  return std::make_pair(i, false);}boost::asio::streambuf b;boost::asio::read_until(s, b, match_whitespace);

Reads data until a specified character is encountered.

class match_char{public:  explicit match_char(char c) : c_(c) {}  template <typename Iterator>  std::pair<Iterator, bool> operator()(Iterator begin, Iterator end) const  {    Iterator i = begin;    while (i != end)      if (c_ == *i++)        return std::make_pair(i, true);    return std::make_pair(i, false);  }private:  char c_;};namespace boost { namespace asio {  template <> struct is_match_condition<match_char>    : public boost::true_type {};} } // namespace boost::asioboost::asio::streambuf b;boost::asio::read_until(s, b, match_char('a'));

 

(I don't understand what is_match_condition is. It seems that an extension is added to the namespace. No specific examples are provided .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.