Panda3D
 All Classes Functions Variables Enumerations
socket_fdset.h
1 #ifndef __SOCKET_FDSET_H__
2 #define __SOCKET_FDSET_H__
3 
4 ////////////////////////////////////////////////////
5 //
6 //rhh
7 // This class needs to be broken into 2 classes: the gathering class and the processing functions.
8 // The functions should be set up as template functions
9 //
10 // Add a helper class socket_select. May want to totally separate the select and collect functionality
11 // fits more with the normal Berkeley mind set... ** Not ** Should think about using POLL() on BSD-based systems
12 //
13 //////////////////////////////////////////////////////////
14 #include "pandabase.h"
15 #include "numeric_types.h"
16 #include "time_base.h"
17 #include "socket_ip.h"
18 
19 class Socket_fdset {
20 PUBLISHED:
21  inline Socket_fdset();
22  inline void setForSocket(const Socket_IP &incon);
23  inline bool IsSetFor(const Socket_IP &incon) const;
24  inline int WaitForRead(bool zeroFds, PN_uint32 sleep_time = 0xffffffff);
25  inline int WaitForWrite(bool zeroFds, PN_uint32 sleep_time = 0xffffffff);
26  inline int WaitForError(bool zeroFds, PN_uint32 sleep_time = 0xffffffff);
27 
28  inline int WaitForRead(bool zeroFds, const Time_Span & timeout);
29  inline void clear();
30 
31 private:
32  inline void setForSocketNative(const SOCKET inid);
33  inline bool isSetForNative(const SOCKET inid) const;
34 
35  friend struct Socket_Selector;
36 
37  SOCKET _maxid;
38 
39 #ifndef CPPPARSER
40  mutable fd_set _the_set;
41 #endif
42 };
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function name : Socket_fdset::Socket_fdset
46 // Description : The constructor
47 ////////////////////////////////////////////////////////////////////
49 {
50  clear();
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function name : Socket_fdset::setForSocketNative
55 // Description : This does the physical manipulation of the set getting read for the base call
56 ////////////////////////////////////////////////////////////////////
57 inline void Socket_fdset::setForSocketNative(SOCKET inid)
58 {
59  assert( inid >= 0);
60 #ifndef WIN32
61  assert(inid < FD_SETSIZE);
62 #endif
63 
64  FD_SET(inid, &_the_set);
65  if (_maxid < inid)
66  _maxid = inid;
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function name : Socket_fdset::isSetForNative
71 // Description : Answer the question: was the socket marked for reading
72 // there's a subtle difference in the NSPR version: it will respond if
73 // the socket had an error
74 ////////////////////////////////////////////////////////////////////
75 inline bool Socket_fdset::isSetForNative(SOCKET inid) const
76 {
77  assert( inid >= 0);
78 #ifndef WIN32
79  assert(inid < FD_SETSIZE);
80 #endif
81 
82  return (FD_ISSET(inid, &_the_set) != 0);
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function name : Socket_fdset::IsSetFor
87 // Description : check to see if a socket object has been marked for reading
88 ////////////////////////////////////////////////////////////////////
89 inline bool Socket_fdset::IsSetFor(const Socket_IP & incon) const
90 {
91  return isSetForNative(incon.GetSocket());
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function name : WaitForRead
96 // Description :
97 ////////////////////////////////////////////////////////////////////
98 inline int Socket_fdset::WaitForRead(bool zeroFds, PN_uint32 sleep_time)
99 {
100  int retVal = 0;
101  if (sleep_time == 0xffffffff)
102  {
103  retVal = DO_SELECT(_maxid + 1, &_the_set, NULL, NULL, NULL);
104  }
105  else
106  {
107  timeval timeoutValue;
108  timeoutValue.tv_sec = sleep_time / 1000;
109  timeoutValue.tv_usec = (sleep_time % 1000) * 1000;
110 
111  retVal = DO_SELECT(_maxid + 1, &_the_set, NULL, NULL, &timeoutValue);
112  }
113  if (zeroFds)
114  clear();
115 
116  return retVal;
117 }
118 
119 //////////////////////////////////////////////////////////////
120 // Function name : Socket_fdset::WaitForRead
121 // Description :
122 //////////////////////////////////////////////////////////////
123 inline int Socket_fdset::WaitForRead(bool zeroFds, const Time_Span & timeout)
124 {
125  timeval localtv = timeout.GetTval();
126 
127  int retVal = DO_SELECT(_maxid + 1, &_the_set, NULL, NULL, &localtv);
128  if (zeroFds)
129  clear();
130 
131  return retVal;
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function name : Socket_fdset::zeroOut
136 // Description : Marks the content as empty
137 ////////////////////////////////////////////////////////////////////
138 inline void Socket_fdset::clear()
139 {
140  _maxid = 0;
141  FD_ZERO(&_the_set);
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function name : Socket_fdset::setForSocket
146 // Description :
147 ////////////////////////////////////////////////////////////////////
148 inline void Socket_fdset::setForSocket(const Socket_IP &incon)
149 {
150  setForSocketNative(incon.GetSocket());
151 }
152 
153 ////////////////////////////////
154 ////////////////////////////////////////////////////////////////////
155 // Function name : Socket_fdset::WaitForWrite
156 // Description : This is the function that will wait till
157 // one of the sockets is ready for writing
158 ////////////////////////////////////////////////////////////////////
159 inline int Socket_fdset::WaitForWrite(bool zeroFds, PN_uint32 sleep_time)
160 {
161  int retVal = 0;
162  if (sleep_time == 0xffffffff)
163  {
164  retVal = DO_SELECT(_maxid + 1, NULL, &_the_set, NULL, NULL);
165  }
166  else
167  {
168  timeval timeoutValue;
169  timeoutValue.tv_sec = sleep_time / 1000;
170  timeoutValue.tv_usec = (sleep_time % 1000) * 1000;
171 
172  retVal = DO_SELECT(_maxid + 1, NULL, &_the_set, NULL, &timeoutValue);
173  }
174  if (zeroFds)
175  clear();
176 
177  return retVal;
178 }
179 
180 //////////////////////////////////////////////////////////////
181 // Function name : Socket_fdset::WaitForError
182 // Description : This is the function that will wait till
183 // one of the sockets is in error state
184 //////////////////////////////////////////////////////////////
185 inline int Socket_fdset::WaitForError(bool zeroFds, PN_uint32 sleep_time)
186 {
187  int retVal = 0;
188  if (sleep_time == 0xffffffff)
189  {
190  retVal = DO_SELECT(_maxid + 1, NULL, NULL, &_the_set, NULL);
191  }
192  else
193  {
194  timeval timeoutValue;
195  timeoutValue.tv_sec = sleep_time / 1000;
196  timeoutValue.tv_usec = (sleep_time % 1000) * 1000;
197 
198  retVal = DO_SELECT(_maxid + 1, NULL, NULL, &_the_set, &timeoutValue);
199  }
200  if (zeroFds)
201  clear();
202 
203  return retVal;
204 }
205 
206 
207 #endif //__SOCKET_FDSET_H__
Base functionality for a INET domain Socket this call should be the starting point for all other unix...
Definition: socket_ip.h:34
Socket_fdset()
The constructor.
Definition: socket_fdset.h:48
bool IsSetFor(const Socket_IP &incon) const
check to see if a socket object has been marked for reading
Definition: socket_fdset.h:89
int WaitForError(bool zeroFds, PN_uint32 sleep_time=0xffffffff)
This is the function that will wait till one of the sockets is in error state.
Definition: socket_fdset.h:185
void clear()
Marks the content as empty.
Definition: socket_fdset.h:138
SOCKET GetSocket()
Gets the base socket type.
Definition: socket_ip.h:240
int WaitForWrite(bool zeroFds, PN_uint32 sleep_time=0xffffffff)
This is the function that will wait till one of the sockets is ready for writing. ...
Definition: socket_fdset.h:159