SelectorBase.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifdef _MSC_VER
26  #pragma warning(disable : 4127) // "conditional expression is constant" generated by the FD_SET macro
27 #endif
28 
30 // Headers
32 #include <SFML/Network/SelectorBase.hpp>
33 
34 
35 namespace sf
36 {
41 myMaxSocket(0)
42 {
43  Clear();
44 }
45 
46 
50 void SelectorBase::Add(SocketHelper::SocketType Socket)
51 {
52  FD_SET(Socket, &mySet);
53 
54  int Size = static_cast<int>(Socket);
55  if (Size > myMaxSocket)
56  myMaxSocket = Size;
57 }
58 
59 
63 void SelectorBase::Remove(SocketHelper::SocketType Socket)
64 {
65  FD_CLR(Socket, &mySet);
66 }
67 
68 
73 {
74  FD_ZERO(&mySet);
75  FD_ZERO(&mySetReady);
76 
77  myMaxSocket = 0;
78 }
79 
80 
86 unsigned int SelectorBase::Wait(float Timeout)
87 {
88  // Setup the timeout structure
89  timeval Time;
90  Time.tv_sec = static_cast<long>(Timeout);
91  Time.tv_usec = (static_cast<long>(Timeout * 1000) % 1000) * 1000;
92 
93  // Prepare the set of sockets to return
94  mySetReady = mySet;
95 
96  // Wait until one of the sockets is ready for reading, or timeout is reached
97  int NbSockets = select(myMaxSocket + 1, &mySetReady, NULL, NULL, Timeout > 0 ? &Time : NULL);
98 
99  return NbSockets >= 0 ? static_cast<unsigned int>(NbSockets) : 0;
100 }
101 
102 
108 SocketHelper::SocketType SelectorBase::GetSocketReady(unsigned int Index)
109 {
110  // The standard FD_xxx interface doesn't define a direct access,
111  // so we must go through the whole set to find the socket we're looking for
112  for (int i = 0; i < myMaxSocket + 1; ++i)
113  {
114  if (FD_ISSET(i, &mySetReady))
115  {
116  // Current socket is ready, but is it the Index-th one ?
117  if (Index > 0)
118  {
119  Index--;
120  }
121  else
122  {
123  return static_cast<SocketHelper::SocketType>(i);
124  }
125  }
126  }
127 
128  // Invalid index : return an invalid socket
130 }
131 
132 } // namespace sf
unsigned int Wait(float Timeout=0.f)
Wait and collect sockets which are ready for reading.
void Remove(SocketHelper::SocketType Socket)
Remove a socket.
void Add(SocketHelper::SocketType Socket)
Add a socket to watch.
SelectorBase()
Default constructor.
void Clear()
Remove all sockets.
static SocketType InvalidSocket()
Return the value of the invalid socket.
SocketHelper::SocketType GetSocketReady(unsigned int Index)
After a call to Wait(), get the Index-th socket which is ready for reading.