Logoff monitoring and processing of file descriptors

Source: Internet
Author: User

Logoff provides the addFd function to add the file descriptor to be monitored. This file descriptor is specified by the caller. The caller must also specify the I/O (readable or writable) event to be monitored. In addition, you can specify the callback processing function (and the private data required) used to process I/O events ). HandleEvent can be overloaded in the LooperCallback subclass to handle I/O events. The definition of LooperCallback is as follows (see the file loopback. h): As shown in the preceding code comments, when handleEvent returns 0, it means that the file descriptor monitoring will be canceled after processing, and 1 will continue to be monitored. You can also implement the following categories (see file logoff. h, note that it is in lower case, it is a different file in different paths) callback function, in which the implementation of the I/O event processing, and then specified to the addFd function, the Code is as follows: in fact, the callback function is eventually encapsulated into the following class (see the file logoff. h): the first half of the addFd function of logoff mainly checks the passed parameters (see the file logoff. cpp): In the second half, the epoll monitoring file descriptor is used (see row 440 below). If the parameter already exists, replace the parameter with (row 447 ). A new file descriptor monitoring request Request is created and added to the request list mRequests (row 445 ). If it already exists, replace the original item (row 452 ). When there is an I/O event that contains readable data, the caller who calls pollOnce/pollInner will wake up from the sleep wait of epoll_wait in pollInner and start to execute the subsequent code. As shown in the following pollInner code snippet: it first checks whether the data on the MPs queue is awakened, if not (the else branch at line 256 ), it indicates that data events are generated on the file descriptor added by addFd. After determining which descriptor monitors the request item (row 257), determine the event (row 260 ~ 263 ). Then, press this information into the reply queue as the reply Response, and the code after pollInner will continue to process the queue. This is done to quickly respond to an event, record it, and continue to process the event later. The code snippet behind the pollInner function is as follows: Therefore, the file descriptor monitoring can be implemented by using the pollOnce and addFd functions of logoff. When no data arrives, the pollOnce caller will wait for sleep. When the data arrives, the caller will automatically wake up and execute the specified callback handler (if any ).

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.