Selector.cpp

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 
00026 // Headers
00028 #include <SFML/Network/Selector.h>
00029 #include <SFML/Network/Selector.hpp>
00030 #include <SFML/Internal.h>
00031 #include <vector>
00032 
00033 
00034 // WARNING : this structure must always be the SAME as in Network/SocketTCP.h
00035 struct sfSocketTCP
00036 {
00037     sf::SocketTCP This;
00038 };
00039 // WARNING : this structure must always be the SAME as in Network/SocketUDP.h
00040 struct sfSocketUDP
00041 {
00042     sf::SocketUDP This;
00043 };
00044 struct sfSelectorTCP
00045 {
00046     sf::SelectorTCP This;
00047 };
00048 struct sfSelectorUDP
00049 {
00050     sf::SelectorUDP This;
00051 };
00052 
00053 
00054 
00058 sfSelectorTCP* sfSelectorTCP_Create() {return new sfSelectorTCP;}
00059 sfSelectorUDP* sfSelectorUDP_Create() {return new sfSelectorUDP;}
00060 
00061 
00065 void sfSelectorTCP_Destroy(sfSelectorTCP* Selector) {delete Selector;}
00066 void sfSelectorUDP_Destroy(sfSelectorUDP* Selector) {delete Selector;}
00067 
00068 
00072 void sfSelectorTCP_Add(sfSelectorTCP* Selector, sfSocketTCP* Socket) {CSFML_CALL(Selector, Add(Socket->This));}
00073 void sfSelectorUDP_Add(sfSelectorUDP* Selector, sfSocketUDP* Socket) {CSFML_CALL(Selector, Add(Socket->This));}
00074 
00075 
00079 void sfSelectorTCP_Remove(sfSelectorTCP* Selector, sfSocketTCP* Socket) {CSFML_CALL(Selector, Remove(Socket->This));}
00080 void sfSelectorUDP_Remove(sfSelectorUDP* Selector, sfSocketUDP* Socket) {CSFML_CALL(Selector, Remove(Socket->This));}
00081 
00082 
00086 void sfSelectorTCP_Clear(sfSelectorTCP* Selector) {CSFML_CALL(Selector, Clear());}
00087 void sfSelectorUDP_Clear(sfSelectorUDP* Selector) {CSFML_CALL(Selector, Clear());}
00088 
00089 
00095 size_t sfSelectorTCP_GetSocketsReady(sfSelectorTCP* Selector, sfSocketTCP** Sockets, float Timeout)
00096 {
00097     CSFML_CHECK_RETURN(Selector, 0);
00098 
00099     std::vector<sf::SocketTCP> Ready;
00100     Selector->This.GetSocketsReady(Ready, Timeout);
00101 
00102     if (Sockets)
00103     {
00104         for (size_t i = 0; i < Ready.size(); ++i)
00105         {
00106             sfSocketTCP_Destroy(Sockets[i]);
00107             Sockets[i] = sfSocketTCP_Create();
00108             Sockets[i]->This = Ready[i];
00109         }
00110     }
00111 
00112     return Ready.size();
00113 }
00114 size_t sfSelectorUDP_GetSocketsReady(sfSelectorUDP* Selector, sfSocketUDP** Sockets, float Timeout)
00115 {
00116     CSFML_CHECK_RETURN(Selector, 0);
00117 
00118     std::vector<sf::SocketUDP> Ready;
00119     Selector->This.GetSocketsReady(Ready, Timeout);
00120 
00121     if (Sockets)
00122     {
00123         for (size_t i = 0; i < Ready.size(); ++i)
00124         {
00125             sfSocketUDP_Destroy(Sockets[i]);
00126             Sockets[i] = sfSocketUDP_Create();
00127             Sockets[i]->This = Ready[i];
00128         }
00129     }
00130 
00131     return Ready.size();
00132 }