2007-08-15 13:28:28 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/sessionmanager.h>
|
|
|
|
|
#include <net/senderthread.h>
|
|
|
|
|
#include <net/serverexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SessionManager::SessionManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionManager::~SessionManager()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-21 17:27:13 +00:00
|
|
|
bool
|
|
|
|
|
SessionManager::HasSessions() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
return !m_sessionMap.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void
|
|
|
|
|
SessionManager::AddSession(boost::shared_ptr<SessionData> sessionData)
|
2007-08-20 15:30:22 +00:00
|
|
|
{
|
|
|
|
|
AddSession(SessionWrapper(sessionData, boost::shared_ptr<PlayerData>()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::AddSession(SessionWrapper session)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
2007-08-20 15:30:22 +00:00
|
|
|
SessionMap::iterator pos = m_sessionMap.lower_bound(session.sessionData->GetSocket());
|
2007-08-15 13:28:28 +00:00
|
|
|
|
|
|
|
|
// If pos points to a pair whose key is equivalent to the socket, this handle
|
|
|
|
|
// already exists within the list.
|
2007-08-20 15:30:22 +00:00
|
|
|
if (pos != m_sessionMap.end() && session.sessionData->GetSocket() == pos->first)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_CONN_EXISTS, 0);
|
|
|
|
|
}
|
2007-08-20 15:30:22 +00:00
|
|
|
m_sessionMap.insert(pos, SessionMap::value_type(session.sessionData->GetSocket(), session));
|
2007-08-15 13:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::SetSessionPlayerData(SOCKET session, boost::shared_ptr<PlayerData> playerData)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
SessionMap::iterator pos = m_sessionMap.find(session);
|
|
|
|
|
|
|
|
|
|
if (pos != m_sessionMap.end())
|
|
|
|
|
pos->second.playerData = playerData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::RemoveSession(SOCKET session)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
m_sessionMap.erase(session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionWrapper
|
|
|
|
|
SessionManager::Select(unsigned timeoutMsec)
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper retSession;
|
|
|
|
|
SOCKET maxSock = INVALID_SOCKET;
|
|
|
|
|
fd_set rdset;
|
|
|
|
|
FD_ZERO(&rdset);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
// Collect all sockets.
|
2007-08-15 13:28:28 +00:00
|
|
|
SOCKET tmpSock = i->first;
|
|
|
|
|
FD_SET(tmpSock, &rdset);
|
|
|
|
|
if (tmpSock > maxSock || maxSock == INVALID_SOCKET)
|
|
|
|
|
maxSock = tmpSock;
|
2007-09-03 16:35:45 +00:00
|
|
|
|
|
|
|
|
// Check if a packet is available.
|
|
|
|
|
if (!i->second.sessionData->GetReceiveBuffer().receivedPackets.empty())
|
|
|
|
|
{
|
|
|
|
|
retSession = i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-08-15 13:28:28 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-03 16:35:45 +00:00
|
|
|
if (!retSession.sessionData.get())
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
if (maxSock == INVALID_SOCKET)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
Thread::Msleep(timeoutMsec); // just sleep if there is no session
|
2007-08-15 13:28:28 +00:00
|
|
|
}
|
2007-09-03 16:35:45 +00:00
|
|
|
else
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
// wait for data
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
timeout.tv_sec = timeoutMsec / 1000;
|
|
|
|
|
timeout.tv_usec = (timeoutMsec % 1000) * 1000;
|
|
|
|
|
int selectResult = select(maxSock + 1, &rdset, NULL, NULL, &timeout);
|
|
|
|
|
if (!IS_VALID_SELECT(selectResult))
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
throw ServerException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
|
|
|
|
|
}
|
|
|
|
|
if (selectResult > 0) // one (or more) of the sockets is readable
|
|
|
|
|
{
|
|
|
|
|
// Check which socket is readable, return the first.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
2007-09-03 16:35:45 +00:00
|
|
|
if (FD_ISSET(i->first, &rdset))
|
|
|
|
|
{
|
|
|
|
|
retSession = i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
2007-08-15 13:28:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionWrapper
|
|
|
|
|
SessionManager::GetSessionByPlayerName(const string playerName) const
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper tmpSession;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::const_iterator session_end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (session_i != session_end)
|
|
|
|
|
{
|
|
|
|
|
// Check all players which are fully connected.
|
2007-08-21 13:18:18 +00:00
|
|
|
if (session_i->second.sessionData->GetState() != SessionData::Init)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
|
|
|
|
assert(tmpPlayer.get());
|
|
|
|
|
if (tmpPlayer->GetName() == playerName)
|
|
|
|
|
{
|
|
|
|
|
tmpSession = session_i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return tmpSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionWrapper
|
|
|
|
|
SessionManager::GetSessionByUniquePlayerId(unsigned uniqueId) const
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper tmpSession;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::const_iterator session_end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (session_i != session_end)
|
|
|
|
|
{
|
|
|
|
|
// Check all players which are fully connected.
|
2007-08-21 13:18:18 +00:00
|
|
|
if (session_i->second.sessionData->GetState() != SessionData::Init)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
|
|
|
|
assert(tmpPlayer.get());
|
|
|
|
|
if (tmpPlayer->GetUniqueId() == uniqueId)
|
|
|
|
|
{
|
|
|
|
|
tmpSession = session_i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return tmpSession;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 15:30:22 +00:00
|
|
|
PlayerDataList
|
|
|
|
|
SessionManager::GetPlayerDataList() const
|
|
|
|
|
{
|
|
|
|
|
PlayerDataList playerList;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::const_iterator session_end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (session_i != session_end)
|
|
|
|
|
{
|
2007-08-21 13:18:18 +00:00
|
|
|
// Get all players in the game.
|
|
|
|
|
if (session_i->second.sessionData->GetState() == SessionData::Game)
|
2007-08-20 15:30:22 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
|
|
|
|
assert(tmpPlayer.get());
|
|
|
|
|
assert(!tmpPlayer->GetName().empty());
|
|
|
|
|
playerList.push_back(tmpPlayer);
|
|
|
|
|
}
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return playerList;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-01 22:59:55 +00:00
|
|
|
PlayerIdList
|
|
|
|
|
SessionManager::GetPlayerIdList() const
|
|
|
|
|
{
|
|
|
|
|
PlayerIdList playerList;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::const_iterator session_end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (session_i != session_end)
|
|
|
|
|
{
|
|
|
|
|
// Get all players in the game.
|
|
|
|
|
if (session_i->second.sessionData->GetState() == SessionData::Game)
|
|
|
|
|
{
|
|
|
|
|
playerList.push_back(session_i->second.playerData->GetUniqueId());
|
|
|
|
|
}
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return playerList;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 15:30:22 +00:00
|
|
|
bool
|
|
|
|
|
SessionManager::IsPlayerConnected(const string &playerName) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
|
|
SessionWrapper tmpSession = GetSessionByPlayerName(playerName);
|
|
|
|
|
|
|
|
|
|
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
|
|
|
|
retVal = true;
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
SessionManager::IsPlayerConnected(unsigned uniqueId) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
|
|
SessionWrapper tmpSession = GetSessionByUniquePlayerId(uniqueId);
|
|
|
|
|
|
|
|
|
|
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
|
|
|
|
retVal = true;
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-29 14:05:41 +00:00
|
|
|
void
|
|
|
|
|
SessionManager::ForEach(boost::function<void (SessionWrapper)> func)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
func((*i).second);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-07 21:23:37 +00:00
|
|
|
unsigned
|
|
|
|
|
SessionManager::CountReadySessions() const
|
|
|
|
|
{
|
|
|
|
|
unsigned counter = 0;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::const_iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::const_iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if ((*i).second.sessionData->IsReady())
|
|
|
|
|
++counter;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return counter;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void
|
|
|
|
|
SessionManager::Clear()
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
// Sockets will be closed automatically.
|
|
|
|
|
m_sessionMap.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
SessionManager::GetRawSessionCount()
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
return m_sessionMap.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-21 13:18:18 +00:00
|
|
|
SessionManager::SendToAllSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet, SessionData::State state)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
assert(i->second.sessionData.get());
|
|
|
|
|
|
2007-08-21 13:18:18 +00:00
|
|
|
// Send each client (with a certain state) a copy of the packet.
|
|
|
|
|
if (i->second.sessionData->GetState() == state)
|
2007-08-15 13:28:28 +00:00
|
|
|
sender.Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-21 13:18:18 +00:00
|
|
|
SessionManager::SendToAllButOneSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet, SOCKET except, SessionData::State state)
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
// Send each fully connected client but one a copy of the packet.
|
2007-08-21 13:18:18 +00:00
|
|
|
if (i->second.sessionData->GetState() == state)
|
2007-08-15 13:28:28 +00:00
|
|
|
if (i->first != except)
|
|
|
|
|
sender.Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|