00001 #ifndef __SOCKET_SELECTOR_H__
00002 #define __SOCKET_SELECTOR_H__
00003
00004
00005
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
00031
00032
00033
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
00052
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
00064
00065
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
00077
00078
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__