Unfortunately, this morning I encountered a select problem. Fortunately, I/O processing modules of the Network were encapsulated. This afternoon, I/O processing encapsulation Based on Poll was completed quickly,
You only need to replace one line of code to easily switch from the select architecture to poll, and also to epoll. You can switch the network I/O processing architecture as you like !!! Here is the interface, the template is used, and the inheritance of C ++ is not used here. There are several considerations: (1) efficiency. The polymorphism implemented by the template occurs during compilation, the inherited and overloaded virtual functions are used to address the actual address of the function based on the vtable pointer when the program is running. Of course, the address is slower than the template, and a 4-byte vptr pointer (2) may be added) interface-Oriented Programming: inheritance is generally the use of objects with actual logic meanings, such as human beings and black people. However, our network processing model only has different processing methods. Therefore, we recommend that you use a template to better use the following code in a header file. Otherwise, there may be compilation problems, for more information, see <C ++ templates> Chapter 6th. The access to fd adopts the iteration mode. You must start the iteration before iteration. The specific network model is invisible to callers for the FD storage form. Only iteration pointers can be obtained to access the internal FD list of the network model. The data structure of each implementation class memory can be optimized by itself. Currently, all use an array template. <class T>
Class interfaceioquery
{
PRIVATE: T member; public: // Add a concerned FD void remove (int * PFD, int ioption); // test whether the FD is readable 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 the concerned event void setqueryoption (INT ioption );
// Register an FD
Void registerfd (int pfd, int ioption); // round robin operation,-1 permanent wait, 0 round robin,> 0, Round Robin by time int selectwithtime (INT itime );
// Start traversing the FD list
Int * getselectfdbegin (); // get the pointer int * getselectfdnext () ;}; for details about select and poll, see the attachment for interface implementation. The sample code used is as follows:
// Set the concerned event m_selector.setqueryoption (op_read); // register the FD and the event that this FD cares about
M_selector.registerfd (m_workpipefd [0], op_read); int iready; (;;)
{// 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 time out", m_ithreadid );
}
Else
{// Start iterative access
M_selector.getselectfdbegin (); int * iterator;
// Access is completed for the null flag
While (null! = (Iterator = m_selector.getselectfdnext ()))
{
If (m_selector.iscanread (iterator ))
{
If (* iterator = m_workpipefd [0])
{
Int irecive;
Int Iread = r_read (* iterator, (void *) (& irecive), sizeof (irecive); If (Iread <= 0)
{
M_log.writelog (log_err, "doselect ()", "Work % d READ pipe % d fail", m_ithreadid, * iterator );
}
Else
{
M_log.writelog (log_info, "doselect ()", "Work % d recevie fd % d success", m_ithreadid, irecive );
M_selector.registerfd (irecive, op_read );
}
}
Else
{
Replaytoclient (iterator );
}
-- Iready; If (0 = iready)
{
Break;
}
}
}
}
}