2013-03-04 16:13:08 -05:00
|
|
|
|
|
|
|
// ListenThread.cpp
|
|
|
|
|
|
|
|
// Implements the cListenThread class representing the thread that listens for client connections
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "ListenThread.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
cListenThread::cListenThread(cCallback & a_Callback, cSocket::eFamily a_Family) :
|
2013-03-04 16:13:08 -05:00
|
|
|
super("ListenThread"),
|
2013-03-05 04:53:29 -05:00
|
|
|
m_Callback(a_Callback),
|
|
|
|
m_Family(a_Family)
|
2013-03-04 16:13:08 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cListenThread::~cListenThread()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cListenThread::Initialize(const AString & a_PortsString)
|
|
|
|
{
|
|
|
|
ASSERT(m_Sockets.empty()); // Not yet started
|
|
|
|
|
|
|
|
if (!CreateSockets(a_PortsString))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cListenThread::Start(void)
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
if (m_Sockets.empty())
|
|
|
|
{
|
|
|
|
// There are no sockets listening, either forgotten to initialize or the user specified no listening ports
|
|
|
|
// Report as successful, though
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-04 16:13:08 -05:00
|
|
|
return super::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cListenThread::Stop(void)
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
if (m_Sockets.empty())
|
|
|
|
{
|
|
|
|
// No sockets means no thread was running in the first place
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
m_ShouldTerminate = true;
|
|
|
|
|
|
|
|
// Close one socket to wake the thread up from the select() call
|
|
|
|
m_Sockets[0].CloseSocket();
|
|
|
|
|
|
|
|
// Wait for the thread to finish
|
|
|
|
super::Wait();
|
|
|
|
|
|
|
|
// Clean up all sockets
|
|
|
|
m_Sockets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cListenThread::SetReuseAddr(bool a_Reuse)
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
ASSERT(m_Sockets.empty()); // Must not have been Initialize()d yet
|
2013-03-04 16:13:08 -05:00
|
|
|
|
|
|
|
m_ShouldReuseAddr = a_Reuse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cListenThread::CreateSockets(const AString & a_PortsString)
|
|
|
|
{
|
|
|
|
AStringVector Ports = StringSplit(a_PortsString, ",");
|
|
|
|
|
|
|
|
if (Ports.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
const char * FamilyStr = "";
|
|
|
|
switch (m_Family)
|
|
|
|
{
|
|
|
|
case cSocket::IPv4: FamilyStr = "IPv4: "; break;
|
|
|
|
case cSocket::IPv6: FamilyStr = "IPv6: "; break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unknown address family");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
for (AStringVector::const_iterator itr = Ports.begin(), end = Ports.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
int Port = atoi(Trim(*itr).c_str());
|
|
|
|
if ((Port <= 0) || (Port > 65535))
|
|
|
|
{
|
|
|
|
LOGWARNING("Invalid port specified: \"%s\".", Trim(*itr).c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-05 04:53:29 -05:00
|
|
|
m_Sockets.push_back(cSocket::CreateSocket(m_Family));
|
2013-03-04 16:13:08 -05:00
|
|
|
if (!m_Sockets.back().IsValid())
|
|
|
|
{
|
|
|
|
LOGERROR("Cannot create listening socket for port %d: \"%s\"", Port, cSocket::GetLastErrorString().c_str());
|
|
|
|
m_Sockets.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ShouldReuseAddr)
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
if (!m_Sockets.back().SetReuseAddress())
|
2013-03-04 16:13:08 -05:00
|
|
|
{
|
|
|
|
LOG("Port %d cannot reuse addr, syscall failed: \"%s\".", Port, cSocket::GetLastErrorString().c_str());
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 04:53:29 -05:00
|
|
|
|
|
|
|
// Bind to port:
|
|
|
|
bool res = false;
|
|
|
|
switch (m_Family)
|
|
|
|
{
|
|
|
|
case cSocket::IPv4: res = m_Sockets.back().BindToAnyIPv4(Port); break;
|
|
|
|
case cSocket::IPv6: res = m_Sockets.back().BindToAnyIPv6(Port); break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unknown address family");
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
LOGWARNING("Cannot bind port %d: \"%s\".", Port, cSocket::GetLastErrorString().c_str());
|
|
|
|
m_Sockets.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Sockets.back().Listen())
|
|
|
|
{
|
|
|
|
LOGWARNING("Cannot listen on port %d: \"%s\".", Port, cSocket::GetLastErrorString().c_str());
|
|
|
|
m_Sockets.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-05 06:43:14 -05:00
|
|
|
LOGINFO("%sPort %d is open for connections", FamilyStr, Port);
|
2013-03-04 16:13:08 -05:00
|
|
|
} // for itr - Ports[]
|
|
|
|
|
|
|
|
return !(m_Sockets.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cListenThread::Execute(void)
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
if (m_Sockets.empty())
|
|
|
|
{
|
|
|
|
LOGD("Empty cListenThread, ending thread now.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
// Find the highest socket number:
|
|
|
|
cSocket::xSocket Highest = m_Sockets[0].GetSocket();
|
|
|
|
for (cSockets::iterator itr = m_Sockets.begin(), end = m_Sockets.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
if (itr->GetSocket() > Highest)
|
|
|
|
{
|
|
|
|
Highest = itr->GetSocket();
|
|
|
|
}
|
|
|
|
} // for itr - m_Sockets[]
|
|
|
|
|
|
|
|
while (!m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
// Put all sockets into a FD set:
|
|
|
|
fd_set fdRead;
|
|
|
|
FD_ZERO(&fdRead);
|
|
|
|
for (cSockets::iterator itr = m_Sockets.begin(), end = m_Sockets.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
FD_SET(itr->GetSocket(), &fdRead);
|
|
|
|
} // for itr - m_Sockets[]
|
|
|
|
|
|
|
|
if (select(Highest + 1, &fdRead, NULL, NULL, NULL) == -1)
|
|
|
|
{
|
|
|
|
LOG("select(R) call failed in cListenThread: \"%s\"", cSocket::GetLastErrorString().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (cSockets::iterator itr = m_Sockets.begin(), end = m_Sockets.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
if (FD_ISSET(itr->GetSocket(), &fdRead))
|
|
|
|
{
|
|
|
|
cSocket Client = itr->Accept();
|
|
|
|
m_Callback.OnConnectionAccepted(Client);
|
|
|
|
}
|
|
|
|
} // for itr - m_Sockets[]
|
|
|
|
} // while (!m_ShouldTerminate)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|