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

Source: Internet
Author: User

Asynchronous run (), Run_one (), poll (), Poll_One ()in order to implement a listening loop, the Io_service class provides 4 methods, such as run (), Run_one (), poll (), and Poll_one (). When you use Service.run () Most of the time, you can. You will be here to learn what other methods have done. Continuous running once again, if there is an action waiting to be performed, run () will execute until you manually call Io_service::stop (). To ensure that Io_service is always executed, you usually add one or more asynchronous operations, and then when they are executed, you continue to add asynchronous operations continuously, such as the following code: using namespace Boost::asio;io_service Service ; Ip::tcp::socket sock (service); Char buff_read[1024], buff_write[1024] = "OK"; void On_read (const BOOST::SYSTEM::ERROR_ Code &err, std::size_t bytes); void On_write (const boost::system::error_code &err, std::size_t bytes) { Sock.async_read_some (Buffer (buff_read), on_read);} void On_read (const boost::system::error_code &err, std::size_t bytes) {//... process the read ... Chapter 2[]sock.async_write_some (buffer (buff_write,3), on_write);} void On_connect (const boost::system::error_code &err) {sock.async_read_some (buffer (buff_read), on_read);} int main (int argc, char* argv[]) {ip::tcp::endpoint EP (ip::address::from_string ("127.0.0.1"), 2001); sock.async_ Connect (EP, on_connect); Service.run ();} When Service.run () is called, there is an asynchronous operation waiting. When the socket is connected to the server, On_connect is called, and it adds an asynchronous operation. When the On_connect is finished, we leave a wait operation (read). When On_read is called, we write a response, which adds another wait operation. When the On_read is finished, we leave a wait action (write). When the on_write operation is called, we read another message from the server, which also adds another wait operation. When the On_write is finished, we have a wait operation (read). And then the loop goes on until we close the appRun_one (), poll (), Poll_one () method
I said before that the Async method handler was called before the Io_service::run line thread was called. Because at least 90% to 95%, this is the only way you need to use it, so I'm just going to say it simple. For tuningThis is also true for threads with Run_one (), poll (), or Poll_one (). the Run_one () method performs and distributes at most one asynchronous operation:if there is no waiting operation, the method immediately returns 0If a wait operation is made, the method is blocked before the first operation executes, and then returns 1you can assume that the following two sections of code are equivalent:Io_service Service;Service.run ();//orwhile (!service.stopped ()) service.run_once ();You can use Run_once () to start an asynchronous operation and then wait for it to finish. Io_service Service;bool Write_complete = false;void On_write (const Boost::system::error_code & err, size_t bytes){write_complete = true;}... ..std::string data = "Login OK";write_complete = false;async_write (sock, buffer (data), on_write);Do service.run_once () while (!write_complete);There are also examples of using the Run_one () method, which are included in Boost.asio such as Blocking_tcp_client.cpp and Blocking_udp_client.cpp. Poll_one Methoduse a non-blocking approachmostrun a ready-to-run wait operation:If there is at least one waiting operation and is ready to run in a non-blocking manner, the Poll_one method runs it and returns 1Otherwise, the method returns 0 immediatelyoperation waiting, ready to run in a non-blocking manner, usually means the following:a timer expires, and then its async_wait processing method needs to be calledan I/O operation is completed (such as Async_read), and then its hanlder needs to be calledcustom handler that were previously added to the Io_services instance queue (this will be explained in later chapters)you can use Poll_one to ensure that allthe handler of I/O operations is complete, while doing some other workIo_service Service;While (true) {//Runall done.handler of IO operationswhile (Service.poll_one ());// ... Doing other things here ...} The poll () method runs all pending operations in a non-blocking manner. The following two sections of code are equivalent:Io_service Service;service.poll ();//orwhile (Service.poll_one ());all previous methods will throw Boost::system::system_error exceptions at the time of failure. And this is something that should never happen; The exception thrown here is usually fatal, either a resource is exhausted, or one of your handler throws an exception. In addition, each method consists of an exception that does not throw, but instead returns an Boost::system::error_code Overload:Io_service Service;Boost::system::error_code err = 0;Service.run (err);if (err) std::cout << "Error" << err << Std::endl;

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

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.