Skip to content

Commit

Permalink
服务器连接断开重构
Browse files Browse the repository at this point in the history
  • Loading branch information
jice1001 committed Sep 12, 2016
1 parent 8e75655 commit b7a5b03
Show file tree
Hide file tree
Showing 29 changed files with 321 additions and 355 deletions.
54 changes: 37 additions & 17 deletions game_server/Game_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,36 @@ int Game_Manager::init(int server_id) {
return 0;
}

int Game_Manager::unbind_player(Player &player) {
Server_Manager::unbind_player(player);
player_cid_map_.erase(GET_CID(player.gate_cid(), player.player_cid()));
int Game_Manager::close_client(int gate_cid, int player_cid, int error_code) {
if (Server_Manager::close_client(gate_cid, player_cid, error_code) < 0) return -1;

//将玩家cid投放到js层
push_drop_player_cid(GET_CID(gate_cid, player_cid));

if (error_code != 0) {
//gate与game断开连接不通知客户端,其他情况通知
Block_Buffer buf;
buf.make_player_message(ACTIVE_DISCONNECT, error_code, player_cid);
buf.finish_message();
send_to_gate(gate_cid, buf);
}
return 0;
}

int Game_Manager::recycle_player(int gate_cid, int player_cid) {
Server_Manager::recycle_player(gate_cid, player_cid);

int cid = GET_CID(gate_cid, player_cid);
Game_Player *player = dynamic_cast<Game_Player*>(find_cid_player(cid));
if (!player) {
return -1;
}

player_cid_map_.erase(cid);
player_role_id_map_.erase(player->role_id());
player_account_map_.erase(player->account());
player->reset();
push_player(player);
return 0;
}

Expand Down Expand Up @@ -97,24 +124,15 @@ int Game_Manager::send_to_log(Block_Buffer &buf) {
return LOG_CONNECTOR->send_block(log_cid, buf);
}

int Game_Manager::close_client(int gate_cid, int player_cid, int error_code) {
Block_Buffer buf;
buf.make_player_message(ACTIVE_DISCONNECT, error_code, player_cid);
buf.finish_message();
return send_to_gate(gate_cid, buf);
}

int Game_Manager::process_list(void) {
int32_t cid = 0;

while (1) {
bool all_empty = true;

//掉线玩家列表
//掉线gate列表
if (! drop_gate_cid_list_.empty()) {
all_empty = false;
cid = drop_gate_cid_list_.pop_front();
process_drop_gate_cid(cid);
int gate_cid = drop_gate_cid_list_.pop_front();
process_drop_gate_cid(gate_cid);
}
//定时器列表
if (! tick_list_.empty()) {
Expand All @@ -129,11 +147,13 @@ int Game_Manager::process_list(void) {
return 0;
}

void Game_Manager::process_drop_gate_cid(int gate_cid) {
int Game_Manager::process_drop_gate_cid(int gate_cid) {
for (Player_Cid_Map::iterator iter = player_cid_map_.begin(); iter != player_cid_map_.end(); ) {
if (iter->second->gate_cid() == gate_cid) {
iter->second->link_close();
//gate断开与game的连接,game上所有通过该gate连接进来的玩家数据回收
close_client(gate_cid, iter->second->player_cid(), 0);
player_cid_map_.erase(iter++);
}
}
return 0;
}
19 changes: 9 additions & 10 deletions game_server/Game_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Game_Manager: public Server_Manager {
inline Game_Player *pop_player(void) { return player_pool_.pop(); }
inline int push_player(Game_Player *player) { return player_pool_.push(player); }

virtual int unbind_player(Player &player);
virtual int close_client(int gate_cid, int player_cid, int error_code);
virtual int recycle_player(int gate_cid, int player_cid);
virtual int free_cache(void);
virtual void get_server_info(void);
virtual void print_server_info(void);
Expand All @@ -30,13 +31,10 @@ class Game_Manager: public Server_Manager {
int send_to_db(Block_Buffer &buf);
int send_to_log(Block_Buffer &buf);

//关闭客户端连接
int close_client(int gate_cid, int player_cid, int error_code);

//消息处理
int process_list();
void process_drop_gate_cid(int gate_cid);
void push_drop_gate_cid(int cid);
int process_drop_gate_cid(int gate_cid);
int push_drop_gate_cid(int gate_cid);
int push_tick(int x);
int push_game_gate_data(Block_Buffer *buf);
Block_Buffer* pop_game_gate_data();
Expand All @@ -59,8 +57,8 @@ class Game_Manager: public Server_Manager {
int server_id_;
Player_Pool player_pool_;

Int_List drop_gate_cid_list_; //掉线gate_cid列表,让通过该gate连接到game的所有玩家掉线
Int_List tick_list_; //定时器列表
Int_List drop_gate_cid_list_; //掉线gate_cid列表让通过该gate连接到game的所有玩家掉线
Int_List tick_list_; //定时器列表
Data_List game_gate_data_list_; //gate--game
Data_List game_db_data_list_; //db-->game
Data_List game_master_data_list_; //master-->game
Expand All @@ -74,8 +72,9 @@ class Game_Manager: public Server_Manager {
#define GAME_MANAGER Game_Manager::instance()

////////////////////////////////////////////////////////////////////////////////
inline void Game_Manager::push_drop_gate_cid(int cid) {
drop_gate_cid_list_.push_back(cid);
inline int Game_Manager::push_drop_gate_cid(int gate_cid) {
drop_gate_cid_list_.push_back(gate_cid);
return 0;
}

inline int Game_Manager::push_tick(int x) {
Expand Down
19 changes: 0 additions & 19 deletions game_server/Game_Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,3 @@
Game_Player::Game_Player(void) { }

Game_Player::~Game_Player(void) { }

int Game_Player::tick(Time_Value &now) {
if (recycle_tick_.status == Recycle_Tick::RECYCLE && now > recycle_tick_.recycle_tick) {
LOG_INFO("game player recycle, gate_cid:%d, player_cid:%d,", gate_cid(), player_cid());
GAME_MANAGER->unbind_player(*this);
reset();
GAME_MANAGER->push_player(this);
return 0;
}
return 0;
}

int Game_Player::link_close(bool server_close) {
if (Player::link_close() < 0) return -1;

int cid = GET_CID(gate_cid(), player_cid());
GAME_MANAGER->push_drop_player_cid(cid);
return 0;
}
3 changes: 0 additions & 3 deletions game_server/Game_Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class Game_Player : public Player {
public:
Game_Player(void);
virtual ~Game_Player(void);

virtual int tick(Time_Value &now);
virtual int link_close(bool server_close = false);
};

#endif /* GAME_PLAYER_H_ */
3 changes: 1 addition & 2 deletions game_server/Game_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ Game_Gate_Server *Game_Gate_Server::instance(void) {

void Game_Gate_Server::process_list(void) {
Block_Buffer *buf = 0;
int cid = 0;
while (1) {
bool all_empty = true;
if (!drop_cid_list_.empty()) {
all_empty = false;
cid = drop_cid_list_.pop_front();
int cid = drop_cid_list_.pop_front();
GAME_MANAGER->push_drop_gate_cid(cid);
}
if (!block_list_.empty()) {
Expand Down
14 changes: 3 additions & 11 deletions gate_server/Gate_Client_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ int Gate_Client_Messager::process_block(Block_Buffer &buf) {
Gate_Player *player = dynamic_cast<Gate_Player*>(GATE_MANAGER->find_cid_player(cid));
if (!player) {
LOG_ERROR("cannot find player_cid = %d msg_id = %d ", cid, msg_id);
return GATE_MANAGER->close_client(cid);
return GATE_MANAGER->close_client(0, cid, ERROR_NOT_LOGIN);
}

/// 校验包, 用于防截包/自组包/重复发包
if (GATE_MANAGER->verify_pack()) {
int result = player->verify_msg_info(serial_cipher, msg_time_cipher);
if (result != 0) {
LOG_ERROR("msg verify error, player_cid:%d, len:%d, serial_cipher:%d, msg_time_cipher:%d, msg_id:%d, status:%d", cid, len, serial_cipher, msg_time_cipher, msg_id, status);
Block_Buffer res_buf;
res_buf.make_server_message(msg_id, result);
res_buf.finish_message();
GATE_MANAGER->send_to_client(cid, res_buf);
return GATE_MANAGER->close_client(cid);
return GATE_MANAGER->close_client(0, cid, result);
}
}

Expand Down Expand Up @@ -112,11 +108,7 @@ int Gate_Client_Messager::connect_gate(int cid, Block_Buffer &buf) {
else
{
LOG_WARN("connect_gate, repeat connect, cid:%d, account:%s, session:%s", cid, msg.account.c_str(), msg.session.c_str());
Block_Buffer res_buf;
res_buf.make_server_message(RES_CONNECT_GATE, ERROR_DISCONNECT_RELOGIN);
res_buf.finish_message();
GATE_MANAGER->send_to_client(cid, res_buf);
GATE_MANAGER->close_client(cid);
GATE_MANAGER->close_client(0, cid, ERROR_DISCONNECT_RELOGIN);
}

return 0;
Expand Down
30 changes: 15 additions & 15 deletions gate_server/Gate_Inner_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ int Gate_Inner_Messager::process_login_block(Block_Buffer &buf) {

GATE_MANAGER->add_msg_count(msg_id);
if (msg_id == SYNC_LOGIN_GATE_PLAYER_ACCOUNT) {
MSG_140001 msg;
msg.deserialize(buf);
Block_Buffer player_buf;
player_buf.make_server_message(RES_CONNECT_GATE, status);
MSG_500101 player_msg;
player_msg.account = msg.account;
player_msg.serialize(player_buf);
player_buf.finish_message();
GATE_MANAGER->send_to_client(player_cid, player_buf);

if (status == 0) {
//玩家成功登录
MSG_140001 msg;
msg.deserialize(buf);
Block_Buffer player_buf;
player_buf.make_server_message(RES_CONNECT_GATE, status);
MSG_500101 player_msg;
player_msg.account = msg.account;
player_msg.serialize(player_buf);
player_buf.finish_message();
GATE_MANAGER->send_to_client(player_cid, player_buf);

process_success_login(player_cid, msg.account);
}
else
{
GATE_MANAGER->close_client(player_cid);
} else {
GATE_MANAGER->close_client(0, player_cid, status);
}
}
return 0;
Expand Down Expand Up @@ -81,7 +79,7 @@ int Gate_Inner_Messager::process_game_block(Block_Buffer &buf) {
master_buf.finish_message();
GATE_MANAGER->send_to_master(master_buf);
} else if (msg_id == ACTIVE_DISCONNECT) {
return GATE_MANAGER->close_client(player_cid);
return GATE_MANAGER->close_client(0, player_cid, status);
}

Block_Buffer player_buf;
Expand Down Expand Up @@ -120,6 +118,8 @@ int Gate_Inner_Messager::process_master_block(Block_Buffer &buf) {
buffer.finish_message();
GATE_MANAGER->send_to_game(game_cid, buffer);
return 0;
} else if (msg_id == ACTIVE_DISCONNECT) {
return GATE_MANAGER->close_client(0, player_cid, status);
}

Block_Buffer player_buf;
Expand Down
75 changes: 44 additions & 31 deletions gate_server/Gate_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,49 @@ int Gate_Manager::init(int server_id) {
return 0;
}

int Gate_Manager::unbind_player(Player &player) {
Server_Manager::unbind_player(player);
player_cid_map_.erase(player.player_cid());
int Gate_Manager::close_client(int gate_cid, int player_cid, int error_code) {
if (Server_Manager::close_client(gate_cid, player_cid, error_code) < 0) return -1;

Gate_Player *player = dynamic_cast<Gate_Player*>(find_cid_player(player_cid));
if (player) {
//gate同步玩家下线到game
Block_Buffer game_buf;
game_buf.make_player_message(SYNC_GATE_GAME_PLAYER_LOGOUT, 0, player_cid);
game_buf.finish_message();
GATE_MANAGER->send_to_game(player->game_cid(), game_buf);

//gate同步玩家下线到master
Block_Buffer master_buf;
master_buf.make_player_message(SYNC_GATE_MASTER_PLAYER_LOGOUT, 0, player_cid);
master_buf.finish_message();
GATE_MANAGER->send_to_master(master_buf);
}

if (error_code != 0) {
//客户端主动断开连接不通知客户端,其他情况通知
Block_Buffer buf;
buf.make_server_message(ACTIVE_DISCONNECT, error_code);
buf.finish_message();
send_to_client(player_cid, buf);
}
return 0;
}

int Gate_Manager::recycle_player(int gate_cid, int player_cid) {
Server_Manager::recycle_player(gate_cid, player_cid);
//断开服务器与客户端的通信层连接
GATE_CLIENT_SERVER->receive().push_drop(player_cid);

Gate_Player *player = dynamic_cast<Gate_Player*>(find_cid_player(player_cid));
if (!player) {
return -1;
}

player_cid_map_.erase(player->player_cid());
player_role_id_map_.erase(player->role_id());
player_account_map_.erase(player->account());
player->reset();
push_player(player);
return 0;
}

Expand All @@ -58,20 +98,6 @@ int Gate_Manager::free_cache(void) {
return 0;
}

int Gate_Manager::close_list_tick(Time_Value &now) {
Close_Info info;
while (! close_list_.empty()) {
info = close_list_.front();
if (now - info.timestamp > Time_Value(2, 0)) {
close_list_.pop_front();
GATE_CLIENT_SERVER->receive().push_drop(info.cid);
} else {
break;
}
}
return 0;
}

void Gate_Manager::get_server_info(void) {
gate_client_server_info_.reset();
gate_login_connector_info_.reset();
Expand Down Expand Up @@ -131,16 +157,6 @@ int Gate_Manager::send_to_master(Block_Buffer &buf) {
return GATE_MASTER_CONNECTOR->send_block(master_cid, buf);
}

int Gate_Manager::close_client(int player_cid) {
if (player_cid >= 2) {
Close_Info info(player_cid, tick_time());
close_list_.push_back(info);
} else {
LOG_ERROR("close_client, player_cid < 2");
}
return 0;
}

int Gate_Manager::process_list(void) {
int32_t cid = 0;
Block_Buffer *buf = 0;
Expand Down Expand Up @@ -221,10 +237,7 @@ int Gate_Manager::process_list(void) {
}

void Gate_Manager::process_drop_cid(int cid) {
Gate_Player *player = dynamic_cast<Gate_Player*>(GATE_MANAGER->find_cid_player(cid));
if (player) {
player->link_close();
}
close_client(0, cid, 0);
}

void Gate_Manager::get_server_ip_port(int player_cid, std::string &ip, int &port) {
Expand Down
7 changes: 2 additions & 5 deletions gate_server/Gate_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Gate_Manager: public Server_Manager {
Gate_Player *pop_player(void) { return player_pool_.pop(); }
int push_player(Gate_Player *player) { return player_pool_.push(player); }

virtual int unbind_player(Player &player);
virtual int close_client(int gate_cid, int player_cid, int error_code);
virtual int recycle_player(int gate_cid, int player_cid);
virtual int free_cache(void);
virtual int close_list_tick(Time_Value &now);
virtual void get_server_info(void);
virtual void print_server_info(void);

Expand All @@ -43,9 +43,6 @@ class Gate_Manager: public Server_Manager {
int send_to_login(Block_Buffer &buf);
int send_to_master(Block_Buffer &buf);

//关闭客户端连接
int close_client(int player_cid);

//消息处理
int process_list();
void process_drop_cid(int cid);
Expand Down
Loading

0 comments on commit b7a5b03

Please sign in to comment.