Panda3D
socket_selector.h
1 #ifndef __SOCKET_SELECTOR_H__
2 #define __SOCKET_SELECTOR_H__
3 
4 ////////////////////////////////////////////////////
5 // This is a structure on purpose. only used as a helper class to save on typing
6 //
7 ////////////////////////////////////////////////////
9 {
10  Socket_fdset _read;
11  Socket_fdset _write;
12  Socket_fdset _error;
13  int _answer;
14 
15  Socket_Selector() : _answer( -1)
16  {
17  }
18 
19  Socket_Selector(const Socket_fdset &fd) : _read(fd), _write(fd), _error(fd) , _answer( -1)
20  {
21  }
22 
23  int WaitFor(const Time_Span &timeout);
24  int WaitFor_All(const Socket_fdset & fd, const Time_Span & timeout);
25  int WaitFor_Read_Error(const Socket_fdset & fd, const Time_Span & timeout);
26  int WaitFor_Write_Error(const Socket_fdset & fd, const Time_Span & timeout);
27 };
28 
29 //////////////////////////////////////////////////////////////
30 // Function name : Socket_Selector::WaitFor
31 // Description : This function is the reason this call exists..
32 // It will wait for a read, write or error condition
33 // on a socket or it will time out
34 //////////////////////////////////////////////////////////////
35 inline int Socket_Selector::WaitFor(const Time_Span &timeout)
36 {
37  SOCKET local_max = 0;
38  if (local_max < _read._maxid)
39  local_max = _read._maxid;
40  if (local_max < _write._maxid)
41  local_max = _write._maxid;
42  if (local_max < _error._maxid)
43  local_max = _error._maxid;
44 
45  timeval localtv = timeout.GetTval();
46  _answer = DO_SELECT(local_max + 1, &_read._the_set, &_write._the_set, &_error._the_set, &localtv);
47  return _answer;
48 }
49 
50 //////////////////////////////////////////////////////////////
51 // Function name : Socket_Selector::WaitFor_All
52 // Description : Helper function to utilize the WaitFor function
53 //////////////////////////////////////////////////////////////
54 inline int Socket_Selector::WaitFor_All(const Socket_fdset & fd, const Time_Span & timeout)
55 {
56  _read = fd;
57  _write = fd;
58  _error = fd;
59  return WaitFor(timeout);
60 }
61 
62 //////////////////////////////////////////////////////////////
63 // Function name : Socket_Selector::WaitFor_Read_Error
64 // Description : Helper function for WaitFor
65 // Only looks for readability and errors
66 //////////////////////////////////////////////////////////////
67 inline int Socket_Selector::WaitFor_Read_Error(const Socket_fdset & fd, const Time_Span & timeout)
68 {
69  _read = fd;
70  _write.clear();
71  _error = fd;
72  return WaitFor(timeout);
73 }
74 
75 //////////////////////////////////////////////////////////////
76 // Function name : Socket_Selector::WaitFor_Write_Error
77 // Description : Helper function for WaitFor
78 // Only looks for writability and errors
79 //////////////////////////////////////////////////////////////
80 inline int Socket_Selector::WaitFor_Write_Error(const Socket_fdset & fd, const Time_Span & timeout)
81 {
82  _read.clear();
83  _write = fd;
84  _error = fd;
85  return WaitFor(timeout);
86 }
87 
88 #endif //__SOCKET_SELECTOR_H__
int WaitFor_Write_Error(const Socket_fdset &fd, const Time_Span &timeout)
Helper function for WaitFor Only looks for writability and errors.
int WaitFor(const Time_Span &timeout)
This function is the reason this call exists.
int WaitFor_All(const Socket_fdset &fd, const Time_Span &timeout)
Helper function to utilize the WaitFor function.
void clear()
Marks the content as empty.
Definition: socket_fdset.h:138
int WaitFor_Read_Error(const Socket_fdset &fd, const Time_Span &timeout)
Helper function for WaitFor Only looks for readability and errors.