mirror of
https://github.com/HikikoMarmy/Champions-Reborn-Server.git
synced 2026-04-05 08:59:54 -03:00
Realm and User management
This commit is contained in:
@@ -1,29 +1,39 @@
|
||||
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "../Lobby Server/Event/NotifyClientReqConnect.h"
|
||||
#include "../Lobby Server/Event/NotifyGameDiscovered.h"
|
||||
|
||||
GameSession::GameSession()
|
||||
{
|
||||
m_owner.reset();
|
||||
|
||||
m_gameIndex = 0;
|
||||
m_type = GameType::Public;
|
||||
m_state = GameState::NotReady;
|
||||
m_minimumLevel = 0;
|
||||
m_maximumLevel = 0;
|
||||
m_currentPlayers = 0;
|
||||
m_maximumPlayers = 0;
|
||||
|
||||
m_gameLocation.clear();
|
||||
m_gameName.clear();
|
||||
m_ownerName.clear();
|
||||
m_gameData.clear();
|
||||
m_description.clear();
|
||||
}
|
||||
|
||||
GameSession::~GameSession()
|
||||
{
|
||||
m_owner.reset();
|
||||
|
||||
}
|
||||
m_gameIndex = 0;
|
||||
m_type = GameType::Public;
|
||||
m_state = GameState::NotReady;
|
||||
m_minimumLevel = 0;
|
||||
m_maximumLevel = 0;
|
||||
m_currentPlayers = 0;
|
||||
m_maximumPlayers = 0;
|
||||
|
||||
sptr_user GameSession::GetHost()
|
||||
{
|
||||
return m_userList[ 0 ];
|
||||
m_gameLocation.clear();
|
||||
m_gameName.clear();
|
||||
m_ownerName.clear();
|
||||
m_gameData.clear();
|
||||
m_description.clear();
|
||||
}
|
||||
@@ -5,11 +5,6 @@ public:
|
||||
GameSession();
|
||||
~GameSession();
|
||||
|
||||
sptr_user GetHost();
|
||||
|
||||
bool JoinUser( sptr_user user );
|
||||
|
||||
public:
|
||||
enum class GameType
|
||||
{
|
||||
Public,
|
||||
@@ -19,23 +14,23 @@ public:
|
||||
enum class GameState
|
||||
{
|
||||
NotReady,
|
||||
Open,
|
||||
Started
|
||||
Open
|
||||
} m_state;
|
||||
|
||||
std::weak_ptr< RealmUser > m_owner;
|
||||
|
||||
int32_t m_gameIndex;
|
||||
|
||||
std::wstring m_gameLocation;
|
||||
std::wstring m_gameName;
|
||||
std::wstring m_ownerName;
|
||||
|
||||
std::string m_gameData;
|
||||
|
||||
int32_t m_maximumPlayers;
|
||||
std::string m_description;
|
||||
int8_t m_currentPlayers;
|
||||
int8_t m_maximumPlayers;
|
||||
int32_t m_minimumLevel;
|
||||
int32_t m_maximumLevel;
|
||||
|
||||
// User Information
|
||||
std::vector< sptr_user > m_userList;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GameSession > sptr_game_session;
|
||||
@@ -15,26 +15,33 @@ GameSessionManager::~GameSessionManager()
|
||||
{
|
||||
}
|
||||
|
||||
void GameSessionManager::Process()
|
||||
void GameSessionManager::OnDisconnectUser( sptr_user user )
|
||||
{
|
||||
for( auto &gameSession : m_gameSessionList)
|
||||
if( !user || user->m_gameId < 0 )
|
||||
return;
|
||||
|
||||
const auto gameId = user->m_gameId;
|
||||
|
||||
auto session = FindGame( gameId );
|
||||
if( !session )
|
||||
return;
|
||||
|
||||
auto owner = session->m_owner.lock();
|
||||
if( !owner )
|
||||
{
|
||||
ProcessGameSession( gameSession );
|
||||
Log::Error( "Game session owner not found! [%d]", gameId );
|
||||
ForceTerminateGame( gameId );
|
||||
return;
|
||||
}
|
||||
|
||||
if( owner->m_sessionId == user->m_sessionId )
|
||||
{
|
||||
Log::Info( "Game session owner disconnected! [%d]", gameId );
|
||||
ForceTerminateGame( gameId );
|
||||
}
|
||||
}
|
||||
|
||||
void GameSessionManager::ProcessGameSession( sptr_game_session gameSession )
|
||||
{
|
||||
for( auto &member : gameSession->m_userList )
|
||||
{
|
||||
if( member == nullptr )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
|
||||
bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName )
|
||||
{
|
||||
auto new_session = std::make_shared< GameSession >();
|
||||
|
||||
@@ -42,20 +49,18 @@ bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring
|
||||
new_session->m_gameIndex = m_gameIndex;
|
||||
new_session->m_gameLocation = L"Kelethin";
|
||||
new_session->m_gameName = gameName;
|
||||
new_session->m_minimumLevel = minimumLevel;
|
||||
new_session->m_maximumLevel = maximumLevel;
|
||||
new_session->m_minimumLevel = 1;
|
||||
new_session->m_maximumLevel = 9999;
|
||||
|
||||
new_session->m_gameData.resize(256);
|
||||
new_session->m_gameData.resize( 256 );
|
||||
|
||||
owner->is_host = true;
|
||||
owner->is_ready = false;
|
||||
owner->discovery_addr = "";
|
||||
owner->discovery_port = 0;
|
||||
owner->discovery_state = DiscoveryState::Initial_Connect;
|
||||
owner->m_isHost = true;
|
||||
owner->m_discoveryAddr = "";
|
||||
|
||||
owner->game_id = m_gameIndex;
|
||||
new_session->m_userList.push_back(owner);
|
||||
owner->m_gameId = m_gameIndex;
|
||||
new_session->m_owner = owner;
|
||||
|
||||
std::lock_guard< std::mutex > lock( m_dataMutex );
|
||||
m_gameSessionList.push_back( new_session );
|
||||
|
||||
m_gameIndex++;
|
||||
@@ -63,14 +68,14 @@ bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
|
||||
bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring gameName )
|
||||
{
|
||||
// Check if the game name or host session id is already in use
|
||||
for( auto &gameSession : m_gameSessionList )
|
||||
{
|
||||
if( gameSession->m_type != GameSession::GameType::Private )
|
||||
continue;
|
||||
|
||||
|
||||
if( gameSession->m_gameName == gameName )
|
||||
{
|
||||
Log::Error( "Game name is already in use! [%S]", gameName.c_str() );
|
||||
@@ -84,20 +89,18 @@ bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring
|
||||
new_session->m_gameIndex = m_gameIndex;
|
||||
new_session->m_gameLocation = L"Kelethin";
|
||||
new_session->m_gameName = gameName;
|
||||
new_session->m_minimumLevel = minimumLevel;
|
||||
new_session->m_maximumLevel = maximumLevel;
|
||||
new_session->m_minimumLevel = 1;
|
||||
new_session->m_maximumLevel = 9999;
|
||||
|
||||
new_session->m_gameData.resize( 256 );
|
||||
|
||||
owner->is_host = true;
|
||||
owner->is_ready = false;
|
||||
owner->discovery_addr = "";
|
||||
owner->discovery_port = 0;
|
||||
owner->discovery_state = DiscoveryState::Initial_Connect;
|
||||
owner->m_isHost = true;
|
||||
owner->m_discoveryAddr = "";
|
||||
|
||||
owner->game_id = m_gameIndex;
|
||||
new_session->m_userList.push_back( owner );
|
||||
owner->m_gameId = m_gameIndex;
|
||||
new_session->m_owner = owner;
|
||||
|
||||
std::lock_guard< std::mutex > lock( m_dataMutex );
|
||||
m_gameSessionList.push_back( new_session );
|
||||
|
||||
m_gameIndex++;
|
||||
@@ -105,24 +108,20 @@ bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GameSessionManager::CancelGameSession( sptr_user user )
|
||||
bool GameSessionManager::ForceTerminateGame( int32_t gameId )
|
||||
{
|
||||
auto gameId = user->game_id;
|
||||
|
||||
if( gameId < 0 )
|
||||
{
|
||||
Log::Error( "Game session not found! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard< std::mutex > lock( m_dataMutex );
|
||||
|
||||
auto it = std::find_if( m_gameSessionList.begin(), m_gameSessionList.end(), [ &gameId ]( sptr_game_session gameSession )
|
||||
{
|
||||
return gameSession->m_gameIndex == gameId;
|
||||
} );
|
||||
|
||||
Log::Debug( "CancelGameSession : [%d]", gameId );
|
||||
Log::Debug( "TODO: Notify all users in the game session" );
|
||||
|
||||
if( it != m_gameSessionList.end() )
|
||||
{
|
||||
m_gameSessionList.erase( it );
|
||||
@@ -134,9 +133,11 @@ bool GameSessionManager::CancelGameSession( sptr_user user )
|
||||
|
||||
sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
|
||||
{
|
||||
if (gameId < 0) return nullptr;
|
||||
if( gameId < 0 ) return nullptr;
|
||||
|
||||
for( auto &gameSession : m_gameSessionList)
|
||||
std::lock_guard< std::mutex > lock( m_dataMutex );
|
||||
|
||||
for( auto &gameSession : m_gameSessionList )
|
||||
{
|
||||
if( gameSession->m_gameIndex == gameId )
|
||||
{
|
||||
@@ -147,150 +148,186 @@ sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool GameSessionManager::SetGameOpen(sptr_user user)
|
||||
sptr_game_session GameSessionManager::FindGame(const std::wstring& gameName)
|
||||
{
|
||||
auto gameId = user->game_id;
|
||||
auto session = FindGame(gameId);
|
||||
if( gameName.empty() ) return nullptr;
|
||||
|
||||
if (session == nullptr)
|
||||
std::lock_guard< std::mutex > lock( m_dataMutex );
|
||||
|
||||
for( auto &gameSession : m_gameSessionList )
|
||||
{
|
||||
Log::Error("Game session not found! [%d]", gameId);
|
||||
if( gameSession->m_gameName == gameName )
|
||||
{
|
||||
return gameSession;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool GameSessionManager::RequestOpen( sptr_user user )
|
||||
{
|
||||
auto gameId = user->m_gameId;
|
||||
auto session = FindGame( gameId );
|
||||
|
||||
if( session == nullptr )
|
||||
{
|
||||
Log::Error( "Game session not found! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
if( session->m_state != GameSession::GameState::NotReady )
|
||||
if( session->m_state == GameSession::GameState::Open )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( user->m_discoveryAddr.empty() )
|
||||
{
|
||||
Log::Error( "User discovery address is empty! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
session->m_state = GameSession::GameState::Open;
|
||||
|
||||
user->is_ready = true;
|
||||
// Tell the host its own address.
|
||||
NotifyGameDiscovered msg(user->m_discoveryAddr, user->m_discoveryPort);
|
||||
user->sock->send( msg );
|
||||
|
||||
Log::Info("Game session is open! [%d]", gameId);
|
||||
Log::Info( "Game Session [%d] Discoverable on %s", gameId, user->m_discoveryAddr.c_str() );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GameSessionManager::JoinGame(sptr_user user)
|
||||
bool GameSessionManager::RequestCancel( sptr_user user )
|
||||
{
|
||||
auto gameId = user->game_id;
|
||||
auto session = FindGame(gameId);
|
||||
if( !user || user->m_gameId < 0 )
|
||||
return false;
|
||||
|
||||
if (session == nullptr)
|
||||
const int gameId = user->m_gameId;
|
||||
|
||||
std::lock_guard<std::mutex> lock( m_dataMutex );
|
||||
|
||||
auto it = std::find_if( m_gameSessionList.begin(), m_gameSessionList.end(),
|
||||
[ gameId ]( const sptr_game_session &gameSession )
|
||||
{
|
||||
Log::Error("Game session not found! [%d]", gameId);
|
||||
return gameSession->m_gameIndex == gameId;
|
||||
} );
|
||||
|
||||
if( it == m_gameSessionList.end() )
|
||||
return false;
|
||||
|
||||
const auto &session = *it;
|
||||
|
||||
auto owner = session->m_owner.lock();
|
||||
if( !owner )
|
||||
{
|
||||
Log::Error( "Game session owner not found! [%d]", gameId );
|
||||
ForceTerminateGame( gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
auto host = session->GetHost();
|
||||
|
||||
// Check that the user isn't already in the list.
|
||||
auto it = std::find_if(session->m_userList.begin(), session->m_userList.end(), [&user](sptr_user member)
|
||||
if( owner->m_sessionId != user->m_sessionId )
|
||||
{
|
||||
return member == user;
|
||||
});
|
||||
|
||||
if (it != session->m_userList.end())
|
||||
{
|
||||
Log::Error("User is already in the game session! [%d]", gameId);
|
||||
Log::Error( "User is not the host! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add the user to the game session.
|
||||
user->is_host = false;
|
||||
user->is_ready = false;
|
||||
session->m_userList.push_back(user);
|
||||
|
||||
// Notify the host that a user is joining.
|
||||
NotifyClientRequestConnect msg_a( user->discovery_addr, user->discovery_port );
|
||||
host->tcp->send( msg_a );
|
||||
|
||||
// Update the user with their discovery address.
|
||||
NotifyGameDiscovered msg_b( user->discovery_addr, user->discovery_port );
|
||||
user->tcp->send( msg_b );
|
||||
|
||||
Log::Info("User [%S] joined game session! [%d]", user->session_id.c_str(), gameId);
|
||||
|
||||
m_gameSessionList.erase( it );
|
||||
return true;
|
||||
}
|
||||
|
||||
void GameSessionManager::RemoveUser(sptr_user user)
|
||||
bool GameSessionManager::RequestJoin( sptr_user join_user )
|
||||
{
|
||||
if( user == nullptr )
|
||||
{
|
||||
Log::Error( "User is null!" );
|
||||
return;
|
||||
}
|
||||
|
||||
auto session = FindGame( user->game_id );
|
||||
const auto gameId = join_user->m_gameId;
|
||||
auto session = FindGame( gameId );
|
||||
|
||||
if( session == nullptr )
|
||||
{
|
||||
Log::Error( "Game session not found! [%d]", user->game_id );
|
||||
return;
|
||||
Log::Error( "Game session not found! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = std::find_if( session->m_userList.begin(), session->m_userList.end(), [ &user ]( sptr_user member )
|
||||
if( session->m_state != GameSession::GameState::Open )
|
||||
{
|
||||
return member == user;
|
||||
} );
|
||||
|
||||
if( it != session->m_userList.end() )
|
||||
{
|
||||
session->m_userList.erase( it );
|
||||
Log::Error( "Game session not open! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
if( session->m_userList.empty() )
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
auto host_user = session->m_owner.lock();
|
||||
|
||||
std::vector<sptr_game_session> GameSessionManager::GetPublicGameSessionList() const
|
||||
{
|
||||
std::vector< sptr_game_session > list;
|
||||
|
||||
for( auto &game : m_gameSessionList )
|
||||
if( host_user == nullptr )
|
||||
{
|
||||
if( game->m_type == GameSession::GameType::Public )
|
||||
{
|
||||
list.push_back( game );
|
||||
}
|
||||
Log::Error( "Host not found! [%d]", gameId );
|
||||
ForceTerminateGame( gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
return list;
|
||||
if( host_user->m_discoveryAddr.empty() )
|
||||
{
|
||||
Log::Error( "User discovery address is empty! [%d]", gameId );
|
||||
ForceTerminateGame( gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
join_user->m_isHost = false;
|
||||
|
||||
// First, notify the host that a client is trying to connect.
|
||||
NotifyClientRequestConnect msgNotifyReqConnect(join_user->m_discoveryAddr, join_user->m_discoveryPort);
|
||||
host_user->sock->send(msgNotifyReqConnect);
|
||||
|
||||
// Then, tell the joiner where to send packets.
|
||||
NotifyClientDiscovered msgClientDiscovered(host_user->m_discoveryAddr, host_user->m_discoveryPort);
|
||||
join_user->sock->send(msgClientDiscovered);
|
||||
|
||||
Log::Info( "Send User Address to host [%S] from %s:%d", join_user->m_sessionId.c_str(), join_user->m_discoveryAddr.c_str(), join_user->m_discoveryPort );
|
||||
|
||||
|
||||
|
||||
Log::Info( "Send Host Address to user [%S] from %s:%d", join_user->m_sessionId.c_str(), host_user->m_discoveryAddr.c_str(), join_user->m_discoveryPort );
|
||||
|
||||
Log::Info( "User [%S] Joining game session... [%d]", join_user->m_sessionId.c_str(), gameId );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<sptr_game_session> GameSessionManager::GetAvailableGameSessionList() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_dataMutex );
|
||||
|
||||
std::vector<sptr_game_session> list;
|
||||
|
||||
for( auto &game : m_gameSessionList)
|
||||
for( const auto &game : m_gameSessionList )
|
||||
{
|
||||
if (game->m_type != GameSession::GameType::Public)
|
||||
continue;
|
||||
|
||||
//if( game->m_state != GameSession::GameState::Open)
|
||||
// continue;
|
||||
|
||||
list.push_back(game);
|
||||
if( game->m_type == GameSession::GameType::Public &&
|
||||
game->m_state == GameSession::GameState::Open )
|
||||
{
|
||||
list.push_back( game );
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
std::vector<sptr_game_session> GameSessionManager::GetPublicGameSessionList() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_dataMutex );
|
||||
|
||||
std::vector<sptr_game_session> list;
|
||||
for( const auto &game : m_gameSessionList )
|
||||
{
|
||||
if( game->m_type == GameSession::GameType::Public )
|
||||
list.push_back( game );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
std::vector<sptr_game_session> GameSessionManager::GetPrivateGameSessionList() const
|
||||
{
|
||||
std::vector<sptr_game_session> list;
|
||||
std::lock_guard<std::mutex> lock( m_dataMutex );
|
||||
|
||||
for( auto &game : m_gameSessionList )
|
||||
std::vector<sptr_game_session> list;
|
||||
for( const auto &game : m_gameSessionList )
|
||||
{
|
||||
if( game->m_type == GameSession::GameType::Private )
|
||||
{
|
||||
list.push_back( game );
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ class GameSessionManager {
|
||||
private:
|
||||
static inline std::unique_ptr< GameSessionManager > m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
static inline std::mutex m_dataMutex;
|
||||
|
||||
int32_t m_gameIndex;
|
||||
std::vector< sptr_game_session > m_gameSessionList;
|
||||
@@ -16,7 +17,7 @@ public:
|
||||
GameSessionManager( const GameSessionManager & ) = delete;
|
||||
GameSessionManager &operator=( const GameSessionManager & ) = delete;
|
||||
|
||||
static GameSessionManager& Get()
|
||||
static GameSessionManager &Get()
|
||||
{
|
||||
std::lock_guard< std::mutex > lock( m_mutex );
|
||||
if( m_instance == nullptr )
|
||||
@@ -27,20 +28,19 @@ public:
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
void Process();
|
||||
void ProcessGameSession( sptr_game_session gameSession );
|
||||
|
||||
bool CreatePublicGameSession( sptr_user user, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
|
||||
bool CreatePrivateGameSession( sptr_user user, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
|
||||
bool CancelGameSession( sptr_user user );
|
||||
void OnDisconnectUser( sptr_user user );
|
||||
|
||||
bool CreatePublicGameSession( sptr_user user, std::wstring gameName );
|
||||
bool CreatePrivateGameSession( sptr_user user, std::wstring gameName );
|
||||
bool ForceTerminateGame( const int32_t gameId );
|
||||
sptr_game_session FindGame( const int32_t gameId );
|
||||
sptr_game_session FindGame( const std::wstring &gameName );
|
||||
|
||||
bool SetGameOpen( sptr_user user );
|
||||
bool JoinGame( sptr_user user );
|
||||
void RemoveUser( sptr_user user );
|
||||
bool RequestOpen( sptr_user user );
|
||||
bool RequestCancel( sptr_user user );
|
||||
bool RequestJoin( sptr_user user );
|
||||
|
||||
std::vector< sptr_game_session > GetPublicGameSessionList() const;
|
||||
std::vector< sptr_game_session > GetAvailableGameSessionList() const;
|
||||
std::vector< sptr_game_session > GetPublicGameSessionList() const;
|
||||
std::vector< sptr_game_session > GetPrivateGameSessionList() const;
|
||||
};
|
||||
@@ -2,54 +2,30 @@
|
||||
|
||||
RealmUser::RealmUser()
|
||||
{
|
||||
tcp = nullptr;
|
||||
udp = std::make_shared<RealmUDPSocket>();
|
||||
sock = nullptr;
|
||||
|
||||
session_id = L"";
|
||||
m_sessionId = L"";
|
||||
|
||||
local_addr = "";
|
||||
discovery_addr = "";
|
||||
discovery_port = 0;
|
||||
discovery_state = DiscoveryState::None;
|
||||
m_localAddr = "";
|
||||
m_discoveryAddr = "";
|
||||
|
||||
is_host = false;
|
||||
is_ready = false;
|
||||
|
||||
game_id = 0;
|
||||
|
||||
player_name = "";
|
||||
player_class = "";
|
||||
player_level = 0;
|
||||
m_isHost = false;
|
||||
m_gameId = 0;
|
||||
}
|
||||
|
||||
RealmUser::~RealmUser()
|
||||
{
|
||||
if( tcp )
|
||||
if( sock )
|
||||
{
|
||||
tcp->flag.disconnected = true;
|
||||
tcp.reset();
|
||||
sock->flag.disconnected = true;
|
||||
sock.reset();
|
||||
}
|
||||
|
||||
if( udp )
|
||||
{
|
||||
udp->flag.disconnected = true;
|
||||
udp.reset();
|
||||
}
|
||||
m_sessionId = L"";
|
||||
|
||||
session_id = L"";
|
||||
|
||||
local_addr = "";
|
||||
discovery_addr = "";
|
||||
discovery_port = 0;
|
||||
discovery_state = DiscoveryState::None;
|
||||
|
||||
is_host = false;
|
||||
is_ready = false;
|
||||
|
||||
game_id = 0;
|
||||
|
||||
player_name = "";
|
||||
player_class = "";
|
||||
player_level = 0;
|
||||
m_localAddr = "";
|
||||
m_discoveryAddr = "";
|
||||
|
||||
m_isHost = false;
|
||||
m_gameId = 0;
|
||||
}
|
||||
@@ -1,40 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "RealmCharacterData.h"
|
||||
|
||||
enum class DiscoveryState
|
||||
{
|
||||
None,
|
||||
Host_Waiting,
|
||||
Initial_Connect,
|
||||
Negotiating,
|
||||
Joining,
|
||||
Joined
|
||||
};
|
||||
|
||||
class RealmUser {
|
||||
public:
|
||||
RealmUser();
|
||||
~RealmUser();
|
||||
|
||||
public:
|
||||
sptr_tcp_socket tcp; // For Realm Lobby
|
||||
sptr_udp_socket udp; // For Discovery
|
||||
sptr_socket sock; // For Realm Lobby
|
||||
|
||||
std::wstring session_id; // Unique Session ID for the user (Generated at login)
|
||||
std::wstring m_sessionId; // Unique Session ID for the user (Generated at login)
|
||||
|
||||
bool is_host;
|
||||
bool is_ready;
|
||||
int32_t game_id;
|
||||
bool m_isHost; // True if this user is the host of a realm
|
||||
int32_t m_gameId; // Unique ID of the realm
|
||||
|
||||
std::string local_addr;
|
||||
std::string discovery_addr;
|
||||
int32_t discovery_port;
|
||||
DiscoveryState discovery_state;
|
||||
|
||||
std::string player_name;
|
||||
std::string player_class;
|
||||
int32_t player_level;
|
||||
std::string m_localAddr; // Local IP address of the user, passed from the client during realm creation.
|
||||
std::string m_discoveryAddr;
|
||||
int32_t m_discoveryPort;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< RealmUser > sptr_user;
|
||||
|
||||
@@ -11,7 +11,8 @@ RealmUserManager::~RealmUserManager()
|
||||
|
||||
std::wstring RealmUserManager::GenerateSessionId()
|
||||
{
|
||||
// TODO : Use something better than rand()
|
||||
// TODO : Maybe use something better than rand()
|
||||
// but it is just a temporary ID
|
||||
std::wstring sessionId;
|
||||
for( int i = 0; i < MAX_SESSION_ID_LENGTH; i++ )
|
||||
{
|
||||
@@ -21,14 +22,14 @@ std::wstring RealmUserManager::GenerateSessionId()
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::CreateUser( sptr_tcp_socket socket )
|
||||
sptr_user RealmUserManager::CreateUser( sptr_socket socket )
|
||||
{
|
||||
Log::Debug( "ClientManager::CreateUser() - Created new user" );
|
||||
|
||||
auto user = std::make_shared< RealmUser >();
|
||||
|
||||
user->session_id = GenerateSessionId();
|
||||
user->tcp = socket;
|
||||
user->m_sessionId = GenerateSessionId();
|
||||
user->sock = socket;
|
||||
|
||||
m_users.push_back( user );
|
||||
|
||||
@@ -40,13 +41,13 @@ void RealmUserManager::RemoveUser( sptr_user user )
|
||||
auto it = std::find( m_users.begin(), m_users.end(), user );
|
||||
if( it == m_users.end() )
|
||||
{
|
||||
Log::Error( "RemoveUser : [%S] not found", user->session_id.c_str() );
|
||||
Log::Error( "RemoveUser : [%S] not found", user->m_sessionId.c_str() );
|
||||
return;
|
||||
}
|
||||
|
||||
GameSessionManager::Get().RemoveUser((*it));
|
||||
|
||||
Log::Debug( "RemoveUser : [%S]", user->session_id.c_str() );
|
||||
GameSessionManager::Get().OnDisconnectUser( user );
|
||||
|
||||
Log::Debug( "RemoveUser : [%S]", user->m_sessionId.c_str() );
|
||||
m_users.erase( it );
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
|
||||
{
|
||||
auto it = std::find_if( m_users.begin(), m_users.end(), [ &sessionId ]( sptr_user user )
|
||||
{
|
||||
return user->session_id == sessionId;
|
||||
return user->m_sessionId == sessionId;
|
||||
} );
|
||||
|
||||
if( it == m_users.end() )
|
||||
@@ -66,11 +67,11 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
|
||||
RemoveUser((*it));
|
||||
}
|
||||
|
||||
void RealmUserManager::RemoveUser( const sptr_tcp_socket socket )
|
||||
void RealmUserManager::RemoveUser( const sptr_socket socket )
|
||||
{
|
||||
auto it = std::find_if( m_users.begin(), m_users.end(), [ &socket ]( sptr_user user )
|
||||
{
|
||||
return user->tcp == socket;
|
||||
return user->sock == socket;
|
||||
} );
|
||||
|
||||
if( it == m_users.end() )
|
||||
@@ -86,7 +87,7 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
|
||||
{
|
||||
for( auto &user : m_users )
|
||||
{
|
||||
if( user->session_id == sessionId )
|
||||
if( user->m_sessionId == sessionId )
|
||||
{
|
||||
return user;
|
||||
}
|
||||
@@ -95,11 +96,11 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::GetUser( const sptr_tcp_socket socket )
|
||||
sptr_user RealmUserManager::GetUser( const sptr_socket socket )
|
||||
{
|
||||
auto it = std::find_if(m_users.begin(), m_users.end(), [&socket](sptr_user user)
|
||||
{
|
||||
return user->tcp == socket;
|
||||
return user->sock == socket;
|
||||
});
|
||||
|
||||
if (it == m_users.end())
|
||||
@@ -111,22 +112,7 @@ sptr_user RealmUserManager::GetUser( const sptr_tcp_socket socket )
|
||||
return (*it);
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::GetUserByAddress( const sockaddr_in* remoteAddr)
|
||||
int32_t RealmUserManager::GetUserCount() const
|
||||
{
|
||||
for( auto &user : m_users )
|
||||
{
|
||||
if( nullptr == user->udp )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compare the address
|
||||
if( user->udp->remote_addr.sin_addr.s_addr == remoteAddr->sin_addr.s_addr &&
|
||||
user->udp->remote_addr.sin_port == remoteAddr->sin_port )
|
||||
{
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return static_cast< int32_t >( m_users.size() );
|
||||
}
|
||||
|
||||
@@ -28,15 +28,13 @@ public:
|
||||
|
||||
public:
|
||||
static std::wstring GenerateSessionId();
|
||||
sptr_user CreateUser( sptr_tcp_socket socket );
|
||||
sptr_user CreateUser( sptr_socket socket );
|
||||
void RemoveUser( sptr_user user );
|
||||
void RemoveUser( const std::wstring &sessionId );
|
||||
void RemoveUser( const sptr_tcp_socket socket );
|
||||
void RemoveUser( const sptr_socket socket );
|
||||
|
||||
sptr_user GetUser( const std::wstring &sessionId );
|
||||
sptr_user GetUser( const sptr_tcp_socket socket );
|
||||
sptr_user GetUser( const sptr_socket socket );
|
||||
|
||||
sptr_user GetUserByAddress(const sockaddr_in *remoteAddr );
|
||||
|
||||
private:
|
||||
int32_t GetUserCount() const;
|
||||
};
|
||||
Reference in New Issue
Block a user