Skip to content

Commit

Permalink
重构Player类,抽象出基类
Browse files Browse the repository at this point in the history
  • Loading branch information
jice1001 committed Sep 7, 2016
1 parent baadf24 commit 0283e9c
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 232 deletions.
59 changes: 4 additions & 55 deletions game_server/Game_Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,15 @@
* Author: zhangyalei
*/

#include "Common_Func.h"
#include "Game_Manager.h"

Game_Player::Game_Player(void):
gate_cid_(0),
player_cid_(0)
{
}
Game_Player::Game_Player(void) { }

Game_Player::~Game_Player(void) { }

int Game_Player::respond_success_result(int msg_id, Block_Buffer *buf) {
return respond_error_result(msg_id, 0, buf);
}

int Game_Player::respond_error_result(int msg_id, int err, Block_Buffer *buf) {
if (buf == 0) {
Block_Buffer msg_buf;
msg_buf.make_player_message(msg_id, err, player_cid_);
msg_buf.finish_message();
return GAME_MANAGER->send_to_gate(gate_cid_, msg_buf);
} else {
size_t head_len = sizeof(int16_t) + 3 * sizeof(int32_t); ///len(int16_t), msg_id(int32_t), status(int32_t), player_cid(int32_t)
if ((size_t)buf->get_read_idx() < head_len) {
LOG_ERROR("Block_Buffer space error, msg_id:%d", msg_id);
return 0;
}

/// insert buf head
size_t rd_idx = buf->get_read_idx();
size_t wr_idx = buf->get_write_idx();

buf->set_read_idx(buf->get_read_idx() - head_len);
int16_t len = buf->readable_bytes() - sizeof(int16_t);
buf->set_write_idx(buf->get_read_idx());

buf->write_int16(len);
buf->write_int32(msg_id);
buf->write_int32(err); /// status
buf->write_int32(player_cid_);
buf->set_write_idx(wr_idx);
GAME_MANAGER->send_to_gate(gate_cid_, *buf);

buf->set_read_idx(rd_idx); /// 复位传入的buf参数
return 0;
}
}

void Game_Player::reset(void) {
gate_cid_ = 0;
player_cid_ = 0;
recycle_tick_.reset();
}

