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