Skip to content

Commit

Permalink
修改buffer读取数据方法
Browse files Browse the repository at this point in the history
  • Loading branch information
jice1001 committed Aug 25, 2016
1 parent 8d0db30 commit 41923db
Show file tree
Hide file tree
Showing 37 changed files with 459 additions and 883 deletions.
16 changes: 7 additions & 9 deletions config/struct/message.define
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
*/

enum Client_Login_Message {
REQ_CLIENT_REGISTER = 100000, //注册
REQ_CLIENT_LOGIN = 100001, //登录
REQ_CONNECT_GATE = 100002, //客户端登录gate
REQ_HEARTBEAT = 100003, //心跳
REQ_CONNECT_LOGIN = 100001, //客户端登录login
REQ_CONNECT_GATE = 100101, //客户端登录gate
REQ_SEND_HEARTBEAT = 100102, //发送心跳到gate
};

enum Login_Client_Message {
RES_CLIENT_REGISTER = 500000, //注册(返回)
RES_CLIENT_LOGIN = 500001, //登录(返回)
RES_CONNECT_GATE = 500002, //客户端登录gate(返回)
RES_HEARTBEAT = 500003, //心跳(返回)
RES_CONNECT_LOGIN = 500001, //客户端登录login(返回)
RES_CONNECT_GATE = 500101, //客户端登录gate(返回)
RES_SEND_HEARTBEAT = 500102, //发送心跳到gate(返回)
};

