Additional user variables for chatrooms and match making.

Session Recovery for disconnects
This commit is contained in:
HikikoMarmy
2025-07-01 13:59:24 +01:00
parent b4ab6cd31b
commit e15570ceec
4 changed files with 130 additions and 38 deletions

View File

@@ -1,4 +1,5 @@
#include "RealmUser.h"
#include "RealmCharacter.h"
RealmUser::RealmUser()
{
@@ -7,15 +8,20 @@ RealmUser::RealmUser()
m_gameType = RealmGameType::CHAMPIONS_OF_NORRATH;
m_accountId = -1;
m_characterId = -1;
m_sessionId = L"";
m_username = L"";
m_characterId = 0;
m_localAddr = "";
m_discoveryAddr = "";
m_discoveryPort = 0;
m_isLoggedIn = false;
m_isHost = false;
m_gameId = 0;
m_memberId = -1;
m_gameId = -1;
m_publicRoomId = -1;
m_privateRoomId = -1;
}
RealmUser::~RealmUser()
@@ -29,13 +35,18 @@ RealmUser::~RealmUser()
m_gameType = RealmGameType::CHAMPIONS_OF_NORRATH;
m_accountId = 0;
m_characterId = 0;
m_sessionId = L"";
m_username = L"";
m_characterId = 0;
m_localAddr = "";
m_discoveryAddr = "";
m_discoveryPort = 0;
m_isLoggedIn = false;
m_isHost = false;
m_memberId = 0;
m_gameId = 0;
}
m_publicRoomId = -1;
m_privateRoomId = -1;
}

View File

@@ -5,6 +5,7 @@
#include <array>
#include "RealmCharacter.h"
#include "../Common/Constant.h"
#include "../Network/RealmSocket.h"
@@ -13,22 +14,43 @@ public:
RealmUser();
~RealmUser();
bool operator==( const RealmUser &other ) const
{
return m_sessionId == other.m_sessionId || m_accountId == other.m_accountId;
}
bool operator<( const RealmUser &other ) const
{
if( m_sessionId != other.m_sessionId )
return m_sessionId < other.m_sessionId;
return m_accountId < other.m_accountId;
}
public:
sptr_socket sock; // For Realm Lobby
RealmGameType m_gameType; // Champions of Norrath or Return to Arms
std::wstring m_sessionId; // Temporary Session ID
int64_t m_accountId; // Unique ID of the account
std::wstring m_sessionId; // Temporary Session ID
std::wstring m_username; // Username of the user
std::wstring m_chatHandle;
int32_t m_characterId; // ID of selected Net Character
sptr_realm_character m_character; // Currently selected character
bool m_isLoggedIn; // True if the user has successfully authenticated and logged in
bool m_isHost; // True if this user is the host of a realm
int8_t m_memberId;
int32_t m_gameId; // Unique ID of the realm
bool m_isHost; // True if this user is the host of a realm
int32_t m_gameId; // Unique ID of the realm
int32_t m_publicRoomId; // Used for public chat rooms
int32_t m_privateRoomId; // Used for private chat rooms
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;
std::string m_localAddr; // Local IP address of the user, passed from the client during Realm creation.
int32_t m_localPort;
std::string m_discoveryAddr;
int32_t m_discoveryPort;
int32_t m_characterId;
sptr_realm_character m_character;
};
using sptr_user = std::shared_ptr< RealmUser >;
using wptr_user = std::weak_ptr< RealmUser >;

View File

