Panda3D
|
00001 #ifndef __SOCKET_SELECTOR_H__ 00002 #define __SOCKET_SELECTOR_H__ 00003 00004 //////////////////////////////////////////////////// 00005 // This is a structure on purpose. only used as a helper class to save on typing 00006 // 00007 //////////////////////////////////////////////////// 00008 struct Socket_Selector 00009 { 00010 Socket_fdset _read; 00011 Socket_fdset _write; 00012 Socket_fdset _error; 00013 int _answer; 00014 00015 Socket_Selector() : _answer( -1) 00016 { 00017 } 00018 00019 Socket_Selector(const Socket_fdset &fd) : _read(fd), _write(fd), _error(fd) , _answer( -1) 00020 { 00021 } 00022 00023 int WaitFor(const Time_Span &timeout); 00024 int WaitFor_All(const Socket_fdset & fd, const Time_Span & timeout); 00025 int WaitFor_Read_Error(const Socket_fdset & fd, const Time_Span & timeout); 00026 int WaitFor_Write_Error(const Socket_fdset & fd, const Time_Span & timeout); 00027 }; 00028 00029 ////////////////////////////////////////////////////////////// 00030 // Function name : Socket_Selector::WaitFor 00031 // Description : This function is the reason this call exists.. 00032 // It will wait for a read, write or error condition 00033 // on a socket or it will time out 00034 ////////////////////////////////////////////////////////////// 00035 inline int Socket_Selector::WaitFor(const Time_Span &timeout) 00036 { 00037 SOCKET local_max = 0; 00038 if (local_max < _read._maxid) 00039 local_max = _read._maxid; 00040 if (local_max < _write._maxid) 00041 local_max = _write._maxid; 00042 if (local_max < _error._maxid) 00043 local_max = _error._maxid; 00044 00045 timeval localtv = timeout.GetTval(); 00046 _answer = DO_SELECT(local_max + 1, &_read._the_set, &_write._the_set, &_error._the_set, &localtv); 00047 return _answer; 00048 } 00049 00050 ////////////////////////////////////////////////////////////// 00051 // Function name : Socket_Selector::WaitFor_All 00052 // Description : Helper function to utilize the WaitFor function 00053 ////////////////////////////////////////////////////////////// 00054 inline int Socket_Selector::WaitFor_All(const Socket_fdset & fd, const Time_Span & timeout) 00055 { 00056 _read = fd; 00057 _write = fd; 00058 _error = fd; 00059 return WaitFor(timeout); 00060 } 00061 00062 ////////////////////////////////////////////////////////////// 00063 // Function name : Socket_Selector::WaitFor_Read_Error 00064 // Description : Helper function for WaitFor 00065 // Only looks for readability and errors 00066 ////////////////////////////////////////////////////////////// 00067 inline int Socket_Selector::WaitFor_Read_Error(const Socket_fdset & fd, const Time_Span & timeout) 00068 { 00069 _read = fd; 00070 _write.clear(); 00071 _error = fd; 00072 return WaitFor(timeout); 00073 } 00074 00075 ////////////////////////////////////////////////////////////// 00076 // Function name : Socket_Selector::WaitFor_Write_Error 00077 // Description : Helper function for WaitFor 00078 // Only looks for writability and errors 00079 ////////////////////////////////////////////////////////////// 00080 inline int Socket_Selector::WaitFor_Write_Error(const Socket_fdset & fd, const Time_Span & timeout) 00081 { 00082 _read.clear(); 00083 _write = fd; 00084 _error = fd; 00085 return WaitFor(timeout); 00086 } 00087 00088 #endif //__SOCKET_SELECTOR_H__