enum Client_Master_Message {
Expand All @@ -49,7 +47,7 @@ enum Master_Client_Message {
RES_GUILD_ALLOW_JOIN = 510104, //允许入帮(返回)
RES_GUILD_KICK_OUT = 510105, //踢出帮会(返回)
RES_FETCH_RANK = 510201, //拉取排行榜信息(返回)
RES_PLAYER_CHANGE_SCENE = 510300, //切换场景时通知客户端选择哪个game
RES_PLAYER_CHANGE_SCENE = 510300, //切换场景时通知客户端选择哪个game
};

enum Client_Game_Message {
Expand Down
46 changes: 16 additions & 30 deletions config/struct/msg_struct.xml
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
<root>

<!--Client与Login通信消息-->
<MSG_100000>
<!--注册-->
<head msg_id="100000"/>
<arg type="string" name="account"/>
<arg type="string" name="password"/>
</MSG_100000>

<MSG_500000>
<head msg_id="500000"/>
<arg type="string" name="ip" desc="gate_ip"/>
<arg type="int32" name="port" desc="gate_port"/>
<arg type="string" name="session"/>
</MSG_500000>

<MSG_100001>
<!--登录-->
<!--客户端登录login-->
<head msg_id="100001"/>
<arg type="string" name="account"/>
<arg type="string" name="password"/>
</MSG_100001>

<MSG_500001>
<head msg_id="500001"/>
<arg type="string" name="ip" desc="gate_ip"/>
<arg type="int32" name="port" desc="gate_port"/>
<arg type="string" name="gate_ip"/>
<arg type="int32" name="gate_port"/>
<arg type="string" name="session"/>
</MSG_500001>

<MSG_100002>
<MSG_100101>
<!--客户端登录gate-->
<head msg_id="100002"/>
<head msg_id="100101"/>
<arg type="string" name="account"/>
<arg type="string" name="session" desc="登录验证的seesion"/>
</MSG_100002>
</MSG_100101>

<MSG_500002>
<head msg_id="500002"/>
<MSG_500101>
<head msg_id="500101"/>
<arg type="string" name="account"/>
</MSG_500002>
</MSG_500101>

<MSG_100003>
<!--心跳-->
<head msg_id="100003"/>
<MSG_100102>
<!--发送心跳到gate-->
<head msg_id="100102"/>
<arg type="int32" name="client_time"/>
</MSG_100003>
</MSG_100102>

<MSG_500003>
<head msg_id="500003"/>
<MSG_500102>
<head msg_id="500102"/>
<arg type="int32" name="client_time"/>
<arg type="int32" name="server_time"/>
</MSG_500003>
</MSG_500102>

<!--Client与Master通信消息-->
<MSG_110001>
Expand Down
9 changes: 6 additions & 3 deletions db_server/DB_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ int DB_Manager::start(void) {

int DB_Manager::push_data_block(Block_Buffer *buf) {
int read_idx = buf->get_read_idx();
/*int32_t cid*/ buf->read_int32();
/*int16_t len*/ buf->read_int16();
int32_t msg_id = buf->read_int32();
int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
buf->read_int32(cid);
buf->read_int16(len);
buf->read_int32(msg_id);
buf->set_read_idx(read_idx);

switch (msg_id) {
Expand Down
36 changes: 22 additions & 14 deletions db_server/DB_Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ int DB_Worker::process_data_block(Block_Buffer *buffer) {
return -1;
}

int32_t cid = buffer->read_int32();
/*int16_t len*/ buffer->read_int16();
int32_t msg_id = buffer->read_int32();
int32_t status = buffer->read_int32();
int32_t cid = 0;
int16_t len = 0;
int32_t msg_id = 0;
int32_t status = 0;
buffer->read_int32(cid);
buffer->read_int16(len);
buffer->read_int32(msg_id);
buffer->read_int32(status);

switch (msg_id) {
case SYNC_GAME_DB_LOAD_PLAYER: {
std::string account = buffer->read_string();
std::string account;
buffer->read_string(account);
process_load_player(cid, account);
break;
}
Expand All @@ -85,7 +90,8 @@ int DB_Worker::process_data_block(Block_Buffer *buffer) {
break;
}
case SYNC_MASTER_DB_DELETE_DATA: {
std::string struct_name = buffer->read_string();
std::string struct_name;
buffer->read_string(struct_name);
Struct_Name_Map::iterator iter = DB_MANAGER->db_struct_name_map().find(struct_name);
if(iter == DB_MANAGER->db_struct_name_map().end()){
LOG_ERROR("Can not find the struct_name: %s", struct_name.c_str());
Expand Down Expand Up @@ -115,12 +121,12 @@ int DB_Worker::process_create_player(int cid, Create_Role_Info &role_info) {
int64_t role_id = 0;
Block_Buffer buf;
if ((role_id = DB_MANAGER->create_player(role_info)) > 0) {
buf.make_inner_message(SYNC_DB_GAME_CREATE_PLAYER, ROLE_SUCCESS_CREATE);
buf.make_server_message(SYNC_DB_GAME_CREATE_PLAYER, ROLE_SUCCESS_CREATE);
buf.write_string(role_info.account);
//创建所有玩家表,构建登录消息buf
create_player_data(role_id, buf);
} else {
buf.make_inner_message(SYNC_DB_GAME_CREATE_PLAYER, ROLE_HAS_EXIST);
buf.make_server_message(SYNC_DB_GAME_CREATE_PLAYER, ROLE_HAS_EXIST);
buf.write_string(role_info.account);
}
buf.finish_message();
Expand All @@ -142,7 +148,7 @@ int DB_Worker::process_load_player(int cid, std::string &account) {
}

Block_Buffer buf;
buf.make_inner_message(SYNC_DB_GAME_LOAD_PLAYER, status);
buf.make_server_message(SYNC_DB_GAME_LOAD_PLAYER, status);
buf.write_string(account);
if (role_id > 0) {
load_player_data(role_id, buf);
Expand All @@ -154,14 +160,16 @@ int DB_Worker::process_load_player(int cid, std::string &account) {

int DB_Worker::process_save_player(int cid, int status, Block_Buffer &buffer) {
//先把两个额外字段读出来,再保存玩家数据,防止buffer错乱
bool logout = buffer.read_bool();
std::string account = buffer.read_string();
bool logout;
std::string account;
buffer.read_bool(logout);
buffer.read_string(account);
if (logout) {
//离线保存
save_player_data(buffer);

Block_Buffer buf;
buf.make_inner_message(SYNC_DB_GAME_SAVE_PLAYER);
buf.make_server_message(SYNC_DB_GAME_SAVE_PLAYER, 0);
buf.write_string(account);
buf.finish_message();
DB_MANAGER->send_data_block(cid, buf);
Expand All @@ -174,7 +182,7 @@ int DB_Worker::process_save_player(int cid, int status, Block_Buffer &buffer) {

int DB_Worker::process_create_guild(int cid, Create_Guild_Info &guild_info) {
Block_Buffer buf;
buf.make_inner_message(SYNC_DB_MASTER_CREATE_GUILD);
buf.make_server_message(SYNC_DB_MASTER_CREATE_GUILD, 0);
guild_info.guild_id = DB_MANAGER->create_guild(guild_info);
guild_info.serialize(buf);
buf.finish_message();
Expand All @@ -193,7 +201,7 @@ int DB_Worker::process_load_master(int cid) {
}

Block_Buffer buf;
buf.make_inner_message(it->second->msg_id() + 400000);
buf.make_server_message(it->second->msg_id() + 400000, 0);
it->second->load_data(0, buf);
buf.finish_message();
DB_MANAGER->send_data_block(cid, buf);
Expand Down
37 changes: 24 additions & 13 deletions db_server/Mongo_Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void Mongo_Struct::load_data(int64_t key_index, Block_Buffer &buffer) {

void Mongo_Struct::save_data(Block_Buffer &buffer) {
BSONObjBuilder set_builder;
int64_t key_index = buffer.peek_int64();
int64_t key_index = 0;
buffer.peek_int64(key_index);
LOG_INFO("table %s save key_index:%ld", table_name().c_str(), key_index);
if (key_index <= 0) {
return;
Expand All @@ -91,17 +92,20 @@ void Mongo_Struct::save_data(Block_Buffer &buffer) {
}

void Mongo_Struct::save_data_vector(Block_Buffer &buffer) {
uint16_t count = buffer.read_uint16();
uint16_t count = 0;
buffer.read_uint16(count);
LOG_INFO("table %s save size:%d", table_name().c_str(), count);
for(int i = 0; i < count; i++) {
save_data(buffer);
}
}

void Mongo_Struct::delete_data(Block_Buffer &buffer) {
uint16_t count = buffer.read_uint16();
uint16_t count = 0;
buffer.read_uint16(count);
for(int i = 0; i < count; i++) {
int64_t key_index = buffer.read_int64();
int64_t key_index = 0;
buffer.read_int64(key_index);
MONGO_CONNECTION.remove(table_name(), MONGO_QUERY(index_name() << (long long int)(key_index)));
}
}
Expand Down Expand Up @@ -253,31 +257,38 @@ void Mongo_Struct::build_buffer_struct(const Field_Info &field_info, Block_Buffe

void Mongo_Struct::build_bson_arg(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder){
if(field_info.field_type == "int8") {
int8_t value = buffer.read_int8();
int8_t value = 0;
buffer.read_int8(value);
builder << field_info.field_name << (int)value;
}
else if(field_info.field_type == "int16") {
int16_t value = buffer.read_int16();
int16_t value = 0;
buffer.read_int16(value);
builder << field_info.field_name << (int)value;
}
else if(field_info.field_type == "int32") {
int32_t value = buffer.read_int32();
int32_t value = 0;
buffer.read_int32(value);
builder << field_info.field_name << (int)value;
}
else if(field_info.field_type == "int64") {
int64_t value = buffer.read_int64();
int64_t value = 0;
buffer.read_int64(value);
builder << field_info.field_name << (long long int)value;
}
else if(field_info.field_type == "double") {
double value = buffer.read_double();
double value = 0;
buffer.read_double(value);
builder << field_info.field_name << value;
}
else if(field_info.field_type == "bool") {
bool value = buffer.read_bool();
bool value = false;
buffer.read_bool(value);
builder << field_info.field_name << value;
}
else if(field_info.field_type == "string") {
std::string value = buffer.read_string();
std::string value = "";
buffer.read_string(value);
builder << field_info.field_name << value;
}
else {
Expand All @@ -287,8 +298,8 @@ void Mongo_Struct::build_bson_arg(const Field_Info &field_info, Block_Buffer &bu

void Mongo_Struct::build_bson_vector(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder) {
std::vector<BSONObj> bson_vec;
uint16_t vec_size = buffer.read_uint16();

uint16_t vec_size = 0;
buffer.read_uint16(vec_size);
if(is_struct(field_info.field_type)) {
for(uint16_t i = 0; i < vec_size; ++i) {
BSONObjBuilder obj_builder;
Expand Down
Loading

0 comments on commit 41923db

Please sign in to comment.