SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

SelectorBase.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00025 #ifdef _MSC_VER
00026     #pragma warning(disable : 4127) // "conditional expression is constant" generated by the FD_SET macro
00027 #endif
00028 
00030 // Headers
00032 #include <SFML/Network/SelectorBase.hpp>
00033 
00034 
00035 namespace sf
00036 {
00040 SelectorBase::SelectorBase() :
00041 myMaxSocket(0)
00042 {
00043     Clear();
00044 }
00045 
00046 
00050 void SelectorBase::Add(SocketHelper::SocketType socket)
00051 {
00052     FD_SET(socket, &mySet);
00053 
00054     int size = static_cast<int>(socket);
00055     if (size > myMaxSocket)
00056         myMaxSocket = size;
00057 }
00058 
00059 
00063 void SelectorBase::Remove(SocketHelper::SocketType socket)
00064 {
00065     FD_CLR(socket, &mySet);
00066 }
00067 
00068 
00072 void SelectorBase::Clear()
00073 {
00074     FD_ZERO(&mySet);
00075     FD_ZERO(&mySetReady);
00076 
00077     myMaxSocket = 0;
00078 }
00079 
00080 
00086 unsigned int SelectorBase::Wait(float timeout)
00087 {
00088     // Setup the timeout structure
00089     timeval time;
00090     time.tv_sec  = static_cast<long>(timeout);
00091     time.tv_usec = (static_cast<long>(timeout * 1000) % 1000) * 1000;
00092 
00093     // Prepare the set of sockets to return
00094     mySetReady = mySet;
00095 
00096     // Wait until one of the sockets is ready for reading, or timeout is reached
00097     int nbSockets = select(myMaxSocket + 1, &mySetReady, NULL, NULL, timeout > 0 ? &time : NULL);
00098 
00099     return nbSockets >= 0 ? static_cast<unsigned int>(nbSockets) : 0;
00100 }
00101 
00102 
00108 SocketHelper::SocketType SelectorBase::GetSocketReady(unsigned int index) const
00109 {
00110     // We have to drop the const for FD_ISSET
00111     fd_set* set = const_cast<fd_set*>(&mySetReady);
00112 
00113     // The standard FD_xxx interface doesn't define a direct access,
00114     // so we must iterate through the whole set to find the socket that we're looking for
00115     for (int i = 0; i < myMaxSocket + 1; ++i)
00116     {
00117         if (FD_ISSET(i, set))
00118         {
00119             // Current socket is ready, but is it the index-th one ?
00120             if (index > 0)
00121             {
00122                 index--;
00123             }
00124             else
00125             {
00126                 return static_cast<SocketHelper::SocketType>(i);
00127             }
00128         }
00129     }
00130 
00131     // Invalid index : return an invalid socket
00132     return SocketHelper::InvalidSocket();
00133 }
00134 
00135 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::