int Game_Player::tick(Time_Value &now) {
if (recycle_tick_.status == Recycle_Tick::RECYCLE && now > recycle_tick_.recycle_tick) {
LOG_DEBUG("player recycle, player_cid_:%d", player_cid_);
LOG_INFO("game player recycle, gate_cid:%d, player_cid:%d,", gate_cid(), player_cid());
GAME_MANAGER->unbind_game_player(*this);
reset();
GAME_MANAGER->push_game_player(this);
Expand All @@ -69,12 +21,9 @@ int Game_Player::tick(Time_Value &now) {
}

int Game_Player::link_close() {
if (recycle_tick_.status == Recycle_Tick::RECYCLE)
return 0;

recycle_tick_.set(Recycle_Tick::RECYCLE);
if (Player::link_close() < 0) return -1;

int cid = GET_CID(gate_cid_, player_cid_);
int cid = GET_CID(gate_cid(), player_cid());
GAME_MANAGER->push_drop_player_cid(cid);
return 0;
}
22 changes: 3 additions & 19 deletions game_server/Game_Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,15 @@
#ifndef GAME_PLAYER_H_
#define GAME_PLAYER_H_

#include "Public_Struct.h"
#include "Player.h"

class Game_Player {
class Game_Player : public Player {
public:
Game_Player(void);
virtual ~Game_Player(void);

int respond_success_result(int msg_id, Block_Buffer *buf = 0);
int respond_error_result(int msg_id, int err, Block_Buffer *buf = 0);

inline void set_gate_cid(int gate_cid, int player_cid) {
gate_cid_ = gate_cid;
player_cid_ = player_cid;
}
inline int gate_cid(void) { return gate_cid_; }
inline int player_cid(void) { return player_cid_; }

void reset(void);
int tick(Time_Value &now);
int link_close(void);

private:
int gate_cid_; //gate连接game的cid
int player_cid_; //player连接gate的cid
Recycle_Tick recycle_tick_;
virtual int link_close(void);
};

#endif /* GAME_PLAYER_H_ */
2 changes: 1 addition & 1 deletion gate_server/Gate_Client_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int Gate_Client_Messager::process_block(Block_Buffer &buf) {
if (msg_id >= CLIENT_MASTER_MESSAGE_START && msg_id <= CLIENT_MASTER_MESSAGE_END) {
GATE_MANAGER->send_to_master(player_buf);
} else if (msg_id >= CLIENT_GAME_MESSAGE_START && msg_id <= CLIENT_GAME_MESSAGE_END) {
GATE_MANAGER->send_to_game(player->get_game_cid(), player_buf);
GATE_MANAGER->send_to_game(player->game_cid(), player_buf);
}
else {
LOG_ERROR("msg_id:%d error", msg_id);
Expand Down
2 changes: 1 addition & 1 deletion gate_server/Gate_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ Gate_Player *Gate_Manager::find_account_gate_player(std::string& account){
}

int Gate_Manager::unbind_gate_player(Gate_Player &player) {
player_cid_map_.erase(player.get_player_cid());
player_cid_map_.erase(player.player_cid());
player_account_map_.erase(player.get_account());
return 0;
}
Expand Down
29 changes: 11 additions & 18 deletions gate_server/Gate_Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@
#include "Gate_Manager.h"
#include "Common_Func.h"

Gate_Player::Gate_Player(void) :
player_cid_(0),
game_cid_(0) { }
Gate_Player::Gate_Player(void) { }

Gate_Player::~Gate_Player(void) { }

void Gate_Player::reset(void) {
player_cid_ = 0;
game_cid_ = 0;
account_.clear();
msg_info_.reset();
recycle_tick_.reset();
}

int Gate_Player::tick(Time_Value &now) {
if (recycle_tick_.status == Recycle_Tick::RECYCLE && now > recycle_tick_.recycle_tick) {
GATE_MANAGER->unbind_gate_player(*this);
Expand All @@ -30,21 +20,24 @@ int Gate_Player::tick(Time_Value &now) {
return 0;
}

int Gate_Player::link_close() {
if (recycle_tick_.status == Recycle_Tick::RECYCLE)
return 0;
void Gate_Player::reset(void) {
Player::reset();
account_.clear();
msg_info_.reset();
}

recycle_tick_.set(Recycle_Tick::RECYCLE);
int Gate_Player::link_close() {
if (Player::link_close() < 0) return -1;

//gate同步玩家下线到game
Block_Buffer game_buf;
game_buf.make_player_message(SYNC_GATE_GAME_PLAYER_LOGOUT, 0, player_cid_);
game_buf.make_player_message(SYNC_GATE_GAME_PLAYER_LOGOUT, 0, player_cid());
game_buf.finish_message();
GATE_MANAGER->send_to_game(game_cid_, game_buf);
GATE_MANAGER->send_to_game(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.make_player_message(SYNC_GATE_MASTER_PLAYER_LOGOUT, 0, player_cid());
master_buf.finish_message();
GATE_MANAGER->send_to_master(master_buf);

Expand Down
19 changes: 5 additions & 14 deletions gate_server/Gate_Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,26 @@
#ifndef GATE_PLAYER_H_
#define GATE_PLAYER_H_

#include "Public_Struct.h"
#include "Player.h"

class Gate_Player {
class Gate_Player : public Player {
public:
Gate_Player(void);
virtual ~Gate_Player(void);

void set_player_cid(int cid) { player_cid_ = cid; }
int get_player_cid(void) { return player_cid_; }
void set_game_cid(int cid) { game_cid_ = cid; }
int get_game_cid(void) { return game_cid_; }
void set_account(std::string &account) { account_ = account; }
std::string& get_account(void) { return account_; }
void reset(void);

int tick(Time_Value &now);
int link_close(void);
virtual void reset(void);
virtual int link_close(void);

/// 验证包序列和时间戳
/// 返回0为验证通过, -1操作过于频繁, -2包序号错, -3包时间戳错
///验证包序列和时间戳 返回0为验证通过, -1操作过于频繁, -2包序号错, -3包时间戳错
int verify_msg_info(uint32_t serial_cipher, uint32_t msg_time_cipher);
void reset_msg_info(void) { msg_info_.reset(); }

private:
int player_cid_; //client和gate连接的cid
int game_cid_; //player所在game_cid
std::string account_;
Msg_Info msg_info_;
Recycle_Tick recycle_tick_;
};

#endif /* GATE_PLAYER_H_ */
2 changes: 1 addition & 1 deletion login_server/Login_Client_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int Login_Client_Messager::connect_login(int cid, int msg_id, Block_Buffer &buf)
}

player->reset();
player->set_cid(cid);
player->set_player_cid(cid);
Login_Player_Info player_info;
player_info.session_tick = Time_Value::gettimeofday().sec();
player_info.account = msg.account;
Expand Down
2 changes: 1 addition & 1 deletion login_server/Login_Inner_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int Login_Inner_Messager::gate_login_player_account(int gate_cid, int32_t player
&& player->login_player_info().gate_port == msg.gate_port) {
//验证玩家session成功,关闭玩家与login的连接,开启玩家与gate的连接
gate_buf.make_player_message(SYNC_LOGIN_GATE_PLAYER_ACCOUNT, 0, player_cid);
LOGIN_MANAGER->close_client(player->get_cid());
LOGIN_MANAGER->close_client(player->player_cid());
} else {
LOG_DEBUG("login check session error, session:%s, account:%s, gate_ip:%s, gate_port:%d, gate_cid:%d, player_cid:%d",
msg.session.c_str(), msg.account.c_str(), msg.gate_ip.c_str(), msg.gate_port, gate_cid, player_cid);
Expand Down
6 changes: 3 additions & 3 deletions login_server/Login_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Login_Player *Login_Manager::find_cid_login_player(int cid) {

int Login_Manager::unbind_login_player(Login_Player &player) {
player_account_map_.erase(player.login_player_info().account);
player_cid_map_.erase(player.get_cid());
player_cid_map_.erase(player.player_cid());
return 0;
}

Expand Down Expand Up @@ -267,8 +267,8 @@ int Login_Manager::player_tick(Time_Value &now) {
for (Login_Player_Account_Map::iterator it = t_accouont_map.begin(); it != t_accouont_map.end(); ++it) {
if (it->second){
if (now.sec() - it->second->login_player_info().session_tick > 10) {
LOG_INFO("client drop from login, cid:%d, account:%s", it->second->get_cid(), it->second->login_player_info().account.c_str());
LOGIN_CLIENT_SERVER->receive().push_drop(it->second->get_cid()); //断开客户端与login的连接
LOG_INFO("client drop from login, cid:%d, account:%s", it->second->player_cid(), it->second->login_player_info().account.c_str());
LOGIN_CLIENT_SERVER->receive().push_drop(it->second->player_cid()); //断开客户端与login的连接
it->second->link_close();
}
it->second->tick(now);
Expand Down
20 changes: 14 additions & 6 deletions login_server/Login_Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@

#include "Login_Manager.h"

Login_Player::Login_Player(void) : cid_(0) { }
Login_Player::Login_Player(void) { }

Login_Player::~Login_Player(void) { }

void Login_Player::reset(void) {
cid_ = -1;
recycle_tick_.reset();
}

int Login_Player::tick(Time_Value &now) {
if (recycle_tick_.status == Recycle_Tick::RECYCLE && now > recycle_tick_.recycle_tick) {
LOGIN_MANAGER->unbind_login_player(*this);
Expand All @@ -24,3 +19,16 @@ int Login_Player::tick(Time_Value &now) {
}
return 0;
}

void Login_Player::reset(void) {
Player::reset();
player_info_.reset();
}

int Login_Player::link_close() {
if (Player::link_close() < 0) return -1;

//修改session_tick的值,防止login_manager对同一个链接重复push_drop
player_info_.session_tick += 10;
return 0;
}
43 changes: 6 additions & 37 deletions login_server/Login_Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,22 @@
#ifndef LOGIN_PLAYER_H_
#define LOGIN_PLAYER_H_

#include "Public_Struct.h"
#include "Player.h"

class Login_Player {
class Login_Player : public Player {
public:
Login_Player(void);
virtual ~Login_Player(void);

void set_cid(int cid);
int get_cid(void);
void set_player_info(const Login_Player_Info &player_info);
Login_Player_Info const &login_player_info(void) const;
void set_player_info(const Login_Player_Info &player_info) { player_info_ = player_info; }
Login_Player_Info const &login_player_info(void) { return player_info_; }

void reset(void);
int tick(Time_Value &now);
int link_close(void);
virtual void reset(void);
virtual int link_close(void);

private:
int cid_;
Recycle_Tick recycle_tick_;
Login_Player_Info player_info_;
};

////////////////////////////////////////////////////////////////////////////////
inline void Login_Player::set_cid(int cid) {
cid_ = cid;
}

inline int Login_Player::get_cid(void) {
return cid_;
}

inline void Login_Player::set_player_info(const Login_Player_Info &player_info) {
player_info_ = player_info;
}

inline Login_Player_Info const &Login_Player::login_player_info(void) const {
return player_info_;
}

inline int Login_Player::link_close() {
if (recycle_tick_.status == Recycle_Tick::RECYCLE)
return 0;

recycle_tick_.set(Recycle_Tick::RECYCLE);
//修改session_tick的值,防止login_manager对同一个链接重复push_drop
player_info_.session_tick += 10;
return 0;
}

#endif /* LOGIN_PLAYER_H_ */
Loading

0 comments on commit 0283e9c

Please sign in to comment.