Skip to content

Commit

Permalink
增加接收和发送字节统计
Browse files Browse the repository at this point in the history
  • Loading branch information
jice1001 committed Sep 13, 2016
1 parent 61c4ab1 commit 028878a
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 14 deletions.
4 changes: 4 additions & 0 deletions game_server/Game_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int Game_Manager::send_to_gate(int gate_cid, Block_Buffer &buf) {
LOG_ERROR("gate_cid = %d", gate_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GAME_GATE_SERVER->send_block(gate_cid, buf);
}

Expand All @@ -101,6 +102,7 @@ int Game_Manager::send_to_master(Block_Buffer &buf) {
LOG_ERROR("master_cid = %d", master_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GAME_MASTER_CONNECTOR->send_block(master_cid, buf);
}

Expand All @@ -110,6 +112,7 @@ int Game_Manager::send_to_db(Block_Buffer &buf) {
LOG_ERROR("db_cid = %d", db_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GAME_DB_CONNECTOR->send_block(db_cid, buf);
}

Expand All @@ -119,6 +122,7 @@ int Game_Manager::send_to_log(Block_Buffer &buf) {
LOG_ERROR("db_cid = %d", log_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return LOG_CONNECTOR->send_block(log_cid, buf);
}

Expand Down
2 changes: 2 additions & 0 deletions gate_server/Gate_Client_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Gate_Client_Messager *Gate_Client_Messager::instance(void) {
}

int Gate_Client_Messager::process_block(Block_Buffer &buf) {
GATE_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down
6 changes: 6 additions & 0 deletions gate_server/Gate_Inner_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Gate_Inner_Messager *Gate_Inner_Messager::instance(void) {
}

int Gate_Inner_Messager::process_login_block(Block_Buffer &buf) {
GATE_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -53,6 +55,8 @@ int Gate_Inner_Messager::process_login_block(Block_Buffer &buf) {
}

int Gate_Inner_Messager::process_game_block(Block_Buffer &buf) {
GATE_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -91,6 +95,8 @@ int Gate_Inner_Messager::process_game_block(Block_Buffer &buf) {
}

int Gate_Inner_Messager::process_master_block(Block_Buffer &buf) {
GATE_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down
4 changes: 4 additions & 0 deletions gate_server/Gate_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ int Gate_Manager::send_to_client(int player_cid, Block_Buffer &buf) {
LOG_ERROR("player_cid = %d", player_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GATE_CLIENT_SERVER->send_block(player_cid, buf);
}

Expand All @@ -130,6 +131,7 @@ int Gate_Manager::send_to_game(int cid, Block_Buffer &buf) {
}
Game_Connector_Map::iterator iter = game_connector_map_.find(cid);
if(iter != game_connector_map_.end()) {
add_send_bytes(buf.readable_bytes());
iter->second.connector->send_block(cid, buf);
} else {
LOG_ERROR("game_connector_map_ can not find game_cid = %d", cid);
Expand All @@ -143,6 +145,7 @@ int Gate_Manager::send_to_login(Block_Buffer &buf) {
LOG_ERROR("login_cid = %d", login_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GATE_LOGIN_CONNECTOR->send_block(login_cid, buf);
}

Expand All @@ -152,6 +155,7 @@ int Gate_Manager::send_to_master(Block_Buffer &buf) {
LOG_ERROR("master_cid = %d", master_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return GATE_MASTER_CONNECTOR->send_block(master_cid, buf);
}

Expand Down
2 changes: 2 additions & 0 deletions login_server/Login_Client_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Login_Client_Messager *Login_Client_Messager::instance(void) {
}

int Login_Client_Messager::process_block(Block_Buffer &buf) {
LOGIN_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down
2 changes: 2 additions & 0 deletions login_server/Login_Inner_Messager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Login_Inner_Messager *Login_Inner_Messager::instance(void) {
}

int Login_Inner_Messager::process_gate_block(Block_Buffer &buf) {
LOGIN_MANAGER->add_recv_bytes(buf.readable_bytes());

int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down
3 changes: 2 additions & 1 deletion login_server/Login_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int Login_Manager::send_to_client(int player_cid, Block_Buffer &buf) {
LOG_ERROR("player_cid = %d", player_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return LOGIN_CLIENT_SERVER->send_block(player_cid, buf);
}

Expand All @@ -100,7 +101,7 @@ int Login_Manager::send_to_gate(int gate_cid, Block_Buffer &buf){
LOG_ERROR("gate_cid = %d", gate_cid);
return -1;
}

add_send_bytes(buf.readable_bytes());
return LOGIN_GATE_SERVER->send_block(gate_cid, buf);
}

Expand Down
5 changes: 5 additions & 0 deletions master_server/Master_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ int Master_Manager::send_to_gate(int gate_cid, Block_Buffer &buf) {
LOG_ERROR("gate_cid = %d", gate_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return MASTER_GATE_SERVER->send_block(gate_cid, buf);
}

Expand All @@ -116,6 +117,7 @@ int Master_Manager::send_to_game(int game_cid, Block_Buffer &buf) {
LOG_ERROR("game_cid = %d", game_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return MASTER_GAME_SERVER->send_block(game_cid, buf);
}

Expand All @@ -125,6 +127,7 @@ int Master_Manager::send_to_db(Block_Buffer &buf) {
LOG_ERROR("db_cid = %d", db_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return MASTER_DB_CONNECTOR->send_block(db_cid, buf);
}

Expand All @@ -134,6 +137,7 @@ int Master_Manager::send_to_log(Block_Buffer &buf) {
LOG_ERROR("log_cid = %d", log_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return LOG_CONNECTOR->send_block(log_cid, buf);
}

Expand All @@ -142,6 +146,7 @@ int Master_Manager::send_to_http(int http_cid, Block_Buffer &buf) {
LOG_ERROR("game_cid = %d", http_cid);
return -1;
}
add_send_bytes(buf.readable_bytes());
return MASTER_HTTP_SERVER->send_block(http_cid, buf);
}

Expand Down
13 changes: 8 additions & 5 deletions misc/Server_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Server_Manager::Server_Manager(void):
tick_time_(Time_Value::zero),
player_tick_(Time_Value::zero),
server_info_tick_(Time_Value::zero),
msg_count_(false) { }
msg_count_(false),
total_recv_bytes(0),
total_send_bytes(0) { }

Server_Manager::~Server_Manager(void) {}

Expand Down Expand Up @@ -164,13 +166,13 @@ int Server_Manager::player_tick(Time_Value &now) {
}

int Server_Manager::server_info_tick(Time_Value &now) {
if (now - server_info_tick_ < Time_Value(300, 0))
if (now - server_info_tick_ < Time_Value(30, 0))
return 0;
server_info_tick_ = now;

get_server_info();
print_server_info();
print_msg_count();
print_msg_info();
return 0;
}

Expand All @@ -181,10 +183,11 @@ void Server_Manager::print_server_info(void) {
LOG_INFO("%s server_id:%d block_pool_ free = %d, used = %d", server_name_.c_str(), server_id_, block_pool_.free_obj_list_size(), block_pool_.used_obj_list_size());
}

void Server_Manager::print_msg_count(void) {
void Server_Manager::print_msg_info(void) {
std::stringstream stream;
for (Msg_Count_Map::iterator it = msg_count_map_.begin(); it != msg_count_map_.end(); ++it) {
stream << (it->first) << "\t" << (it->second) << std::endl;
}
LOG_INFO("%s server_id:%d msg_count:%d content:%s", server_name_.c_str(), server_id_, msg_count_map_.size(), stream.str().c_str());
LOG_INFO("%s server_id:%d total_recv_bytes:%d total_send_bytes:%d msg_count:%d %s",
server_name_.c_str(), server_id_, total_recv_bytes, total_send_bytes, msg_count_map_.size(), stream.str().c_str());
}
28 changes: 20 additions & 8 deletions misc/Server_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ class Server_Manager: public Thread {
virtual void get_server_info(void);
virtual void print_server_info(void);

//消息数量统计
void print_msg_count(void);
//消息统计
void print_msg_info(void);
inline void add_msg_count(int msg_id) {
if (msg_count_) {
++(msg_count_map_[msg_id]);
}
}
inline void add_recv_bytes(int bytes) {
if (msg_count_) {
total_recv_bytes += bytes;
}
}
inline void add_send_bytes(int bytes) {
if (msg_count_) {
total_send_bytes += bytes;
}
}

protected:
Player_Cid_Map player_cid_map_;
Expand All @@ -94,18 +104,20 @@ class Server_Manager: public Thread {

private:
Block_Pool block_pool_;
Close_List close_list_; //其中的连接cid在2秒后投递到通信层关闭
Close_List close_list_; //连接cid在2秒后投递到通信层关闭

int server_id_; //服务器id
int server_status_; //服务器状态
std::string server_name_; //服务器名字
Time_Value server_start_time_; //服务器开始时间
Time_Value tick_time_; //服务器tick时间
int server_id_; //服务器id
int server_status_; //服务器状态
std::string server_name_; //服务器名字
Time_Value server_start_time_; //服务器开始时间
Time_Value tick_time_; //服务器tick时间
Time_Value player_tick_;
Time_Value server_info_tick_;

bool msg_count_;
Msg_Count_Map msg_count_map_;
int total_recv_bytes; //总共接收的字节
int total_send_bytes; //总共发送的字节
};

#endif /* SERVER_MANAGER_H_ */
14 changes: 14 additions & 0 deletions v8/Server_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void get_game_timer_id(const FunctionCallbackInfo<Value>& args) {
void pop_game_gate_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = GAME_MANAGER->pop_game_gate_data();
if (buf) {
GAME_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t gate_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -71,6 +73,8 @@ void pop_game_gate_msg_object(const FunctionCallbackInfo<Value>& args) {
void pop_game_master_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = GAME_MANAGER->pop_game_master_data();
if (buf) {
GAME_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t master_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -99,6 +103,8 @@ void pop_game_master_msg_object(const FunctionCallbackInfo<Value>& args) {
void pop_game_db_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = GAME_MANAGER->pop_game_db_data();
if (buf) {
GAME_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t db_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -242,6 +248,8 @@ void get_master_timer_id(const FunctionCallbackInfo<Value>& args) {
void pop_master_gate_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = MASTER_MANAGER->pop_master_gate_data();
if (buf) {
MASTER_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t gate_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -270,6 +278,8 @@ void pop_master_gate_msg_object(const FunctionCallbackInfo<Value>& args) {
void pop_master_game_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = MASTER_MANAGER->pop_master_game_data();
if (buf) {
MASTER_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t game_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -298,6 +308,8 @@ void pop_master_game_msg_object(const FunctionCallbackInfo<Value>& args) {
void pop_master_db_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = MASTER_MANAGER->pop_master_db_data();
if (buf) {
MASTER_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t db_cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
Expand Down Expand Up @@ -326,6 +338,8 @@ void pop_master_db_msg_object(const FunctionCallbackInfo<Value>& args) {
void pop_master_http_msg_object(const FunctionCallbackInfo<Value>& args) {
Block_Buffer *buf = MASTER_MANAGER->pop_master_http_data();
if (buf) {
MASTER_MANAGER->add_recv_bytes(buf->readable_bytes());

int32_t http_cid = 0;
std::string post_data = "";
buf->read_int32(http_cid);
Expand Down

0 comments on commit 028878a

Please sign in to comment.