Realm and User management

This commit is contained in:
HikikoMarmy
2025-04-14 03:54:41 +01:00
parent ea09bc2109
commit 2525d7be5a
8 changed files with 244 additions and 261 deletions

View File

@@ -1,29 +1,39 @@
#include "../global_define.h" #include "../global_define.h"
#include "../Lobby Server/Event/NotifyClientReqConnect.h"
#include "../Lobby Server/Event/NotifyGameDiscovered.h"
GameSession::GameSession() GameSession::GameSession()
{ {
m_owner.reset();
m_gameIndex = 0; m_gameIndex = 0;
m_type = GameType::Public;
m_state = GameState::NotReady; m_state = GameState::NotReady;
m_minimumLevel = 0; m_minimumLevel = 0;
m_maximumLevel = 0; m_maximumLevel = 0;
m_currentPlayers = 0;
m_maximumPlayers = 0; m_maximumPlayers = 0;
m_gameLocation.clear(); m_gameLocation.clear();
m_gameName.clear(); m_gameName.clear();
m_ownerName.clear(); m_ownerName.clear();
m_gameData.clear(); m_gameData.clear();
m_description.clear();
} }
GameSession::~GameSession() 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() m_gameLocation.clear();
{ m_gameName.clear();
return m_userList[ 0 ]; m_ownerName.clear();
m_gameData.clear();
m_description.clear();
} }

View File

@@ -5,11 +5,6 @@ public:
GameSession(); GameSession();
~GameSession(); ~GameSession();
sptr_user GetHost();
bool JoinUser( sptr_user user );
public:
enum class GameType enum class GameType
{ {
Public, Public,
@@ -19,23 +14,23 @@ public:
enum class GameState enum class GameState
{ {
NotReady, NotReady,
Open, Open
Started
} m_state; } m_state;
std::weak_ptr< RealmUser > m_owner;
int32_t m_gameIndex; int32_t m_gameIndex;
std::wstring m_gameLocation; std::wstring m_gameLocation;
std::wstring m_gameName; std::wstring m_gameName;
std::wstring m_ownerName; std::wstring m_ownerName;
std::string m_gameData; std::string m_gameData;
std::string m_description;
int32_t m_maximumPlayers; int8_t m_currentPlayers;
int8_t m_maximumPlayers;
int32_t m_minimumLevel; int32_t m_minimumLevel;
int32_t m_maximumLevel; int32_t m_maximumLevel;
// User Information
std::vector< sptr_user > m_userList;
}; };
typedef std::shared_ptr< GameSession > sptr_game_session; typedef std::shared_ptr< GameSession > sptr_game_session;

View File

