Skip to content

Commit

Permalink
Removed dbmap/ers from login-server (#3658)
Browse files Browse the repository at this point in the history
Converted online_db to unordered_map
Converted auth_db to unordered_map
Removed ers_report option
  • Loading branch information
Lemongrass3110 committed Nov 7, 2018
1 parent 78edf85 commit 75d24ad
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 136 deletions.
153 changes: 80 additions & 73 deletions src/login/login.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand All @@ -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;
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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;
}

Expand All @@ -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 )
struct online_login_data* p = login_get_online_user( account_id );

if( p == nullptr ){
return;
if( p->waiting_disconnect != INVALID_TIMER )
delete_timer(p->waiting_disconnect, login_waiting_disconnect_timer);
}

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;

idb_remove(online_db, account_id);
return node;
}

void login_remove_auth_node( uint32 account_id ){
auth_db.erase( account_id );
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
27 changes: 8 additions & 19 deletions src/login/login.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
37 changes: 16 additions & 21 deletions src/login/loginchrif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 &&
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

ld = (struct online_login_data*)idb_get(online_db,acc.account_id);
struct online_login_data* ld = login_get_online_user( acc.account_id );

if( ld == NULL )
if( ld == nullptr ){
return 0;
}

login_log( host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed" );
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

2 comments on commit 75d24ad

@RadianFord
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be the reason why im getting these closed connection thing?

[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Saved Inventory (0) data to table inventory for char_id: 1
[Info]: Saved Cart (0) data to table cart_inventory for char_id: 1
[Info]: Saved char 1 - Radian: status skills.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Closed connection from '209.97.128.183'.
[Info]: Saved Inventory (0) data to table inventory for char_id: 1
[Info]: Saved Cart (0) data to table cart_inventory for char_id: 1
[Info]: Saved char 1 - Radian: status.
[Info]: Closed connection from '209.97.128.183'.

@Lemongrass3110
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RadianFord what you show is char server not login server.

Please sign in to comment.