Removed dbmap/ers from login-server (#3658)

Converted online_db to unordered_map
Converted auth_db to unordered_map
Removed ers_report option
This commit is contained in:
Lemongrass3110 2018-11-08 00:03:09 +01:00 committed by GitHub
parent 78edf851a0
commit 75d24ad1ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 137 deletions

View File

@ -7,10 +7,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <unordered_map>
#include "../common/cli.hpp" #include "../common/cli.hpp"
#include "../common/core.hpp" #include "../common/core.hpp"
#include "../common/db.hpp"
#include "../common/malloc.hpp" #include "../common/malloc.hpp"
#include "../common/md5calc.hpp" #include "../common/md5calc.hpp"
#include "../common/mmo.hpp" #include "../common/mmo.hpp"
@ -20,6 +20,7 @@
#include "../common/socket.hpp" //ip2str #include "../common/socket.hpp" //ip2str
#include "../common/strlib.hpp" #include "../common/strlib.hpp"
#include "../common/timer.hpp" #include "../common/timer.hpp"
#include "../common/utilities.hpp"
#include "../common/utils.hpp" #include "../common/utils.hpp"
#include "../config/core.hpp" #include "../config/core.hpp"
@ -30,14 +31,16 @@
#include "logincnslif.hpp" #include "logincnslif.hpp"
#include "loginlog.hpp" #include "loginlog.hpp"
using namespace rathena;
#define LOGIN_MAX_MSG 30 /// Max number predefined in msg_conf #define LOGIN_MAX_MSG 30 /// Max number predefined in msg_conf
static char* msg_table[LOGIN_MAX_MSG]; /// Login Server messages_conf static char* msg_table[LOGIN_MAX_MSG]; /// Login Server messages_conf
//definition of exported var declared in .h //definition of exported var declared in .h
struct mmo_char_server ch_server[MAX_SERVERS]; /// char server data struct mmo_char_server ch_server[MAX_SERVERS]; /// char server data
struct Login_Config login_config; /// Configuration of login-serv struct Login_Config login_config; /// Configuration of login-serv
DBMap* online_db; std::unordered_map<uint32,struct online_login_data> online_db;
DBMap* auth_db; std::unordered_map<uint32,struct auth_node> auth_db;
// account database // account database
AccountDB* accounts = NULL; AccountDB* accounts = NULL;
@ -65,20 +68,8 @@ int parse_console(const char* buf){
return cnslif_parse(buf); return cnslif_parse(buf);
} }
/** struct online_login_data* login_get_online_user( uint32 account_id ){
* Sub function to create an online_login_data and save it to db. return util::umap_find( online_db, account_id );
* @param key: Key of the database entry
* @param ap: args
* @return : Data identified by the key to be put in the database
* @see DBCreateData
*/
DBData login_create_online_user(DBKey key, va_list args) {
struct online_login_data* p;
CREATE(p, struct online_login_data, 1);
p->account_id = key.i;
p->char_server = -1;
p->waiting_disconnect = INVALID_TIMER;
return db_ptr2data(p);
} }
/** /**
@ -89,13 +80,24 @@ DBData login_create_online_user(DBKey key, va_list args) {
* @return the new online_login_data for that user * @return the new online_login_data for that user
*/ */
struct online_login_data* login_add_online_user(int char_server, uint32 account_id){ struct online_login_data* login_add_online_user(int char_server, uint32 account_id){
struct online_login_data* p; struct online_login_data* p = login_get_online_user( account_id );
p = (struct online_login_data*)idb_ensure(online_db, account_id, login_create_online_user);
if( p == nullptr ){
// Allocate the player
p = &online_db[account_id];
p->account_id = account_id;
p->char_server = char_server; p->char_server = char_server;
p->waiting_disconnect = INVALID_TIMER;
}else{
p->char_server = char_server;
if( p->waiting_disconnect != INVALID_TIMER ){ if( p->waiting_disconnect != INVALID_TIMER ){
delete_timer( p->waiting_disconnect, login_waiting_disconnect_timer ); delete_timer( p->waiting_disconnect, login_waiting_disconnect_timer );
p->waiting_disconnect = INVALID_TIMER; p->waiting_disconnect = INVALID_TIMER;
} }
}
return p; return p;
} }
@ -106,14 +108,38 @@ struct online_login_data* login_add_online_user(int char_server, uint32 account_
* @param account_id : aid to remove from db * @param account_id : aid to remove from db
*/ */
void login_remove_online_user(uint32 account_id) { void login_remove_online_user(uint32 account_id) {
struct online_login_data* p; struct online_login_data* p = login_get_online_user( account_id );
p = (struct online_login_data*)idb_get(online_db, account_id);
if( p == NULL )
return;
if( p->waiting_disconnect != INVALID_TIMER )
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
idb_remove(online_db, account_id); if( p == nullptr ){
return;
}
if( p->waiting_disconnect != INVALID_TIMER ){
delete_timer( p->waiting_disconnect, login_waiting_disconnect_timer );
}
online_db.erase( account_id );
}
struct auth_node* login_get_auth_node( uint32 account_id ){
return util::umap_find( auth_db, account_id );
}
struct auth_node* login_add_auth_node( struct login_session_data* sd, uint32 ip ){
struct auth_node* node = &auth_db[sd->account_id];
node->account_id = sd->account_id;
node->login_id1 = sd->login_id1;
node->login_id2 = sd->login_id2;
node->sex = sd->sex;
node->ip = ip;
node->clienttype = sd->clienttype;
return node;
}
void login_remove_auth_node( uint32 account_id ){
auth_db.erase( account_id );
} }
/** /**
@ -128,51 +154,31 @@ void login_remove_online_user(uint32 account_id) {
* @return :0 * @return :0
*/ */
TIMER_FUNC(login_waiting_disconnect_timer){ TIMER_FUNC(login_waiting_disconnect_timer){
struct online_login_data* p = (struct online_login_data*)idb_get(online_db, id); struct online_login_data* p = login_get_online_user( id );
if( p != NULL && p->waiting_disconnect == tid && p->account_id == id ){
if( p != nullptr && p->waiting_disconnect == tid && p->account_id == id ){
p->waiting_disconnect = INVALID_TIMER; p->waiting_disconnect = INVALID_TIMER;
login_remove_online_user(id); login_remove_online_user(id);
idb_remove(auth_db, id); login_remove_auth_node(id);
} }
return 0; return 0;
} }
/** void login_online_db_setoffline( int char_server ){
* Sub function to apply on online_db. for( std::pair<uint32,struct online_login_data> pair : online_db ){
* Mark a character as offline. if( char_server == -1 ){
* @param data: 1 entry in the db pair.second.char_server = -1;
* @param ap: args
* @return : Value to be added up by the function that is applying this
* @see DBApply
*/
int login_online_db_setoffline(DBKey key, DBData *data, va_list ap) {
struct online_login_data* p = (struct online_login_data*)db_data2ptr(data);
int server = va_arg(ap, int);
if( server == -1 ) {
p->char_server = -1;
if( p->waiting_disconnect != INVALID_TIMER ) {
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
p->waiting_disconnect = INVALID_TIMER;
}
}
else if( p->char_server == server )
p->char_server = -2; //Char server disconnected.
return 0;
}
/** if( pair.second.waiting_disconnect != INVALID_TIMER ){
* Sub function of login_online_data_cleanup. delete_timer( pair.second.waiting_disconnect, login_waiting_disconnect_timer );
* Checking if all users in db are still connected to a char-server, and remove them if they aren't. pair.second.waiting_disconnect = INVALID_TIMER;
* @param data: 1 entry in the db }
* @param ap: args }else if( pair.second.char_server == char_server ){
* @return: Value to be added up by the function that is applying this // Char server disconnected.
* @see DBApply pair.second.char_server = -2;
*/ }
static int login_online_data_cleanup_sub(DBKey key, DBData *data, va_list ap) { }
struct online_login_data *character= (struct online_login_data*)db_data2ptr(data);
if (character->char_server == -2) //Unknown server.. set them offline
login_remove_online_user(character->account_id);
return 0;
} }
/** /**
@ -185,7 +191,13 @@ static int login_online_data_cleanup_sub(DBKey key, DBData *data, va_list ap) {
* @return : 0 * @return : 0
*/ */
static TIMER_FUNC(login_online_data_cleanup){ static TIMER_FUNC(login_online_data_cleanup){
online_db->foreach(online_db, login_online_data_cleanup_sub); for( std::pair<uint32,struct online_login_data> pair : online_db ){
// Unknown server.. set them offline
if( pair.second.char_server == -2 ){
login_remove_online_user( pair.first );
}
}
return 0; return 0;
} }
@ -778,8 +790,8 @@ void do_final(void) {
} }
accounts = NULL; // destroyed in account_engine accounts = NULL; // destroyed in account_engine
online_db->destroy(online_db, NULL); online_db.clear();
auth_db->destroy(auth_db, NULL); auth_db.clear();
do_final_loginchrif(); do_final_loginchrif();
@ -858,13 +870,8 @@ int do_init(int argc, char** argv) {
// initialize static and dynamic ipban system // initialize static and dynamic ipban system
ipban_init(); ipban_init();
// Online user database init
online_db = idb_alloc(DB_OPT_RELEASE_DATA);
add_timer_func_list(login_waiting_disconnect_timer, "waiting_disconnect_timer"); add_timer_func_list(login_waiting_disconnect_timer, "waiting_disconnect_timer");
// Interserver auth init
auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
// set default parser as parse_login function // set default parser as parse_login function
set_defaultparse(logclif_parse); set_defaultparse(logclif_parse);

View File

@ -129,7 +129,6 @@ struct online_login_data {
int waiting_disconnect; int waiting_disconnect;
int char_server; int char_server;
}; };
extern DBMap* online_db; // uint32 account_id -> struct online_login_data*
/// Auth database /// Auth database
#define AUTH_TIMEOUT 30000 #define AUTH_TIMEOUT 30000
@ -141,19 +140,11 @@ struct auth_node {
char sex; char sex;
uint8 clienttype; uint8 clienttype;
}; };
extern DBMap* auth_db; // uint32 account_id -> struct auth_node*
///Accessors ///Accessors
AccountDB* login_get_accounts_db(void); AccountDB* login_get_accounts_db(void);
/** struct online_login_data* login_get_online_user( uint32 account_id );
* Sub function to create an online_login_data and save it to db.
* @param key: Key of the database entry
* @param ap: args
* @return : Data identified by the key to be put in the database
* @see DBCreateData
*/
DBData login_create_online_user(DBKey key, va_list args);
/** /**
* Function to add a user in online_db. * Function to add a user in online_db.
@ -172,6 +163,12 @@ struct online_login_data* login_add_online_user(int char_server, uint32 account_
*/ */
void login_remove_online_user(uint32 account_id); void login_remove_online_user(uint32 account_id);
struct auth_node* login_get_auth_node( uint32 account_id );
struct auth_node* login_add_auth_node( struct login_session_data* sd, uint32 ip );
void login_remove_auth_node( uint32 account_id );
/** /**
* Timered function to disconnect a user from login. * Timered function to disconnect a user from login.
* This is done either after auth_ok or kicked by char-server. * This is done either after auth_ok or kicked by char-server.
@ -185,15 +182,7 @@ void login_remove_online_user(uint32 account_id);
*/ */
TIMER_FUNC(login_waiting_disconnect_timer); TIMER_FUNC(login_waiting_disconnect_timer);
/** void login_online_db_setoffline( int char_server );
* Sub function to apply on online_db.
* Mark a character as offline.
* @param data: 1 entry in the db
* @param ap: args
* @return : Value to be added up by the function that is applying this
* @see DBApply
*/
int login_online_db_setoffline(DBKey key, DBData *data, va_list ap);
/** /**
* Test to determine if an IP come from LAN or WAN. * Test to determine if an IP come from LAN or WAN.

View File

@ -73,7 +73,6 @@ int logchrif_parse_reqauth(int fd, int id,char* ip){
if( RFIFOREST(fd) < 23 ) if( RFIFOREST(fd) < 23 )
return 0; return 0;
else{ else{
struct auth_node* node;
uint32 account_id = RFIFOL(fd,2); uint32 account_id = RFIFOL(fd,2);
uint32 login_id1 = RFIFOL(fd,6); uint32 login_id1 = RFIFOL(fd,6);
uint32 login_id2 = RFIFOL(fd,10); uint32 login_id2 = RFIFOL(fd,10);
@ -82,9 +81,10 @@ int logchrif_parse_reqauth(int fd, int id,char* ip){
int request_id = RFIFOL(fd,19); int request_id = RFIFOL(fd,19);
RFIFOSKIP(fd,23); RFIFOSKIP(fd,23);
node = (struct auth_node*)idb_get(auth_db, account_id); struct auth_node* node = login_get_auth_node( account_id );
if( runflag == LOGINSERVER_ST_RUNNING && if( runflag == LOGINSERVER_ST_RUNNING &&
node != NULL && node != nullptr &&
node->account_id == account_id && node->account_id == account_id &&
node->login_id1 == login_id1 && node->login_id1 == login_id1 &&
node->login_id2 == login_id2 && node->login_id2 == login_id2 &&
@ -105,7 +105,7 @@ int logchrif_parse_reqauth(int fd, int id,char* ip){
WFIFOSET(fd,21); WFIFOSET(fd,21);
// each auth entry can only be used once // each auth entry can only be used once
idb_remove(auth_db, account_id); login_remove_auth_node( account_id );
}else{// authentication not found }else{// authentication not found
ShowStatus("Char-server '%s': authentication of the account %d REFUSED (ip: %s).\n", ch_server[id].name, account_id, ip); ShowStatus("Char-server '%s': authentication of the account %d REFUSED (ip: %s).\n", ch_server[id].name, account_id, ip);
WFIFOHEAD(fd,21); WFIFOHEAD(fd,21);
@ -529,17 +529,13 @@ int logchrif_parse_updonlinedb(int fd, int id){
if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
return 0; return 0;
else{ else{
uint32 i, users; //Set all chars from this char-server offline first
online_db->foreach(online_db, login_online_db_setoffline, id); //Set all chars from this char-server offline first login_online_db_setoffline( id );
users = RFIFOW(fd,4);
for (i = 0; i < users; i++) { for( uint32 i = 0, users = RFIFOW(fd, 4); i < users; i++) {
int aid = RFIFOL(fd,6+i*4); uint32 aid = RFIFOL(fd,6+i*4);
struct online_login_data *p = (struct online_login_data*)idb_ensure(online_db, aid, login_create_online_user);
p->char_server = id; login_add_online_user( id, aid );
if (p->waiting_disconnect != INVALID_TIMER){
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
p->waiting_disconnect = INVALID_TIMER;
}
} }
RFIFOSKIP(fd,RFIFOW(fd,2)); RFIFOSKIP(fd,RFIFOW(fd,2));
} }
@ -588,7 +584,7 @@ int logchrif_parse_updcharip(int fd, int id){
*/ */
int logchrif_parse_setalloffline(int fd, int id){ int logchrif_parse_setalloffline(int fd, int id){
ShowInfo("Setting accounts from char-server %d offline.\n", id); ShowInfo("Setting accounts from char-server %d offline.\n", id);
online_db->foreach(online_db, login_online_db_setoffline, id); login_online_db_setoffline( id );
RFIFOSKIP(fd,2); RFIFOSKIP(fd,2);
return 1; return 1;
} }
@ -628,12 +624,11 @@ int logchrif_parse_pincode_authfail(int fd){
struct mmo_account acc; struct mmo_account acc;
AccountDB* accounts = login_get_accounts_db(); AccountDB* accounts = login_get_accounts_db();
if( accounts->load_num(accounts, &acc, RFIFOL(fd,2) ) ){ if( accounts->load_num(accounts, &acc, RFIFOL(fd,2) ) ){
struct online_login_data* ld; struct online_login_data* ld = login_get_online_user( acc.account_id );
ld = (struct online_login_data*)idb_get(online_db,acc.account_id); if( ld == nullptr ){
if( ld == NULL )
return 0; return 0;
}
login_log( host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed" ); login_log( host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed" );
} }
@ -850,7 +845,7 @@ void logchrif_server_destroy(int id){
* @param id: id of char-serv (should be >0, FIXME) * @param id: id of char-serv (should be >0, FIXME)
*/ */
void logchrif_server_reset(int id) { void logchrif_server_reset(int id) {
online_db->foreach(online_db, login_online_db_setoffline, id); //Set all chars from this char server to offline. login_online_db_setoffline(id); //Set all chars from this char server to offline.
logchrif_server_destroy(id); logchrif_server_destroy(id);
logchrif_server_init(id); logchrif_server_init(id);
} }

View File

@ -47,7 +47,6 @@ static void logclif_auth_ok(struct login_session_data* sd) {
uint8 server_num, n; uint8 server_num, n;
uint32 subnet_char_ip; uint32 subnet_char_ip;
struct auth_node* node;
int i; int i;
#if PACKETVER < 20170315 #if PACKETVER < 20170315
@ -89,7 +88,8 @@ static void logclif_auth_ok(struct login_session_data* sd) {
} }
{ {
struct online_login_data* data = (struct online_login_data*)idb_get(online_db, sd->account_id); struct online_login_data* data = login_get_online_user( sd->account_id );
if( data ) if( data )
{// account is already marked as online! {// account is already marked as online!
if( data->char_server > -1 ) if( data->char_server > -1 )
@ -108,7 +108,7 @@ static void logclif_auth_ok(struct login_session_data* sd) {
if( data->char_server == -1 ) if( data->char_server == -1 )
{// client has authed but did not access char-server yet {// client has authed but did not access char-server yet
// wipe previous session // wipe previous session
idb_remove(auth_db, sd->account_id); login_remove_auth_node(sd->account_id);
login_remove_online_user(sd->account_id); login_remove_online_user(sd->account_id);
data = NULL; data = NULL;
} }
@ -150,22 +150,13 @@ static void logclif_auth_ok(struct login_session_data* sd) {
WFIFOSET(fd,header+size*server_num); WFIFOSET(fd,header+size*server_num);
// create temporary auth entry // create temporary auth entry
CREATE(node, struct auth_node, 1); login_add_auth_node( sd, ip );
node->account_id = sd->account_id;
node->login_id1 = sd->login_id1;
node->login_id2 = sd->login_id2;
node->sex = sd->sex;
node->ip = ip;
node->clienttype = sd->clienttype;
idb_put(auth_db, sd->account_id, node);
{
struct online_login_data* data;
// mark client as 'online' // mark client as 'online'
data = login_add_online_user(-1, sd->account_id); struct online_login_data* data = login_add_online_user(-1, sd->account_id);
// schedule deletion of this node // schedule deletion of this node
data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0); data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0);
} }
}
/** /**
* Inform client that auth has failed. * Inform client that auth has failed.

View File

@ -7,7 +7,6 @@
#include <string.h> #include <string.h>
#include "../common/cli.hpp" #include "../common/cli.hpp"
#include "../common/ers.hpp"
#include "../common/md5calc.hpp" #include "../common/md5calc.hpp"
#include "../common/mmo.hpp" //cbasetype + NAME_LENGTH #include "../common/mmo.hpp" //cbasetype + NAME_LENGTH
#include "../common/showmsg.hpp" //show notice #include "../common/showmsg.hpp" //show notice
@ -143,15 +142,11 @@ int cnslif_parse(const char* buf){
ShowStatus("Console: Account '%s' created successfully.\n", username); ShowStatus("Console: Account '%s' created successfully.\n", username);
} }
} }
else if( strcmpi("ers_report", type) == 0 ){
ers_report();
}
else if( strcmpi("help", type) == 0 ){ else if( strcmpi("help", type) == 0 ){
ShowInfo("Available commands:\n"); ShowInfo("Available commands:\n");
ShowInfo("\t server:shutdown => Stops the server.\n"); ShowInfo("\t server:shutdown => Stops the server.\n");
ShowInfo("\t server:alive => Checks if the server is running.\n"); ShowInfo("\t server:alive => Checks if the server is running.\n");
ShowInfo("\t server:reloadconf => Reload config file: \"%s\"\n", login_config.loginconf_name); ShowInfo("\t server:reloadconf => Reload config file: \"%s\"\n", login_config.loginconf_name);
ShowInfo("\t ers_report => Displays database usage.\n");
ShowInfo("\t create:<username> <password> <sex:M|F> => Creates a new account.\n"); ShowInfo("\t create:<username> <password> <sex:M|F> => Creates a new account.\n");
} }
return 1; return 1;