interface-oriented network processing model (encapsulation Select, Epoll, poll)
Source: Internet
Author: User
It was unfortunate that I encountered a select problem in the morning, but fortunately I had previously encapsulated the network I/O process module, and this afternoon soon completed a poll I/O processing package, which requires only 1 lines of code to be replaced, and can be easily converted from the Select schema to the poll, The same can be converted to Epoll. You can switch network I/O processing architecture as you wish ... Here is the interface, using the template, there is no use of C + + inheritance, there are the following considerations: (1) efficiency, the template implementation of the polymorphism is occurring at compile time, while the inherited overloaded virtual functions are run through the program, according to vtable refers to the actual address of the function, of course, than The template is slow, and may also add 4 bytes of vptr pointers (2) to interface programming: Inheritance is generally the use of objects with actual logical meanings, such as between people and blacks. and our network processing model, just different processing methods, so use the template better some of the following code to put in a header file, otherwise there is a compilation problem, see the <<C++templates>> 6th chapter here the access to FD is an iterative mode and must be preceded by a Move to start the iteration, so that the iteration can be iterated in sequence. The specific network model is not visible to the caller for the FD deposit form, only gets the iteration pointer and accesses the internal FD list of the network model. Each implementation class memory data structure itself can be optimized, is currently all using array template <class t>
Class Interfaceioquery
{
Private:t member; Public://Add a concern for FD void Remove (int* pFd, int ioption); Test whether the FD is readable by bool Iscanread (int* piterator); Test whether the FD can write bool Iscanwrite (int* piterator); Test whether the FD is abnormal bool Isexcept (int* piterator); Set Care event void setqueryoption (int ioption);
Register for an FD
void registerfd (int pFd, int ioption); Polling operation,-1 permanent wait, 0 polling, >0, polling int selectwithtime (int itime) by time;
Start traversing the FD list
int* Getselectfdbegin (); Get the next FD pointer int* getselectfdnext (); }; Specific select and poll, the implementation of the interface see the attachment, the use of the sample code is as follows:
Set the event of concern M_selector.setqueryoption (Op_read); Register FD and the events of the FD concern
M_SELECTOR.REGISTERFD (M_workpipefd[0], op_read); int iready; for (;;)
{//select operation
Iready = M_selector.selectwithtime (-1); if (Iready < 0)
{
Throw CException (errno);
}
else if (iready = 0)
{
M_log.writelog (Log_info, "Doselect ()", "work%d do select with 1 second", M_ithreadid);
}
Else
{//Start iterative access
M_selector.getselectfdbegin (); Int* iterator;
Access completed for null identity
while (NULL!= (iterator = M_selector.getselectfdnext ()))
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