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

Source: Internet
Author: User

asynchronous I/O in a client applicationthe main process and the synchronous client application are somewhat similar, but the difference is that Boost.asio is in the middle of the Async_read and Async_write requests each time. The first scenario I implemented in the fourth chapter is the client and the server. You should remember that at the end of each asynchronous operation, I started another asynchronous operation so that the Service.run () method would not end. to accommodate the second case, you need to use the following code snippet:
void On_connect () {       do_read ();
   }   void Do_read () {
       Async_read (sock_, buffer (read_buffer_),                   mem_fn2 (read_complete,_1,_2), mem_fn2 (on_read,_1,_2));
   }   void On_read (const Error_code & err, size_t bytes) {
       if (err) stop ();       if (!started ()) return;       std::string msg (read_buffer_, bytes);       if (Msg.find ("clients") = = 0) on_clients (msg);       else ...
   }   void On_clients (const std::string & msg) {
       std::string clients = MSG.SUBSTR (8);       Std::cout << username_ << ", New client list:" << clients;       Do_write ("Clients ok\n");

}

note that as soon as we connect successfully, we start reading from the server. Each on_[event] method ends us by writing a reply to the server. the beauty of using asynchrony is that you can use Boost.asio to manage, which combines I/O network operations with other asynchronous operations. Although its process is not as clear as the process of synchronization, you can still imagine it in a synchronized way. Suppose you read files from a Web server and then save them to a database (asynchronously). You can think of this process as the following flowchart:
asynchronous I/O for server-side applicationsnow to show is two popular situations, conditions 1 (pull) and situation 2 (push)The first case is also the asynchronous server I implemented in the 4th client and server. At the end of each asynchronous operation, I will start another asynchronous operation, so that Service.run () will not end. now it's time to show the clipped frame code. The following are all members of the Talk_to_client class:
void Start () {       ...
       Do_read (); First, we wait for the client to login   }
   void On_read (const Error_code & err, size_t bytes) {       std::string msg (read_buffer_, bytes);       if (Msg.find ("login") = = 0) on_login (msg);       else if (Msg.find ("ping") = = 0) on_ping ();

Else ...}

   void On_login (const std::string & msg) {       std::istringstream in (msg);       In >> username_ >> username_;       Do_write ("Login ok\n");
   }   void Do_write (const std::string & msg) {
       Std::copy (Msg.begin (), Msg.end (), write_buffer_);       Sock_.async_write_some (Buffer (Write_buffer_, msg.size ()),
                               MEM_FN2 (on_write,_1,_2));
   }   void On_write (const Error_code & err, size_t bytes) {

Do_read (); }

Simply put, we always wait for a read operation, and as soon as one occurs, we process and then return the result to the client. We can modify the above code to complete a push server
void Start () {       ...
       On_new_client_event ();   }
   void On_new_client_event () {       std::ostringstream msg;       MSG << "Client Count" << clients.size ();       for (Array::const_iterator B = Clients.begin (), E = clients.
   End ();           (*b)->do_write (Msg.str ());

}

void On_read (const Error_code & err, size_t bytes) {    std::string msg (read_buffer_, bytes);    Basically here, we are acknowledge    //That we are clients received our notifications
}void do_write (const std::string & msg) {
    Std::copy (Msg.begin (), Msg.end (), write_buffer_);    Sock_.async_write_some (Buffer (Write_buffer_, msg.size ()),
                            MEM_FN2 (on_write,_1,_2));
}void on_write (const Error_code & err, size_t bytes) {

Do_read (); }

as long as an event occurs, we assume that it is on_new_client_event, and all clients that need to be notified are sent a message. When they reply, we simply think they have confirmed receipt of the event. Note that we will never run out of waiting asynchronous operations (so Service.run () will not end) because we have been waiting for a new client:
Ip::tcp::acceptor ACC (Service, Ip::tcp::endpoint (Ip::tcp::v4 (),   8001));   void Handle_accept (talk_to_client::p TR Client, const Error_code & err)   {
       Client->start ();       Talk_to_client::p TR new_client = Talk_to_client::new_ ();       Acc.async_accept (New_client->sock (), Bind (Handle_accept,new_
   client,_1));   }








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

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.