2004-08-16 17:19:17 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-02 05:56:24 +02:00
|
|
|
#include "Session.h"
|
|
|
|
#include "Network.h"
|
|
|
|
#include "ps/CLogger.h"
|
2004-08-16 17:19:17 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void CSessionManager::Register(CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CScopeLock scopeLock(m_Mutex);
|
2005-02-21 18:13:31 +01:00
|
|
|
SessionMap::iterator it;
|
|
|
|
|
|
|
|
// The total number of references to this session should be 1 and only 1
|
|
|
|
// Either there's one in m_Sessions or there's one in m_AddQueue. There
|
|
|
|
// shall be no references in the remove queue.
|
|
|
|
// m_Sessions is Read-Only
|
|
|
|
|
|
|
|
if ((it = m_RemoveQueue.find(pSession)) != m_RemoveQueue.end())
|
|
|
|
m_RemoveQueue.erase(it);
|
|
|
|
if (m_Sessions.find(pSession) == m_Sessions.end())
|
|
|
|
m_AddQueue[pSession]=pSession;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
void CSessionManager::Deregister(CNetSession *pSession)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
|
|
|
CScopeLock scopeLock(m_Mutex);
|
2005-02-21 18:13:31 +01:00
|
|
|
SessionMap::iterator it;
|
|
|
|
|
|
|
|
// The total number of references to this session should be 2 or 0:
|
|
|
|
// One in m_Sessions and one in the remove queue or 0 in both. None in
|
|
|
|
// m_AddQueue.
|
|
|
|
// m_Sessions is Read-Only
|
|
|
|
|
|
|
|
if ((it = m_AddQueue.find(pSession)) != m_AddQueue.end())
|
|
|
|
m_AddQueue.erase(it);
|
|
|
|
if (m_Sessions.find(pSession) != m_Sessions.end())
|
|
|
|
m_RemoveQueue.insert(make_pair(pSession, pSession));
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSessionManager::Poll()
|
|
|
|
{
|
|
|
|
m_Mutex.Lock();
|
2005-02-21 18:13:31 +01:00
|
|
|
SessionMap::iterator it;
|
|
|
|
|
|
|
|
// De/Register should've made sure that it doesn't matter which order we
|
|
|
|
// process the two queues.
|
|
|
|
for (it=m_AddQueue.begin();it != m_AddQueue.end();++it)
|
|
|
|
{
|
|
|
|
m_Sessions.insert(*it);
|
|
|
|
}
|
|
|
|
m_AddQueue.clear();
|
|
|
|
for (it=m_RemoveQueue.begin();it != m_RemoveQueue.end();++it)
|
|
|
|
{
|
|
|
|
m_Sessions.erase(it->second);
|
|
|
|
}
|
|
|
|
m_RemoveQueue.clear();
|
|
|
|
|
|
|
|
it=m_Sessions.begin();
|
2004-08-16 17:19:17 +02:00
|
|
|
while (it != m_Sessions.end())
|
|
|
|
{
|
|
|
|
CNetMessage *pMsg;
|
2005-02-21 18:13:31 +01:00
|
|
|
|
|
|
|
// Extraneous? well, sessions are perfectly able to delete other
|
|
|
|
// sessions (or cause them to be deleted) in their message handler
|
|
|
|
if (m_RemoveQueue.find(it->second) != m_RemoveQueue.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((pMsg = it->second->TryPop()) != NULL)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
|
|
|
CNetSession *pSess=it->second;
|
|
|
|
m_Mutex.Unlock();
|
|
|
|
if (!pSess->HandleMessage(pMsg))
|
|
|
|
{
|
2004-09-21 16:40:43 +02:00
|
|
|
LOG(WARNING, LOG_CAT_NET, "CSessionManager::Poll(): Unhandled message %s.", pMsg->GetString().c_str());
|
2004-08-16 17:19:17 +02:00
|
|
|
delete pMsg;
|
|
|
|
}
|
|
|
|
m_Mutex.Lock();
|
2005-02-21 18:13:31 +01:00
|
|
|
|
|
|
|
if (m_RemoveQueue.find(it->second) != m_RemoveQueue.end())
|
|
|
|
break;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
m_Mutex.Unlock();
|
|
|
|
}
|