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 <string.h>
#include <string>
#include <unordered_map>
#include "../common/cli.hpp"
#include "../common/core.hpp"
#include "../common/db.hpp"
#include "../common/malloc.hpp"
#include "../common/md5calc.hpp"
#include "../common/mmo.hpp"
@ -20,6 +20,7 @@
#include "../common/socket.hpp" //ip2str
#include "../common/strlib.hpp"
#include "../common/timer.hpp"
#include "../common/utilities.hpp"
#include "../common/utils.hpp"
#include "../config/core.hpp"
@ -30,14 +31,16 @@
#include "logincnslif.hpp"
#include "loginlog.hpp"
using namespace rathena;
#define LOGIN_MAX_MSG 30 /// Max number predefined in msg_conf
static char* msg_table[LOGIN_MAX_MSG]; /// Login Server messages_conf
//definition of exported var declared in .h
struct mmo_char_server ch_server[MAX_SERVERS]; /// char server data
struct Login_Config login_config; /// Configuration of login-serv
DBMap* online_db;
DBMap* auth_db;
std::unordered_map<uint32,struct online_login_data> online_db;
std::unordered_map<uint32,struct auth_node> auth_db;
// account database
AccountDB* accounts = NULL;
@ -65,20 +68,8 @@ int parse_console(const char* buf){
return cnslif_parse(buf);
}
/**
* 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) {
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);
struct online_login_data* login_get_online_user( uint32 account_id ){
return util::umap_find( online_db, account_id );
}
/**
@ -89,13 +80,24 @@ DBData login_create_online_user(DBKey key, va_list args) {
* @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* p;
p = (struct online_login_data*)idb_ensure(online_db, account_id, login_create_online_user);
p->char_server = char_server;
if( p->waiting_disconnect != INVALID_TIMER ) {
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
struct online_login_data* p = login_get_online_user( account_id );
if( p == nullptr ){
// Allocate the player
p = &online_db[account_id];
p->account_id = account_id;
p->char_server = char_server;
p->waiting_disconnect = INVALID_TIMER;
}else{
p->char_server = char_server;
if( p->waiting_disconnect != INVALID_TIMER ){
delete_timer( p->waiting_disconnect, login_waiting_disconnect_timer );
p->waiting_disconnect = INVALID_TIMER;
}
}
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
*/
void login_remove_online_user(uint32 account_id) {
struct online_login_data* p;
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);
struct online_login_data* p = login_get_online_user( account_id );
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
*/
TIMER_FUNC(login_waiting_disconnect_timer){
struct online_login_data* p = (struct online_login_data*)idb_get(online_db, id);
if( p != NULL && p->waiting_disconnect == tid && p->account_id == id ){
struct online_login_data* p = login_get_online_user( id );
if( p != nullptr && p->waiting_disconnect == tid && p->account_id == id ){
p->waiting_disconnect = INVALID_TIMER;
login_remove_online_user(id);
idb_remove(auth_db, id);
login_remove_auth_node(id);
}
return 0;
}
/**
* 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) {
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;
void login_online_db_setoffline( int char_server ){
for( std::pair<uint32,struct online_login_data> pair : online_db ){
if( char_server == -1 ){
pair.second.char_server = -1;
if( pair.second.waiting_disconnect != INVALID_TIMER ){
delete_timer( pair.second.waiting_disconnect, login_waiting_disconnect_timer );
pair.second.waiting_disconnect = INVALID_TIMER;
}
}else if( pair.second.char_server == char_server ){
// Char server disconnected.
pair.second.char_server = -2;
}
}
else if( p->char_server == server )
p->char_server = -2; //Char server disconnected.
return 0;
}
/**
* Sub function of login_online_data_cleanup.
* Checking if all users in db are still connected to a char-server, and remove them if they aren't.
* @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
*/
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
*/
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;
}
@ -778,8 +790,8 @@ void do_final(void) {
}
accounts = NULL; // destroyed in account_engine
online_db->destroy(online_db, NULL);
auth_db->destroy(auth_db, NULL);
online_db.clear();
auth_db.clear();
do_final_loginchrif();
@ -858,13 +870,8 @@ int do_init(int argc, char** argv) {
// initialize static and dynamic ipban system
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");
// Interserver auth init
auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
// set default parser as parse_login function
set_defaultparse(logclif_parse);

View File

@ -129,7 +129,6 @@ struct online_login_data {
int waiting_disconnect;
int char_server;
};
extern DBMap* online_db; // uint32 account_id -> struct online_login_data*
/// Auth database
#define AUTH_TIMEOUT 30000
@ -141,19 +140,11 @@ struct auth_node {
char sex;
uint8 clienttype;
};
extern DBMap* auth_db; // uint32 account_id -> struct auth_node*
///Accessors
AccountDB* login_get_accounts_db(void);
/**
* 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);
struct online_login_data* login_get_online_user( uint32 account_id );
/**
* 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);
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.
* 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);
/**
* 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);
void login_online_db_setoffline( int char_server );
/**
* 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 )
return 0;
else{
struct auth_node* node;
uint32 account_id = RFIFOL(fd,2);
uint32 login_id1 = RFIFOL(fd,6);
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);
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 &&
node != NULL &&
node != nullptr &&
node->account_id == account_id &&
node->login_id1 == login_id1 &&
node->login_id2 == login_id2 &&
@ -105,7 +105,7 @@ int logchrif_parse_reqauth(int fd, int id,char* ip){
WFIFOSET(fd,21);
// each auth entry can only be used once
idb_remove(auth_db, account_id);
login_remove_auth_node( account_id );
}else{// authentication not found
ShowStatus("Char-server '%s': authentication of the account %d REFUSED (ip: %s).\n", ch_server[id].name, account_id, ip);
WFIFOHEAD(fd,21);
@ -529,17 +529,13 @@ int logchrif_parse_updonlinedb(int fd, int id){
if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
return 0;
else{
uint32 i, users;
online_db->foreach(online_db, login_online_db_setoffline, id); //Set all chars from this char-server offline first
users = RFIFOW(fd,4);
for (i = 0; i < users; i++) {
int 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;
if (p->waiting_disconnect != INVALID_TIMER){
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
p->waiting_disconnect = INVALID_TIMER;
}
//Set all chars from this char-server offline first
login_online_db_setoffline( id );
for( uint32 i = 0, users = RFIFOW(fd, 4); i < users; i++) {
uint32 aid = RFIFOL(fd,6+i*4);
login_add_online_user( id, aid );
}
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){
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);
return 1;
}
@ -628,12 +624,11 @@ int logchrif_parse_pincode_authfail(int fd){
struct mmo_account acc;
AccountDB* accounts = login_get_accounts_db();
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 == NULL )
if( ld == nullptr ){
return 0;
}
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)
*/
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_init(id);
}

View File

@ -47,7 +47,6 @@ static void logclif_auth_ok(struct login_session_data* sd) {
uint8 server_num, n;
uint32 subnet_char_ip;
struct auth_node* node;
int i;
#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 )
{// account is already marked as online!
if( data->char_server > -1 )
@ -108,7 +108,7 @@ static void logclif_auth_ok(struct login_session_data* sd) {
if( data->char_server == -1 )
{// client has authed but did not access char-server yet
// wipe previous session
idb_remove(auth_db, sd->account_id);
login_remove_auth_node(sd->account_id);
login_remove_online_user(sd->account_id);
data = NULL;
}
@ -150,21 +150,12 @@ static void logclif_auth_ok(struct login_session_data* sd) {
WFIFOSET(fd,header+size*server_num);
// create temporary auth entry
CREATE(node, struct auth_node, 1);
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'
data = login_add_online_user(-1, sd->account_id);
// schedule deletion of this node
data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0);
}
login_add_auth_node( sd, ip );
// mark client as 'online'
struct online_login_data* data = login_add_online_user(-1, sd->account_id);
// schedule deletion of this node
data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0);
}
/**

View File

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