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