Skip to content

Commit

Permalink
Merge branch 'bugfix/socket_selector'
Browse files Browse the repository at this point in the history
  • Loading branch information
eXpl0it3r committed Aug 18, 2014
2 parents 6ad7b21 + 3b9e246 commit b96d330
Showing 1 changed file with 73 additions and 18 deletions.
91 changes: 73 additions & 18 deletions src/SFML/Network/SocketSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <SFML/Network/Socket.hpp>
#include <SFML/Network/SocketImpl.hpp>
#include <SFML/System/Err.hpp>
#include <algorithm>
#include <utility>

#ifdef _MSC_VER
Expand All @@ -41,9 +42,10 @@ namespace sf
////////////////////////////////////////////////////////////
struct SocketSelector::SocketSelectorImpl
{
fd_set AllSockets; ///< Set containing all the sockets handles
fd_set SocketsReady; ///< Set containing handles of the sockets that are ready
int MaxSocket; ///< Maximum socket handle
fd_set allSockets; ///< Set containing all the sockets handles
fd_set socketsReady; ///< Set containing handles of the sockets that are ready
int maxSocket; ///< Maximum socket handle
int socketCount; ///< Number of socket handles
};


Expand Down Expand Up @@ -76,39 +78,77 @@ void SocketSelector::add(Socket& socket)
SocketHandle handle = socket.getHandle();
if (handle != priv::SocketImpl::invalidSocket())
{
if (handle < FD_SETSIZE)
{
FD_SET(handle, &m_impl->AllSockets);

int size = static_cast<int>(handle);
if (size > m_impl->MaxSocket)
m_impl->MaxSocket = size;
#if defined(SFML_SYSTEM_WINDOWS)

if (m_impl->socketCount >= FD_SETSIZE)
{
err() << "The socket can't be added to the selector because the "
<< "selector is full. This is a limitation of your operating "
<< "system's FD_SETSIZE setting.";
return;
}
else

if (FD_ISSET(handle, &m_impl->allSockets))
return;

m_impl->socketCount++;

#else

if (handle >= FD_SETSIZE)
{
err() << "The socket can't be added to the selector because its "
<< "ID is too high. This is a limitation of your operating "
<< "system's FD_SETSIZE setting.";
return;
}

// SocketHandle is an int in POSIX
m_impl->maxSocket = std::max(m_impl->maxSocket, handle);

#endif

FD_SET(handle, &m_impl->allSockets);
}
}


////////////////////////////////////////////////////////////
void SocketSelector::remove(Socket& socket)
{
FD_CLR(socket.getHandle(), &m_impl->AllSockets);
FD_CLR(socket.getHandle(), &m_impl->SocketsReady);
SocketHandle handle = socket.getHandle();
if (handle != priv::SocketImpl::invalidSocket())
{

#if defined(SFML_SYSTEM_WINDOWS)

if (!FD_ISSET(handle, &m_impl->allSockets))
return;

m_impl->socketCount--;

#else

if (handle >= FD_SETSIZE)
return;

#endif

FD_CLR(handle, &m_impl->allSockets);
FD_CLR(handle, &m_impl->socketsReady);
}
}


////////////////////////////////////////////////////////////
void SocketSelector::clear()
{
FD_ZERO(&m_impl->AllSockets);
FD_ZERO(&m_impl->SocketsReady);
FD_ZERO(&m_impl->allSockets);
FD_ZERO(&m_impl->socketsReady);

m_impl->MaxSocket = 0;
m_impl->maxSocket = 0;
m_impl->socketCount = 0;
}


Expand All @@ -121,10 +161,11 @@ bool SocketSelector::wait(Time timeout)
time.tv_usec = static_cast<long>(timeout.asMicroseconds() % 1000000);

// Initialize the set that will contain the sockets that are ready
m_impl->SocketsReady = m_impl->AllSockets;
m_impl->socketsReady = m_impl->allSockets;

// Wait until one of the sockets is ready for reading, or timeout is reached
int count = select(m_impl->MaxSocket + 1, &m_impl->SocketsReady, NULL, NULL, timeout != Time::Zero ? &time : NULL);
// The first parameter is ignored on Windows
int count = select(m_impl->maxSocket + 1, &m_impl->socketsReady, NULL, NULL, timeout != Time::Zero ? &time : NULL);

return count > 0;
}
Expand All @@ -133,7 +174,21 @@ bool SocketSelector::wait(Time timeout)
////////////////////////////////////////////////////////////
bool SocketSelector::isReady(Socket& socket) const
{
return FD_ISSET(socket.getHandle(), &m_impl->SocketsReady) != 0;
SocketHandle handle = socket.getHandle();
if (handle != priv::SocketImpl::invalidSocket())
{

#if !defined(SFML_SYSTEM_WINDOWS)

if (handle >= FD_SETSIZE)
return false;

#endif

return FD_ISSET(handle, &m_impl->socketsReady) != 0;
}

return false;
}


Expand Down

0 comments on commit b96d330

Please sign in to comment.