2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
2009-01-07 19:37:05 +00:00
|
|
|
* Copyright (C) 2007-2009 by Lothar May *
|
2007-11-16 15:24:25 +00:00
|
|
|
* *
|
|
|
|
|
* 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>
|
2009-06-07 08:51:43 +00:00
|
|
|
#include <net/senderhelper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/serverexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SessionManager::SessionManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionManager::~SessionManager()
|
|
|
|
|
{
|
2009-06-04 22:13:52 +00:00
|
|
|
Clear();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
SessionManager::HasSessions() const
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
return !m_sessionMap.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::AddSession(boost::shared_ptr<SessionData> sessionData)
|
|
|
|
|
{
|
|
|
|
|
AddSession(SessionWrapper(sessionData, boost::shared_ptr<PlayerData>()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::AddSession(SessionWrapper session)
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
SessionMap::iterator pos = m_sessionMap.lower_bound(session.sessionData->GetId());
|
|
|
|
|
|
|
|
|
|
// If pos points to a pair whose key is equivalent to the socket, this handle
|
|
|
|
|
// already exists within the list.
|
|
|
|
|
if (pos != m_sessionMap.end() && session.sessionData->GetId() == pos->first)
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_CONN_EXISTS, 0);
|
|
|
|
|
}
|
|
|
|
|
m_sessionMap.insert(pos, SessionMap::value_type(session.sessionData->GetId(), session));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::SetSessionPlayerData(SessionId session, boost::shared_ptr<PlayerData> playerData)
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
SessionMap::iterator pos = m_sessionMap.find(session);
|
|
|
|
|
|
|
|
|
|
if (pos != m_sessionMap.end())
|
|
|
|
|
pos->second.playerData = playerData;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-26 06:58:49 +00:00
|
|
|
bool
|
2007-11-16 15:24:25 +00:00
|
|
|
SessionManager::RemoveSession(SessionId session)
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2010-09-26 06:58:49 +00:00
|
|
|
return m_sessionMap.erase(session) == 1;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-11 21:41:30 +00:00
|
|
|
SessionWrapper
|
|
|
|
|
SessionManager::GetSessionById(SessionId id) const
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper tmpSession;
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
SessionMap::const_iterator pos = m_sessionMap.find(id);
|
|
|
|
|
if (pos != m_sessionMap.end())
|
|
|
|
|
tmpSession = pos->second;
|
|
|
|
|
return tmpSession;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
SessionWrapper
|
2011-02-08 16:00:43 +00:00
|
|
|
SessionManager::GetSessionByPlayerName(const string &playerName) const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
SessionWrapper tmpSession;
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
if (session_i->second.sessionData->GetState() != SessionData::Init)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
|
|
|
|
if (!tmpPlayer.get())
|
|
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0);
|
|
|
|
|
if (tmpPlayer->GetName() == playerName)
|
|
|
|
|
{
|
|
|
|
|
tmpSession = session_i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return tmpSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SessionWrapper
|
2009-09-27 21:46:44 +00:00
|
|
|
SessionManager::GetSessionByUniquePlayerId(unsigned uniqueId, bool initSessions) const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
SessionWrapper tmpSession;
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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.
|
2009-09-27 21:46:44 +00:00
|
|
|
if (initSessions || session_i->second.sessionData->GetState() != SessionData::Init)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
2010-03-31 13:34:35 +00:00
|
|
|
if (tmpPlayer && tmpPlayer->GetUniqueId() == uniqueId)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
tmpSession = session_i->second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return tmpSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlayerDataList
|
|
|
|
|
SessionManager::GetPlayerDataList() const
|
|
|
|
|
{
|
|
|
|
|
PlayerDataList playerList;
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
|
|
|
|
if (!tmpPlayer.get() || tmpPlayer->GetName().empty())
|
|
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0);
|
|
|
|
|
playerList.push_back(tmpPlayer);
|
|
|
|
|
}
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return playerList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlayerIdList
|
2009-08-15 14:08:48 +00:00
|
|
|
SessionManager::GetPlayerIdList(SessionData::State state) const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
PlayerIdList playerList;
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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.
|
2009-08-15 14:08:48 +00:00
|
|
|
if (session_i->second.sessionData->GetState() == state)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
playerList.push_back(session_i->second.playerData->GetUniqueId());
|
|
|
|
|
}
|
|
|
|
|
++session_i;
|
|
|
|
|
}
|
|
|
|
|
return playerList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
SessionManager::IsPlayerConnected(const string &playerName) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
|
|
SessionWrapper tmpSession = GetSessionByPlayerName(playerName);
|
|
|
|
|
|
2010-07-14 08:17:07 +00:00
|
|
|
if (tmpSession.sessionData && tmpSession.playerData)
|
2007-11-16 15:24:25 +00:00
|
|
|
retVal = true;
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
SessionManager::IsPlayerConnected(unsigned uniqueId) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
|
|
SessionWrapper tmpSession = GetSessionByUniquePlayerId(uniqueId);
|
|
|
|
|
|
2010-07-14 08:17:07 +00:00
|
|
|
if (tmpSession.sessionData && tmpSession.playerData)
|
2007-11-16 15:24:25 +00:00
|
|
|
retVal = true;
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-02 19:50:49 +00:00
|
|
|
bool
|
|
|
|
|
SessionManager::IsClientAddressConnected(const std::string &clientAddress) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
boost::recursive_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->GetClientAddr() == clientAddress)
|
|
|
|
|
{
|
|
|
|
|
retVal = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void
|
|
|
|
|
SessionManager::ForEach(boost::function<void (SessionWrapper)> func)
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2008-03-04 23:47:45 +00:00
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
SessionMap::iterator next = i;
|
|
|
|
|
next++;
|
2008-03-06 15:27:31 +00:00
|
|
|
func((*i).second);
|
2008-03-04 23:47:45 +00:00
|
|
|
i = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
unsigned
|
|
|
|
|
SessionManager::CountReadySessions() const
|
|
|
|
|
{
|
|
|
|
|
unsigned counter = 0;
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::ResetAllReadyFlags()
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
(*i).second.sessionData->ResetReadyFlag();
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SessionManager::Clear()
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2009-06-04 22:13:52 +00:00
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-08-17 10:11:51 +00:00
|
|
|
boost::system::error_code ec;
|
2009-06-04 22:13:52 +00:00
|
|
|
while (i != end)
|
|
|
|
|
{
|
2009-08-17 10:11:51 +00:00
|
|
|
i->second.sessionData->GetAsioSocket()->close(ec);
|
2009-06-04 22:13:52 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
m_sessionMap.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
SessionManager::GetRawSessionCount()
|
|
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
return m_sessionMap.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-07 08:51:43 +00:00
|
|
|
SessionManager::SendToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionData::State state)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if (!i->second.sessionData.get())
|
|
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0);
|
|
|
|
|
|
|
|
|
|
// Send each client (with a certain state) a copy of the packet.
|
|
|
|
|
if (i->second.sessionData->GetState() == state)
|
2009-08-02 15:11:40 +00:00
|
|
|
sender.Send(i->second.sessionData, packet);
|
2007-11-16 15:24:25 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 21:41:30 +00:00
|
|
|
void
|
2009-06-07 08:51:43 +00:00
|
|
|
SessionManager::SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionData::State state)
|
2008-05-11 21:41:30 +00:00
|
|
|
{
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
|
|
|
|
|
|
|
|
|
SessionMap::iterator i = m_sessionMap.begin();
|
|
|
|
|
SessionMap::iterator end = m_sessionMap.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if (!i->second.sessionData.get())
|
|
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0);
|
|
|
|
|
|
|
|
|
|
// Send each client (with a certain state) a copy of the packet.
|
|
|
|
|
if (i->second.sessionData->GetState() == state && i->second.sessionData->WantsLobbyMsg())
|
2009-08-02 15:11:40 +00:00
|
|
|
sender.Send(i->second.sessionData, packet);
|
2008-05-11 21:41:30 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void
|
2009-06-07 08:51:43 +00:00
|
|
|
SessionManager::SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionId except, SessionData::State state)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2008-03-06 15:27:31 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
if (i->second.sessionData->GetState() == state)
|
|
|
|
|
if (i->first != except)
|
2009-08-02 15:11:40 +00:00
|
|
|
sender.Send(i->second.sessionData, packet);
|
2007-11-16 15:24:25 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|