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

Source: Internet
Author: User

PS: Yesterday overtime to the early morning, today sleep weeping ...


Buffer function Wrapper

throughout the code you will find that whenever we need to read and write to a buffer, the code wraps the actual buffer object in a buffer () method, and then passes it to the method call:
Char buff[512];   Sock.async_receive (Buff), on_read);
Basically, we include any buffer in a class so that the Boost.asio method can traverse the buffer, for example, you use the following code:
Sock.async_receive (Some_buffer, On_read);
instance Some_buffer will encounter some requirements, called constbuffersequence or Mutablebuffersequence (you can see them in the Boost.asio documentation). It is very complicated to create your own class to handle the details of these requirements, but Boost.asio has provided some classes to handle these requirements. So you don't have to access these buffers directly, but use the buffer () method. 
Confidently, you can wrap the types listed below into a buffer () method:
A char[] Const array
A byte-size void * pointer
A string of type std::string
A pod-const array (the pod represents pure data, and no action is made on the constructor and the deallocation)
Std::vector of a POD data
A boost::array that contains pod data
A std::array that contains pod data
The following code is valid:
   
  
    1.  struct   pod_sample {int i; long l; char c;};   ... char b1[512]; void * B2 = new char[512];  
        std::string B3; b3.resize (128);   Pod_sample b4[16]; Std::vector<pod_sample> b5;   B5.resize (16);   Boost::array<pod_sample,16> b6;   Std::array<pod_sample,16> B7;   Sock.async_send (Buffer (B1), on_read);   Sock.async_send (Buffer (b2,512), on_read);   Sock.async_send (Buffer (B3), on_read);   Sock.async_send (Buffer (B4), on_read);   Sock.async_send (Buffer (B5), on_read);   Sock.async_send (Buffer (B6), on_read);
      Sock.async_send (Buffer (b7), on_read);  
All in all: instead of creating your own class to handle the needs of constbuffersequence or mutablebuffersequence, create a buffer that can be preserved when you need it, and then return a Mutable_Theclass of the Buffers_1 instance, and we did so earlier in the Shared_buffer class.
read/write/connect free functionBoost.asio provides free functions to let you handle I/O, and we analyze them in four groups. Connect MethodThese methods connect the socket to an endpoint. Connect (socket, begin [, end] [, condition]): This method synchronizes the connection by trying the endpoint of the queue from start to end. The begin iterator is the return result of the Socket_type::resolver::query call (you may need to review the section of the endpoint). Special tip end iterators are optional; you can ignore it. You can provide a condition method that is called after each connection attempt. Usage is Iterator connect_condition (const Boost::system::error_code & err,Iterator next);. You can choose to return an iterator that is not next, so you can skip some endpoints. async_connect (socket, begin [, end] [, condition], handler): This method invokes the connection method asynchronously, at the end, it invokes the completion processing method. Usage is void handler (constBoost::system::error_code & err, Iterator Iterator);. The second parameter passed to the processing method is an iterator to the endpoint where the connection was successful. (or end iterator). examples of this are:using namespace Boost::asio::ip;tcp::resolver Resolver (service); Tcp::resolver::iterator iter = resolver.resolve ( tcp::resolver::query ("www.yahoo.com"," ")); Tcp::socket Sock (service);Connect (sock, ITER);a host name can be parsed into multiple addresses, and connect and Async_connect can help you to get out of the hard work of trying each address and then finding an available address, because they've done it for you.

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

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.