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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
/* Thread safe server session manager. */
|
|
|
|
|
|
|
|
|
|
#ifndef _SESSIONMANAGER_H_
|
|
|
|
|
#define _SESSIONMANAGER_H_
|
|
|
|
|
|
2009-05-07 22:36:51 +00:00
|
|
|
#include <boost/function.hpp>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/sessiondata.h>
|
|
|
|
|
#include <playerdata.h>
|
|
|
|
|
#include <core/thread.h>
|
|
|
|
|
|
|
|
|
|
class NetPacket;
|
2009-06-07 08:51:43 +00:00
|
|
|
class SenderHelper;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
struct SessionWrapper
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper() {}
|
|
|
|
|
SessionWrapper(boost::shared_ptr<SessionData> s, boost::shared_ptr<PlayerData> p)
|
|
|
|
|
: sessionData(s), playerData(p) {}
|
|
|
|
|
boost::shared_ptr<SessionData> sessionData;
|
|
|
|
|
boost::shared_ptr<PlayerData> playerData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SessionManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SessionManager();
|
|
|
|
|
virtual ~SessionManager();
|
|
|
|
|
|
|
|
|
|
bool HasSessions() const;
|
|
|
|
|
|
|
|
|
|
void AddSession(boost::shared_ptr<SessionData> sessionData); // new Sessions without player data
|
|
|
|
|
void AddSession(SessionWrapper session);
|
|
|
|
|
void SetSessionPlayerData(SessionId session, boost::shared_ptr<PlayerData> playerData);
|
2010-09-26 06:58:49 +00:00
|
|
|
bool RemoveSession(SessionId session);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-05-11 21:41:30 +00:00
|
|
|
SessionWrapper GetSessionById(SessionId id) const;
|
2011-02-08 16:00:43 +00:00
|
|
|
SessionWrapper GetSessionByPlayerName(const std::string &playerName) const;
|
2009-09-27 21:46:44 +00:00
|
|
|
SessionWrapper GetSessionByUniquePlayerId(unsigned uniqueId, bool initSessions = false) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
PlayerDataList GetPlayerDataList() const;
|
2009-08-15 14:08:48 +00:00
|
|
|
PlayerIdList GetPlayerIdList(SessionData::State state) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
bool IsPlayerConnected(const std::string &playerName) const;
|
|
|
|
|
bool IsPlayerConnected(unsigned uniqueId) const;
|
2010-10-02 19:50:49 +00:00
|
|
|
bool IsClientAddressConnected(const std::string &clientAddress) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void ForEach(boost::function<void (SessionWrapper)> func);
|
|
|
|
|
|
|
|
|
|
unsigned CountReadySessions() const;
|
|
|
|
|
void ResetAllReadyFlags();
|
|
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
unsigned GetRawSessionCount();
|
|
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
void SendToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionData::State state);
|
|
|
|
|
void SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionData::State state);
|
|
|
|
|
void SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionId except, SessionData::State state);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
typedef std::map<SessionId, SessionWrapper> SessionMap;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
SessionMap m_sessionMap;
|
2008-03-06 15:27:31 +00:00
|
|
|
mutable boost::recursive_mutex m_sessionMapMutex;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|