@@ -1,12 +1,13 @@
#include "RealmUserManager.h"
#include "GameSessionManager.h"
#include "ChatRoomManager.h"
#include "../Network/Event/NotifyForcedLogout.h"
#include "../Common/Constant.h"
#include "../Database/Database.h"
#include "../logging.h"
RealmUserManager::RealmUserManager()
UserManager::UserManager()
{
std::random_device rd;
rng.seed( rd() );
@@ -14,11 +15,11 @@ RealmUserManager::RealmUserManager()
m_users.clear();
}
RealmUserManager::~RealmUserManager()
UserManager::~UserManager()
{
}
std::wstring RealmUserManager::GenerateSessionId()
std::wstring UserManager::GenerateSessionId()
{
static const wchar_t charset[] = L"0123456789ABCDEF";
std::uniform_int_distribution<int> dist( 0, 15 );
@@ -34,7 +35,7 @@ std::wstring RealmUserManager::GenerateSessionId()
return sessionId;
}
sptr_user RealmUserManager::CreateUser( sptr_socket socket, RealmGameType clientType )
sptr_user UserManager::CreateUser( sptr_socket socket, RealmGameType clientType )
{
Log::Debug( "ClientManager::CreateUser() - Created new user" );
@@ -49,49 +50,65 @@ sptr_user RealmUserManager::CreateUser( sptr_socket socket, RealmGameType client
return user;
}
void RealmUserManager::RemoveUser( sptr_user user )
void UserManager::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->m_sessionId.c_str() );
Log::Error( "RemoveUser : [{}] not found", user->m_sessionId );
return;
}
Database::Get().DeleteSession( user->m_sessionId );
GameSessionManager::Get().OnDisconnectUser( user );
ChatRoomManager::Get().OnDisconnectUser( user );
Log::Debug( "RemoveUser : [%S]", user->m_sessionId.c_str() );
Log::Debug( "RemoveUser : [{}][{}]", user->m_username, user->m_sessionId );
std::lock_guard< std::mutex > lock( m_mutex );
m_users.erase( it );
}
void RealmUserManager::RemoveUser( const std::wstring &sessionId )
void UserManager::RemoveUser( const std::wstring &sessionId )
{
auto user = FindUserBySessionId( sessionId );
if( !user )
{
Log::Error( "RemoveUser : [%S] not found", sessionId.c_str() );
Log::Error( "RemoveUser : [{}] not found", sessionId );
return;
}
RemoveUser( user );
}
void RealmUserManager::RemoveUser( const sptr_socket socket )
void UserManager::RemoveUser( const sptr_socket socket )
{
auto user = FindUserBySocket( socket );
if( !user )
{
Log::Error( "RemoveUser : [%S] not found", socket->remote_ip.c_str() );
Log::Error( "RemoveUser : [{}] not found", socket->remote_ip );
return;
}
RemoveUser( user );
}
void RealmUserManager::DisconnectUser( sptr_user user, const std::string reason )
void UserManager::Disconnect( sptr_socket socket, const std::string reason )
{
if( nullptr == socket )
{
return;
}
Log::Debug( "DisconnectSocket : [{}]. Reason: {}", socket->remote_ip, reason );
socket->send( NotifyForcedLogout() );
socket->flag.disconnected_wait = true;
RemoveUser( socket );
}
void UserManager::Disconnect( sptr_user user, const std::string reason )
{
if( nullptr == user )
{
@@ -100,17 +117,16 @@ void RealmUserManager::DisconnectUser( sptr_user user, const std::string reason
if( user->sock != nullptr )
{
NotifyForcedLogout notifyLogout;
user->sock->send( notifyLogout );
user->sock->send( NotifyForcedLogout() );
user->sock->flag.disconnected_wait = true;
}
Log::Debug( "DisconnectUser : [%S]. Reason: %s", user->m_sessionId.c_str(), reason.c_str() );
Log::Debug( "DisconnectUser : [{}]. Reason: {}", user->m_sessionId, reason );
RemoveUser( user );
}
sptr_user RealmUserManager::FindUserBySessionId( const std::wstring &sessionId )
sptr_user UserManager::FindUserBySessionId( const std::wstring &sessionId )
{
std::lock_guard<std::mutex> lock( m_mutex );
auto it = std::find_if( m_users.begin(), m_users.end(), [ & ]( const sptr_user &user )
@@ -120,7 +136,7 @@ sptr_user RealmUserManager::FindUserBySessionId( const std::wstring &sessionId )
return ( it != m_users.end() ) ? *it : nullptr;
}
sptr_user RealmUserManager::FindUserBySocket( const sptr_socket &socket )
sptr_user UserManager::FindUserBySocket( const sptr_socket &socket )
{
std::lock_guard<std::mutex> lock( m_mutex );
auto it = std::find_if( m_users.begin(), m_users.end(), [ & ]( const sptr_user &user )
@@ -130,7 +146,46 @@ sptr_user RealmUserManager::FindUserBySocket( const sptr_socket &socket )
return ( it != m_users.end() ) ? *it : nullptr;
}
int32_t RealmUserManager::GetUserCount() const
sptr_user UserManager::RecoverUserBySession( const std::wstring &sessionId, const sptr_socket &socket )
{
auto user = FindUserBySocket( socket );
if( user == nullptr )
{
Log::Error( "RecoverUserBySession: User not found for socket: {}", socket->remote_ip );
return nullptr;
}
if( sessionId.empty() )
{
Log::Error( "RecoverUserBySession: Empty session ID provided." );
return nullptr;
}
const auto [account_id, character_id] = Database::Get().GetSession( sessionId, socket->remote_ip );
if( account_id < 0 )
{
Log::Error( "RecoverUserBySession: Failed to get session for account ID: {}, session ID: {}", account_id, sessionId );
return nullptr;
}
user->m_accountId = account_id;
user->m_sessionId = sessionId;
user->m_character = Database::Get().LoadCharacterData( account_id, character_id );
Log::Debug( "RecoverUserBySession: User recovered with session ID: {}, account ID: {}", sessionId, account_id );
return user;
}
int32_t UserManager::GetUserCount() const
{
return static_cast< int32_t >( m_users.size() );
}
std::vector<sptr_user> UserManager::GetUserList()
{
std::lock_guard<std::mutex> lock( m_mutex );
return m_users;
}

View File

@@ -8,31 +8,35 @@
#include "RealmUser.h"
class RealmUserManager {
class UserManager {
private:
public:
static RealmUserManager &Get()
static UserManager &Get()
{
static RealmUserManager instance;
static UserManager instance;
return instance;
}
RealmUserManager( const RealmUserManager & ) = delete;
RealmUserManager &operator=( const RealmUserManager & ) = delete;
RealmUserManager();
~RealmUserManager();
UserManager( const UserManager & ) = delete;
UserManager &operator=( const UserManager & ) = delete;
UserManager();
~UserManager();
std::wstring GenerateSessionId();
sptr_user CreateUser( sptr_socket socket, RealmGameType clientType );
void RemoveUser( sptr_user user );
void RemoveUser( const std::wstring &sessionId );
void RemoveUser( const sptr_socket socket );
void DisconnectUser( sptr_user user, const std::string reason );
void Disconnect( sptr_socket socket, const std::string reason );
void Disconnect( sptr_user user, const std::string reason );
sptr_user FindUserBySessionId( const std::wstring &sessionId );
sptr_user FindUserBySocket( const sptr_socket &socket );
sptr_user RecoverUserBySession( const std::wstring &sessionId, const sptr_socket& socket );
int32_t GetUserCount() const;
std::vector< sptr_user > GetUserList();
private:
std::mutex m_mutex;