Boost.asio C + + Network programming Translator (27)

Source: Internet
Author: User

Streambuf classas I said before, Streambuf inherits from Std::streambuf. Just like Std::streambuf itself, it cannot copy constructs. In addition, it has some additional methods, as follows:Streambuf ([Max_size,][allocator]): This method constructs a Streambuf object. You can choose to specify a maximum buffer size and an allocator that allocates/frees memory when needed. Prepare (N): This method returns a sub buffer, which is used to hold contiguous n characters. It can be used to read or write. The results returned by the Boost.asio method can be used in any free function that handles Read/write, not just those used to handle STREAMBUF objects. data (): This method returns the entire buffer as a sequential string and is then used for writing. The results returned by the Boost.asio method can be used in any free function that handles writes, not just those used to handle STREAMBUF objects. Comsume (N): In this method, the data is removed from the input queue (from the read operation)commit (N): In this method, the data is removed from the output queue (from the write operation) and then added to the input queue (prepared for the read operation). size (): This method returns the size of the entire Streambuf object in bytes. max_size (): This method returns the maximum number of bytes that can be saved. In addition to the last two methods, other methods are not so easy to understand. First, most of the time you will pass the STREAMBUF as a parameter to the Read/write free function, as shown in the following code snippet:
    1. Read_until (sock, buf, "\ n"); Read to BUF in write   (sock, buf);//write from BUF
if you want to pass the entire buffer to a free function as shown in the previous code snippet, the method will ensure that the position where the input and output pointers to buffer is pointing is incremented. That is, if you have data to read, you can read it. For example:
    1. Read_until (sock, buf, ' \ n ');   Std::cout << &buf << Std::endl;
the code above will output what you just wrote from the socket. The following code will not output anything:
Read (sock, Buf.prepare (+), transfer_exactly (+));   Std::cout << &buf << Std::endl;
The byte is read, but the input pointer does not move, you need to move it yourself, as shown in the following code snippet:
Read (sock, Buf.prepare (+), transfer_exactly (+));   Buf.commit (+);   Std::cout << &buf << Std::endl;
Similarly, suppose you need to write from the Streambuf object, and if you use the Write free function, you need to do the following:
Streambuf buf;   Std::ostream out (&BUF);   Out << "Hi there" << Std::endl;   Write (sock, buf);
The following code will send hi there three times:
Streambuf buf;   Std::ostream out (&BUF);   Out << "Hi there" << Std::endl;   for (int i = 0; i < 3; ++i)
       Write (sock, Buf.data ());
The reason for this is that buffer has never been consumed because the data is still in place. If you want to consume it, use the following code snippet:
Streambuf buf;   Std::ostream out (&BUF);   Out << "Hi there" << Std::endl;   Write (sock, Buf.data ());   Buf.consume (9);
Overall, you'd better choose to handle the entire STREAMBUF instance. Use the above method if adjustments are required. Although you can use the same streambuf in both read and write operations, you suggest that you use two separate, one to read the other, it will make things simple and clear, and you will avoid a lot of possible bugs.
free functions for handling Streambuf objectsThe following list shows the Boost.asio free function that can also handle STREAMBUF objects:
  • Read (sock, buf[, Completion_function]): This method reads the contents from the socket into the Streambuf object. The completion method is optional. If so, it is called every time the read operation succeeds, and then tells Boost.asio whether the operation is complete (if not, it continues to read). Its format is: size_t completion (const Boost::system::error_code & err, size_t bytes_transfered); If the completion method returns 0, We think that the read operation is complete, and if it is not 0, it represents the maximum number of bytes to be read by the Read_some method of the next call to stream
  • Read_at (Random_stream, offset, buf [, Completion_function]): This method reads from a stream that supports random reads. Note that it is not applied to the socket (since they do not have random-read models, they are unidirectional and always forward).
  • Read_until (sock, buf, char | string | regex | match_condition): This method has been read until a condition satisfies a feature. Either a char-type data is read, or a string is read, or a string that is currently read can match a regular expression, or the Match_condition method tells us to end this method. The format of the Match_condition method is:pair<iterator,bool> match (iterator begin, iterator end); , iterator represents Buffers_ iterator<streambuf::const_buffers_type>. If it matches, you need to return a pair (Passed_end_of_match is set to true). If there is no match, you need to return the pair (begin is set to false).
  • Write (sock, buf [, Completion_function]): This method writes all the contents of the Streambuf object. The completion method is optional and behaves like the completion method of the Read (): Returns 0 when the write operation completes, or returns a non-0 number representing the maximum number of bytes to write for the next Write_some method that calls stream.
  • Write_at (Random_stream,offset, buf [, Completion_function]): This method is used to write to a stream that supports random storage. Again, it is not applied to the socket.
  • Async_read (sock, buf [, Competion_function], handler): This method is an asynchronous implementation of read (), and the format for handler is: void handler (const Boost::system: : Error_code, size_t bytes).
  • Async_read_at (Radom_stream, offset, buf [, Completion_function], handler): This method is an asynchronous implementation of the Read_at ().
  • Async_read_until (sock, buf, char | string | regex | Match_ condition, handler): This method is an asynchronous implementation of the Read_until ().
  • Async_write (sock, buf [, Completion_function], handler): This method is an asynchronous implementation of write ().
  • Async_write_at (Random_stream,offset, buf [, Completion_function], handler): This method is an asynchronous implementation of the Write_at ().
Let's say you need to read a vowel letter all the time:
    1.  streambuf   Buf  BOOL Is_vowel (char c) { 
        return c = = ' A ' | | c = = ' E ' | | c = = ' I ' | | c = = ' O ' | |   c = = ' u '; }  
        size_t read_complete (Boost::system::       Error_code, size_t bytes) {const char * begin = Buffer_cast<const char*> (Buf.data ());       if (bytes = = 0) return 1; while (bytes > 0)  
        if (Is_vowel (*beg in++)) return 0;  
        else--bytes; return 1;  
       } ... read (sock, buf, read_complete);  
the thing to note here is to access the buffer in Read_complete (), which is buffer_cast<> and Buf.data. If you want to use the regular, the above example will be simpler:

Read_until (sock, buf, Boost::regex ("^[aeiou]+"));

or we can change the example to get the Match_condition method working:
   Streambuf buf;   BOOL Is_vowel (char c) {
       return c = = ' A ' | | c = = ' E ' | | c = = ' I ' | | c = = ' O ' | | c = = ' u ';   }
   typedef buffers_iterator<streambuf::const_buffers_type> iterator;   std::p air<iterator,bool> Match_vowel (iterator B, iterator e) {
       while (b! = e)           if (Is_vowel (*b++)) return Std::make_pair (b, true);
       Return Std::make_pair (E, false);   }
   ...   size_t bytes = Read_until (sock, buf, Match_vowel);
when using Read_until, there is a difficulty here; you need to remember the number of bytes you have read since the lower buffer may read more bytes (unlike when using read ()). For example:
Std::cout << &buf << Std::endl;
The above code may output more bytes than the read_until reads.

Boost.asio C + + Network programming Translator (27)

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.