D:/Programmation/Cpp/SFML/src/SFML/Network/Selector.inl

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 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 
00029 template <typename SocketType>
00030 sfSelector<SocketType>::sfSelector() :
00031 myMaxSocket(0)
00032 {
00033     Clear();
00034 }
00035 
00036 
00040 template <typename SocketType>
00041 void sfSelector<SocketType>::Add(SocketType Socket)
00042 {
00043     FD_SET(Socket.mySocket, &mySet);
00044 
00045     if (static_cast<int>(Socket.mySocket) > myMaxSocket)
00046         myMaxSocket = static_cast<int>(Socket.mySocket);
00047 }
00048 
00049 
00053 template <typename SocketType>
00054 void sfSelector<SocketType>::Remove(SocketType Socket)
00055 {
00056     FD_CLR(Socket.mySocket, &mySet);
00057 }
00058 
00059 
00063 template <typename SocketType>
00064 void sfSelector<SocketType>::Clear()
00065 {
00066     FD_ZERO(&mySet);
00067 
00068     myMaxSocket = 0;
00069 }
00070 
00071 
00075 template <typename SocketType>
00076 bool sfSelector<SocketType>::GetSocketsReady(std::vector<SocketType>& Sockets, unsigned int Timeout)
00077 {
00078     // First of all, clear the array to fill...
00079     Sockets.clear();
00080 
00081     // Setup the timeout structure
00082     timeval Time;
00083     Time.tv_sec  = Timeout / 1000;
00084     Time.tv_usec = (Timeout % 1000) * 1000;
00085 
00086     // Use a copy of the set, as it will be modified by select()
00087     fd_set Set = mySet;
00088 
00089     // Wait until one of the sockets is ready for reading, or timeout is reached
00090     if (select(myMaxSocket + 1, &Set, NULL, NULL, Timeout ? &Time : NULL) != 0)
00091     {
00092         // One or more sockets are ready : put them into the array
00093         for (int i = 0; i < myMaxSocket + 1; ++i)
00094         {
00095             if (FD_ISSET(i, &Set))
00096                 Sockets.push_back(SocketType(static_cast<sf_private::sfSocketType>(i)));
00097         }
00098 
00099         return true;
00100     }
00101     else
00102     {
00103         // Timeout reached...
00104         return false;
00105     }
00106 }

Generated for SFML by  doxygen 1.5.2