@@ -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 ) bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName )
{
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 )
{ {
auto new_session = std::make_shared< GameSession >(); 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_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin"; new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName; new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel; new_session->m_minimumLevel = 1;
new_session->m_maximumLevel = maximumLevel; new_session->m_maximumLevel = 9999;
new_session->m_gameData.resize(256); new_session->m_gameData.resize( 256 );
owner->is_host = true; owner->m_isHost = true;
owner->is_ready = false; owner->m_discoveryAddr = "";
owner->discovery_addr = "";
owner->discovery_port = 0;
owner->discovery_state = DiscoveryState::Initial_Connect;
owner->game_id = m_gameIndex; owner->m_gameId = m_gameIndex;
new_session->m_userList.push_back(owner); new_session->m_owner = owner;
std::lock_guard< std::mutex > lock( m_dataMutex );
m_gameSessionList.push_back( new_session ); m_gameSessionList.push_back( new_session );
m_gameIndex++; m_gameIndex++;
@@ -63,14 +68,14 @@ bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring
return true; 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 // Check if the game name or host session id is already in use
for( auto &gameSession : m_gameSessionList ) for( auto &gameSession : m_gameSessionList )
{ {
if( gameSession->m_type != GameSession::GameType::Private ) if( gameSession->m_type != GameSession::GameType::Private )
continue; continue;
if( gameSession->m_gameName == gameName ) if( gameSession->m_gameName == gameName )
{ {
Log::Error( "Game name is already in use! [%S]", gameName.c_str() ); 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_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin"; new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName; new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel; new_session->m_minimumLevel = 1;
new_session->m_maximumLevel = maximumLevel; new_session->m_maximumLevel = 9999;
new_session->m_gameData.resize( 256 ); new_session->m_gameData.resize( 256 );
owner->is_host = true; owner->m_isHost = true;
owner->is_ready = false; owner->m_discoveryAddr = "";
owner->discovery_addr = "";
owner->discovery_port = 0;
owner->discovery_state = DiscoveryState::Initial_Connect;
owner->game_id = m_gameIndex; owner->m_gameId = m_gameIndex;
new_session->m_userList.push_back( owner ); new_session->m_owner = owner;
std::lock_guard< std::mutex > lock( m_dataMutex );
m_gameSessionList.push_back( new_session ); m_gameSessionList.push_back( new_session );
m_gameIndex++; m_gameIndex++;
@@ -105,24 +108,20 @@ bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring
return true; return true;
} }
bool GameSessionManager::CancelGameSession( sptr_user user ) bool GameSessionManager::ForceTerminateGame( int32_t gameId )
{ {
auto gameId = user->game_id;
if( gameId < 0 ) if( gameId < 0 )
{ {
Log::Error( "Game session not found! [%d]", gameId );
return false; 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 ) auto it = std::find_if( m_gameSessionList.begin(), m_gameSessionList.end(), [ &gameId ]( sptr_game_session gameSession )
{ {
return gameSession->m_gameIndex == gameId; 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() ) if( it != m_gameSessionList.end() )
{ {
m_gameSessionList.erase( it ); m_gameSessionList.erase( it );
@@ -134,9 +133,11 @@ bool GameSessionManager::CancelGameSession( sptr_user user )
sptr_game_session GameSessionManager::FindGame( const int32_t gameId ) 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 ) if( gameSession->m_gameIndex == gameId )
{ {
@@ -147,150 +148,186 @@ sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
return nullptr; return nullptr;
} }
bool GameSessionManager::SetGameOpen(sptr_user user) sptr_game_session GameSessionManager::FindGame(const std::wstring& gameName)
{ {
auto gameId = user->game_id; if( gameName.empty() ) return nullptr;
auto session = FindGame(gameId);
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; return false;
} }
if( session->m_state != GameSession::GameState::NotReady ) if( session->m_state == GameSession::GameState::Open )
{ {
return false; return false;
} }
if( user->m_discoveryAddr.empty() )
{
Log::Error( "User discovery address is empty! [%d]", gameId );
return false;
}
session->m_state = GameSession::GameState::Open; 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; return true;
} }
bool GameSessionManager::JoinGame(sptr_user user) bool GameSessionManager::RequestCancel( sptr_user user )
{ {
auto gameId = user->game_id; if( !user || user->m_gameId < 0 )
auto session = FindGame(gameId); 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; return false;
} }
auto host = session->GetHost(); if( owner->m_sessionId != user->m_sessionId )
// 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)
{ {
return member == user; Log::Error( "User is not the host! [%d]", gameId );
});
if (it != session->m_userList.end())
{
Log::Error("User is already in the game session! [%d]", gameId);
return false; return false;
} }
// Add the user to the game session. m_gameSessionList.erase( it );
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);
return true; return true;
} }
void GameSessionManager::RemoveUser(sptr_user user) bool GameSessionManager::RequestJoin( sptr_user join_user )
{ {
if( user == nullptr ) const auto gameId = join_user->m_gameId;
{ auto session = FindGame( gameId );
Log::Error( "User is null!" );
return;
}
auto session = FindGame( user->game_id );
if( session == nullptr ) if( session == nullptr )
{ {
Log::Error( "Game session not found! [%d]", user->game_id ); Log::Error( "Game session not found! [%d]", gameId );
return; 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; Log::Error( "Game session not open! [%d]", gameId );
} ); return false;
if( it != session->m_userList.end() )
{
session->m_userList.erase( it );
} }
if( session->m_userList.empty() ) auto host_user = session->m_owner.lock();
{
}
}
std::vector<sptr_game_session> GameSessionManager::GetPublicGameSessionList() const if( host_user == nullptr )
{
std::vector< sptr_game_session > list;
for( auto &game : m_gameSessionList )
{ {
if( game->m_type == GameSession::GameType::Public ) Log::Error( "Host not found! [%d]", gameId );
{ ForceTerminateGame( gameId );
list.push_back( game ); 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::vector<sptr_game_session> GameSessionManager::GetAvailableGameSessionList() const
{ {
std::lock_guard<std::mutex> lock( m_dataMutex );
std::vector<sptr_game_session> list; std::vector<sptr_game_session> list;
for( const auto &game : m_gameSessionList )
for( auto &game : m_gameSessionList)
{ {
if (game->m_type != GameSession::GameType::Public) if( game->m_type == GameSession::GameType::Public &&
continue; game->m_state == GameSession::GameState::Open )
{
//if( game->m_state != GameSession::GameState::Open) list.push_back( game );
// continue; }
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; return list;
} }
std::vector<sptr_game_session> GameSessionManager::GetPrivateGameSessionList() const 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 ) if( game->m_type == GameSession::GameType::Private )
{
list.push_back( game ); list.push_back( game );
}
} }
return list; return list;
} }

View File

@@ -6,6 +6,7 @@ class GameSessionManager {
private: private:
static inline std::unique_ptr< GameSessionManager > m_instance; static inline std::unique_ptr< GameSessionManager > m_instance;
static inline std::mutex m_mutex; static inline std::mutex m_mutex;
static inline std::mutex m_dataMutex;
int32_t m_gameIndex; int32_t m_gameIndex;
std::vector< sptr_game_session > m_gameSessionList; std::vector< sptr_game_session > m_gameSessionList;
@@ -16,7 +17,7 @@ public:
GameSessionManager( const GameSessionManager & ) = delete; GameSessionManager( const GameSessionManager & ) = delete;
GameSessionManager &operator=( const GameSessionManager & ) = delete; GameSessionManager &operator=( const GameSessionManager & ) = delete;
static GameSessionManager& Get() static GameSessionManager &Get()
{ {
std::lock_guard< std::mutex > lock( m_mutex ); std::lock_guard< std::mutex > lock( m_mutex );
if( m_instance == nullptr ) if( m_instance == nullptr )
@@ -27,20 +28,19 @@ public:
return *m_instance; return *m_instance;
} }
void Process(); void OnDisconnectUser( sptr_user user );
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 );
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 int32_t gameId );
sptr_game_session FindGame( const std::wstring &gameName );
bool SetGameOpen( sptr_user user ); bool RequestOpen( sptr_user user );
bool JoinGame( sptr_user user ); bool RequestCancel( sptr_user user );
void RemoveUser( 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 > GetAvailableGameSessionList() const;
std::vector< sptr_game_session > GetPublicGameSessionList() const;
std::vector< sptr_game_session > GetPrivateGameSessionList() const; std::vector< sptr_game_session > GetPrivateGameSessionList() const;
}; };

View File

@@ -2,54 +2,30 @@
RealmUser::RealmUser() RealmUser::RealmUser()
{ {
tcp = nullptr; sock = nullptr;
udp = std::make_shared<RealmUDPSocket>();
session_id = L""; m_sessionId = L"";
local_addr = ""; m_localAddr = "";
discovery_addr = ""; m_discoveryAddr = "";
discovery_port = 0;
discovery_state = DiscoveryState::None;
is_host = false; m_isHost = false;
is_ready = false; m_gameId = 0;
game_id = 0;
player_name = "";
player_class = "";
player_level = 0;
} }
RealmUser::~RealmUser() RealmUser::~RealmUser()
{ {
if( tcp ) if( sock )
{ {
tcp->flag.disconnected = true; sock->flag.disconnected = true;
tcp.reset(); sock.reset();
} }
if( udp ) m_sessionId = L"";
{
udp->flag.disconnected = true;
udp.reset();
}
session_id = L""; m_localAddr = "";
m_discoveryAddr = "";
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_isHost = false;
m_gameId = 0;
} }

View File

@@ -1,40 +1,21 @@
#pragma once #pragma once
#include "RealmCharacterData.h"
enum class DiscoveryState
{
None,
Host_Waiting,
Initial_Connect,
Negotiating,
Joining,
Joined
};
class RealmUser { class RealmUser {
public: public:
RealmUser(); RealmUser();
~RealmUser(); ~RealmUser();
public: public:
sptr_tcp_socket tcp; // For Realm Lobby sptr_socket sock; // For Realm Lobby
sptr_udp_socket udp; // For Discovery
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 m_isHost; // True if this user is the host of a realm
bool is_ready; int32_t m_gameId; // Unique ID of the realm
int32_t game_id;
std::string local_addr; std::string m_localAddr; // Local IP address of the user, passed from the client during realm creation.
std::string discovery_addr; std::string m_discoveryAddr;
int32_t discovery_port; int32_t m_discoveryPort;
DiscoveryState discovery_state;
std::string player_name;
std::string player_class;
int32_t player_level;
}; };
typedef std::shared_ptr< RealmUser > sptr_user; typedef std::shared_ptr< RealmUser > sptr_user;

View File

@@ -11,7 +11,8 @@ RealmUserManager::~RealmUserManager()
std::wstring RealmUserManager::GenerateSessionId() 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; std::wstring sessionId;
for( int i = 0; i < MAX_SESSION_ID_LENGTH; i++ ) for( int i = 0; i < MAX_SESSION_ID_LENGTH; i++ )
{ {
@@ -21,14 +22,14 @@ std::wstring RealmUserManager::GenerateSessionId()
return sessionId; return sessionId;
} }
sptr_user RealmUserManager::CreateUser( sptr_tcp_socket socket ) sptr_user RealmUserManager::CreateUser( sptr_socket socket )
{ {
Log::Debug( "ClientManager::CreateUser() - Created new user" ); Log::Debug( "ClientManager::CreateUser() - Created new user" );
auto user = std::make_shared< RealmUser >(); auto user = std::make_shared< RealmUser >();
user->session_id = GenerateSessionId(); user->m_sessionId = GenerateSessionId();
user->tcp = socket; user->sock = socket;
m_users.push_back( user ); 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 ); auto it = std::find( m_users.begin(), m_users.end(), user );
if( it == m_users.end() ) 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; 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 ); 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 ) 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() ) if( it == m_users.end() )
@@ -66,11 +67,11 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
RemoveUser((*it)); 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 ) 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() ) if( it == m_users.end() )
@@ -86,7 +87,7 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
{ {
for( auto &user : m_users ) for( auto &user : m_users )
{ {
if( user->session_id == sessionId ) if( user->m_sessionId == sessionId )
{ {
return user; return user;
} }
@@ -95,11 +96,11 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
return nullptr; 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) 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()) if (it == m_users.end())
@@ -111,22 +112,7 @@ sptr_user RealmUserManager::GetUser( const sptr_tcp_socket socket )
return (*it); return (*it);
} }
sptr_user RealmUserManager::GetUserByAddress( const sockaddr_in* remoteAddr) int32_t RealmUserManager::GetUserCount() const
{ {
for( auto &user : m_users ) return static_cast< int32_t >( m_users.size() );
{
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;
} }

View File

@@ -28,15 +28,13 @@ public:
public: public:
static std::wstring GenerateSessionId(); static std::wstring GenerateSessionId();
sptr_user CreateUser( sptr_tcp_socket socket ); sptr_user CreateUser( sptr_socket socket );
void RemoveUser( sptr_user user ); void RemoveUser( sptr_user user );
void RemoveUser( const std::wstring &sessionId ); 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 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 ); int32_t GetUserCount() const;
private:
}; };