├── .cproject ├── .gitignore ├── .project ├── Makefile ├── ReadMe.md ├── config ├── bag │ └── item.json ├── monster │ ├── ai_behavior.xml │ └── ai_config.json ├── player │ ├── level.json │ └── util.json ├── server │ ├── server_conf.json │ └── server_misc.json ├── sql │ ├── account.sql │ ├── game.sql │ └── log.sql ├── struct │ ├── game_struct.xml │ ├── log_struct.xml │ ├── message.define │ └── msg_struct.xml └── vip │ ├── recharge.json │ └── vip.json ├── db_server ├── DB_Manager.cpp ├── DB_Manager.h ├── DB_Server.cpp ├── DB_Server.h ├── DB_Worker.cpp ├── DB_Worker.h ├── Mongo_Operator.cpp ├── Mongo_Operator.h ├── Mongo_Struct.cpp ├── Mongo_Struct.h ├── Mysql_Operator.cpp ├── Mysql_Operator.h ├── Mysql_Struct.cpp └── Mysql_Struct.h ├── doc ├── gen_msg.sh ├── git使用说明.txt ├── struct_tool ├── websocket.html ├── 压力测试20160715.docx ├── 服务器导出API.xlsx ├── 服务器技术说明.docx └── 服务器架构图.ppt ├── env ├── curl-7.50.1.tar.gz ├── install_server.sh ├── jsoncpp-bin-0.5.0-release.tar.bz2 ├── mongodb-bin-linux-x86_64-3.0.8.tar.gz ├── mongodb-lib-3.0.8.tar.gz ├── mysql-connector-c++-1.1.7-linux-el7-x86-64bit.tar.gz ├── netlib.tar.gz ├── openssl-1.0.1g.tar.gz └── v8.tar.gz ├── game_server ├── AI_Manager.cpp ├── AI_Manager.h ├── Aoi_Entity.cpp ├── Aoi_Entity.h ├── Aoi_Manager.cpp ├── Aoi_Manager.h ├── Behavior_Tree.cpp ├── Behavior_Tree.h ├── Game_Connector.cpp ├── Game_Connector.h ├── Game_Manager.cpp ├── Game_Manager.h ├── Game_Player.cpp ├── Game_Player.h ├── Game_Scene.cpp ├── Game_Scene.h ├── Game_Server.cpp ├── Game_Server.h ├── Game_Timer.cpp ├── Game_Timer.h ├── Scene_Entity.cpp ├── Scene_Entity.h ├── Scene_Manager.cpp └── Scene_Manager.h ├── gate_server ├── Gate_Client_Messager.cpp ├── Gate_Client_Messager.h ├── Gate_Connector.cpp ├── Gate_Connector.h ├── Gate_Inner_Messager.cpp ├── Gate_Inner_Messager.h ├── Gate_Manager.cpp ├── Gate_Manager.h ├── Gate_Player.cpp ├── Gate_Player.h ├── Gate_Server.cpp ├── Gate_Server.h ├── Gate_Timer.cpp └── Gate_Timer.h ├── js ├── bag.js ├── config.js ├── enum.js ├── error.js ├── game_player.js ├── game_server.js ├── guild.js ├── mail.js ├── master_player.js ├── master_server.js ├── message.js ├── npc.js ├── offline.js ├── rank.js ├── struct.js ├── timer.js └── util.js ├── lib ├── Accept.cpp ├── Accept.h ├── Base_Struct.cpp ├── Base_Struct.h ├── Block_Buffer.h ├── Block_List.h ├── Block_Pool_Group.h ├── Common_Func.cpp ├── Common_Func.h ├── Config.cpp ├── Config.h ├── Connect.cpp ├── Connect.h ├── Connector.cpp ├── Connector.h ├── Date_Time.cpp ├── Date_Time.h ├── Epoll_Watcher.cpp ├── Epoll_Watcher.h ├── Event_Handler.h ├── Hot_Update.cpp ├── Hot_Update.h ├── Http_Client.cpp ├── Http_Client.h ├── Http_Parser_Wrap.cpp ├── Http_Parser_Wrap.h ├── Lib_Log.cpp ├── Lib_Log.h ├── List.h ├── Log.cpp ├── Log.h ├── Log_Connector.cpp ├── Log_Connector.h ├── Mutex_Guard.h ├── Mysql_Conn.cpp ├── Mysql_Conn.h ├── Object_Pool.h ├── Priority_Queue.h ├── Public_Define.cpp ├── Public_Define.h ├── Receive.cpp ├── Receive.h ├── Send.cpp ├── Send.h ├── Server.cpp ├── Server.h ├── Sha1.cpp ├── Sha1.h ├── Svc.cpp ├── Svc.h ├── Svc_Http.cpp ├── Svc_Http.h ├── Svc_Static_List.h ├── Svc_Tcp.cpp ├── Svc_Tcp.h ├── Svc_WebSocket.cpp ├── Svc_WebSocket.h ├── Thread.h ├── Thread_Mutex.h ├── Time_Value.cpp ├── Time_Value.h ├── Xml.cpp ├── Xml.h ├── http_parser.cpp ├── http_parser.h ├── tinystr.cpp ├── tinystr.h ├── tinyxml.cpp ├── tinyxml.h ├── tinyxmlerror.cpp └── tinyxmlparser.cpp ├── log_server ├── Log_DB.cpp ├── Log_DB.h ├── Log_File.cpp ├── Log_File.h ├── Log_Manager.cpp ├── Log_Manager.h ├── Log_Server.cpp ├── Log_Server.h ├── Log_Struct.cpp ├── Log_Struct.h ├── Log_Timer.cpp └── Log_Timer.h ├── login_server ├── Login_Client_Messager.cpp ├── Login_Client_Messager.h ├── Login_Inner_Messager.cpp ├── Login_Inner_Messager.h ├── Login_Manager.cpp ├── Login_Manager.h ├── Login_Player.cpp ├── Login_Player.h ├── Login_Server.cpp ├── Login_Server.h ├── Login_Timer.cpp └── Login_Timer.h ├── main.cpp ├── master_server ├── Master_Connector.cpp ├── Master_Connector.h ├── Master_Manager.cpp ├── Master_Manager.h ├── Master_Player.cpp ├── Master_Player.h ├── Master_Server.cpp ├── Master_Server.h ├── Master_Timer.cpp └── Master_Timer.h ├── misc ├── Daemon.cpp ├── Daemon.h ├── Daemon_Server.cpp ├── Daemon_Server.h ├── Debug_Server.cpp ├── Debug_Server.h ├── Inner_Msg.cpp ├── Inner_Msg.h ├── Message.h ├── Msg_Manager.cpp ├── Msg_Manager.h ├── Msg_Struct.cpp ├── Msg_Struct.h ├── Player.cpp ├── Player.h ├── Public_Struct.cpp ├── Public_Struct.h ├── Server_Config.cpp ├── Server_Config.h ├── Server_Manager.cpp └── Server_Manager.h └── v8 ├── Entity_Wrap.cpp ├── Entity_Wrap.h ├── Server_Wrap.cpp ├── Server_Wrap.h ├── V8_Manager.cpp ├── V8_Manager.h ├── V8_Wrap.cpp └── V8_Wrap.h /.gitignore: -------------------------------------------------------------------------------- 1 | /Debug/ 2 | /Release/ 3 | /natives_blob.bin 4 | /snapshot_blob.bin 5 | /server 6 | /obj/ 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | ###在这里添加源文件目录 3 | SRCDIR= ./\ 4 | ./db_server/\ 5 | ./game_server/\ 6 | ./gate_server/\ 7 | ./log_server/\ 8 | ./login_server/\ 9 | ./master_server/\ 10 | ./misc/\ 11 | ./v8/ 12 | 13 | ###这里定义目标文件目录 14 | OBJDIR =./obj/ 15 | 16 | TARGET_NAME=server 17 | 18 | BIN=./ 19 | 20 | INCLUDE=-I/usr/local/include/netlib\ 21 | $(addprefix -I, $(SRCDIR)) 22 | 23 | LIBDIR=-L./ 24 | 25 | LIB=-lnetlib\ 26 | -lv8\ 27 | -lv8_libplatform\ 28 | -lcurl\ 29 | -lcrypto\ 30 | -lmongoclient\ 31 | -lmysqlcppconn\ 32 | -ljsoncpp\ 33 | -lboost_filesystem\ 34 | -lboost_thread-mt\ 35 | 36 | CC=g++ 37 | 38 | DEPENS=-MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" 39 | 40 | DEBUGFLAG=-O0 -g3 -c -Wall -fmessage-length=0 -std=c++11 41 | 42 | RELEASEFLAG=-O3 -Wall -c -fmessage-length=0 -std=c++11 43 | 44 | LDFLAG= 45 | 46 | BIN_TARGET=$(OBJDIR)bin/$(TARGET_NAME) 47 | 48 | SRCS=$(wildcard $(addsuffix *.cpp, $(SRCDIR))) 49 | 50 | OBJECTS:=$(addprefix $(OBJDIR), $(subst ./,,$(SRCS:.cpp=.o))) 51 | 52 | .PHONY:all mkobjdir clean 53 | 54 | all:mkobjdir $(BIN_TARGET) 55 | 56 | -include $(OBJECTS:.o=.d) 57 | 58 | $(BIN_TARGET):$(OBJECTS) 59 | $(CC) $(LDFLAG) -o $@ $^ $(LIBDIR) $(LIB) 60 | @echo " " 61 | @echo "Finished building target: $(TARGET_NAME)" 62 | @echo " " 63 | @-cp -f $(BIN_TARGET) $(BIN) 64 | 65 | $(OBJDIR)%.o:%.cpp 66 | ifeq ($(MODE), DEBUG) 67 | @echo "Building DEBUG MODE target $@" 68 | $(CC) $(INCLUDE) $(DEBUGFLAG) $(DEPENS) -o "$(@)" "$(<)" 69 | else 70 | @echo "Building RELEASE MODE target $@" 71 | $(CC) $(INCLUDE) $(RELEASEFLAG) $(DEPENS) -o "$(@)" "$(<)" 72 | endif 73 | @echo " " 74 | 75 | mkobjdir: 76 | @test -d $(OBJDIR) || (mkdir $(OBJDIR) && mkdir $(OBJDIR)bin $(addprefix $(OBJDIR), $(subst ./,,$(SRCDIR)))) 77 | 78 | clean: 79 | -rm -rf $(OBJDIR) 80 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | 服务器是异步非阻塞的多进程多线程架构,运行于CentOS7操作系统。服务器底层使用C++编写,高效稳定, 2 | 使用TCP协议进行进程间以及与客户端的通信。同时也支持UDP、Websocket、Http协议。 3 | 网络通信协议使用自定义格式,具备加密功能,安全高效。游戏数据库支持MongoDB和Mysql,可以选择自己想要的数据库。 4 | 逻辑层使用简单高效的Javascript进行开发,集成Google V8引擎解析脚本,保证脚本运行效率,有js经验的开发人员可以快速上手。 5 | 6 | 服务器分为LoginServer,GateServer,GameServer,MasterServer,DBServer,LogServer。 7 | 8 | LoginServer:客户端发送账号密码到LoginServer,LoginServer去AccountDb验证, 9 | 验证失败断开连接,验证成功,则生成session, 根据玩家账号hash和gate在线人数选择GateIp,GatePort,发送给客户端。 10 | 11 | GateServer:接受客户端发过来的连接,向LoginServer进行session验证,验证成功玩家可以正常登录, 12 | 然后就开始接收客户端消息,转发到Game,Master,类似网关中转器。 13 | 14 | GameServer: 15 | 1、创建角色,角色登录,从DbServer加载数据返回给client,存储玩家数据 16 | 2、场景管理模块 17 | 3、AOI模块 18 | 4、定时器管理模块 19 | 5、怪物AI模块 20 | 6、Player管理模块 21 | 7、C++消息解析包装成js对象 22 | 23 | MasterServer: 24 | 1、管理GameServer,玩家在GameServer进程间跳转 25 | 2、存储在线玩家数据,进行相关校验验证,存储每个玩家对应的gameserver 26 | 3、存储公共数据,例如帮派,好友,邮件,聊天,组队 27 | 28 | V8:V8引擎管理模块,js代码解析,执行js代码,C++函数导出 29 | 30 | DbServer: 31 | 1、建立mongodb引擎和存取数据接口 32 | 2、建立Mysql引擎和存取数据接口 33 | 34 | LogServer: 35 | 1、实现写服务器文件日志功能 36 | 2、实现写玩家操作日志到Mysql Log数据库 37 | -------------------------------------------------------------------------------- /config/bag/item.json: -------------------------------------------------------------------------------- 1 | { 2 | "100000001": { 3 | "sort": 1, 4 | "name": "测试武器1", 5 | "color": 2, 6 | "attack": 10, 7 | "exp": 20, 8 | "level_exp" : [100,200,300,400,500], 9 | "career": 3, 10 | "part": 0, 11 | "level": 0, 12 | "price": 10 13 | }, 14 | 15 | "100000002": { 16 | "sort": 1, 17 | "name": "测试武器2", 18 | "color": 2, 19 | "attack": 10, 20 | "exp": 20, 21 | "level_exp" : [100,200,300,400,500], 22 | "career": 3, 23 | "part": 0, 24 | "level": 0, 25 | "price": 10 26 | }, 27 | 28 | "100000003": { 29 | "sort": 1, 30 | "name": "测试武器3", 31 | "color": 2, 32 | "attack": 10, 33 | "exp": 20, 34 | "level_exp" : [100,200,300,400,500], 35 | "career": 3, 36 | "part": 0, 37 | "level": 0, 38 | "price": 10 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /config/monster/ai_behavior.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /config/monster/ai_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "thinking_interval" : 1000, 3 | "low_hp" : 30, 4 | "low_mp" : 30 5 | } 6 | -------------------------------------------------------------------------------- /config/player/level.json: -------------------------------------------------------------------------------- 1 | { 2 | "1" : { 3 | "level_exp" : 25 4 | }, 5 | "2" : { 6 | "level_exp" : 50 7 | }, 8 | "3" : { 9 | "level_exp" : 100 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /config/player/util.json: -------------------------------------------------------------------------------- 1 | { 2 | "max_player_level" : 90, 3 | "max_vip_level" : 2, 4 | "daily_refresh_time" : 8 5 | } 6 | -------------------------------------------------------------------------------- /config/server/server_conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "receive_timeout" : 300, //服务器与客户端心跳等待最大时间 单位:s 3 | "server_send_interval" : 100, //服务器定时发送数据时间 单位:us 4 | "connector_send_interval" : 100, //内部服务器定时发送数据时间 单位:us 5 | //各个服务器配置 6 | "log_server" : { 7 | "server_id" : 1, //服务器id 8 | "server_ip" : "127.0.0.1", //日志服务器ip 9 | "server_port" : 8000 , //日志服务器监听端口 10 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 11 | }, 12 | "db_server" : { 13 | "server_id" : 1, //服务器id 14 | "server_ip" : "127.0.0.1", //数据库服务器ip 15 | "server_port" : 8100 , //数据库服务器监听端口 16 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 17 | }, 18 | "login_client_server" : { 19 | "server_id" : 1, //服务器id 20 | "server_ip" : "127.0.0.1", //登录服务器ip 21 | "server_port" : 8200, //登录服务器监听客户端连接的端口 22 | "network_protocol" : 0 //网络协议类型 0:Tcp 1:Udp 2:Websocket 3:Http 23 | }, 24 | "login_gate_server" : { 25 | "server_id" : 1, //服务器id 26 | "server_ip" : "127.0.0.1", //登录服务器ip 27 | "server_port" : 8201, //登录服务器监听网关服连接的端口 28 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 29 | }, 30 | "master_gate_server" : { 31 | "server_id" : 1, //服务器id 32 | "server_ip" : "127.0.0.1", //中央服务器ip 33 | "server_port" : 8300, //中央服务器监听网关服连接的端口 34 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 35 | }, 36 | "master_game_server" : { 37 | "server_id" : 1, //服务器id 38 | "server_ip" : "127.0.0.1", //中央服务器ip 39 | "server_port" : 8301, //中央服务器监听游戏服连接的端口 40 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 41 | }, 42 | "master_http_server" : { 43 | "server_id" : 1, //服务器id 44 | "server_ip" : "127.0.0.1", //中央服务器ip 45 | "server_port" : 8080, //中央服务器监听后台连接的端口 46 | "network_protocol" : 3 //网络协议类型 0:Tcp 1:Udp 2:Websocket 3:Http 47 | }, 48 | "game_server" : [ 49 | { 50 | "server_id" : 1, //服务器id 51 | "server_ip" : "127.0.0.1", //游戏服务器ip 52 | "server_port" : 8400, //游戏服务器监听网关服连接的端口 53 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 54 | }, 55 | { 56 | "server_id" : 2, //服务器id 57 | "server_ip" : "127.0.0.1", //游戏服务器ip 58 | "server_port" : 8401, //游戏服务器监听网关服连接的端口 59 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 60 | } 61 | ], 62 | "gate_server" : [ 63 | { 64 | "server_id" : 1, //服务器id 65 | "server_ip" : "127.0.0.1", //网关服务器ip 66 | "server_port" : 8500, //网关服务器监听客户端连接的端口 67 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 68 | }, 69 | { 70 | "server_id" : 2, //服务器id 71 | "server_ip" : "127.0.0.1", //网关服务器ip 72 | "server_port" : 8501, //网关服务器监听客户端连接的端口 73 | "network_protocol" : 0 //网络协议类型 0:TCP 1:UDP 2:WEBSOCKET 74 | } 75 | ] 76 | } 77 | -------------------------------------------------------------------------------- /config/server/server_misc.json: -------------------------------------------------------------------------------- 1 | { 2 | "server_type" : 1, //服务器架构类型 1:多进程 2:多线程 3 | "db_type" : 1, //数据库类型 1:mongodb 2:mysql 4 | "server_label" : "test", //服务器标签,ps查询进程时候区分用 5 | "lib_log_switcher" : 0, //网络库日志开关,1表示写入磁盘文件 6 | "server_log_switcher" : 0, //服务器日志开关,1表示写入磁盘文件 7 | "verify_pack" : 1, //是否开启数据包安全验证 8 | "msg_count" : 1, //是否开启消息统计 9 | "agent_num" : 1, //平台编号,比如37wan编号为1,生成角色id,帮派id时候用 10 | "server_num" : 1, //服务器编号,比如1服编号为1,生成角色id,帮派id时候用 11 | "aoi_radius" : 10, //aoi范围单位 12 | "game_db_struct_path" : "config/struct/game_struct.xml", 13 | "log_db_struct_path" : "config/struct/log_struct.xml", 14 | "msg_struct_path" : "config/struct/msg_struct.xml", 15 | "game_server_path" : "js/game_server.js", 16 | "master_server_path" : "js/master_server.js", 17 | "ai_behavior_path" : "config/monster/ai_behavior.xml", 18 | //mongodb游戏数据库配置 19 | "mongodb_game" : { 20 | "ip" : "127.0.0.1", 21 | "port" : 27017 22 | }, 23 | //mysql游戏数据库配置 24 | "mysql_game" : { 25 | "ip" : "127.0.0.1", 26 | "port" : 3306, 27 | "user" : "root", 28 | "password" : "123456", 29 | "dbname" : "game", 30 | "dbpoolname" : "game_pool" 31 | }, 32 | //mysql 帐号数据库配置 33 | "mysql_account" : { 34 | "ip" : "127.0.0.1", 35 | "port" : 3306, 36 | "user" : "root", 37 | "password" : "123456", 38 | "dbname" : "account", 39 | "dbpoolname" : "account_pool" 40 | }, 41 | //mysql 日志数据库配置 42 | "mysql_log" : { 43 | "ip" : "127.0.0.1", 44 | "port" : 3306, 45 | "user" : "root", 46 | "password" : "123456", 47 | "dbname" : "log", 48 | "dbpoolname" : "log_pool" 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /config/sql/account.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `account` DEFAULT CHARACTER SET utf8; 2 | USE `account`; 3 | 4 | DROP TABLE IF EXISTS `account_info`; 5 | CREATE TABLE `account_info` ( 6 | account varchar(120) NOT NULL, 7 | password varchar(120) NOT NULL default '', 8 | PRIMARY KEY (`account`) 9 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 10 | 11 | 12 | -------------------------------------------------------------------------------- /config/sql/game.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `game` DEFAULT CHARACTER SET utf8; 2 | 3 | USE `game`; 4 | 5 | DROP TABLE IF EXISTS `global`; 6 | CREATE TABLE `global` ( 7 | type varchar(120) NOT NULL, 8 | value bigint(20) NOT NULL default '0', 9 | PRIMARY KEY (`type`) 10 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 11 | 12 | DROP TABLE IF EXISTS `role`; 13 | CREATE TABLE `role` ( 14 | role_id bigint(20) NOT NULL auto_increment, 15 | role_name varchar(120) NOT NULL default '', 16 | account varchar(120) NOT NULL default '', 17 | client_ip varchar(120) NOT NULL default '', 18 | agent_num int(11) NOT NULL default '0', 19 | server_num int(11) NOT NULL default '0', 20 | level int(11) NOT NULL default '0', 21 | exp int(11) NOT NULL default '0', 22 | gender tinyint(3) NOT NULL default '0', 23 | career tinyint(3) NOT NULL default '0', 24 | create_time int(11) NOT NULL default '0', 25 | login_time int(11) NOT NULL default '0', 26 | logout_time int(11) NOT NULL default '0', 27 | vip_level int(11) NOT NULL default '0', 28 | vip_exp int(11) NOT NULL default '0', 29 | charge_gold int(11) NOT NULL default '0', 30 | guild_id bigint(20) NOT NULL default '0', 31 | guild_name varchar(120) NOT NULL default '', 32 | scene_id int(11) NOT NULL default '0', 33 | pos_x int(11) NOT NULL default '0', 34 | pos_y int(11) NOT NULL default '0', 35 | pos_z int(11) NOT NULL default '0', 36 | PRIMARY KEY (role_id) 37 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 38 | 39 | DROP TABLE IF EXISTS `bag`; 40 | CREATE TABLE `bag` ( 41 | role_id bigint(20) NOT NULL auto_increment, 42 | copper int(11) NOT NULL default '0', 43 | gold int(11) NOT NULL default '0', 44 | item_map text NOT NULL, 45 | PRIMARY KEY (role_id) 46 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 47 | 48 | DROP TABLE IF EXISTS `mail`; 49 | CREATE TABLE `mail` ( 50 | role_id bigint(20) NOT NULL auto_increment, 51 | total_count int(11) NOT NULL default '0', 52 | mail_map text NOT NULL, 53 | PRIMARY KEY (role_id) 54 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 55 | 56 | DROP TABLE IF EXISTS `guild`; 57 | CREATE TABLE `guild` ( 58 | guild_id bigint(20) NOT NULL auto_increment, 59 | guild_name varchar(120) NOT NULL default '', 60 | chief_id bigint(20) NOT NULL default '0', 61 | create_time int(11) NOT NULL default '0', 62 | member_list text NOT NULL, 63 | app_list text NOT NULL, 64 | PRIMARY KEY (guild_id) 65 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 66 | 67 | DROP TABLE IF EXISTS `rank`; 68 | CREATE TABLE `rank` ( 69 | rank_type bigint(20) NOT NULL auto_increment, 70 | min_value int(11) NOT NULL default '0', 71 | min_role_id bigint(20) NOT NULL default '0', 72 | member_map text NOT NULL, 73 | PRIMARY KEY (rank_type) 74 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 75 | 76 | DROP TABLE IF EXISTS `offline`; 77 | CREATE TABLE `offline` ( 78 | role_id bigint(20) NOT NULL auto_increment, 79 | guild_id bigint(20) NOT NULL default '0', 80 | guild_name varchar(120) NOT NULL default '', 81 | PRIMARY KEY (role_id) 82 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 83 | -------------------------------------------------------------------------------- /config/sql/log.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `log` DEFAULT CHARACTER SET utf8; 2 | 3 | USE `log`; 4 | 5 | DROP TABLE IF EXISTS `logout`; 6 | CREATE TABLE `logout` ( 7 | role_id bigint(20) NOT NULL, 8 | role_name varchar(120) NOT NULL default '', 9 | account varchar(120) NOT NULL default '', 10 | level int(11) NOT NULL default '0', 11 | client_ip varchar(120) NOT NULL default '', 12 | login_time int(11) NOT NULL default '0', 13 | logout_time int(11) NOT NULL default '0' 14 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 15 | 16 | -------------------------------------------------------------------------------- /config/struct/log_struct.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /config/vip/recharge.json: -------------------------------------------------------------------------------- 1 | { 2 | "10400" : { 3 | "vip_exp" : 150, 4 | "order" : 23, 5 | "gold" : 150 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /config/vip/vip.json: -------------------------------------------------------------------------------- 1 | { 2 | "0" : { 3 | "max_buy_vitality" : 1, 4 | "level_exp" : 50, 5 | "refresh_shop_count" : 3, 6 | "exchange_count" : 3 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /db_server/DB_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DB_Manager.h 3 | * 4 | * Created on: Dec 29, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DB_MANAGER_H_ 9 | #define DB_MANAGER_H_ 10 | 11 | #include "DB_Worker.h" 12 | #include "Log.h" 13 | #include "Object_Pool.h" 14 | #include "Thread_Mutex.h" 15 | 16 | class DB_Manager { 17 | public: 18 | typedef Object_Pool DB_Worker_Pool; 19 | typedef std::vector DB_Worker_Vector; 20 | typedef boost::unordered_map DB_Cache_Id_Map; 21 | typedef boost::unordered_map DB_Cache_Account_Map; 22 | 23 | static DB_Manager *instance(void); 24 | static void destroy(void); 25 | 26 | int init(int server_id); 27 | int start(void); 28 | 29 | int push_data_block(Block_Buffer *buf); 30 | int send_data_block(int cid, Block_Buffer &buf); 31 | 32 | inline Struct_Id_Map& db_struct_id_map() { return db_struct_id_map_; } 33 | inline Struct_Name_Map& db_struct_name_map() { return db_struct_name_map_; } 34 | inline DB_Cache_Id_Map& db_cache_id_map() { return db_cache_id_map_; } 35 | inline DB_Cache_Account_Map& db_cache_account_map() { return db_cache_account_map_; } 36 | 37 | inline Base_Struct *get_player_data_struct() { 38 | return db_struct_name_map_.find("Player_Data")->second; 39 | } 40 | inline Base_Struct *get_master_data_struct() { 41 | return db_struct_name_map_.find("Master_Data")->second; 42 | } 43 | 44 | int64_t create_player(Create_Role_Info &role_info); 45 | int64_t create_guild(Create_Guild_Info &role_info); 46 | 47 | private: 48 | DB_Manager(void); 49 | virtual ~DB_Manager(void); 50 | DB_Manager(const DB_Manager &); 51 | const DB_Manager &operator=(const DB_Manager &); 52 | 53 | private: 54 | static DB_Manager *instance_; 55 | 56 | DB_Worker_Pool db_worker_pool_; 57 | DB_Worker_Vector db_worker_vec_; 58 | 59 | Struct_Id_Map db_struct_id_map_; 60 | Struct_Name_Map db_struct_name_map_; 61 | DB_Cache_Id_Map db_cache_id_map_; 62 | DB_Cache_Account_Map db_cache_account_map_; 63 | 64 | int load_player_num_; //加载数据的玩家数量 65 | int create_player_num_; //创建的玩家数量 66 | int save_player_num_; //保存数据的玩家数量 67 | int db_data_num_; //保存数据库的消息数量 68 | int db_type_; //数据库类型 69 | }; 70 | 71 | #define DB_MANAGER DB_Manager::instance() 72 | 73 | #endif /* DB_MANAGER_H_ */ 74 | -------------------------------------------------------------------------------- /db_server/DB_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * DB_Server.cpp 3 | * 4 | * Created on: Dec 29, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "DB_Server.h" 9 | #include "DB_Manager.h" 10 | 11 | DB_Server::DB_Server(void) { } 12 | 13 | DB_Server::~DB_Server(void) { } 14 | 15 | DB_Server *DB_Server::instance_ = 0; 16 | 17 | DB_Server *DB_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new DB_Server; 20 | return instance_; 21 | } 22 | 23 | void DB_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | if (!block_list_.empty()) { 27 | buf = block_list_.pop_front(); 28 | DB_MANAGER->push_data_block(buf); 29 | } else { 30 | //没有数据时候延迟 31 | Time_Value::sleep(Time_Value(0,100)); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /db_server/DB_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DB_Server.h 3 | * 4 | * Created on: Dec 29, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DB_SERVER_H_ 9 | #define DB_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class DB_Server: public Server { 14 | public: 15 | static DB_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | DB_Server(void); 21 | virtual ~DB_Server(void); 22 | DB_Server(const DB_Server &); 23 | const DB_Server &operator=(const DB_Server &); 24 | 25 | private: 26 | static DB_Server *instance_; 27 | }; 28 | 29 | #define DB_SERVER DB_Server::instance() 30 | 31 | #endif /* DB_SERVER_H_ */ 32 | -------------------------------------------------------------------------------- /db_server/DB_Worker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DB_Worker.h 3 | * 4 | * Created on: Dec 29, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DB_WORKER_H_ 9 | #define DB_WORKER_H_ 10 | 11 | #include "Public_Struct.h" 12 | #include "Thread.h" 13 | #include "Thread_Mutex.h" 14 | #include "Block_List.h" 15 | 16 | class DB_Worker: public Thread { 17 | public: 18 | typedef Block_List Data_List; 19 | 20 | DB_Worker(void); 21 | virtual ~DB_Worker(void); 22 | 23 | void run_handler(void); 24 | void process_list(void); 25 | 26 | //其他数据库消息 27 | int process_data_block(Block_Buffer *buffer); 28 | //创建角色 29 | int process_create_player(int cid, Create_Role_Info &role_info); 30 | //加载玩家数据 31 | int process_load_player(int cid, std::string &account); 32 | //保存玩家数据 33 | int process_save_player(int cid, int status, Block_Buffer &buffer); 34 | // 创建公会 35 | int process_create_guild(int cid, Create_Guild_Info &guild_info); 36 | // 加载master数据 37 | int process_load_master(int cid); 38 | 39 | //玩家数据处理 40 | int create_player_data(int64_t role_id, Block_Buffer &buffer); 41 | int load_player_data(int64_t role_id, Block_Buffer &buffer); 42 | int save_player_data(Block_Buffer &buffer); 43 | 44 | // 创建角色 45 | int push_create_player(Block_Buffer *buffer); 46 | //加载玩家数据 47 | int push_load_player(Block_Buffer *buffer); 48 | //保存玩家数据 49 | int push_save_player(Block_Buffer *buffer); 50 | //其他数据库消息 51 | int push_data_block(Block_Buffer *buffer); 52 | 53 | private: 54 | Data_List create_player_list_; //创建的玩家数据列表 55 | Data_List load_player_list_; //加载的玩家数据列表 56 | Data_List save_player_list_; //保存的玩家数据列表 57 | Data_List data_list_; //其他数据库消息消息列表 58 | }; 59 | 60 | //////////////////////////////////////////////////////////////////////////////// 61 | inline int DB_Worker::push_create_player(Block_Buffer *buffer) { 62 | create_player_list_.push_back(buffer); 63 | return 0; 64 | } 65 | 66 | inline int DB_Worker::push_load_player(Block_Buffer *buffer) { 67 | load_player_list_.push_back(buffer); 68 | return 0; 69 | } 70 | 71 | inline int DB_Worker::push_save_player(Block_Buffer *buffer) { 72 | save_player_list_.push_back(buffer); 73 | return 0; 74 | } 75 | 76 | inline int DB_Worker::push_data_block(Block_Buffer *buffer) { 77 | data_list_.push_back(buffer); 78 | return 0; 79 | } 80 | 81 | #endif /* DB_WORKER_H_ */ 82 | -------------------------------------------------------------------------------- /db_server/Mongo_Operator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mongo_Operator.h 3 | * 4 | * Created on: Dec 29, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DB_OPERATOR_H_ 9 | #define DB_OPERATOR_H_ 10 | 11 | #include "Public_Struct.h" 12 | #include "Object_Pool.h" 13 | #include "mongo/client/dbclient.h" 14 | 15 | using namespace mongo; 16 | 17 | class Mongo_Operator { 18 | public: 19 | typedef boost::unordered_map Connection_Map; 20 | typedef Object_Pool Connection_Pool; 21 | 22 | static Mongo_Operator *instance(void); 23 | mongo::DBClientConnection &connection(void); 24 | 25 | int init(void); 26 | int load_db_cache(void); 27 | 28 | int64_t generate_id(std::string type); 29 | int64_t create_player(Create_Role_Info &role_info); 30 | int64_t create_guild(Create_Guild_Info &guild_info); 31 | 32 | private: 33 | Mongo_Operator(void); 34 | virtual ~Mongo_Operator(void); 35 | Mongo_Operator(const Mongo_Operator &); 36 | const Mongo_Operator &operator=(const Mongo_Operator &); 37 | 38 | private: 39 | static Mongo_Operator *instance_; 40 | Connection_Pool connection_pool_; 41 | Connection_Map connection_map_; 42 | MUTEX connection_map_lock_; 43 | 44 | int agent_num_; //代理编号 45 | int server_num_; //服务器编号 46 | }; 47 | 48 | #define MONGO_INSTANCE Mongo_Operator::instance() 49 | #define MONGO_CONNECTION MONGO_INSTANCE->connection() 50 | 51 | #endif /* DB_OPERATOR_H_ */ 52 | -------------------------------------------------------------------------------- /db_server/Mongo_Struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mongo_Struct.h 3 | * 4 | * Created on: Jul 20, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MONGO_STRUCT_H_ 9 | #define MONGO_STRUCT_H_ 10 | 11 | #include "Base_Struct.h" 12 | #include "mongo/client/dbclient.h" 13 | 14 | using namespace mongo; 15 | class Mongo_Struct: public Base_Struct { 16 | public: 17 | Mongo_Struct(Xml &xml, TiXmlNode *node); 18 | virtual ~Mongo_Struct(); 19 | 20 | virtual void create_data(int64_t key_index, Block_Buffer &buffer); 21 | virtual void load_data(int64_t key_index, Block_Buffer &buffer); 22 | virtual void save_data(Block_Buffer &buffer); 23 | virtual void save_data_vector(Block_Buffer &buffer); 24 | virtual void delete_data(Block_Buffer &buffer); 25 | 26 | private: 27 | void create_data_arg(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder, int64_t key_index); 28 | void create_data_vector(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder); 29 | void create_data_struct(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder); 30 | 31 | void build_buffer_arg(const Field_Info &field_info, Block_Buffer &buffer, BSONObj &bsonobj); 32 | void build_buffer_vector(const Field_Info &field_info, Block_Buffer &buffer, BSONObj &bsonobj); 33 | void build_buffer_struct(const Field_Info &field_info, Block_Buffer &buffer, BSONObj &bsonobj); 34 | 35 | void build_bson_arg(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder); 36 | void build_bson_vector(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder); 37 | void build_bson_struct(const Field_Info &field_info, Block_Buffer &buffer, BSONObjBuilder &builder); 38 | }; 39 | 40 | #endif /* MONGO_STRUCT_H_ */ 41 | -------------------------------------------------------------------------------- /db_server/Mysql_Operator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mysql_Operator.h 3 | * 4 | * Created on: Jul 20, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MYSQL_OPERATOR_H_ 9 | #define MYSQL_OPERATOR_H_ 10 | 11 | #include "Mysql_Conn.h" 12 | #include "Public_Struct.h" 13 | 14 | class Mysql_Operator { 15 | public: 16 | typedef boost::unordered_map Connection_Map; 17 | 18 | static Mysql_Operator *instance(void); 19 | Mysql_Conn *connection(void); 20 | 21 | int init(void); 22 | int load_db_cache(void); 23 | 24 | int64_t generate_id(std::string type); 25 | int64_t create_player(Create_Role_Info &role_info); 26 | int64_t create_guild(Create_Guild_Info &guild_info); 27 | 28 | private: 29 | Mysql_Operator(void); 30 | virtual ~Mysql_Operator(void); 31 | Mysql_Operator(const Mysql_Operator &); 32 | const Mysql_Operator &operator=(const Mysql_Operator &); 33 | 34 | private: 35 | static Mysql_Operator *instance_; 36 | Connection_Map connection_map_; 37 | MUTEX connection_map_lock_; 38 | 39 | int agent_num_; //代理编号 40 | int server_num_; //服务器编号 41 | }; 42 | 43 | #define MYSQL_INSTANCE Mysql_Operator::instance() 44 | #define MYSQL_CONNECTION MYSQL_INSTANCE->connection() 45 | 46 | #endif /* MYSQL_OPERATOR_H_ */ 47 | -------------------------------------------------------------------------------- /db_server/Mysql_Struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mysql_Struct.h 3 | * 4 | * Created on: Jul 20, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MYSQL_STRUCT_H_ 9 | #define MYSQL_STRUCT_H_ 10 | 11 | #include "Base_Struct.h" 12 | #include "Mysql_Conn.h" 13 | 14 | class Mysql_Struct: public Base_Struct { 15 | public: 16 | Mysql_Struct(Xml &xml, TiXmlNode *node); 17 | virtual ~Mysql_Struct(); 18 | 19 | virtual void create_data(int64_t key_index, Block_Buffer &buffer); 20 | virtual void load_data(int64_t key_index, Block_Buffer &buffer); 21 | virtual void save_data(Block_Buffer &buffer); 22 | virtual void save_data_vector(Block_Buffer &buffer); 23 | virtual void delete_data(Block_Buffer &buffer); 24 | 25 | private: 26 | int create_data_arg(const Field_Info &field_info, Block_Buffer &buffer, Block_Buffer &blob_buffer); 27 | int create_data_vector(const Field_Info &field_info, Block_Buffer &buffer, Block_Buffer &blob_buffer); 28 | int create_data_struct(const Field_Info &field_info, Block_Buffer &buffer, Block_Buffer &blob_buffer); 29 | 30 | void build_buffer_arg(const Field_Info &field_info, Block_Buffer &buffer, sql::ResultSet *result); 31 | void build_buffer_vector(const Field_Info &field_info, Block_Buffer &buffer, sql::ResultSet *result); 32 | void build_buffer_struct(const Field_Info &field_info, Block_Buffer &buffer, sql::ResultSet *result); 33 | 34 | int build_len_arg(const Field_Info &field_info, Block_Buffer &buffer); 35 | int build_len_vector(const Field_Info &field_info, Block_Buffer &buffer); 36 | int build_len_struct(const Field_Info &field_info, Block_Buffer &buffer); 37 | }; 38 | 39 | #endif /* MYSQL_STRUCT_H_ */ 40 | -------------------------------------------------------------------------------- /doc/gen_msg.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | DEFINE_PATH='../config/struct/' 4 | JS_TARGET='../js' 5 | 6 | function gen_msg(){ 7 | gen_js 8 | gen_msgid 9 | cp_file 10 | rm_file 11 | } 12 | 13 | function gen_js(){ 14 | ./struct_tool js $DEFINE_PATH 'game_struct.xml' 'struct' 15 | ./struct_tool js $DEFINE_PATH 'msg_struct.xml' 'struct' 16 | } 17 | 18 | function gen_msgid(){ 19 | ./struct_tool msg $DEFINE_PATH 'message.define' 'msg' 20 | } 21 | 22 | function cp_file(){ 23 | wildcard='.*' 24 | cp -rf JS/* $JS_TARGET 25 | cp -rf '../config/struct/game_struct.xml' '../../robot/config' 26 | cp -rf '../config/struct/msg_struct.xml' '../../robot/config' 27 | } 28 | 29 | function rm_file(){ 30 | rm -rf JS 31 | } 32 | 33 | gen_msg -------------------------------------------------------------------------------- /doc/git使用说明.txt: -------------------------------------------------------------------------------- 1 | linux生成sshkey方法 2 | 1.ls -al ~/.ssh 3 | 2.ssh-keygen -t rsa -b 4096 -C "784599938@qq.com" 4 | 3.ssh-agent -s 5 | eval $(ssh-agent -s) 6 | ssh-add ~/.ssh/id_rsa 7 | 4.cat /root/.ssh/id_rsa.pub copy content to github 8 | 5.ssh -T git@github.com 9 | 10 | 将本地工作区代码还原为git服务器上面版本 11 | git checkout -- js/hero.js 12 | 13 | change your romote url from https to ssh: 14 | git remote set-url origin git@github.com:xxx/xxx.git 15 | git remote -v 16 | 17 | change your romote url from ssh to https: 18 | git remote set-url origin https://github.com/xxx/xxx.git 19 | git remote -v 20 | 21 | 本地提交项目到github 22 | 1、本地配置 23 | git config --global user.name 'zhangyalei' 24 | git config --global user.email '784599938@qq.com' 25 | 2、新建Git项目并提交到Github 26 | mkdir testdir 27 | cd testdir 28 | touch README.md 29 | git init #初始化一个本地库 30 | git add README.md #添加文件到本地仓库 31 | git rm README.md #本地倒库内删除 32 | git commit -m "first commit" #提交到本地库并备注,此时变更仍在本地。 33 | git commit -a ##自动更新变化的文件,a可以理解为auto 34 | git remote add xxx git@github.com:xxx/xxx.git #增加一个远程服务器的别名。 35 | git remote add xxx https://github.com/xxx/xxx.git #增加一个远程服务器的别名。 36 | git remote rm xxx ##删除远程版本库的别名 37 | git push -u remotename master #将本地文件提交到Github的remoname版本库中。此时才更新了本地变更到github服务上。 38 | 39 | 分支版本操作 40 | 1、创建和合并分支 41 | git branch #显示当前分支是master 42 | git branch new-feature #创建分支 43 | git checkout new-feature #切换到新分支 44 | vi page_cache.inc.php 45 | git add page_cache.inc.php 46 | git commit -a -m "added initial version of page cache" 47 | git push origin new-feature ##把分支提交到远程服务器,只是把分支结构和内容提交到远程,并没有发生和主干的合并行为。 48 | 2、如果new-feature分支成熟了,觉得有必要合并进master 49 | git checkout master #切换到新主干 50 | git merge new-feature ##把分支合并到主干 51 | git branch #显示当前分支是master 52 | git push #此时主干中也合并了new-feature的代码 -------------------------------------------------------------------------------- /doc/struct_tool: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/doc/struct_tool -------------------------------------------------------------------------------- /doc/websocket.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | websocket test 7 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /doc/压力测试20160715.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/doc/压力测试20160715.docx -------------------------------------------------------------------------------- /doc/服务器导出API.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/doc/服务器导出API.xlsx -------------------------------------------------------------------------------- /doc/服务器技术说明.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/doc/服务器技术说明.docx -------------------------------------------------------------------------------- /doc/服务器架构图.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/doc/服务器架构图.ppt -------------------------------------------------------------------------------- /env/curl-7.50.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/curl-7.50.1.tar.gz -------------------------------------------------------------------------------- /env/jsoncpp-bin-0.5.0-release.tar.bz2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/jsoncpp-bin-0.5.0-release.tar.bz2 -------------------------------------------------------------------------------- /env/mongodb-bin-linux-x86_64-3.0.8.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/mongodb-bin-linux-x86_64-3.0.8.tar.gz -------------------------------------------------------------------------------- /env/mongodb-lib-3.0.8.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/mongodb-lib-3.0.8.tar.gz -------------------------------------------------------------------------------- /env/mysql-connector-c++-1.1.7-linux-el7-x86-64bit.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/mysql-connector-c++-1.1.7-linux-el7-x86-64bit.tar.gz -------------------------------------------------------------------------------- /env/netlib.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/netlib.tar.gz -------------------------------------------------------------------------------- /env/openssl-1.0.1g.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/openssl-1.0.1g.tar.gz -------------------------------------------------------------------------------- /env/v8.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javapretty/game_server/a6f40a1393fde830338bd84eaca9f59bccee975b/env/v8.tar.gz -------------------------------------------------------------------------------- /game_server/AI_Manager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 19, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #include "Xml.h" 7 | #include "AI_Manager.h" 8 | #include "Scene_Entity.h" 9 | #include "Server_Config.h" 10 | 11 | AI_Manager* AI_Manager::instance_ = NULL; 12 | 13 | AI_Manager *AI_Manager::instance() { 14 | if(instance_ == NULL) 15 | instance_ = new AI_Manager; 16 | return instance_; 17 | } 18 | 19 | AI_Manager::AI_Manager() 20 | { 21 | 22 | } 23 | 24 | AI_Manager::~AI_Manager() { 25 | 26 | } 27 | 28 | void AI_Manager::init() { 29 | Xml xml; 30 | const Json::Value &server_misc = SERVER_CONFIG->server_misc(); 31 | xml.load_xml(server_misc["ai_behavior_path"].asString().c_str()); 32 | TiXmlNode *node = xml.get_root_node(); 33 | XML_LOOP_BEGIN(node) 34 | std::string label = xml.get_key(node); 35 | int monster_id = xml.get_attr_int(node, "id"); 36 | Behavior_Tree *tree = new Behavior_Tree(monster_id); 37 | tree->init(xml, xml.enter_node(node, label.c_str())); 38 | behavior_tree_map_[tree->monster_id()] = tree; 39 | XML_LOOP_END(node) 40 | } 41 | 42 | int AI_Manager::thinking(Local &npc_obj, Isolate *isolate) { 43 | Local id_name = String::NewFromUtf8(isolate, "type", NewStringType::kNormal).ToLocalChecked(); 44 | int ai_id = npc_obj->Get(id_name)->Int32Value(isolate->GetCurrentContext()).FromMaybe(0); 45 | BEHAVIOR_TREE_MAP::iterator iter = behavior_tree_map_.find(ai_id); 46 | if(iter == behavior_tree_map_.end()){ 47 | return -1; 48 | } 49 | Behavior_Tree *tree = iter->second; 50 | tree->process(npc_obj, isolate); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /game_server/AI_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 19, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef AI_MANAGER_H_ 7 | #define AI_MANAGER_H_ 8 | 9 | #include "Public_Struct.h" 10 | #include "Behavior_Tree.h" 11 | #include "include/v8.h" 12 | 13 | using namespace v8; 14 | 15 | typedef boost::unordered_map BEHAVIOR_TREE_MAP; 16 | 17 | class AI_Manager { 18 | public: 19 | static AI_Manager *instance(); 20 | void init(); 21 | int thinking(Local &npc_obj, Isolate *isolate); 22 | 23 | private: 24 | AI_Manager(); 25 | ~AI_Manager(); 26 | private: 27 | static AI_Manager *instance_; 28 | BEHAVIOR_TREE_MAP behavior_tree_map_; 29 | }; 30 | 31 | #define AI_MANAGER AI_Manager::instance() 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /game_server/Aoi_Entity.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 11, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #include "Log.h" 7 | #include "Aoi_Entity.h" 8 | #include "Scene_Entity.h" 9 | 10 | Aoi_Entity::Aoi_Entity(): 11 | entity_(), 12 | x_pos_(), 13 | y_pos_(), 14 | aoi_map_() 15 | { 16 | 17 | } 18 | 19 | Aoi_Entity::~Aoi_Entity() { 20 | 21 | } 22 | 23 | void Aoi_Entity::add_aoi_entity(Aoi_Entity *entity, bool active) { 24 | if(aoi_map_.find(entity->entity_id()) != aoi_map_.end()) 25 | return; 26 | aoi_map_[entity->entity_id()] = entity; 27 | if(active) 28 | entity->add_aoi_entity(this, false); 29 | } 30 | 31 | void Aoi_Entity::del_aoi_entity(Aoi_Entity *entity, bool active) { 32 | if(aoi_map_.find(entity->entity_id()) == aoi_map_.end()) 33 | return; 34 | aoi_map_.erase(entity->entity_id()); 35 | if(active) 36 | entity->del_aoi_entity(this, false); 37 | } 38 | 39 | void Aoi_Entity::update_aoi_map(AOI_MAP &new_map) { 40 | for(AOI_MAP::iterator iter = aoi_map_.begin(); 41 | iter != aoi_map_.end(); iter++){ 42 | if(new_map.find(iter->second->entity_id()) == new_map.end()){ 43 | del_aoi_entity(iter->second, true); 44 | } 45 | } 46 | for(AOI_MAP::iterator iter = new_map.begin(); 47 | iter != new_map.end(); iter++){ 48 | if(aoi_map_.find(iter->second->entity_id()) == aoi_map_.end()){ 49 | add_aoi_entity(iter->second, true); 50 | } 51 | } 52 | // LOG_ERROR("%d's aoi_map_size is %d", entity_id(), aoi_map_.size()); 53 | // for(AOI_MAP::iterator iter = aoi_map_.begin(); 54 | // iter != aoi_map_.end(); iter++){ 55 | // LOG_ERROR("%d's aoi_entity is %d", entity_id(), iter->second->entity_id()); 56 | // } 57 | } 58 | 59 | void Aoi_Entity::clear_aoi_map() { 60 | for(AOI_MAP::iterator iter = aoi_map_.begin(); iter != aoi_map_.end(); iter++){ 61 | del_aoi_entity(iter->second, true); 62 | } 63 | } 64 | 65 | ENTITY_ID Aoi_Entity::entity_id(){ 66 | return entity_->entity_id(); 67 | } 68 | 69 | Position &Aoi_Entity::pos(){ 70 | return entity_->pos(); 71 | } 72 | 73 | Position &Aoi_Entity::opos(){ 74 | return entity_->opos(); 75 | } 76 | 77 | float Aoi_Entity::radius(){ 78 | return entity_->radius(); 79 | } 80 | 81 | void Aoi_Entity::reset() { 82 | entity_ = NULL; 83 | aoi_map_.clear(); 84 | } 85 | -------------------------------------------------------------------------------- /game_server/Aoi_Entity.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 11, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef AOI_ENTITY_H_ 7 | #define AOI_ENTITY_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "Public_Struct.h" 14 | #include "Scene_Entity.h" 15 | 16 | class Aoi_Entity; 17 | 18 | typedef std::list AOI_LIST; 19 | typedef boost::unordered_map AOI_MAP; 20 | 21 | class Aoi_Entity { 22 | public: 23 | Aoi_Entity(); 24 | ~Aoi_Entity(); 25 | void update_aoi_map(AOI_MAP &new_map); 26 | void add_aoi_entity(Aoi_Entity *entity, bool active); 27 | void del_aoi_entity(Aoi_Entity *entity, bool active); 28 | void clear_aoi_map(); 29 | ENTITY_ID entity_id(); 30 | Position &pos(); 31 | Position &opos(); 32 | float radius(); 33 | void reset(); 34 | 35 | inline Scene_Entity *scene_entity(){return entity_;} 36 | inline void set_scene_entity(Scene_Entity *entity){entity_ = entity;} 37 | inline void x_pos(AOI_LIST::iterator iter){x_pos_ = iter;} 38 | inline void y_pos(AOI_LIST::iterator iter){y_pos_ = iter;} 39 | inline AOI_LIST::iterator x_pos(){return x_pos_;} 40 | inline AOI_LIST::iterator y_pos(){return y_pos_;} 41 | inline AOI_MAP &aoi_map(){return aoi_map_;} 42 | private: 43 | Scene_Entity *entity_; 44 | AOI_LIST::iterator x_pos_; 45 | AOI_LIST::iterator y_pos_; 46 | AOI_MAP aoi_map_; 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /game_server/Aoi_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 10, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef AOI_MANAGER_H_ 7 | #define AOI_MANAGER_H_ 8 | 9 | #include "Object_Pool.h" 10 | #include "Aoi_Entity.h" 11 | 12 | typedef Object_Pool Aoi_Entity_Pool; 13 | 14 | class Aoi_Manager { 15 | public: 16 | Aoi_Manager(); 17 | ~Aoi_Manager(); 18 | static Aoi_Entity *create_aoi_entity(Scene_Entity *entity); 19 | static void reclaim_aoi_entity(Aoi_Entity *entity); 20 | int on_enter_aoi(Aoi_Entity *entity); 21 | int on_update_aoi(Aoi_Entity *entity); 22 | int on_leave_aoi(Aoi_Entity *entity); 23 | 24 | private: 25 | void insert_entity(Aoi_Entity *entity); 26 | void update_list(Aoi_Entity *entity, bool direct, int xy); 27 | void update_aoi_map(Aoi_Entity *entity); 28 | private: 29 | static Aoi_Entity_Pool aoi_entity_pool_; 30 | AOI_LIST x_list_; 31 | AOI_LIST y_list_; 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /game_server/Behavior_Tree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Aug 20, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef BEHAVIOR_TREE_H 7 | #define BEHAVIOR_TREE_H 8 | 9 | #include "Public_Struct.h" 10 | #include "Scene_Entity.h" 11 | #include "include/v8.h" 12 | using namespace v8; 13 | 14 | class AI_Action { 15 | public: 16 | AI_Action(std::string action); 17 | ~AI_Action(); 18 | bool process(Local &npc_obj, Isolate *isolate); 19 | private: 20 | std::string action_name_; 21 | }; 22 | 23 | class AI_Node { 24 | public: 25 | typedef std::vector AI_NODE_VEC; 26 | AI_Node(){} 27 | virtual ~AI_Node(){}; 28 | void init(Xml &xml, TiXmlNode *node); 29 | virtual bool process(Local &npc_obj, Isolate *isolate) = 0; 30 | protected: 31 | AI_NODE_VEC action_vec_; 32 | }; 33 | 34 | class Parallel_Node : public AI_Node { 35 | public: 36 | Parallel_Node(); 37 | ~Parallel_Node(); 38 | bool process(Local &npc_obj, Isolate *isolate); 39 | private: 40 | }; 41 | 42 | class Select_Node : public AI_Node { 43 | public: 44 | Select_Node(); 45 | ~Select_Node(); 46 | bool process(Local &npc_obj, Isolate *isolate); 47 | private: 48 | }; 49 | 50 | class Sequence_Node : public AI_Node { 51 | public: 52 | Sequence_Node(); 53 | ~Sequence_Node(); 54 | bool process(Local &npc_obj, Isolate *isolate); 55 | private: 56 | }; 57 | 58 | class Condition_Node : public AI_Node { 59 | public: 60 | Condition_Node(std::string action); 61 | ~Condition_Node(); 62 | bool process(Local &npc_obj, Isolate *isolate); 63 | private: 64 | AI_Action *action_; 65 | }; 66 | 67 | class Action_Node : public AI_Node { 68 | public: 69 | Action_Node(std::string action); 70 | ~Action_Node(); 71 | bool process(Local &npc_obj, Isolate *isolate); 72 | private: 73 | AI_Action *action_; 74 | }; 75 | 76 | class Behavior_Tree : public AI_Node { 77 | public: 78 | Behavior_Tree(int monster_id); 79 | ~Behavior_Tree(); 80 | bool process(Local &npc_obj, Isolate *isolate); 81 | 82 | inline int monster_id(){return monster_id_;} 83 | private: 84 | int monster_id_; 85 | }; 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /game_server/Game_Connector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Game_Connector.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Game_Connector.h" 9 | #include "Game_Manager.h" 10 | 11 | Game_DB_Connector::Game_DB_Connector(void) { } 12 | 13 | Game_DB_Connector::~Game_DB_Connector(void) { } 14 | 15 | Game_DB_Connector *Game_DB_Connector::instance_ = 0; 16 | 17 | Game_DB_Connector *Game_DB_Connector::instance(void) { 18 | if (! instance_) 19 | instance_ = new Game_DB_Connector; 20 | return instance_; 21 | } 22 | 23 | void Game_DB_Connector::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | if (!block_list_.empty()) { 27 | buf = block_list_.pop_front(); 28 | GAME_MANAGER->push_game_db_data(buf); 29 | } else { 30 | //没有数据时候延迟 31 | Time_Value::sleep(Time_Value(0,100)); 32 | } 33 | } 34 | } 35 | 36 | ///////////////////////////////////////////////////////////////////////////// 37 | 38 | Game_Master_Connector::Game_Master_Connector(void) { } 39 | 40 | Game_Master_Connector::~Game_Master_Connector(void) { } 41 | 42 | Game_Master_Connector *Game_Master_Connector::instance_ = 0; 43 | 44 | Game_Master_Connector *Game_Master_Connector::instance(void) { 45 | if (! instance_) 46 | instance_ = new Game_Master_Connector; 47 | return instance_; 48 | } 49 | 50 | void Game_Master_Connector::process_list(void) { 51 | Block_Buffer *buf = 0; 52 | while (1) { 53 | if (!block_list_.empty()) { 54 | buf = block_list_.pop_front(); 55 | GAME_MANAGER->push_game_master_data(buf); 56 | } else { 57 | //没有数据时候延迟 58 | Time_Value::sleep(Time_Value(0,100)); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /game_server/Game_Connector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Game_Connector.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef GAME_CONNECTOR_H_ 9 | #define GAME_CONNECTOR_H_ 10 | 11 | #include "Connector.h" 12 | 13 | class Game_DB_Connector: public Connector { 14 | public: 15 | static Game_DB_Connector *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Game_DB_Connector(void); 21 | virtual ~Game_DB_Connector(void); 22 | Game_DB_Connector(const Game_DB_Connector &); 23 | const Game_DB_Connector &operator=(const Game_DB_Connector &); 24 | 25 | private: 26 | static Game_DB_Connector *instance_; 27 | }; 28 | 29 | #define GAME_DB_CONNECTOR Game_DB_Connector::instance() 30 | 31 | /////////////////////////////////////////////////////////////////////////// 32 | 33 | class Game_Master_Connector: public Connector { 34 | public: 35 | static Game_Master_Connector *instance(void); 36 | 37 | virtual void process_list(void); 38 | 39 | private: 40 | Game_Master_Connector(void); 41 | virtual ~Game_Master_Connector(void); 42 | Game_Master_Connector(const Game_Master_Connector &); 43 | const Game_Master_Connector &operator=(const Game_Master_Connector &); 44 | 45 | private: 46 | static Game_Master_Connector *instance_; 47 | }; 48 | 49 | #define GAME_MASTER_CONNECTOR Game_Master_Connector::instance() 50 | 51 | #endif /* GAME_CONNECTOR_H_ */ 52 | -------------------------------------------------------------------------------- /game_server/Game_Player.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Game_Manager.h" 7 | 8 | Game_Player::Game_Player(void) { } 9 | 10 | Game_Player::~Game_Player(void) { } 11 | -------------------------------------------------------------------------------- /game_server/Game_Player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GAME_PLAYER_H_ 7 | #define GAME_PLAYER_H_ 8 | 9 | #include "Player.h" 10 | 11 | class Game_Player : public Player { 12 | public: 13 | Game_Player(void); 14 | virtual ~Game_Player(void); 15 | }; 16 | 17 | #endif /* GAME_PLAYER_H_ */ 18 | -------------------------------------------------------------------------------- /game_server/Game_Scene.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #include "Log.h" 7 | #include "Game_Scene.h" 8 | 9 | Game_Scene::Game_Scene(int type, SCENE_ID id): 10 | scence_type_(type), 11 | scene_id_(id), 12 | aoi_manager_(new Aoi_Manager), 13 | map_(), 14 | grid_width_(0.f) 15 | { 16 | load_scene_map(); 17 | } 18 | 19 | Game_Scene::~Game_Scene(){ 20 | 21 | } 22 | 23 | int Game_Scene::load_scene_map(){ 24 | // map_ = { 25 | // {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0}, 26 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 27 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 28 | // {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, 29 | // {0,0,0,1,0,0,0,0,0,0,1,1,0,0,0}, 30 | // {0,0,0,1,0,0,1,0,0,0,1,0,0,0,0}, 31 | // {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, 32 | // {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, 33 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 34 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 35 | // {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 36 | // {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 37 | // {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 38 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 39 | // {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 40 | // }; 41 | return 0; 42 | } 43 | 44 | int Game_Scene::on_enter_scene(Scene_Entity *entity){ 45 | entities_map_[entity->entity_id()] = entity; 46 | entity->scene(this); 47 | aoi_manager_->on_enter_aoi(entity->aoi_entity()); 48 | return 0; 49 | } 50 | 51 | int Game_Scene::on_update_scene(Scene_Entity *entity) { 52 | // if(!check_move(entity->pos(), pos)){ 53 | // 54 | // return -1; 55 | // } 56 | //entity->on_update_position(); 57 | aoi_manager_->on_update_aoi(entity->aoi_entity()); 58 | return 0; 59 | } 60 | 61 | int Game_Scene::on_leave_scene(Scene_Entity *entity) { 62 | entities_map_.erase(entity->entity_id()); 63 | entity->scene(NULL); 64 | aoi_manager_->on_leave_aoi(entity->aoi_entity()); 65 | return 0; 66 | } 67 | 68 | bool Game_Scene::check_move(Position opos, Position pos) { 69 | return map_[(int)pos.x][(int)pos.y] != 1; 70 | } 71 | -------------------------------------------------------------------------------- /game_server/Game_Scene.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef GAME_SCENE_H_ 7 | #define GAME_SCENE_H_ 8 | 9 | #include "Scene_Entity.h" 10 | #include "Aoi_Manager.h" 11 | 12 | typedef int32_t SCENE_ID; 13 | 14 | class Game_Scene { 15 | public: 16 | Game_Scene(int type, SCENE_ID id); 17 | ~Game_Scene(); 18 | int load_scene_map(); 19 | int on_enter_scene(Scene_Entity *entity); 20 | int on_update_scene(Scene_Entity *entity); 21 | int on_leave_scene(Scene_Entity *entity); 22 | 23 | inline SCENE_ID scene_id(){return scene_id_;} 24 | private: 25 | bool check_move(Position opos, Position pos); 26 | private: 27 | int scence_type_; 28 | SCENE_ID scene_id_; 29 | ENTITIES_MAP entities_map_; 30 | Aoi_Manager *aoi_manager_; 31 | int map_[15][15]; 32 | float grid_width_; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /game_server/Game_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Game_Server.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Game_Server.h" 9 | #include "Game_Manager.h" 10 | 11 | Game_Gate_Server::Game_Gate_Server(void) { } 12 | 13 | Game_Gate_Server::~Game_Gate_Server(void) { } 14 | 15 | Game_Gate_Server *Game_Gate_Server::instance_ = 0; 16 | 17 | Game_Gate_Server *Game_Gate_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new Game_Gate_Server; 20 | return instance_; 21 | } 22 | 23 | void Game_Gate_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | bool all_empty = true; 27 | if (!drop_cid_list_.empty()) { 28 | all_empty = false; 29 | int cid = drop_cid_list_.pop_front(); 30 | GAME_MANAGER->push_drop_gate_cid(cid); 31 | } 32 | if (!block_list_.empty()) { 33 | all_empty = false; 34 | buf = block_list_.pop_front(); 35 | GAME_MANAGER->push_game_gate_data(buf); 36 | } 37 | if (all_empty) { 38 | //没有数据时候延迟 39 | Time_Value::sleep(Time_Value(0,100)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /game_server/Game_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Game_Server.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef GAME_SERVER_H_ 9 | #define GAME_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class Game_Gate_Server: public Server { 14 | public: 15 | static Game_Gate_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Game_Gate_Server(void); 21 | virtual ~Game_Gate_Server(void); 22 | Game_Gate_Server(const Game_Gate_Server &); 23 | const Game_Gate_Server &operator=(const Game_Gate_Server &); 24 | 25 | private: 26 | static Game_Gate_Server *instance_; 27 | }; 28 | 29 | #define GAME_GATE_SERVER Game_Gate_Server::instance() 30 | 31 | #endif /* GAME_SERVER_H_ */ 32 | -------------------------------------------------------------------------------- /game_server/Game_Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Game_Manager.h" 7 | #include "Game_Timer.h" 8 | 9 | Game_Timer_Handler::Game_Timer_Handler(void) { } 10 | 11 | Game_Timer_Handler::~Game_Timer_Handler(void) { } 12 | 13 | int Game_Timer_Handler::handle_timeout(const Time_Value &tv) { 14 | GAME_MANAGER->push_tick(tv.sec()); 15 | GAME_TIMER->v8_tick(tv); 16 | return 0; 17 | } 18 | 19 | Game_Timer::Game_Timer(void) { } 20 | 21 | Game_Timer::~Game_Timer(void) { } 22 | 23 | Game_Timer *Game_Timer::instance_; 24 | 25 | Game_Timer *Game_Timer::instance(void) { 26 | if (instance_ == 0) 27 | instance_ = new Game_Timer; 28 | return instance_; 29 | } 30 | 31 | void Game_Timer::run_handler(void) { 32 | register_handler(); 33 | watcher_.loop(); 34 | } 35 | 36 | Epoll_Watcher &Game_Timer::watcher(void) { 37 | return watcher_; 38 | } 39 | 40 | void Game_Timer::register_handler(void) { 41 | Time_Value timeout_tv(0, 100 * 1000); 42 | watcher_.add(&timer_handler_, EVENT_TIMEOUT, &timeout_tv); 43 | } 44 | 45 | void Game_Timer::register_v8_handler(int timer_id, int internal, int first_tick_internal) { 46 | V8_Timer_Handler *handler = v8_timer_pool_.pop(); 47 | handler->timer_id = timer_id; 48 | handler->interval = internal; 49 | handler->next_tick = Time_Value::gettimeofday() + Time_Value(first_tick_internal); 50 | v8_timer_queue_.push(handler); 51 | } 52 | 53 | int Game_Timer::v8_tick(const Time_Value &now){ 54 | while(!v8_timer_queue_.empty() && (now > v8_timer_queue_.top()->next_tick)) { 55 | V8_Timer_Handler *handler = v8_timer_queue_.top(); 56 | v8_timer_queue_.pop(); 57 | v8_timer_list_.push_back(handler->timer_id); 58 | handler->next_tick += Time_Value(handler->interval / 1000, handler->interval % 1000 * 1000); 59 | v8_timer_queue_.push(handler); 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /game_server/Game_Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GAME_TIMER_H_ 7 | #define GAME_TIMER_H_ 8 | 9 | #include "Thread.h" 10 | #include "Epoll_Watcher.h" 11 | #include "Priority_Queue.h" 12 | #include "List.h" 13 | #include "Public_Struct.h" 14 | 15 | class Game_Timer_Handler: public Event_Handler { 16 | public: 17 | Game_Timer_Handler(void); 18 | virtual ~Game_Timer_Handler(void); 19 | 20 | virtual int handle_timeout(const Time_Value &tv); 21 | }; 22 | 23 | class Game_Timer: public Thread { 24 | public: 25 | typedef Object_Pool V8_Timer_Pool; 26 | typedef Priority_Queue V8_Timer_Queue; 27 | typedef List Timer_List; 28 | 29 | static Game_Timer *instance(void); 30 | virtual void run_handler(void); 31 | Epoll_Watcher &watcher(void); 32 | 33 | int v8_tick(const Time_Value &now); 34 | 35 | void register_handler(void); 36 | void register_v8_handler(int timer_id, int internal, int first_tick_internal); //注册js层定时器 37 | int pop_v8_timer(void); //js层获取超时定时器编号 38 | 39 | private: 40 | Game_Timer(void); 41 | virtual ~Game_Timer(void); 42 | Game_Timer(const Game_Timer &); 43 | const Game_Timer &operator=(const Game_Timer &); 44 | 45 | private: 46 | Epoll_Watcher watcher_; 47 | Game_Timer_Handler timer_handler_; 48 | V8_Timer_Pool v8_timer_pool_; 49 | V8_Timer_Queue v8_timer_queue_; 50 | Timer_List v8_timer_list_; 51 | 52 | static Game_Timer *instance_; 53 | }; 54 | 55 | #define GAME_TIMER Game_Timer::instance() 56 | 57 | inline int Game_Timer::pop_v8_timer(void){ 58 | if (v8_timer_list_.empty()) { 59 | return 0; 60 | } 61 | return v8_timer_list_.pop_front(); 62 | } 63 | 64 | #endif /* GAME_TIMER_H_ */ 65 | -------------------------------------------------------------------------------- /game_server/Scene_Entity.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #include "Scene_Entity.h" 7 | #include "Scene_Manager.h" 8 | #include "Game_Manager.h" 9 | #include "Aoi_Entity.h" 10 | #include "Server_Config.h" 11 | #include "Aoi_Manager.h" 12 | 13 | Scene_Entity::Scene_Entity(): 14 | entity_id_(0), 15 | pos_(0, 0, 0), 16 | opos_(0, 0, 0), 17 | scene_(0), 18 | radius_(SERVER_CONFIG->server_misc()["aoi_radius"].asInt()), 19 | aoi_entity_(0) 20 | { 21 | } 22 | 23 | Scene_Entity::~Scene_Entity() { 24 | 25 | } 26 | 27 | void Scene_Entity::reset() { 28 | entity_id_ = 0; 29 | pos_.reset(); 30 | opos_.reset(); 31 | scene_ =0; 32 | radius_ =0; 33 | if(aoi_entity_ != NULL) { 34 | Aoi_Manager::reclaim_aoi_entity(aoi_entity_); 35 | aoi_entity_= NULL; 36 | } 37 | } 38 | 39 | int Scene_Entity::on_update_position(Position new_pos) { 40 | opos_ = pos_; 41 | pos_ = new_pos; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /game_server/Scene_Entity.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef SCENE_ENTITY_H_ 7 | #define SCENE_ENTITY_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "Public_Struct.h" 14 | #include "Game_Player.h" 15 | 16 | typedef int32_t ENTITY_ID; 17 | 18 | class Aoi_Entity; 19 | class Scene_Entity; 20 | class Game_Scene; 21 | 22 | typedef boost::unordered_map ENTITIES_MAP; 23 | 24 | class Scene_Entity { 25 | public: 26 | Scene_Entity(); 27 | ~Scene_Entity(); 28 | int on_update_position(Position new_pos); 29 | void reset(); 30 | 31 | inline ENTITY_ID entity_id(){return entity_id_;} 32 | inline void set_entity_id(ENTITY_ID entity_id){entity_id_ = entity_id;} 33 | inline Position &pos(){return pos_;} 34 | inline Position &opos(){return opos_;} 35 | inline void scene(Game_Scene *scene){scene_ = scene;} 36 | inline Game_Scene *scene(){return scene_;} 37 | inline float radius(){return radius_;} 38 | inline Aoi_Entity *aoi_entity(){return aoi_entity_;} 39 | inline void set_aoi_entity(Aoi_Entity *entity){aoi_entity_ = entity;} 40 | private: 41 | ENTITY_ID entity_id_; 42 | Position pos_, opos_; 43 | Game_Scene *scene_; 44 | float radius_; 45 | Aoi_Entity *aoi_entity_; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /game_server/Scene_Manager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #include "Scene_Manager.h" 7 | #include "Server_Config.h" 8 | #include "Game_Manager.h" 9 | 10 | Scene_Manager *Scene_Manager::instance_ = NULL; 11 | 12 | Scene_Manager *Scene_Manager::instance() { 13 | if(instance_ == NULL) 14 | instance_ = new Scene_Manager(); 15 | return instance_; 16 | } 17 | 18 | Scene_Manager::Scene_Manager(): 19 | scenes_map_() 20 | { 21 | 22 | } 23 | 24 | Scene_Manager::~Scene_Manager(){ 25 | 26 | } 27 | 28 | int Scene_Manager::load_scene() { 29 | LOG_INFO("LOAD SCENE"); 30 | create_new_scene(1001); 31 | return 0; 32 | } 33 | 34 | int Scene_Manager::create_new_scene(int type){ 35 | Game_Scene *scene = new Game_Scene(type, create_scene_id(type)); 36 | scenes_map_[scene->scene_id()] = scene; 37 | LOG_INFO("new scene id %d", scene->scene_id()); 38 | Block_Buffer buffer; 39 | buffer.make_player_message(SYNC_GAME_MASTER_SCENE_INFO, 0, 0); 40 | buffer.write_int32(scene->scene_id()); 41 | buffer.write_int32(GAME_MANAGER->server_id()); 42 | buffer.finish_message(); 43 | GAME_MANAGER->send_to_master(buffer); 44 | return 0; 45 | } 46 | 47 | Game_Scene *Scene_Manager::get_scene(SCENE_ID scene_id){ 48 | SCENES_MAP::iterator iter = scenes_map_.find(scene_id); 49 | if(iter != scenes_map_.end()) 50 | return iter->second; 51 | return NULL; 52 | } 53 | 54 | void Scene_Manager::tick(Time_Value now) { 55 | // if(now - last_sync_ >= Time_Value(0, 1000 * aoi_broadcast_interval_)){ 56 | // GUARD(Thread_Mutex, mon, lock_); 57 | // for(SCENES_MAP::iterator iter = scenes_map_.begin(); iter != scenes_map_.end(); iter++){ 58 | // iter->second->broadcast_aoi_info(); 59 | // } 60 | // last_sync_ = now; 61 | // } 62 | } 63 | 64 | int Scene_Manager::create_scene_id(int type) { 65 | return GAME_MANAGER->server_id() * 10000 + type; 66 | } 67 | 68 | bool Scene_Manager::has_scene(int scene_id) { 69 | SCENES_MAP::iterator iter = scenes_map_.find(scene_id); 70 | return iter != scenes_map_.end(); 71 | } 72 | 73 | Scene_Entity *Scene_Manager::create_scene_entity(ENTITY_ID entity_id) { 74 | Scene_Entity *entity = scene_entity_pool_.pop(); 75 | entity->reset(); 76 | entity->set_entity_id(entity_id); 77 | entity->set_aoi_entity(Aoi_Manager::create_aoi_entity(entity)); 78 | return entity; 79 | } 80 | 81 | void Scene_Manager::reclaim_scene_entity(Scene_Entity *entity) { 82 | if(entity->scene()) 83 | entity->scene()->on_leave_scene(entity); 84 | entity->reset(); 85 | scene_entity_pool_.push(entity); 86 | } 87 | -------------------------------------------------------------------------------- /game_server/Scene_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Jun 21, 2016 3 | * Author: lijunliang 4 | */ 5 | 6 | #ifndef SCENE_MANAGER_H_ 7 | #define SCENE_MANAGER_H_ 8 | 9 | #include "Game_Scene.h" 10 | #include "Public_Struct.h" 11 | #include "Thread_Mutex.h" 12 | #include "Mutex_Guard.h" 13 | 14 | typedef boost::unordered_map SCENES_MAP; 15 | typedef Object_Pool Scene_Entity_Pool; 16 | 17 | class Scene_Manager { 18 | private: 19 | Scene_Manager(); 20 | ~Scene_Manager(); 21 | public: 22 | static Scene_Manager *instance(); 23 | int load_scene(); 24 | int create_new_scene(int type); 25 | Scene_Entity *create_scene_entity(ENTITY_ID entity_id); 26 | void reclaim_scene_entity(Scene_Entity *entity); 27 | Game_Scene *get_scene(SCENE_ID scene_id); 28 | void tick(Time_Value now); 29 | bool has_scene(int scene_id); 30 | private: 31 | int create_scene_id(int type); 32 | private: 33 | static Scene_Manager *instance_; 34 | Scene_Entity_Pool scene_entity_pool_; 35 | SCENES_MAP scenes_map_; 36 | }; 37 | 38 | #define SCENE_MANAGER Scene_Manager::instance() 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /gate_server/Gate_Client_Messager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GATE_CLIENT_MESSAGER_H_ 7 | #define GATE_CLIENT_MESSAGER_H_ 8 | 9 | class Block_Buffer; 10 | class Gate_Client_Messager { 11 | public: 12 | static Gate_Client_Messager *instance(void); 13 | 14 | int process_block(Block_Buffer &buf); 15 | int process_gate_block(int cid, int msg_id, Block_Buffer &buf); 16 | 17 | int connect_gate(int cid, Block_Buffer &buf); 18 | int send_heartbeat(int cid, Block_Buffer &buf); 19 | 20 | private: 21 | Gate_Client_Messager(void); 22 | virtual ~Gate_Client_Messager(void); 23 | Gate_Client_Messager(const Gate_Client_Messager &); 24 | const Gate_Client_Messager &operator=(const Gate_Client_Messager &); 25 | 26 | private: 27 | static Gate_Client_Messager *instance_; 28 | }; 29 | 30 | #define GATE_CLIENT_MESSAGER Gate_Client_Messager::instance() 31 | 32 | #endif /* GATE_CLIENT_MESSAGER_H_ */ 33 | -------------------------------------------------------------------------------- /gate_server/Gate_Connector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Gate_Connector.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Gate_Connector.h" 9 | #include "Gate_Manager.h" 10 | 11 | Gate_Game_Connector::Gate_Game_Connector(void){ } 12 | 13 | Gate_Game_Connector::~Gate_Game_Connector(void) { } 14 | 15 | void Gate_Game_Connector::process_list(void) { 16 | Block_Buffer *buf = 0; 17 | while (1) { 18 | if (!block_list_.empty()) { 19 | buf = block_list_.pop_front(); 20 | GATE_MANAGER->push_gate_game_data(buf); 21 | } else { 22 | //没有数据时候延迟 23 | Time_Value::sleep(Time_Value(0,100)); 24 | } 25 | } 26 | } 27 | 28 | /////////////////////////////////////////////////////////////////// 29 | 30 | Gate_Login_Connector::Gate_Login_Connector(void) { } 31 | 32 | Gate_Login_Connector::~Gate_Login_Connector(void) { } 33 | 34 | Gate_Login_Connector *Gate_Login_Connector::instance_ = 0; 35 | 36 | Gate_Login_Connector *Gate_Login_Connector::instance(void) { 37 | if (! instance_) 38 | instance_ = new Gate_Login_Connector; 39 | return instance_; 40 | } 41 | 42 | void Gate_Login_Connector::process_list(void) { 43 | Block_Buffer *buf = 0; 44 | while (1) { 45 | if (!block_list_.empty()) { 46 | buf = block_list_.pop_front(); 47 | GATE_MANAGER->push_gate_login_data(buf); 48 | } else { 49 | //没有数据时候延迟 50 | Time_Value::sleep(Time_Value(0,100)); 51 | } 52 | } 53 | } 54 | 55 | ////////////////////////////////////////////////////////////////////////////////// 56 | 57 | Gate_Master_Connector::Gate_Master_Connector(void) { } 58 | 59 | Gate_Master_Connector::~Gate_Master_Connector(void) { } 60 | 61 | Gate_Master_Connector *Gate_Master_Connector::instance_ = 0; 62 | 63 | Gate_Master_Connector *Gate_Master_Connector::instance(void) { 64 | if (! instance_) 65 | instance_ = new Gate_Master_Connector; 66 | return instance_; 67 | } 68 | 69 | void Gate_Master_Connector::process_list(void) { 70 | Block_Buffer *buf = 0; 71 | while (1) { 72 | if (!block_list_.empty()) { 73 | buf = block_list_.pop_front(); 74 | GATE_MANAGER->push_gate_master_data(buf); 75 | } else { 76 | //没有数据时候延迟 77 | Time_Value::sleep(Time_Value(0,100)); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /gate_server/Gate_Connector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gate_Connector.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef GATE_CONNECTOR_H_ 9 | #define GATE_CONNECTOR_H_ 10 | 11 | #include "Connector.h" 12 | 13 | class Gate_Game_Connector: public Connector { 14 | public: 15 | Gate_Game_Connector(void); 16 | virtual ~Gate_Game_Connector(void); 17 | 18 | virtual void process_list(void); 19 | }; 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | class Gate_Login_Connector: public Connector { 24 | public: 25 | static Gate_Login_Connector *instance(void); 26 | 27 | virtual void process_list(void); 28 | 29 | private: 30 | Gate_Login_Connector(void); 31 | virtual ~Gate_Login_Connector(void); 32 | Gate_Login_Connector(const Gate_Login_Connector &); 33 | const Gate_Login_Connector &operator=(const Gate_Login_Connector &); 34 | 35 | private: 36 | static Gate_Login_Connector *instance_; 37 | }; 38 | 39 | #define GATE_LOGIN_CONNECTOR Gate_Login_Connector::instance() 40 | 41 | //////////////////////////////////////////////////////////////////////////////// 42 | 43 | class Gate_Master_Connector: public Connector { 44 | public: 45 | static Gate_Master_Connector *instance(void); 46 | 47 | virtual void process_list(void); 48 | 49 | private: 50 | Gate_Master_Connector(void); 51 | virtual ~Gate_Master_Connector(void); 52 | Gate_Master_Connector(const Gate_Master_Connector &); 53 | const Gate_Master_Connector &operator=(const Gate_Master_Connector &); 54 | 55 | private: 56 | static Gate_Master_Connector *instance_; 57 | }; 58 | 59 | #define GATE_MASTER_CONNECTOR Gate_Master_Connector::instance() 60 | 61 | #endif /* GATE_SERVER_GATE_CONNECTOR_H_ */ 62 | -------------------------------------------------------------------------------- /gate_server/Gate_Inner_Messager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GATE_INNER_MESSAGER_H_ 7 | #define GATE_INNER_MESSAGER_H_ 8 | 9 | #include "Public_Struct.h" 10 | 11 | class Gate_Inner_Messager { 12 | public: 13 | static Gate_Inner_Messager *instance(void); 14 | 15 | //login消息处理 16 | int process_login_block(Block_Buffer &buf); 17 | 18 | //game消息处理 19 | int process_game_block(Block_Buffer &buf); 20 | 21 | //master消息处理 22 | int process_master_block(Block_Buffer &buf); 23 | 24 | //玩家成功登录 25 | int process_success_login(int player_cid, std::string &account); 26 | 27 | private: 28 | Gate_Inner_Messager(void); 29 | virtual ~Gate_Inner_Messager(void); 30 | Gate_Inner_Messager(const Gate_Inner_Messager &); 31 | const Gate_Inner_Messager &operator=(const Gate_Inner_Messager &); 32 | 33 | private: 34 | static Gate_Inner_Messager *instance_; 35 | }; 36 | 37 | #define GATE_INNER_MESSAGER Gate_Inner_Messager::instance() 38 | 39 | #endif /* GATE_INNER_MESSAGER_H_ */ 40 | -------------------------------------------------------------------------------- /gate_server/Gate_Player.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Gate_Manager.h" 7 | 8 | Gate_Player::Gate_Player(void) { } 9 | 10 | Gate_Player::~Gate_Player(void) { } 11 | 12 | void Gate_Player::reset(void) { 13 | Player::reset(); 14 | msg_info_.reset(); 15 | } 16 | 17 | int Gate_Player::verify_msg_info(uint32_t serial_cipher, uint32_t msg_time_cipher) { 18 | if (! msg_info_.is_inited) { 19 | msg_info_.hash_key = elf_hash(account().c_str(), account().length()); 20 | } 21 | uint32_t serial = serial_cipher ^ msg_info_.hash_key; 22 | uint32_t msg_time = msg_time_cipher ^ serial_cipher; 23 | 24 | if (! msg_info_.is_inited) { 25 | msg_info_.is_inited = true; 26 | msg_info_.msg_serial = serial; /// 第一条消息序号 27 | msg_info_.msg_timestamp = GATE_MANAGER->tick_time(); /// 第一条消息时间戳 28 | msg_info_.msg_interval_count_ = 1; 29 | msg_info_.msg_interval_timestamp = GATE_MANAGER->tick_time(); 30 | return 0; 31 | } 32 | 33 | ///判断发包频率,3s内发包超过90个判断为非法 34 | if (GATE_MANAGER->tick_time() - msg_info_.msg_interval_timestamp < Time_Value(3, 0)) { 35 | if (msg_info_.msg_interval_count_ > 90) { 36 | return ERROR_MSG_COUNT; 37 | } 38 | } else { 39 | msg_info_.msg_interval_count_ = 0; 40 | msg_info_.msg_interval_timestamp = GATE_MANAGER->tick_time(); 41 | } 42 | 43 | ///判断包的序号 44 | if (serial <= msg_info_.msg_serial || serial - msg_info_.msg_serial > static_cast(10)) { 45 | LOG_ERROR("serial = %d, msg_detail_.msg_serial = %d error", serial, msg_info_.msg_serial); 46 | return ERROR_MSG_SERIAL; 47 | } 48 | 49 | /// 判断包时间戳 50 | Time_Value msg_time_tv(msg_time, 0); 51 | if (std::abs(msg_time - msg_info_.msg_timestamp.sec()) > 900) { 52 | LOG_ERROR("msg_time = %d, msg_timestamp = %d error", msg_time, msg_info_.msg_timestamp.sec()); 53 | return ERROR_MSG_TIME; 54 | } 55 | 56 | msg_info_.msg_serial = serial; 57 | msg_info_.msg_timestamp = msg_time_tv; 58 | ++msg_info_.msg_interval_count_; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /gate_server/Gate_Player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GATE_PLAYER_H_ 7 | #define GATE_PLAYER_H_ 8 | 9 | #include "Player.h" 10 | 11 | class Gate_Player : public Player { 12 | public: 13 | Gate_Player(void); 14 | virtual ~Gate_Player(void); 15 | 16 | virtual void reset(void); 17 | 18 | ///验证包序列和时间戳 返回0为验证通过, -1操作过于频繁, -2包序号错, -3包时间戳错 19 | int verify_msg_info(uint32_t serial_cipher, uint32_t msg_time_cipher); 20 | 21 | private: 22 | Msg_Info msg_info_; 23 | }; 24 | 25 | #endif /* GATE_PLAYER_H_ */ 26 | -------------------------------------------------------------------------------- /gate_server/Gate_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Gate_Server.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Gate_Server.h" 9 | #include "Gate_Manager.h" 10 | 11 | Gate_Client_Server::Gate_Client_Server(void) { } 12 | 13 | Gate_Client_Server::~Gate_Client_Server(void) { } 14 | 15 | Gate_Client_Server *Gate_Client_Server::instance_ = 0; 16 | 17 | Gate_Client_Server *Gate_Client_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new Gate_Client_Server; 20 | return instance_; 21 | } 22 | 23 | void Gate_Client_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | int cid = 0; 26 | while (1) { 27 | bool all_empty = true; 28 | if (!drop_cid_list_.empty()) { 29 | all_empty = false; 30 | cid = drop_cid_list_.pop_front(); 31 | GATE_MANAGER->push_drop_cid(cid); 32 | } 33 | if (!block_list_.empty()) { 34 | all_empty = false; 35 | buf = block_list_.pop_front(); 36 | GATE_MANAGER->push_gate_client_data(buf); 37 | } 38 | if (all_empty) { 39 | //没有数据时候延迟 40 | Time_Value::sleep(Time_Value(0,100)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /gate_server/Gate_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gate_Server.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef GATE_SERVER_H_ 9 | #define GATE_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class Gate_Client_Server: public Server { 14 | public: 15 | static Gate_Client_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Gate_Client_Server(void); 21 | virtual ~Gate_Client_Server(void); 22 | Gate_Client_Server(const Gate_Client_Server &); 23 | const Gate_Client_Server &operator=(const Gate_Client_Server &); 24 | 25 | private: 26 | static Gate_Client_Server *instance_; 27 | }; 28 | 29 | #define GATE_CLIENT_SERVER Gate_Client_Server::instance() 30 | 31 | #endif /* GATE_SERVER_H_ */ 32 | -------------------------------------------------------------------------------- /gate_server/Gate_Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Gate_Manager.h" 7 | #include "Gate_Timer.h" 8 | 9 | Gate_Timer_Handler::Gate_Timer_Handler(void) { } 10 | 11 | Gate_Timer_Handler::~Gate_Timer_Handler(void) { } 12 | 13 | int Gate_Timer_Handler::handle_timeout(const Time_Value &tv) { 14 | return GATE_MANAGER->push_tick(tv.sec()); 15 | } 16 | 17 | Gate_Timer::Gate_Timer(void) { } 18 | 19 | Gate_Timer::~Gate_Timer(void) { } 20 | 21 | Gate_Timer *Gate_Timer::instance_; 22 | 23 | Gate_Timer *Gate_Timer::instance(void) { 24 | if (instance_ == 0) 25 | instance_ = new Gate_Timer; 26 | return instance_; 27 | } 28 | 29 | void Gate_Timer::run_handler(void) { 30 | register_handler(); 31 | watcher_.loop(); 32 | } 33 | 34 | Epoll_Watcher &Gate_Timer::watcher(void) { 35 | return watcher_; 36 | } 37 | 38 | void Gate_Timer::register_handler(void) { 39 | Time_Value timeout_tv(0, 100 * 1000); 40 | watcher_.add(&timer_handler_, EVENT_TIMEOUT, &timeout_tv); 41 | } 42 | -------------------------------------------------------------------------------- /gate_server/Gate_Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 16, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef GATE_TIMER_H_ 7 | #define GATE_TIMER_H_ 8 | 9 | #include "Thread.h" 10 | #include "Epoll_Watcher.h" 11 | #include "Block_Buffer.h" 12 | 13 | class Gate_Timer_Handler: public Event_Handler { 14 | public: 15 | Gate_Timer_Handler(void); 16 | virtual ~Gate_Timer_Handler(void); 17 | 18 | virtual int handle_timeout(const Time_Value &tv); 19 | }; 20 | 21 | class Gate_Timer: public Thread { 22 | public: 23 | static Gate_Timer *instance(void); 24 | virtual void run_handler(void); 25 | 26 | Epoll_Watcher &watcher(void); 27 | void register_handler(void); 28 | 29 | private: 30 | Gate_Timer(void); 31 | virtual ~Gate_Timer(void); 32 | Gate_Timer(const Gate_Timer &); 33 | const Gate_Timer &operator=(const Gate_Timer &); 34 | 35 | private: 36 | Epoll_Watcher watcher_; 37 | Gate_Timer_Handler timer_handler_; 38 | static Gate_Timer *instance_; 39 | }; 40 | 41 | #define GATE_TIMER Gate_Timer::instance() 42 | 43 | #endif /* GATE_TIMER_H_ */ 44 | -------------------------------------------------------------------------------- /js/config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:配置文件管理类 3 | * 作者:张亚磊 4 | * 时间:2016/03/24 5 | */ 6 | 7 | function Config() { 8 | this.item_json = null; //道具配置 9 | this.level_json = null; //玩家等级相关配置 10 | this.util_json = null; //公共综合配置 11 | this.vip_json = null; //vip配置 12 | this.recharge_json = null; //充值相关配置 13 | this.ai_json = null; //ai相关配置 14 | 15 | this.init = function() { 16 | try { 17 | var item_str = read_json("config/bag/item.json"); 18 | this.item_json = JSON.parse(item_str); 19 | 20 | var level_str = read_json("config/player/level.json"); 21 | this.level_json = JSON.parse(level_str); 22 | 23 | var util_str = read_json("config/player/util.json"); 24 | this.util_json = JSON.parse(util_str); 25 | 26 | var vip_str = read_json("config/vip/vip.json"); 27 | this.vip_json = JSON.parse(vip_str); 28 | 29 | var recharge_str = read_json("config/vip/recharge.json"); 30 | this.recharge_json = JSON.parse(recharge_str); 31 | 32 | var ai_str = read_json("config/monster/ai_config.json"); 33 | this.ai_json = JSON.parse(ai_str); 34 | } catch (err) { 35 | print(err.message); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /js/enum.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:枚举类型管理 3 | * 作者:张亚磊 4 | * 时间:2016/02/24 5 | */ 6 | 7 | if (typeof Server_Type == "undefined") { 8 | var Server_Type = {}; 9 | Server_Type.GAME_SERVER = 1; 10 | Server_Type.MASTER_SERVER = 2; 11 | } 12 | 13 | if (typeof Role_Status == "undefined") { 14 | var Role_Status = {}; 15 | Role_Status.ROLE_SUCCESS_LOAD = 0; //加载成功 16 | Role_Status.ROLE_SUCCESS_CREATE = 0; //加载成功 17 | Role_Status.ROLE_NOT_EXIST = 1; //角色不存在 18 | Role_Status.ROLE_HAS_EXIST = 2; // 角色已经存在 19 | Role_Status.ROLE_SAVE_OFFLINE = 3; //角色下线保存 20 | } 21 | 22 | if (typeof Rank_Type == "undefined") { 23 | var Rank_Type = {}; 24 | Rank_Type.LEVEL_RANK = 1; //等级排行 25 | Rank_Type.COMBAT_RANK = 2; //战力排行 26 | } 27 | -------------------------------------------------------------------------------- /js/error.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:错误号管理 3 | * 作者:张亚磊 4 | * 时间:2016/03/14 5 | */ 6 | 7 | if (typeof Error_Code == "undefined") { 8 | var Error_Code = {}; 9 | 10 | Error_Code.ERROR_SERVER_INNER = 10000; //服务器内部错误 11 | Error_Code.ERROR_CLIENT_SESSION = 10001; //session错误 12 | Error_Code.ERROR_CONNECT_TIMEOUT = 10002; //连接超时 13 | Error_Code.ERROR_MSG_COUNT = 10003; //消息数量过多 14 | Error_Code.ERROR_MSG_SERIAL = 10004; //消息序列号错误 15 | Error_Code.ERROR_MSG_TIME = 10005; //消息时间错误 16 | Error_Code.ERROR_DISCONNECT_SELF = 10006; //服务重启中,稍候再试 17 | Error_Code.ERROR_DISCONNECT_RELOGIN = 10007; //账号在其它地方登陆 18 | Error_Code.ERROR_LOGIN_FAIL = 10008; //登录失败 19 | Error_Code.ERROR_NOT_LOGIN = 10009; //玩家未登录 20 | Error_Code.ERROR_SESSION_TIMEOUT = 10010; //session到期 21 | Error_Code.ERROR_SESSION_SUCCESS = 10011; //login验证session成功,断开连接 22 | 23 | Error_Code.ERROR_CLIENT_OPERATE = 10050; //客户端非法操作 24 | Error_Code.ERROR_CLIENT_PARAM = 10051; //客户端参数错误 25 | Error_Code.ERROR_CONFIG_NOT_EXIST = 10052; //配置文件错误 26 | 27 | Error_Code.ERROR_ROLE_NOT_EXIST = 10100; //角色名已存在 28 | Error_Code.ERROR_ROLE_NAME_EXIST = 10101; //角色名已存在 29 | Error_Code.ERROR_ROLE_OFFLINE = 10102; //玩家已离线 30 | Error_Code.ERROR_LEVEL_NOT_ENOUGH = 10103; //角色等级不足 31 | Error_Code.ERROR_VITALITY_TIMES_NOT_ENOUGH = 10104; //购买体力次数不足 32 | Error_Code.ERROR_SKILL_POINT_NOT_ENOUGH = 10105; //技能点不足 33 | 34 | Error_Code.ERROR_BAG_FULL = 10200, //背包已满 35 | Error_Code.ERROR_ITEM_NOT_EXIST = 10201; //物品不存在 36 | Error_Code.ERROR_ITEM_NOT_ENOUGH = 10202; //物品不足 37 | Error_Code.ERROR_COPPER_NOT_ENOUGH = 10203; //铜钱不足 38 | Error_Code.ERROR_GOLD_NOT_ENOUGH = 10204; //元宝不足 39 | Error_Code.ERROR_PRODUCT_NOT_EXIST = 10205; //商品不存在 40 | Error_Code.ERROR_PRODUCT_NOT_ENOUGH = 10206; //商品数量不足 41 | Error_Code.ERROR_REFRESH_NOT_ENOUGH = 10207; //商店刷新次数耗尽 42 | Error_Code.ERROR_EXCHANGE_COUNT_NOT_ENOUGH = 10208; //兑换元宝次数耗尽 43 | 44 | Error_Code.ERROR_GUILD_EXIST = 10300; //公会已存在 45 | Error_Code.ERROR_GUILD_NOT_EXIST = 10301; //公会不存在 46 | } 47 | -------------------------------------------------------------------------------- /js/npc.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:NPC类 3 | * 作者:李俊良 4 | * 时间:2016/08/24 5 | */ 6 | 7 | function NPC(id) { 8 | this.id = id; 9 | this.type = 1000; 10 | this.hp = 0; 11 | this.mp = 0; 12 | this.centity = null; 13 | } 14 | 15 | NPC.prototype.create_npc = function() { 16 | 17 | } 18 | 19 | NPC.prototype.co_low_hp_handler = function() { 20 | if(this.hp < config.ai_json.low_hp) 21 | return true; 22 | return false; 23 | } 24 | 25 | NPC.prototype.co_low_mp_handler = function() { 26 | if(this.mp < config.ai_json.low_mp) 27 | return true; 28 | return false; 29 | } 30 | 31 | NPC.prototype.act_recover_hp_handler = function() { 32 | //print("npc ", this.id, " recover hp"); 33 | } 34 | 35 | NPC.prototype.act_recover_mp_handler = function() { 36 | //print("npc ", this.id, " recover mp"); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /js/offline.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:离线数据管理类 3 | * 作者:李俊良 4 | * 时间:2016/05/17 5 | */ 6 | 7 | function Offline() { 8 | this.offline_map = new Map(); 9 | this.drop_list = new Array(); 10 | this.is_change = false; 11 | } 12 | 13 | Offline.prototype.load_data = function(obj) { 14 | print('load offline data, size:', obj.offline_list.length); 15 | for(var i = 0; i < obj.offline_list.length; i++) { 16 | var offline_info = obj.offline_list[i]; 17 | print('role_id:', offline_info.role_id); 18 | this.offline_map.set(offline_info.role_id, offline_info); 19 | } 20 | } 21 | 22 | Offline.prototype.save_data = function() { 23 | var msg = new MSG_150105(); 24 | for (var value of this.offline_map.values()) { 25 | msg.offline_list.push(value); 26 | } 27 | send_master_msg_to_db(Msg.SYNC_MASTER_DB_SAVE_OFFLINE, msg); 28 | } 29 | 30 | Offline.prototype.save_data_handler = function() { 31 | if(!this.is_change) return; 32 | this.drop_offline(); 33 | this.save_data(); 34 | this.is_change = false; 35 | } 36 | 37 | Offline.prototype.handle_offline_info = function(player) { 38 | var offline_info = this.offline_map.get(player.player_info.role_id); 39 | if(offline_info == null) 40 | return; 41 | 42 | guild_manager.sync_guild_info_to_game(player, offline_info.guild_id, offline_info.guild_name); 43 | this.offline_map.delete(role_id); 44 | this.drop_list.push(role_id); 45 | this.is_change = true; 46 | } 47 | 48 | Offline.prototype.set_offline_detail = function(role_id, guild_id, guild_name) { 49 | var offline_info = this.offline_map.get(role_id); 50 | if (offline_info) { 51 | offline_info.guild_id = guild_id; 52 | offline_info.guild_name = guild_name; 53 | } else { 54 | offline_info = new Offline_Info(); 55 | offline_info.role_id = role_id; 56 | offline_info.guild_id = guild_id; 57 | offline_info.guild_name = guild_name; 58 | this.offline_info.offline_map.set(role_id, offline_info); 59 | } 60 | this.is_change = true; 61 | } 62 | 63 | Offline.prototype.drop_offline = function(){ 64 | if (this.drop_list.length <= 0) return; 65 | 66 | var msg = new MSG_150102(); 67 | msg.struct_name = "Offline_Info"; 68 | msg.index_list = this.drop_list; 69 | send_master_msg_to_db(Msg.SYNC_MASTER_DB_DELETE_DATA, msg); 70 | this.drop_list = []; 71 | } -------------------------------------------------------------------------------- /js/timer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:定时器系统 3 | * 作者:李俊良 4 | * 时间:2016/03/15 5 | */ 6 | 7 | function Timer() { 8 | var game_timer_map = new Map(); 9 | var game_timer_id = 1; 10 | 11 | var master_timer_map = new Map(); 12 | var master_timer_id = 1; 13 | 14 | this.init = function(server_type) { 15 | if (server_type == Server_Type.GAME_SERVER) { 16 | //注册玩家定时器,时间间隔500ms,每次到期遍历在线玩家进行处理 17 | this.register_game_timer(500, 0, this.game_player_handler); 18 | //注册每日刷新定时器,时间间隔24h 19 | this.register_game_timer(util.whole_day_msec, util.get_next_day_tick(config.util_json['daily_refresh_time']), this.game_daily_refresh_handler); 20 | //AI定时器 21 | this.register_game_timer(config.ai_json.thinking_interval, 0, this.npc_ai_handler); 22 | } else if (server_type == Server_Type.MASTER_SERVER) { 23 | //注册玩家定时器,时间间隔500ms,每次到期遍历在线玩家进行处理 24 | this.register_master_timer(500, 0, this.master_player_handler); 25 | //注册每日刷新定时器,时间间隔24h 26 | this.register_master_timer(util.whole_day_msec, util.get_next_day_tick(config.util_json['daily_refresh_time']), this.master_daily_refresh_handler); 27 | //注册公共信息保存定时器 28 | this.register_master_timer(15000, 0, this.master_data_save_handler); 29 | } 30 | } 31 | 32 | //注册game_server定时器 33 | this.register_game_timer = function(interval, next_tick, handler) { 34 | register_game_timer(game_timer_id, interval, next_tick); 35 | game_timer_map.set(game_timer_id, handler); 36 | game_timer_id++; 37 | } 38 | 39 | //获取game_server定时器 40 | this.get_game_timer_handler = function(timer_id) { 41 | return game_timer_map.get(timer_id); 42 | } 43 | 44 | //注册master_server定时器 45 | this.register_master_timer = function(interval, next_tick, handler) { 46 | register_master_timer(master_timer_id, interval, next_tick); 47 | master_timer_map.set(master_timer_id, handler); 48 | master_timer_id++; 49 | } 50 | 51 | //获取master_server定时器 52 | this.get_master_timer_handler = function(timer_id) { 53 | return master_timer_map.get(timer_id); 54 | } 55 | 56 | /////////////////////////////////定时器处理函数////////////////////////////////// 57 | this.game_player_handler = function() { 58 | var now = util.now_sec(); 59 | for (var value of game_player_role_id_map.values()) { 60 | value.tick(now); 61 | } 62 | } 63 | 64 | this.game_daily_refresh_handler = function() { 65 | for (var value of game_player_role_id_map.values()) { 66 | value.daily_refresh(); 67 | } 68 | } 69 | 70 | this.master_player_handler = function() { 71 | var now = util.now_sec(); 72 | for (var value of master_player_role_id_map.values()) { 73 | value.tick(now); 74 | } 75 | } 76 | 77 | this.master_daily_refresh_handler = function() { 78 | for (var value of master_player_role_id_map.values()) { 79 | value.daily_refresh(); 80 | } 81 | } 82 | 83 | this.master_data_save_handler = function() { 84 | guild_manager.save_data_handler(); 85 | rank_manager.save_data(); 86 | offline_manager.save_data_handler(); 87 | } 88 | 89 | this.npc_ai_handler = function(){ 90 | for(var npc of npc_map.values()) { 91 | thinking_in_ai(npc); 92 | } 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /js/util.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 描述:基础函数类 3 | * 作者:张亚磊 4 | * 时间:2016/03/15 5 | */ 6 | 7 | var util = function () {}; 8 | 9 | //一天的秒数 10 | util.whole_day_sec = 60 * 60 * 24; 11 | //一天的毫秒数 12 | util.whole_day_msec = util.whole_day_sec * 1000; 13 | 14 | //获取当前时间戳(秒单位) 15 | util.now_sec = function() { 16 | return Math.round(new Date().getTime()/1000); 17 | } 18 | 19 | //获取当前时间戳(毫秒单位) 20 | util.now_msec = function(date) { 21 | return new Date().getTime(); 22 | } 23 | 24 | //获取当前时间到指定定时时间的间隔(每日) 25 | util.get_next_day_tick = function(hour, min = 0, sec = 0) { 26 | var now = util.now_sec(); 27 | var date = new Date(); 28 | date.setHours(hour); 29 | date.setMinutes(min); 30 | date.setSeconds(sec); 31 | date.setMilliseconds(0); 32 | var time = date.getTime() / 1000; 33 | var next_tick = time - now; 34 | return next_tick > 0 ? next_tick : (next_tick + util.whole_day_sec); 35 | } 36 | 37 | util.is_number = function(value) { 38 | return typeof(value) == "number"; 39 | } 40 | -------------------------------------------------------------------------------- /lib/Accept.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Accept.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef ACCEPT_H_ 9 | #define ACCEPT_H_ 10 | 11 | #include "Thread.h" 12 | 13 | class Server; 14 | class Accept: public Thread { 15 | public: 16 | Accept(void); 17 | virtual ~Accept(void); 18 | 19 | void set(Server *server, int port); 20 | int init(void); 21 | int fini(void); 22 | 23 | void server_listen(void); 24 | void server_accept(void); 25 | virtual int accept_svc(int connfd); 26 | 27 | virtual void run_handler(void); 28 | virtual void exit_handler(void); 29 | 30 | protected: 31 | Server *server_; 32 | 33 | private: 34 | int port_; 35 | int listenfd_; 36 | }; 37 | 38 | #endif /* ACCEPT_H_ */ 39 | -------------------------------------------------------------------------------- /lib/Base_Struct.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Base_Struct.cpp 3 | * 4 | * Created on: May 30, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Base_Struct.h" 9 | 10 | Base_Struct::Base_Struct(Xml &xml, TiXmlNode *node): 11 | struct_name_(), 12 | table_name_(), 13 | index_name_(), 14 | msg_id_(0) 15 | { 16 | field_vec_.clear(); 17 | 18 | if(node){ 19 | struct_name_ = xml.get_key(node); 20 | TiXmlNode* sub_node = xml.enter_node(node, struct_name_.c_str()); 21 | if (sub_node) { 22 | std::string label_name; 23 | XML_LOOP_BEGIN(sub_node) 24 | if((label_name = xml.get_key(sub_node)) == "head"){ 25 | table_name_ = xml.get_attr_str(sub_node, "table_name"); 26 | index_name_ = xml.get_attr_str(sub_node, "index_name"); 27 | msg_id_ = xml.get_attr_int(sub_node, "msg_id"); 28 | } else { 29 | Field_Info field_info; 30 | field_info.field_label = label_name; 31 | field_info.field_type = xml.get_attr_str(sub_node, "type"); 32 | field_info.field_name = xml.get_attr_str(sub_node, "name"); 33 | field_info.key_type = xml.get_attr_str(sub_node, "key_type"); 34 | field_info.key_name = xml.get_attr_str(sub_node, "key_name"); 35 | field_vec_.push_back(field_info); 36 | } 37 | XML_LOOP_END(sub_node) 38 | } 39 | } 40 | } 41 | 42 | Base_Struct::~Base_Struct(){ 43 | struct_name_.clear(); 44 | table_name_.clear(); 45 | index_name_.clear(); 46 | msg_id_ = 0; 47 | field_vec_.clear(); 48 | } 49 | -------------------------------------------------------------------------------- /lib/Base_Struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Base_Struct.h 3 | * 4 | * Created on: May 30, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef BASE_STRUCT_H_ 9 | #define BASE_STRUCT_H_ 10 | 11 | #include "Xml.h" 12 | #include "Block_Buffer.h" 13 | 14 | struct Field_Info { 15 | std::string field_label; 16 | std::string field_type; 17 | std::string field_name; 18 | std::string key_type; 19 | std::string key_name; 20 | }; 21 | 22 | class Base_Struct { 23 | public: 24 | Base_Struct(Xml &xml, TiXmlNode *node); 25 | virtual ~Base_Struct(); 26 | 27 | virtual void create_data(int64_t key_index, Block_Buffer &buffer) {} 28 | virtual void load_data(int64_t key_index, Block_Buffer &buffer) {} 29 | virtual void save_data(Block_Buffer &buffer) {} 30 | virtual void save_data_vector(Block_Buffer &buffer) {} 31 | virtual void delete_data(Block_Buffer &buffer) {} 32 | 33 | inline const std::string &struct_name() { return struct_name_; } 34 | inline const std::string &table_name() { return table_name_; } 35 | inline void set_table_name(const std::string &table_name) { table_name_ = table_name; } 36 | inline const std::string &index_name() { return index_name_; } 37 | inline int32_t msg_id() { return msg_id_; } 38 | inline const std::vector& field_vec() { return field_vec_; } 39 | inline bool is_struct(const std::string &type); 40 | 41 | private: 42 | std::string struct_name_; 43 | std::string table_name_; 44 | std::string index_name_; 45 | int32_t msg_id_; 46 | std::vector field_vec_; 47 | }; 48 | 49 | ///////////////////////////////////////////////////////////////////////////////////////////////// 50 | inline bool Base_Struct::is_struct(const std::string &field_type){ 51 | if(field_type == "int8" || field_type == "int16" || field_type == "int32" || field_type == "int64" || 52 | field_type == "double" || field_type == "bool" || field_type == "string") return false; 53 | return true; 54 | } 55 | 56 | #endif /* BASE_STRUCT_H_ */ 57 | -------------------------------------------------------------------------------- /lib/Block_Pool_Group.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Block_Pool_Group.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef BLOCK_POOL_GROUP_H_ 9 | #define BLOCK_POOL_GROUP_H_ 10 | 11 | #include "Object_Pool.h" 12 | #include "Block_Buffer.h" 13 | #include 14 | 15 | class Block_Pool_Group { 16 | public: 17 | typedef Object_Pool Block_Pool; 18 | typedef std::vector Pool_Group; 19 | 20 | Block_Pool_Group(size_t group_size = 1) 21 | : group_size_(group_size), x_(0) { 22 | init(); 23 | } 24 | 25 | virtual ~Block_Pool_Group(void) { 26 | fini(); 27 | } 28 | 29 | int init(void) { 30 | pool_group_.clear(); 31 | 32 | Block_Pool *pool = 0; 33 | for (size_t i = 0; i < group_size_; ++i) { 34 | if ((pool = new Block_Pool) == 0) { 35 | LIB_LOG_FATAL("new return 0"); 36 | } 37 | pool_group_.push_back(pool); 38 | } 39 | return 0; 40 | } 41 | 42 | int fini(void) { 43 | for (Pool_Group::iterator it = pool_group_.begin(); it != pool_group_.end(); ++it) { 44 | (*it)->clear(); 45 | } 46 | pool_group_.clear(); 47 | return 0; 48 | } 49 | 50 | Block_Buffer *pop_block(int cid) { 51 | return pool_group_[cid % group_size_]->pop(); 52 | } 53 | 54 | int push_block(int cid, Block_Buffer *buf) { 55 | if (! buf) { 56 | LIB_LOG_TRACE("buf == 0"); 57 | return -1; 58 | } 59 | buf->recycle_space(); 60 | return pool_group_[cid % group_size_]->push(buf); 61 | } 62 | 63 | void shrink_all(void) { 64 | for (size_t i = 0; i < group_size_; ++i) { 65 | pool_group_[i]->shrink_all(); 66 | } 67 | } 68 | 69 | void dump_info(void) { 70 | for (size_t i = 0; i < group_size_; ++i) { 71 | LIB_LOG_DEBUG("***pool_group_[%u]: ", i); 72 | pool_group_[i]->debug_info(); 73 | } 74 | } 75 | 76 | void dump_size(void) { 77 | size_t sum_size = 0; 78 | for (Pool_Group::iterator group_it = pool_group_.begin(); group_it != pool_group_.end(); ++group_it) { 79 | sum_size += (* group_it)->sum_size(); 80 | } 81 | } 82 | 83 | void block_group_info(std::vector &group_info) { 84 | Block_Group_Info info; 85 | for (size_t i = 0; i < group_size_; ++i) { 86 | info.free_list_size_ = pool_group_[i]->free_obj_list_size(); 87 | info.used_list_size_ = pool_group_[i]->used_obj_list_size(); 88 | info.sum_bytes_ = pool_group_[i]->sum_size(); 89 | 90 | group_info.push_back(info); 91 | } 92 | } 93 | 94 | private: 95 | Pool_Group pool_group_; 96 | size_t group_size_; 97 | int x_; 98 | }; 99 | 100 | #endif /* BLOCK_POOL_GROUP_H_ */ 101 | -------------------------------------------------------------------------------- /lib/Config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Config.cpp 3 | * 4 | * Created on: Mar 1,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "Config.h" 14 | #include "Lib_Log.h" 15 | 16 | Config::Config(void) { } 17 | 18 | Config::~Config(void) { } 19 | 20 | int Config::load_json_file(const char *file_path, Json::Value &conf_value) { 21 | int fd = 0; 22 | if ((fd = open(file_path, O_RDONLY)) < 0) { 23 | LIB_LOG_FATAL("Failed to open file %s\n error: %s\n.\n", file_path, reader_.getFormatedErrorMessages().c_str()); 24 | } 25 | 26 | struct stat statbuf; 27 | if (fstat(fd, &statbuf) < 0) { 28 | LIB_LOG_FATAL("fstat error file %s\n", file_path); 29 | } 30 | 31 | void *src = 0; 32 | if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 33 | LIB_LOG_FATAL("mmap error file %s\n", file_path); 34 | } 35 | 36 | if (! reader_.parse(static_cast(src), static_cast(src) + statbuf.st_size, conf_value)) { 37 | LIB_LOG_FATAL("Failed to parse file %s\n error: %s\n.\n", file_path, reader_.getFormatedErrorMessages().c_str()); 38 | } 39 | 40 | if (munmap(src, statbuf.st_size) < 0) { 41 | LIB_LOG_FATAL("munmap error file %s\n", file_path); 42 | } 43 | 44 | close(fd); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /lib/Connect.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Connect.cpp 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include "Common_Func.h" 12 | #include "Connect.h" 13 | #include "Connector.h" 14 | 15 | Connect::Connect(void):connector_(0) { } 16 | 17 | Connect::~Connect(void) { } 18 | 19 | int Connect::set(Connector *connector) { 20 | connector_ = connector; 21 | return 0; 22 | } 23 | 24 | int Connect::connect(const char *ip, int port) { 25 | int connfd = 0; 26 | struct sockaddr_in serveraddr; 27 | 28 | if ((connfd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) { 29 | LIB_LOG_FATAL("connect socket fail, connfd = %d", connfd); 30 | } 31 | 32 | memset(&serveraddr, 0, sizeof(serveraddr)); 33 | serveraddr.sin_family = AF_INET; 34 | serveraddr.sin_port = htons(port); 35 | 36 | if (::inet_pton(AF_INET, ip, &serveraddr.sin_addr) <= 0) { 37 | LIB_LOG_ERROR("connect inet_pton fail, ip = %s, port = %d", ip, port); 38 | ::close(connfd); 39 | return -1; 40 | } 41 | 42 | if (::connect(connfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { 43 | LIB_LOG_ERROR("connect fail, ip = %s, port = %d", ip, port); 44 | ::close(connfd); 45 | return -1; 46 | } 47 | 48 | set_nonblock(connfd); 49 | 50 | return connect_svc(connfd); 51 | } 52 | 53 | int Connect::connect_svc(int connfd) { 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /lib/Connect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Connect.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef CONNECT_H_ 9 | #define CONNECT_H_ 10 | 11 | class Connector; 12 | class Connect { 13 | public: 14 | Connect(void); 15 | virtual ~Connect(void); 16 | 17 | int set(Connector *connector); 18 | int connect(const char *ip, int port); 19 | virtual int connect_svc(int connfd); 20 | 21 | protected: 22 | Connector *connector_; 23 | }; 24 | 25 | #endif /* CONNECT_H_ */ 26 | -------------------------------------------------------------------------------- /lib/Date_Time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * DateTime.cpp 3 | * 4 | * Created on: Jan 12,2016 5 | * Author: "zhangyalei" 6 | */ 7 | 8 | #include "Date_Time.h" 9 | 10 | Date_Time::Date_Time(void) : day_(0), month_(0), year_(0), hour_(0), minute_(0), second_(0), microsec_(0), wday_(0) { 11 | this->update (); 12 | } 13 | 14 | 15 | Date_Time::Date_Time(const Time_Value &timevalue) : day_(0), month_(0), year_(0), hour_(0), minute_(0), second_(0), microsec_(0), wday_(0){ 16 | this->update (timevalue); 17 | } 18 | 19 | Date_Time::Date_Time(long day, long month, long year, long hour, long minute, long second, long microsec, long wday) 20 | : day_ (day), month_ (month), year_ (year), hour_ (hour), minute_ (minute), second_ (second), microsec_ (microsec), wday_ (wday) { 21 | 22 | } 23 | 24 | void Date_Time::update(void) { 25 | update(Time_Value::gettimeofday ()); 26 | } 27 | 28 | void Date_Time::update(const Time_Value &timevalue) { 29 | time_t time = timevalue.sec(); 30 | struct tm tm_time; 31 | ::localtime_r(&time, &tm_time); 32 | this->day_ = tm_time.tm_mday; 33 | this->month_ = tm_time.tm_mon + 1; // localtime's months are 0-11 34 | this->year_ = tm_time.tm_year + 1900; // localtime reports years since 1900 35 | this->hour_ = tm_time.tm_hour; 36 | this->minute_ = tm_time.tm_min; 37 | this->second_ = tm_time.tm_sec; 38 | this->microsec_ = timevalue.usec(); 39 | this->wday_ = tm_time.tm_wday; 40 | } 41 | 42 | long Date_Time::day(void) const { 43 | return day_; 44 | } 45 | 46 | void Date_Time::day(long day) { 47 | day_ = day; 48 | } 49 | 50 | long Date_Time::month(void) const { 51 | return month_; 52 | } 53 | 54 | void Date_Time::month(long month) { 55 | month_ = month; 56 | } 57 | 58 | long Date_Time::year(void) const { 59 | return year_; 60 | } 61 | 62 | void Date_Time::year(long year) { 63 | year_ = year; 64 | } 65 | 66 | long Date_Time::hour(void) const { 67 | return hour_; 68 | } 69 | 70 | void Date_Time::hour(long hour) { 71 | hour_ = hour; 72 | } 73 | 74 | long Date_Time::minute(void) const { 75 | return minute_; 76 | } 77 | 78 | void Date_Time::minute(long minute) { 79 | minute_ = minute; 80 | } 81 | 82 | long Date_Time::second(void) const { 83 | return second_; 84 | } 85 | 86 | void Date_Time::second(long second) { 87 | second_ = second; 88 | } 89 | 90 | long Date_Time::microsec(void) const { 91 | return microsec_; 92 | } 93 | 94 | void Date_Time::microsec(long microsec) { 95 | microsec_ = microsec; 96 | } 97 | 98 | long Date_Time::weekday(void) const { 99 | return wday_; 100 | } 101 | 102 | void Date_Time::weekday(long wday) { 103 | wday_ = wday; 104 | } 105 | 106 | time_t Date_Time::time_sec(void) { 107 | struct tm t; 108 | t.tm_year = year_ - 1900; 109 | t.tm_mon = month_ - 1; 110 | t.tm_mday = day_; 111 | t.tm_hour = hour_; 112 | t.tm_min = minute_; 113 | t.tm_sec = second_; 114 | t.tm_isdst = 0; 115 | return mktime(&t); 116 | } 117 | -------------------------------------------------------------------------------- /lib/Date_Time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DateTime.h 3 | * 4 | * Created on: Jan 12,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DATE_TIME_H_ 9 | #define DATE_TIME_H_ 10 | 11 | #include "Time_Value.h" 12 | 13 | class Date_Time { 14 | public: 15 | Date_Time(void); 16 | 17 | explicit Date_Time(const Time_Value &timevalue); 18 | 19 | Date_Time(long day, 20 | long month = 0, 21 | long year = 0, 22 | long hour = 0, 23 | long minute = 0, 24 | long second = 0, 25 | long microsec = 0, 26 | long wday = 0); 27 | 28 | /// Update to the current time/date. 29 | void update(void); 30 | 31 | /// Update to the given Time_Value 32 | void update(const Time_Value &timevalue); 33 | 34 | long day(void) const; 35 | void day(long day); 36 | 37 | long month(void) const; 38 | void month(long month); 39 | 40 | long year(void) const; 41 | void year(long year); 42 | 43 | long hour(void) const; 44 | void hour(long hour); 45 | 46 | long minute(void) const; 47 | void minute(long minute); 48 | 49 | long second(void) const; 50 | void second(long second); 51 | 52 | long microsec(void) const; 53 | void microsec(long microsec); 54 | 55 | long weekday(void) const; 56 | void weekday(long wday); 57 | 58 | time_t time_sec(void); 59 | 60 | private: 61 | long day_; 62 | long month_; 63 | long year_; 64 | long hour_; 65 | long minute_; 66 | long second_; 67 | long microsec_; 68 | long wday_; 69 | }; 70 | 71 | #endif /* DATE_TIME_H_ */ 72 | -------------------------------------------------------------------------------- /lib/Epoll_Watcher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Epoll_Watcher.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | * 7 | * 底层使用epoll实现,主要功能为io事件回调、心跳超时机制、毫秒级定时器 8 | * 9 | */ 10 | 11 | #ifndef EPOLL_WATCHER_H_ 12 | #define EPOLL_WATCHER_H_ 13 | 14 | #include 15 | #include 16 | #include "Event_Handler.h" 17 | #include "Object_Pool.h" 18 | #include "Thread_Mutex.h" 19 | #include "boost/unordered_map.hpp" 20 | //notice: 如果用std::unordered_map, 需添加-std=c++0x参数 -D__GXX_EXPERIMENTAL_CXX0X__ 21 | #include "Public_Define.h" 22 | 23 | class Epoll_Watcher : public Event_Handler { 24 | //Timer 25 | typedef std::priority_queue, Event_Handler::greater> Timer_Queue; 26 | typedef boost::unordered_set Event_Timer_Set; 27 | //IO 28 | typedef std::vector Event_Map; 29 | //心跳 30 | typedef boost::unordered_map Heart_Map; 31 | typedef RE_Mutex Mutex; 32 | 33 | public: 34 | /** 35 | * type可以指定IO附带心跳机制: WITH_IO_HEARTBEAT 36 | * heart_second制定IO心跳超时时间 37 | */ 38 | Epoll_Watcher(int type = 0, int heart_second = 30); 39 | virtual ~Epoll_Watcher(void); 40 | 41 | int init(void); 42 | int fini(void); 43 | 44 | /** 45 | * 添加监听事件 46 | * op可以为EVENT_INPUT, EVENT_OUTPUT, EVENT_TIMEOUT等位集合 47 | * EVENT_INPUT, EVENT_OUTPUT, EVENT_ONCE_IO_IN, EVENT_ONCE_IO_OUT为监听IO事件 48 | * EVENT_TIMEOUT, EVENT_ONCE_TIMER为监听定时器事件 49 | * tv为定时器事件指定的间隔时间 50 | */ 51 | virtual int add(Event_Handler *evh, int op, Time_Value *tv = NULL); 52 | 53 | /// 移除eh的IO/定时器事件监听 54 | virtual int remove(Event_Handler *eh); 55 | 56 | /// 开始事件监听循环 57 | virtual int loop(void); 58 | 59 | /// 结束事件监听循环 60 | virtual int end_loop(void); 61 | 62 | /// 内部接口 63 | virtual int inner_cmd(void); 64 | 65 | /// 使阻塞的epoll_wait返回, 执行新的inner_cmd/定时器 66 | virtual int notify(void); 67 | 68 | private: 69 | int open(void); 70 | int io_open(void); 71 | int timer_open(void); 72 | 73 | int add_io(Event_Handler *evh, int op); 74 | int add_timer(Event_Handler *evh, int op, Time_Value *tv); 75 | 76 | int remove_io(Event_Handler *evh); 77 | int remove_timer(Event_Handler *evh); 78 | 79 | /// 计算epoll_wait的超时时间 80 | int calculate_timeout(void); 81 | 82 | /// 处理已到期定时器事件 83 | void process_timer_event(void); 84 | 85 | void watcher_loop(void); 86 | 87 | int handle_input(void); 88 | int handle_timeout(const Time_Value &tv); 89 | 90 | int next_heart_idx(void); 91 | 92 | private: 93 | int type_; 94 | bool end_flag_; 95 | int epfd_; 96 | int max_events_; 97 | struct epoll_event *events_; 98 | 99 | Event_Map pending_io_map_; 100 | int io_heart_idx_, heart_second_; 101 | Heart_Map io_heart_map_[2]; 102 | Mutex io_lock_; 103 | 104 | Timer_Queue tq_; /// 时间优先队列(最小堆) 105 | Event_Timer_Set timer_set_; 106 | Mutex tq_lock_; 107 | 108 | int pipe_fd_[2]; /// notify管道 109 | }; 110 | 111 | #endif /* EPOLL_WATCHER_H_ */ 112 | -------------------------------------------------------------------------------- /lib/Event_Handler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Event_Handler.cpp 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef EVENT_HANDLER_H_ 9 | #define EVENT_HANDLER_H_ 10 | 11 | #include "Time_Value.h" 12 | 13 | class Epoll_Watcher; 14 | class Event_Handler { 15 | public: 16 | class greater { 17 | public: 18 | inline bool operator()(const Event_Handler *a, const Event_Handler *b) { 19 | return (a->get_absolute_tv()) > (b->get_absolute_tv()); 20 | } 21 | }; 22 | 23 | Event_Handler(void) : io_flag(0), fd_(0), timer_flag(0), reactor_(0), heart_idx_(0) { } 24 | virtual ~Event_Handler(void) { } 25 | 26 | /// callback interface. 27 | virtual int handle_input(void) { 28 | return 0; 29 | } 30 | 31 | virtual int handle_output(void) { 32 | return 0; 33 | } 34 | 35 | virtual int handle_close(void) { 36 | return 0; 37 | } 38 | 39 | virtual int handle_timeout(const Time_Value &tv) { 40 | return 0; 41 | } 42 | 43 | /// getter/setter 44 | int get_fd(void) const { 45 | return fd_; 46 | } 47 | 48 | void set_fd(int fd) { 49 | fd_ = fd; 50 | } 51 | 52 | void set_tv(Time_Value &ab_tv) { 53 | this->absolute_tv_ = ab_tv; 54 | } 55 | 56 | void set_tv(Time_Value &ab_tv, Time_Value &re_tv) { 57 | this->absolute_tv_ = ab_tv; 58 | this->relative_tv_ = re_tv; 59 | } 60 | 61 | const Time_Value &get_absolute_tv(void) const { 62 | return absolute_tv_; 63 | } 64 | 65 | Time_Value &get_relative_tv(void) { 66 | return relative_tv_; 67 | } 68 | 69 | int get_timer_flag(void) const { 70 | return timer_flag; 71 | } 72 | 73 | void set_timer_flag(int flag) { 74 | timer_flag |= flag; 75 | } 76 | 77 | int get_io_flag(void) const { 78 | return io_flag; 79 | } 80 | 81 | void set_io_flag(int flag) { 82 | io_flag |= flag; 83 | } 84 | 85 | int get_heart_idx(void) const { 86 | return heart_idx_; 87 | } 88 | 89 | void set_heart_idx(int idx) { 90 | heart_idx_ = idx; 91 | } 92 | 93 | Epoll_Watcher *get_reactor(void) { 94 | return reactor_; 95 | } 96 | 97 | void set_reactor(Epoll_Watcher *r) { 98 | reactor_ = r; 99 | } 100 | 101 | void reset(void) { 102 | io_flag = 0; 103 | fd_ = 0; 104 | timer_flag = 0; 105 | absolute_tv_ = relative_tv_ = Time_Value::zero; 106 | reactor_ = 0; 107 | heart_idx_ = 0; 108 | } 109 | 110 | private: 111 | int io_flag; 112 | int fd_; 113 | 114 | int timer_flag; 115 | Time_Value absolute_tv_, relative_tv_; 116 | 117 | Epoll_Watcher *reactor_; 118 | 119 | int heart_idx_; 120 | }; 121 | 122 | 123 | #endif /* EVENT_HANDLER_H_ */ 124 | -------------------------------------------------------------------------------- /lib/Hot_Update.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Hot_Update.h 3 | * 4 | * Created on: Mar 1,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef HOT_UPDATE_H_ 9 | #define HOT_UPDATE_H_ 10 | 11 | #include 12 | #include 13 | #include "Thread.h" 14 | #include "boost/unordered_map.hpp" 15 | 16 | class Hot_Update: public Thread { 17 | public: 18 | typedef std::set MD5_STR_SET; 19 | typedef boost::unordered_map MD5_STR_MAP; 20 | 21 | Hot_Update(); 22 | virtual ~Hot_Update(); 23 | 24 | static Hot_Update *instance(void); 25 | 26 | virtual void run_handler(void); 27 | 28 | int notice_update(const std::string module); 29 | 30 | std::string file_md5(std::string file_name); 31 | 32 | void check_config(std::string module); 33 | void init_all_module(void); 34 | void get_md5_str(std::string path, MD5_STR_SET &md5_str_set); 35 | 36 | private: 37 | std::vector update_module_; 38 | Time_Value update_time_; 39 | static Hot_Update *instance_; 40 | MD5_STR_MAP md5_str_map_; 41 | 42 | Time_Value notify_interval_; 43 | }; 44 | #define HOT_UPDATE Hot_Update::instance() 45 | 46 | #endif /* HOTUPDATE_H_ */ 47 | -------------------------------------------------------------------------------- /lib/Http_Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Http_Client.h 3 | * 4 | * Created on: Aug 15,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef HTTP_CLIENT_H_ 9 | #define HTTP_CLIENT_H_ 10 | 11 | #include 12 | #include 13 | #include "Public_Define.h" 14 | 15 | class Http_Client 16 | { 17 | public: 18 | Http_Client(void); 19 | ~Http_Client(void); 20 | 21 | CURLcode post(const std::string & str_url, const std::string & str_post, std::string & str_response); 22 | CURLcode get(const std::string & str_url, std::string & str_response); 23 | std::string upload_byte_file(const std::string &str_url, void* data, int data_len); 24 | bool download_byte_file(const std::string &str_url, Data_Info* data_info); 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /lib/Lib_Log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Lib_Log.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | * 7 | */ 8 | 9 | #ifndef LIB_LOG_H_ 10 | #define LIB_LOG_H_ 11 | 12 | #include 13 | #include 14 | #include "Thread_Mutex.h" 15 | #include "Time_Value.h" 16 | #include "Public_Define.h" 17 | 18 | struct Lib_Log_File { 19 | Lib_Log_File(void): tv(Time_Value::zero), fp(0) { } 20 | 21 | Time_Value tv; 22 | std::string filepath; 23 | FILE *fp; 24 | }; 25 | 26 | class Lib_Log { 27 | public: 28 | typedef Thread_Mutex Lib_Log_File_Lock; 29 | 30 | static int msg_buf_size; 31 | static int backtrace_size; 32 | static std::string msg_head[]; 33 | static std::string lib_log_dir; 34 | 35 | static Lib_Log *instance(void); 36 | static void destroy(void); 37 | 38 | void log_trace(const char *fmt, ...); 39 | void log_debug(const char *fmt, ...); 40 | void log_info(const char *fmt, ...); 41 | void log_warn(const char *fmt, ...); 42 | void log_error(const char *fmt, ...); 43 | void log_fatal(const char *fmt, ...); 44 | 45 | void set_file_switcher(bool file_switcher); 46 | 47 | private: 48 | Lib_Log(void); 49 | virtual ~Lib_Log(void); 50 | 51 | void assembly_msg(int log_flag, const char *fmt, va_list ap); 52 | void logging_file(std::ostringstream &msg_stream); 53 | 54 | void make_lib_log_dir(void); 55 | void make_lib_log_filepath(std::string &path); 56 | 57 | private: 58 | static Lib_Log *instance_; 59 | bool file_switcher_; //是否写入文件 60 | Lib_Log_File_Lock log_lock_; 61 | Lib_Log_File log_file_; 62 | }; 63 | 64 | //////////////////////////////////////////////////////////////////////////////// 65 | 66 | inline void Lib_Log::set_file_switcher(bool file_switcher) { 67 | file_switcher_ = file_switcher; 68 | } 69 | 70 | //打印程序运行堆栈,跟踪记录数据信息 71 | #define LIB_LOG_TRACE(FMT, ...) do { \ 72 | Lib_Log::instance()->log_trace("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 73 | } while (0) 74 | 75 | //调试信息 76 | #define LIB_LOG_DEBUG(FMT, ...) do { \ 77 | Lib_Log::instance()->log_debug("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 78 | } while (0) 79 | 80 | //突出强调应用程序的运行过程 81 | #define LIB_LOG_INFO(FMT, ...) do { \ 82 | Lib_Log::instance()->log_info("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 83 | } while (0) 84 | 85 | //出现潜在错误的情形 86 | #define LIB_LOG_WARN(FMT, ...) do { \ 87 | Lib_Log::instance()->log_warn("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 88 | } while (0) 89 | 90 | //虽然发生错误事件,但仍然不影响系统的继续运行 91 | #define LIB_LOG_ERROR(FMT, ...) do { \ 92 | Lib_Log::instance()->log_error("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 93 | } while (0) 94 | 95 | //严重的错误事件,将会导致应用程序的退出 96 | #define LIB_LOG_FATAL(FMT, ...) do { \ 97 | Lib_Log::instance()->log_fatal("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 98 | } while (0) 99 | 100 | #endif /* LIB_LOG_H_ */ 101 | -------------------------------------------------------------------------------- /lib/List.h: -------------------------------------------------------------------------------- 1 | /* 2 | * List.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LIST_H_ 9 | #define LIST_H_ 10 | 11 | #include 12 | #include "Mutex_Guard.h" 13 | 14 | template 15 | class List { 16 | public: 17 | typedef std::list TList; 18 | typedef typename TList::const_iterator const_iterator; 19 | 20 | List(void); 21 | ~List(void); 22 | 23 | void push_back(const Obj &v); 24 | Obj &front(); 25 | Obj pop_front(); 26 | 27 | bool empty(); 28 | void clear(); 29 | size_t size() const; 30 | size_t size(); 31 | 32 | const_iterator begin() const; 33 | const_iterator end() const; 34 | 35 | const std::list &get_list(); 36 | const std::list &get_list() const; 37 | 38 | private: 39 | TList list_; 40 | size_t size_; 41 | LOCK lock_; 42 | }; 43 | 44 | //////////////////////////////////////////////////////////////////////////////// 45 | 46 | template 47 | List::List(void) : size_(0) { } 48 | 49 | template 50 | List::~List(void) { } 51 | 52 | template 53 | void List::push_back(const Obj &v) { 54 | GUARD(LOCK, mon, this->lock_); 55 | ++size_; 56 | list_.push_back(v); 57 | return ; 58 | } 59 | 60 | template 61 | Obj &List::front() { 62 | GUARD(LOCK, mon, this->lock_); 63 | return list_.front(); 64 | } 65 | 66 | template 67 | Obj List::pop_front() { 68 | GUARD(LOCK, mon, this->lock_); 69 | Obj val = list_.front(); 70 | --size_; 71 | list_.pop_front(); 72 | return val; 73 | } 74 | 75 | template 76 | bool List::empty() { 77 | GUARD(LOCK, mon, this->lock_); 78 | return list_.empty(); 79 | } 80 | 81 | template 82 | void List::clear() { 83 | GUARD(LOCK, mon, this->lock_); 84 | size_ = 0; 85 | list_.clear(); 86 | return ; 87 | } 88 | 89 | template 90 | size_t List::size() const { 91 | GUARD(LOCK, mon, this->lock_); 92 | return size_; 93 | } 94 | 95 | template 96 | size_t List::size() { 97 | GUARD(LOCK, mon, this->lock_); 98 | return size_; 99 | } 100 | 101 | template 102 | const std::list &List::get_list() { 103 | GUARD(LOCK, mon, this->lock_); 104 | return list_; 105 | } 106 | 107 | template 108 | const std::list &List::get_list() const { 109 | GUARD(LOCK, mon, this->lock_); 110 | return list_; 111 | } 112 | 113 | template 114 | typename List::const_iterator List::begin() const { 115 | return list_.begin(); 116 | } 117 | 118 | template 119 | typename List::const_iterator List::end() const { 120 | return list_.end(); 121 | } 122 | 123 | #endif /* LIST_H_ */ 124 | -------------------------------------------------------------------------------- /lib/Log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log.h 3 | * 4 | * Created on: Dec 25,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_H_ 9 | #define LOG_H_ 10 | 11 | #include 12 | #include 13 | #include "Public_Define.h" 14 | 15 | class Log { 16 | public: 17 | static int msg_buf_size; 18 | static int backtrace_size; 19 | static std::string msg_head[]; 20 | 21 | static Log *instance(void); 22 | 23 | void log_trace(const char *fmt, ...); 24 | void log_debug(const char *fmt, ...); 25 | void log_info(const char *fmt, ...); 26 | void log_warn(const char *fmt, ...); 27 | void log_error(const char *fmt, ...); 28 | void log_fatal(const char *fmt, ...); 29 | 30 | void set_log_type(int type, int sub_type = 0); 31 | void set_file_switcher(bool file_switcher); 32 | 33 | void assembly_msg(int log_flag, const char *fmt, va_list ap); 34 | void logging_file(std::ostringstream &msg_stream); 35 | int logging_db(Block_Buffer &buf); 36 | 37 | void free_cache(void); 38 | 39 | private: 40 | Log(void); 41 | virtual ~Log(void); 42 | 43 | private: 44 | static Log *instance_; 45 | int log_type_; //日志类型 46 | int log_sub_type_; //日志子类型 47 | bool file_switcher_; //是否写入文件 48 | }; 49 | 50 | //////////////////////////////////////////////////////////////////////////////// 51 | 52 | inline void Log::set_log_type(int type, int sub_type) { 53 | log_type_ = type; 54 | log_sub_type_ = sub_type; 55 | } 56 | 57 | inline void Log::set_file_switcher(bool file_switcher) { 58 | file_switcher_ = file_switcher; 59 | } 60 | 61 | //打印程序运行堆栈,跟踪记录数据信息 62 | #define LOG_TRACE(FMT, ...) do { \ 63 | Log::instance()->log_trace("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 64 | } while (0) 65 | 66 | //调试信息 67 | #define LOG_DEBUG(FMT, ...) do { \ 68 | Log::instance()->log_debug("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 69 | } while (0) 70 | 71 | //突出强调应用程序的运行过程 72 | #define LOG_INFO(FMT, ...) do { \ 73 | Log::instance()->log_info("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 74 | } while (0) 75 | 76 | //出现潜在错误的情形 77 | #define LOG_WARN(FMT, ...) do { \ 78 | Log::instance()->log_warn("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 79 | } while (0) 80 | 81 | //虽然发生错误事件,但仍然不影响系统的继续运行 82 | #define LOG_ERROR(FMT, ...) do { \ 83 | Log::instance()->log_error("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 84 | } while (0) 85 | 86 | //严重的错误事件,将会导致应用程序的退出 87 | #define LOG_FATAL(FMT, ...) do { \ 88 | Log::instance()->log_fatal("in %s:%d function %s: "#FMT, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 89 | } while (0) 90 | 91 | #endif /* LOG_H_ */ 92 | -------------------------------------------------------------------------------- /lib/Log_Connector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Connector.cpp 3 | * 4 | * Created on: Dec 30,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log_Connector.h" 9 | 10 | Log_Connector::Log_Connector(void) { } 11 | 12 | Log_Connector::~Log_Connector(void) { } 13 | 14 | Log_Connector *Log_Connector::instance_ = 0; 15 | 16 | Log_Connector *Log_Connector::instance(void) { 17 | if (! instance_) 18 | instance_ = new Log_Connector; 19 | return instance_; 20 | } 21 | 22 | int Log_Connector::push_data_block(Block_Buffer &buf, bool use_swap) { 23 | if (buf.readable_bytes() == 0) 24 | return -1; 25 | 26 | Block_Buffer *msg_buf = block_pool_.pop(); 27 | if (! msg_buf) 28 | return -1; 29 | 30 | msg_buf->reset(); 31 | 32 | if (use_swap) { 33 | msg_buf->swap(buf); 34 | } else { 35 | msg_buf->copy(&buf); 36 | } 37 | 38 | block_list_.push_back(msg_buf); 39 | 40 | return 0; 41 | } 42 | 43 | void Log_Connector::process_list(void) { 44 | Block_Buffer *buf = 0; 45 | while (1) { 46 | if (! block_list_.empty()) { 47 | buf = block_list_.front(); 48 | block_list_.pop_front(); 49 | send_block(get_cid(), *buf); 50 | block_pool_.push(buf); 51 | } else { 52 | //没有数据时候延迟 53 | Time_Value::sleep(Time_Value(0,100)); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /lib/Log_Connector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Connector.h 3 | * 4 | * Created on: Dec 30,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_CONNECTOR_H_ 9 | #define LOG_CONNECTOR_H_ 10 | 11 | #include "Connector.h" 12 | 13 | class Log_Connector: public Connector { 14 | public: 15 | typedef Object_Pool Block_Pool; 16 | 17 | static Log_Connector *instance(void); 18 | 19 | int push_data_block(Block_Buffer &buf, bool use_swap = true); 20 | virtual void process_list(void); 21 | 22 | private: 23 | Log_Connector(void); 24 | virtual ~Log_Connector(void); 25 | Log_Connector(const Log_Connector &); 26 | const Log_Connector &operator=(const Log_Connector &); 27 | 28 | private: 29 | static Log_Connector *instance_; 30 | Block_Pool block_pool_; 31 | }; 32 | 33 | #define LOG_CONNECTOR Log_Connector::instance() 34 | 35 | #endif /* LOG_CONNECTOR_H_ */ 36 | -------------------------------------------------------------------------------- /lib/Mutex_Guard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mutex_Guard.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MUTEX_GUARD_H_ 9 | #define MUTEX_GUARD_H_ 10 | 11 | #include "Thread_Mutex.h" 12 | 13 | template 14 | class Mutex_Guard { 15 | public: 16 | Mutex_Guard(LOCK &l); 17 | virtual ~Mutex_Guard(); 18 | 19 | int acquire(void); 20 | int release(void); 21 | 22 | int acquire_read(void); 23 | int acquire_write(void); 24 | 25 | private: 26 | LOCK *lock_; 27 | }; 28 | 29 | //////////////////////////////////////////////////////////////// 30 | 31 | template 32 | class Read_Guard { 33 | public: 34 | Read_Guard(LOCK &l); 35 | virtual ~Read_Guard(); 36 | 37 | int release(void); 38 | int acquire_read(void); 39 | 40 | private: 41 | LOCK *lock_; 42 | }; 43 | 44 | template 45 | Read_Guard::Read_Guard(LOCK &l) : lock_(&l) { 46 | acquire_read(); 47 | } 48 | 49 | template 50 | Read_Guard::~Read_Guard(void) { this->release(); } 51 | 52 | template 53 | int Read_Guard::release(void) { return lock_->release(); } 54 | 55 | template 56 | int Read_Guard::acquire_read(void) { return lock_->acquire_read(); } 57 | 58 | template 59 | class Write_Guard { 60 | public: 61 | Write_Guard(LOCK &l); 62 | virtual ~Write_Guard(); 63 | 64 | int release(void); 65 | int acquire_write(void); 66 | 67 | private: 68 | LOCK *lock_; 69 | }; 70 | 71 | template 72 | Write_Guard::Write_Guard(LOCK &l) : lock_(&l) { 73 | acquire_write(); 74 | } 75 | 76 | template 77 | Write_Guard::~Write_Guard(void) { this->release(); } 78 | 79 | template 80 | int Write_Guard::release(void) { return lock_->release(); } 81 | 82 | template 83 | int Write_Guard::acquire_write(void) { return lock_->acquire_write(); } 84 | 85 | //////////////////////////////////////////////////////////////// 86 | 87 | template 88 | Mutex_Guard::Mutex_Guard(LOCK &l) : lock_(&l) { 89 | //lock_ = &l; 90 | this->acquire(); 91 | } 92 | 93 | template 94 | Mutex_Guard::~Mutex_Guard(void) { 95 | this->release(); 96 | } 97 | 98 | template 99 | int Mutex_Guard::acquire(void) { 100 | return lock_->acquire(); 101 | } 102 | 103 | template 104 | int Mutex_Guard::release(void) { 105 | return lock_->release(); 106 | } 107 | 108 | template 109 | int Mutex_Guard::acquire_read(void) { 110 | return lock_->acquire_read(); 111 | } 112 | 113 | template 114 | int Mutex_Guard::acquire_write(void) { 115 | return lock_->acquire_write(); 116 | } 117 | 118 | //////////////////////////////////////////////////////////////// 119 | 120 | #define GUARD(MUTEX, OBJ, LOCK) Mutex_Guard< MUTEX > OBJ(LOCK); 121 | #define GUARD_READ(MUTEX, OBJ, LOCK) Read_Guard< MUTEX > OBJ(LOCK); 122 | #define GUARD_WRITE(MUTEX, OBJ, LOCK) Write_Guard< MUTEX > OBJ(LOCK); 123 | 124 | #define REACTOR_MUTEX MUTEX 125 | 126 | #endif /* MUTEX_GUARD_H_ */ 127 | -------------------------------------------------------------------------------- /lib/Mysql_Conn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mysql_Conn.h 3 | * 4 | * Created on: Jan 4,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LIB_MYSQL_CONN_H_ 9 | #define LIB_MYSQL_CONN_H_ 10 | 11 | #include "boost/unordered_map.hpp" 12 | #include "Thread_Mutex.h" 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | class DataBuf : public std::streambuf 24 | { 25 | public: 26 | DataBuf(char* buf, size_t len) { 27 | setg(buf, buf, buf+len); 28 | } 29 | }; 30 | 31 | class Mysql_Pool; 32 | class Mysql_Conn { 33 | public: 34 | Mysql_Conn(Mysql_Pool* mysql_pool); 35 | virtual ~Mysql_Conn(); 36 | 37 | int init(void); 38 | sql::PreparedStatement* create_pstmt(const char* str_sql); 39 | std::string& get_pool_name(); 40 | 41 | sql::ResultSet* execute_query(const char* str_sql); 42 | int execute_update(const char* str_sql); 43 | int execute(const char* str_sql); 44 | 45 | private: 46 | Mysql_Pool* mysql_pool_; 47 | sql::Connection* conn_; 48 | sql::Statement* stmt_ ; 49 | }; 50 | 51 | class Mysql_Pool { 52 | public: 53 | Mysql_Pool(std::string& pool_name, std::string& server_ip, uint32_t server_port, 54 | std::string& username, std::string& password, std::string& db_name, int32_t max_conn_cnt); 55 | virtual ~Mysql_Pool(); 56 | 57 | int init(void); 58 | Mysql_Conn* get_mysql_conn(); 59 | void rel_mysql_conn(Mysql_Conn* conn); 60 | 61 | inline std::string& get_pool_name() { return pool_name_; } 62 | inline std::string& get_server_ip() { return server_ip_; } 63 | inline int32_t get_server_port() { return server_port_; } 64 | inline std::string& get_username() { return username_; } 65 | inline std::string& get_passwrod() { return password_; } 66 | inline std::string& get_db_name() { return db_name_; } 67 | inline sql::Driver* get_driver(){return driver_;} 68 | 69 | private: 70 | std::string pool_name_; 71 | std::string server_ip_; 72 | int32_t server_port_; 73 | std::string username_; 74 | std::string password_; 75 | std::string db_name_; 76 | int32_t cur_conn_cnt_; 77 | int32_t max_conn_cnt_; 78 | 79 | sql::Driver* driver_; 80 | std::list free_list_; 81 | Thread_Notify thread_notify_; 82 | }; 83 | 84 | class Mysql_Manager { 85 | public: 86 | static Mysql_Manager* instance(); 87 | int init(std::string& server_ip, int server_port, std::string& username, std::string& password, std::string& db_name, std::string& pool_name, int max_conn_cnt); 88 | 89 | Mysql_Conn* get_mysql_conn(std::string& pool_name); 90 | void rel_mysql_conn(Mysql_Conn* conn); 91 | 92 | private: 93 | Mysql_Manager(); 94 | virtual ~Mysql_Manager(); 95 | 96 | private: 97 | static Mysql_Manager* instance_; 98 | boost::unordered_map mysql_pool_map_; 99 | }; 100 | 101 | #define MYSQL_MANAGER Mysql_Manager::instance() 102 | 103 | #endif /* LIB_MYSQL_CONN_H_ */ 104 | -------------------------------------------------------------------------------- /lib/Priority_Queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Priority_Queue.h 3 | * 4 | * Created on: Mar 22,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef PRIORITY_QUEUE_H_ 9 | #define PRIORITY_QUEUE_H_ 10 | 11 | #include "Mutex_Guard.h" 12 | 13 | //优先队列 14 | template 15 | class Priority_Queue { 16 | public: 17 | typedef std::vector Container; 18 | 19 | Obj top(){ 20 | GUARD(LOCK, mon, this->lock_); 21 | return container_.front(); 22 | } 23 | void push(Obj obj){ 24 | GUARD(LOCK, mon, this->lock_); 25 | container_.push_back(obj); 26 | std::push_heap(container_.begin(), container_.end(), Compare()); 27 | } 28 | void pop(){ 29 | GUARD(LOCK, mon, this->lock_); 30 | std::pop_heap(container_.begin(), container_.end(), Compare()); 31 | container_.pop_back(); 32 | } 33 | bool empty(){ 34 | GUARD(LOCK, mon, this->lock_); 35 | return container_.empty(); 36 | } 37 | private: 38 | Container container_; 39 | LOCK lock_; 40 | }; 41 | 42 | #endif /* PRIORITY_QUEUE_H_ */ 43 | -------------------------------------------------------------------------------- /lib/Public_Define.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Public_Define.cpp 3 | * 4 | * Created on: Aug 6,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Block_Buffer.h" 9 | #include "Public_Define.h" 10 | 11 | void Block_Group_Info::serialize(Block_Buffer &buf) { 12 | buf.write_int32(free_list_size_); 13 | buf.write_int32(used_list_size_); 14 | buf.write_int32(sum_bytes_); 15 | } 16 | 17 | void Block_Group_Info::deserialize(Block_Buffer &buf) { 18 | buf.read_int32(free_list_size_); 19 | buf.read_int32(used_list_size_); 20 | buf.read_int32(sum_bytes_); 21 | } 22 | 23 | void Block_Group_Info::reset(void) { 24 | free_list_size_ = 0; 25 | used_list_size_ = 0; 26 | sum_bytes_ = 0; 27 | } 28 | 29 | void Server_Info::serialize(Block_Buffer &buf) { 30 | buf.write_int32(svc_pool_free_list_size_); 31 | buf.write_int32(svc_pool_used_list_size_); 32 | buf.write_int32(svc_list_size_); 33 | 34 | uint16_t s = block_group_info_.size(); 35 | buf.write_uint16(s); 36 | for (uint16_t i = 0; i < s; ++i) { 37 | block_group_info_[i].serialize(buf); 38 | } 39 | } 40 | 41 | void Server_Info::deserialize(Block_Buffer &buf) { 42 | buf.read_int32(svc_pool_free_list_size_); 43 | buf.read_int32(svc_pool_used_list_size_); 44 | buf.read_int32(svc_list_size_); 45 | 46 | uint16_t count = 0; 47 | buf.read_uint16(count); 48 | Block_Group_Info info; 49 | for (uint16_t i = 0; i < count; ++i) { 50 | info.reset(); 51 | info.deserialize(buf); 52 | block_group_info_.push_back(info); 53 | } 54 | } 55 | 56 | void Server_Info::reset(void) { 57 | svc_pool_free_list_size_ = 0; 58 | svc_pool_used_list_size_ = 0; 59 | svc_list_size_ = 0; 60 | block_group_info_.clear(); 61 | } 62 | -------------------------------------------------------------------------------- /lib/Public_Define.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Public_Define.h 3 | * 4 | * Created on: Aug 6,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef PUBLIC_DEFINE_H_ 9 | #define PUBLIC_DEFINE_H_ 10 | 11 | #include 12 | #include "Time_Value.h" 13 | 14 | const static int SVC_MAX_LIST_SIZE = 1000; 15 | const static int SVC_MAX_PACK_SIZE = 60 * 1024; 16 | 17 | enum NetWork_Protocol { 18 | TCP = 0, 19 | UDP = 1, 20 | WEBSOCKET = 2, 21 | HTTP = 3, 22 | }; 23 | 24 | enum Event_Type { 25 | EVENT_INPUT = 0x1, 26 | EVENT_OUTPUT = 0x2, 27 | EVENT_TIMEOUT = 0x4, 28 | EVENT_ONCE_IO_IN = 0x8, /// 一次性IO输入事件 29 | EVENT_ONCE_IO_OUT = 0x10, /// 一次性IO输出事件 30 | EVENT_ONCE_TIMER = 0x20, /// 一次性定时器事件 31 | WITH_IO_HEARTBEAT = 0x40, /// IO附带心跳机制 32 | }; 33 | 34 | enum Log_Type { 35 | LOG_TRACE = 0, //打印程序运行堆栈,跟踪记录数据信息,与DEBUG相比更细致化的记录信息 36 | LOG_DEBUG = 1, //细粒度信息事件对调试应用程序是非常有帮助的 37 | LOG_INFO = 2, //消息在粗粒度级别上突出强调应用程序的运行过程 38 | LOG_WARN = 3, //会出现潜在错误的情形 39 | LOG_ERROR = 4, //虽然发生错误事件,但仍然不影响系统的继续运行 40 | LOG_FATAL = 5, //严重的错误事件,将会导致应用程序的退出 41 | }; 42 | 43 | enum Color { 44 | BLACK = 30, 45 | RED = 31, 46 | GREEN = 32, 47 | BROWN = 33, 48 | BLUE = 34, 49 | MAGENTA = 35, 50 | CYAN = 36, 51 | GREY = 37, 52 | LRED = 41, 53 | LGREEN = 42, 54 | YELLOW = 43, 55 | LBLUE = 44, 56 | LMAGENTA = 45, 57 | LCYAN = 46, 58 | WHITE = 47 59 | }; 60 | 61 | enum Websocket_Frame { 62 | FRAME_NORMAL = 0x0, 63 | FRAME_FINAL = 0x1, 64 | }; 65 | 66 | enum Websocket_Opcode { 67 | OPCODE_CONTINUATION = 0x0, 68 | OPCODE_TEXT = 0x1, 69 | OPCODE_BINARY = 0x2, 70 | OPCODE_CLOSE = 0x8, 71 | OPCODE_PING = 0x9, 72 | OPCODE_PONG = 0xa, 73 | }; 74 | 75 | struct Data_Info { 76 | int file_size; 77 | int data_len; 78 | char* data; 79 | } ; 80 | 81 | class Block_Buffer; 82 | struct Block_Group_Info { 83 | int free_list_size_; 84 | int used_list_size_; 85 | int sum_bytes_; 86 | 87 | void serialize(Block_Buffer &buf); 88 | void deserialize(Block_Buffer &buf); 89 | void reset(void); 90 | }; 91 | 92 | struct Server_Info { 93 | int svc_pool_free_list_size_; 94 | int svc_pool_used_list_size_; 95 | int svc_list_size_; 96 | std::vector block_group_info_; 97 | 98 | void serialize(Block_Buffer &buf); 99 | void deserialize(Block_Buffer &buf); 100 | void reset(void); 101 | }; 102 | 103 | #endif /* PUBLIC_DEFINE_H_ */ 104 | -------------------------------------------------------------------------------- /lib/Receive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Receive.cpp 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Receive.h" 9 | #include "Server.h" 10 | #include "Connector.h" 11 | 12 | Receive_Watcher::Receive_Watcher(Receive *r, int type, int timeout) 13 | : Epoll_Watcher(type, timeout), 14 | receiver_(r) 15 | { } 16 | 17 | Receive_Watcher::~Receive_Watcher(void) { } 18 | 19 | int Receive_Watcher::inner_cmd(void) { 20 | receiver_->process_drop(); 21 | return 0; 22 | } 23 | 24 | Receive::Receive(void): 25 | server_(0), 26 | connector_(0), 27 | heartbeat_timeout_(Time_Value::zero), 28 | reactor_(0) 29 | { } 30 | 31 | Receive::~Receive(void) { } 32 | 33 | int Receive::set(Server *server, Connector *connector, Time_Value *timeout) { 34 | server_ = server; 35 | connector_ = connector; 36 | if (timeout == 0) 37 | heartbeat_timeout_ = Time_Value::zero; 38 | else 39 | heartbeat_timeout_ = *timeout; 40 | return 0; 41 | } 42 | 43 | int Receive::init(void) { 44 | if (heartbeat_timeout_ == Time_Value::zero) { 45 | if ((reactor_ = new Receive_Watcher(this)) == 0) { 46 | LIB_LOG_FATAL("new Receiver_Watcher"); 47 | } 48 | } else { 49 | if ((reactor_ = new Receive_Watcher(this, WITH_IO_HEARTBEAT, heartbeat_timeout_.sec())) == 0) { 50 | LIB_LOG_FATAL("Receiver new Reactor"); 51 | } 52 | } 53 | return 0; 54 | } 55 | 56 | int Receive::fini(void) { 57 | if (reactor_) { 58 | delete reactor_; 59 | reactor_ = 0; 60 | } 61 | return 0; 62 | } 63 | 64 | void Receive::run_handler(void) { 65 | LIB_LOG_DEBUG("start receiver"); 66 | reactor_->loop(); 67 | } 68 | 69 | Epoll_Watcher *Receive::reactor(void) { 70 | return reactor_; 71 | } 72 | 73 | int Receive::push_drop(int cid) { 74 | drop_list_.push_back(cid); 75 | reactor_->notify(); 76 | return 0; 77 | } 78 | 79 | int Receive::process_drop(void) { 80 | int cid = 0; 81 | Svc *svc = 0; 82 | 83 | while (! drop_list_.empty()) { 84 | cid = drop_list_.pop_front(); 85 | if ((svc = find_svc(cid)) != 0) { 86 | svc->set_closed(true); 87 | if (svc->get_reg_recv()) { 88 | svc->unregister_recv_handler(); 89 | svc->set_reg_recv(false); 90 | } 91 | } 92 | drop_handler(cid); 93 | } 94 | 95 | return 0; 96 | } 97 | 98 | int Receive::register_svc(Svc *svc) { 99 | return reactor_->add(svc, EVENT_INPUT); 100 | } 101 | 102 | int Receive::unregister_svc(Svc *svc) { 103 | return reactor_->remove(svc); 104 | } 105 | 106 | int Receive::drop_handler(int cid) { 107 | LIB_LOG_TRACE("SHOULD NOT HERE"); 108 | return 0; 109 | } 110 | 111 | Svc *Receive::find_svc(int cid) { 112 | LIB_LOG_TRACE("SHOULD NOT HERE"); 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /lib/Receive.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Receive.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef RECEIVE_H_ 9 | #define RECEIVE_H_ 10 | 11 | #include "Thread.h" 12 | #include "Svc.h" 13 | #include "Epoll_Watcher.h" 14 | #include "List.h" 15 | 16 | class Receive; 17 | class Server; 18 | class Connector; 19 | class Receive_Watcher: public Epoll_Watcher { 20 | public: 21 | Receive_Watcher(Receive *r, int type = 0, int timeout = 0); 22 | virtual ~Receive_Watcher(void); 23 | 24 | virtual int inner_cmd(void); 25 | 26 | private: 27 | Receive *receiver_; 28 | }; 29 | 30 | class Receive: public Thread { 31 | public: 32 | typedef List Drop_List; 33 | 34 | Receive(void); 35 | virtual ~Receive(void); 36 | 37 | int set(Server *server, Connector *connector, Time_Value *timeout = 0); 38 | int init(void); 39 | int fini(void); 40 | 41 | virtual void run_handler(void); 42 | 43 | Epoll_Watcher *reactor(void); 44 | 45 | int push_drop(int cid); 46 | int process_drop(void); 47 | 48 | int register_svc(Svc *svc); 49 | int unregister_svc(Svc *svc); 50 | 51 | virtual int drop_handler(int cid); 52 | virtual Svc *find_svc(int cid); 53 | 54 | protected: 55 | Server *server_; 56 | Connector *connector_; 57 | 58 | private: 59 | Drop_List drop_list_; 60 | Time_Value heartbeat_timeout_; 61 | Receive_Watcher *reactor_; 62 | }; 63 | 64 | #endif /* RECEIVE_H_ */ 65 | -------------------------------------------------------------------------------- /lib/Send.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Send.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef SEND_H_ 9 | #define SEND_H_ 10 | 11 | #include "Thread.h" 12 | #include "Epoll_Watcher.h" 13 | #include "List.h" 14 | #include "Block_List.h" 15 | 16 | class Svc; 17 | class Server; 18 | class Connector; 19 | class Send: public Thread, public Event_Handler { 20 | typedef Thread_Mutex Svc_Map_Lock; 21 | typedef boost::unordered_map Svc_Map; 22 | typedef List Drop_List; 23 | 24 | public: 25 | Send(void); 26 | virtual ~Send(void); 27 | 28 | void set(Server *server, Connector *connector, Time_Value &interval); 29 | int init(void); 30 | int fini(void); 31 | 32 | virtual void run_handler(void); 33 | 34 | virtual Block_Buffer *pop_block(int cid); 35 | virtual int push_block(int cid, Block_Buffer *buffer); 36 | 37 | //将要发送的数据包放到svc的send_block_list里面 38 | int push_svc_block(int cid, Block_Buffer &buffer); 39 | 40 | int push_drop(int cid); 41 | int process_drop(void); 42 | 43 | int register_svc(Svc *svc); 44 | int unregister_svc(Svc *svc); 45 | 46 | int register_self_timer(void); 47 | 48 | virtual int drop_handler(int cid); 49 | virtual Svc *find_svc(int cid); 50 | 51 | Epoll_Watcher *reactor(void); 52 | 53 | Time_Value &get_interval(void); 54 | void set_interval(Time_Value &tv); 55 | 56 | virtual int handle_timeout(const Time_Value &tv); 57 | 58 | protected: 59 | Server *server_; 60 | Connector *connector_; 61 | 62 | private: 63 | Epoll_Watcher *reactor_; 64 | Drop_List drop_list_; 65 | 66 | Svc_Map_Lock svc_map_lock_; 67 | Svc_Map svc_map_; 68 | 69 | Time_Value interval_; 70 | bool is_register_self_; 71 | }; 72 | 73 | #endif /* SEND_H_ */ 74 | -------------------------------------------------------------------------------- /lib/Sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Sha1.h 3 | * 4 | * Copyright (C) 1998, 2009 5 | * Paul E. Jones 6 | * All Rights Reserved. 7 | * 8 | ***************************************************************************** 9 | * $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $ 10 | ***************************************************************************** 11 | * 12 | * Description: 13 | * This class implements the Secure Hashing Standard as defined 14 | * in FIPS PUB 180-1 published April 17, 1995. 15 | * 16 | * Many of the variable names in this class, especially the single 17 | * character names, were used because those were the names used 18 | * in the publication. 19 | * 20 | * Please read the file sha1.cpp for more information. 21 | */ 22 | 23 | #ifndef _SHA1_H_ 24 | #define _SHA1_H_ 25 | 26 | class SHA1 27 | { 28 | public: 29 | SHA1(); 30 | virtual ~SHA1(); 31 | 32 | /* 33 | * Re-initialize the class 34 | */ 35 | void Reset(); 36 | 37 | /* 38 | * Returns the message digest 39 | */ 40 | bool Result(unsigned *message_digest_array); 41 | 42 | /* 43 | * Provide input to SHA1 44 | */ 45 | void Input( const unsigned char *message_array, 46 | unsigned length); 47 | void Input( const char *message_array, 48 | unsigned length); 49 | void Input(unsigned char message_element); 50 | void Input(char message_element); 51 | SHA1& operator<<(const char *message_array); 52 | SHA1& operator<<(const unsigned char *message_array); 53 | SHA1& operator<<(const char message_element); 54 | SHA1& operator<<(const unsigned char message_element); 55 | 56 | private: 57 | /* 58 | * Process the next 512 bits of the message 59 | */ 60 | void ProcessMessageBlock(); 61 | 62 | /* 63 | * Pads the current message block to 512 bits 64 | */ 65 | void PadMessage(); 66 | 67 | /* 68 | * Performs a circular left shift operation 69 | */ 70 | inline unsigned CircularShift(int bits, unsigned word); 71 | 72 | unsigned H[5]; // Message digest buffers 73 | 74 | unsigned Length_Low; // Message length in bits 75 | unsigned Length_High; // Message length in bits 76 | 77 | unsigned char Message_Block[64]; // 512-bit message blocks 78 | int Message_Block_Index; // Index into message block array 79 | 80 | bool Computed; // Is the digest computed? 81 | bool Corrupted; // Is the message digest corruped? 82 | }; 83 | 84 | #endif // _SHA1_H_ 85 | -------------------------------------------------------------------------------- /lib/Svc_Http.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Svc_Http.cpp 3 | * 4 | * Created on: Aug 16,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include 9 | #include 10 | #include "Svc_Http.h" 11 | #include "Http_Parser_Wrap.h" 12 | 13 | Svc_Http::Svc_Http(): Svc_Handler() {} 14 | 15 | Svc_Http::~Svc_Http() {} 16 | 17 | int Svc_Http::handle_send(void) { 18 | if (parent_->is_closed()) 19 | return 0; 20 | 21 | int cid = parent_->get_cid(); 22 | Block_Buffer *front_buf = 0; 23 | while (!send_block_list_.empty()) { 24 | front_buf = send_block_list_.front(); 25 | 26 | //构建http消息头 27 | make_http_head(front_buf); 28 | size_t sum_bytes = front_buf->readable_bytes(); 29 | int ret = ::write(parent_->get_fd(), front_buf->get_read_ptr(), sum_bytes); 30 | if (ret == -1) { 31 | if (errno == EINTR) { //被打断, 重写 32 | continue; 33 | } else if (errno == EWOULDBLOCK) { //EAGAIN,下一次超时再写 34 | break; 35 | } else { //其他错误,丢掉该客户端全部数据 36 | LIB_LOG_ERROR("writev error, cid:%d ip:%s port:%d errno:%d", cid, parent_->get_peer_ip().c_str(), parent_->get_peer_port(), errno); 37 | parent_->handle_close(); 38 | return ret; 39 | } 40 | } else { 41 | if ((size_t)ret == sum_bytes) { //本次全部写完, 尝试继续写 42 | parent_->push_block(cid, front_buf); 43 | send_block_list_.pop_front(); 44 | continue; 45 | } else { //未写完, 丢掉已发送的数据, 下一次超时再写,front_buf没有发送成功,留到下一次继续发送 46 | return ret; 47 | } 48 | } 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | int Svc_Http::handle_pack(Block_Vector &block_vec) { 55 | int32_t cid = 0; 56 | Block_Buffer *front_buf = 0; 57 | 58 | while (! recv_block_list_.empty()) { 59 | front_buf = recv_block_list_.front(); 60 | if (! front_buf) { 61 | LIB_LOG_ERROR("front_buf == 0"); 62 | continue; 63 | } 64 | 65 | front_buf->read_int32(cid); 66 | if (front_buf->readable_bytes() <= 0) { /// 数据块异常, 关闭该连接 67 | LIB_LOG_ERROR("cid:%d fd:%d, data block read bytes<0", cid, parent_->get_fd()); 68 | recv_block_list_.pop_front(); 69 | front_buf->reset(); 70 | parent_->push_block(parent_->get_cid(), front_buf); 71 | parent_->handle_close(); 72 | return -1; 73 | } 74 | 75 | Http_Parser_Wrap http_parser; 76 | http_parser.parse_http_content(front_buf->get_read_ptr(), front_buf->readable_bytes()); 77 | 78 | Block_Buffer *data_buf = parent_->pop_block(cid); 79 | data_buf->reset(); 80 | data_buf->write_int32(cid); 81 | data_buf->write_string(http_parser.get_body_content()); 82 | block_vec.push_back(data_buf); 83 | recv_block_list_.pop_front(); 84 | } 85 | 86 | return 0; 87 | } 88 | 89 | void Svc_Http::make_http_head(Block_Buffer *buf) { 90 | std::string str_content = ""; 91 | buf->read_string(str_content); 92 | int content_len = str_content.length(); 93 | char str_http[1024]; 94 | snprintf(str_http, 1024, HTTP_RESPONSE_HTML, content_len, str_content.c_str()); 95 | buf->write_string(str_http); 96 | LIB_LOG_INFO("http msg:%s%s", "\r\n", str_http); 97 | } 98 | -------------------------------------------------------------------------------- /lib/Svc_Http.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Svc_Http.h 3 | * 4 | * Created on: Aug 16,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef SVC_HTTP_H_ 9 | #define SVC_HTTP_H_ 10 | 11 | #include "Svc.h" 12 | 13 | #define HTTP_RESPONSE_HTML "HTTP/1.1 200 OK\r\n"\ 14 | "Connection:close\r\n"\ 15 | "Content-Length:%d\r\n"\ 16 | "Content-Type:text/html;charset=utf-8\r\n\r\n%s" 17 | 18 | class Svc_Http : public Svc_Handler { 19 | public: 20 | Svc_Http(void); 21 | virtual ~Svc_Http(void); 22 | 23 | virtual int handle_send(void); 24 | virtual int handle_pack(Block_Vector &block_vec); 25 | 26 | private: 27 | void make_http_head(Block_Buffer *buf); 28 | }; 29 | 30 | #endif /* SVC_HTTP_H_ */ 31 | -------------------------------------------------------------------------------- /lib/Svc_Tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Svc_Tcp.h 3 | * 4 | * Created on: Apr 19,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef SVC_TCP_H_ 9 | #define SVC_TCP_H_ 10 | 11 | #include "Svc.h" 12 | 13 | class Svc_Tcp : public Svc_Handler { 14 | public: 15 | Svc_Tcp(void); 16 | virtual ~Svc_Tcp(void); 17 | 18 | virtual int handle_send(void); 19 | virtual int handle_pack(Block_Vector &block_vec); 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /lib/Svc_WebSocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Svc_Websocket.h 3 | * 4 | * Created on: Apr 19,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef SVC_WEBSOCKET_H_ 9 | #define SVC_WEBSOCKET_H_ 10 | 11 | #include "Svc.h" 12 | 13 | #define MAGIC_KEY "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" 14 | 15 | class Svc_Websocket : public Svc_Handler { 16 | public: 17 | Svc_Websocket(void); 18 | virtual ~Svc_Websocket(void); 19 | 20 | void reset(void); 21 | virtual int handle_send(void); 22 | virtual int handle_pack(Block_Vector &block_vec); 23 | 24 | private: 25 | //websocket第一步握手 26 | int handshake(Block_Buffer *buffer); 27 | //解析websocket帧头,获取buffer内容 28 | Block_Buffer *get_frame_buffer(int16_t payload_length, uint8_t *masking_key, Block_Buffer *buffer); 29 | //包装websocket帧头 30 | Block_Buffer *make_frame_buffer(Block_Buffer *buffer, uint8_t *op = NULL); 31 | 32 | private: 33 | bool connected_; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /lib/Thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Thread.h 3 | * 4 | * Created on: Dec 16,2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef THREAD_H_ 9 | #define THREAD_H_ 10 | 11 | #include 12 | #include "Lib_Log.h" 13 | 14 | class Thread { 15 | public: 16 | Thread(void) : tid(0) { } 17 | 18 | virtual ~Thread(void) { } 19 | 20 | static void *create_func(void *arg) { 21 | Thread *self = (Thread *)arg; 22 | pthread_cleanup_push(cleanup_func, arg); 23 | self->run_handler(); 24 | pthread_cleanup_pop(1); 25 | return self; 26 | } 27 | 28 | static void cleanup_func(void *arg) { 29 | Thread *self = (Thread *)arg; 30 | self->exit_handler(); 31 | return ; 32 | } 33 | 34 | virtual void run_handler(void) { 35 | LIB_LOG_TRACE("SHOULD NOT HERE"); 36 | } 37 | 38 | virtual void exit_handler(void) { } 39 | 40 | int thr_create(void) { 41 | return ::pthread_create(&tid, NULL, create_func, this); 42 | } 43 | 44 | int thr_cancel(void) { 45 | return ::pthread_cancel(tid); 46 | } 47 | 48 | int thr_join(void) { 49 | return ::pthread_join(tid, NULL); 50 | } 51 | 52 | int thr_cancel_join(void) { 53 | int ret; 54 | if ((ret = thr_cancel()) == 0) { 55 | ret = thr_join(); 56 | } else { 57 | LIB_LOG_TRACE("thr_cancel return ret = ", ret); 58 | } 59 | return ret; 60 | } 61 | 62 | void thr_exit(void *rval_ptr) { 63 | ::pthread_exit(rval_ptr); 64 | } 65 | 66 | pthread_t thread_id() { 67 | return tid; 68 | } 69 | 70 | 71 | private: 72 | pthread_t tid; 73 | }; 74 | 75 | #endif /* THREAD_H_ */ 76 | -------------------------------------------------------------------------------- /lib/Xml.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Xml.h 3 | * 4 | * Created on: Jul 15,2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef XML_H_ 9 | #define XML_H_ 10 | 11 | #include "tinyxml.h" 12 | #include 13 | 14 | #define XML_LOOP_BEGIN(NODE)\ 15 | do { \ 16 | if(NODE->Type() != TiXmlNode::TINYXML_ELEMENT) \ 17 | continue; 18 | 19 | #define XML_LOOP_END(NODE)\ 20 | } while ((NODE = NODE->NextSibling())); 21 | 22 | class Xml { 23 | public: 24 | Xml(); 25 | Xml(const char *path); 26 | ~Xml(); 27 | void load_xml(const char *path); 28 | 29 | TiXmlNode *get_root_node(const char *key = ""); 30 | TiXmlNode *enter_node(TiXmlNode *node, const char *key); 31 | 32 | std::string get_key(TiXmlNode *node); 33 | bool has_key(TiXmlNode *node, const char *key); 34 | 35 | std::string get_val_str(TiXmlNode *node); 36 | int get_val_int(TiXmlNode *node); 37 | float get_val_float(TiXmlNode *node); 38 | 39 | std::string get_attr_str(TiXmlNode* node, const char *key = NULL); 40 | int get_attr_int(TiXmlNode* node, const char *key = NULL); 41 | float get_attr_float(TiXmlNode* node, const char *key = NULL); 42 | 43 | private: 44 | TiXmlElement *rootElement_; 45 | TiXmlDocument *doc_; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /lib/tinystr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any 6 | damages arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any 9 | purpose, including commercial applications, and to alter it and 10 | redistribute it freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must 13 | not claim that you wrote the original software. If you use this 14 | software in a product, an acknowledgment in the product documentation 15 | would be appreciated but is not required. 16 | 17 | 2. Altered source versions must be plainly marked as such, and 18 | must not be misrepresented as being the original software. 19 | 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | */ 23 | 24 | 25 | #ifndef TIXML_USE_STL 26 | 27 | #include "tinystr.h" 28 | 29 | // Error value for find primitive 30 | const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1); 31 | 32 | 33 | // Null rep. 34 | TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, { '\0' } }; 35 | 36 | 37 | void TiXmlString::reserve (size_type cap) 38 | { 39 | if (cap > capacity()) 40 | { 41 | TiXmlString tmp; 42 | tmp.init(length(), cap); 43 | memcpy(tmp.start(), data(), length()); 44 | swap(tmp); 45 | } 46 | } 47 | 48 | 49 | TiXmlString& TiXmlString::assign(const char* str, size_type len) 50 | { 51 | size_type cap = capacity(); 52 | if (len > cap || cap > 3*(len + 8)) 53 | { 54 | TiXmlString tmp; 55 | tmp.init(len); 56 | memcpy(tmp.start(), str, len); 57 | swap(tmp); 58 | } 59 | else 60 | { 61 | memmove(start(), str, len); 62 | set_size(len); 63 | } 64 | return *this; 65 | } 66 | 67 | 68 | TiXmlString& TiXmlString::append(const char* str, size_type len) 69 | { 70 | size_type newsize = length() + len; 71 | if (newsize > capacity()) 72 | { 73 | reserve (newsize + capacity()); 74 | } 75 | memmove(finish(), str, len); 76 | set_size(newsize); 77 | return *this; 78 | } 79 | 80 | 81 | TiXmlString operator + (const TiXmlString & a, const TiXmlString & b) 82 | { 83 | TiXmlString tmp; 84 | tmp.reserve(a.length() + b.length()); 85 | tmp += a; 86 | tmp += b; 87 | return tmp; 88 | } 89 | 90 | TiXmlString operator + (const TiXmlString & a, const char* b) 91 | { 92 | TiXmlString tmp; 93 | TiXmlString::size_type b_len = static_cast( strlen(b) ); 94 | tmp.reserve(a.length() + b_len); 95 | tmp += a; 96 | tmp.append(b, b_len); 97 | return tmp; 98 | } 99 | 100 | TiXmlString operator + (const char* a, const TiXmlString & b) 101 | { 102 | TiXmlString tmp; 103 | TiXmlString::size_type a_len = static_cast( strlen(a) ); 104 | tmp.reserve(a_len + b.length()); 105 | tmp.append(a, a_len); 106 | tmp += b; 107 | return tmp; 108 | } 109 | 110 | 111 | #endif // TIXML_USE_STL 112 | -------------------------------------------------------------------------------- /lib/tinyxmlerror.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com) 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any 7 | damages arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any 10 | purpose, including commercial applications, and to alter it and 11 | redistribute it freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must 14 | not claim that you wrote the original software. If you use this 15 | software in a product, an acknowledgment in the product documentation 16 | would be appreciated but is not required. 17 | 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | */ 24 | 25 | #include "tinyxml.h" 26 | 27 | // The goal of the seperate error file is to make the first 28 | // step towards localization. tinyxml (currently) only supports 29 | // english error messages, but the could now be translated. 30 | // 31 | // It also cleans up the code a bit. 32 | // 33 | 34 | const char* TiXmlBase::errorString[ TiXmlBase::TIXML_ERROR_STRING_COUNT ] = 35 | { 36 | "No error", 37 | "Error", 38 | "Failed to open file", 39 | "Error parsing Element.", 40 | "Failed to read Element name", 41 | "Error reading Element value.", 42 | "Error reading Attributes.", 43 | "Error: empty tag.", 44 | "Error reading end tag.", 45 | "Error parsing Unknown.", 46 | "Error parsing Comment.", 47 | "Error parsing Declaration.", 48 | "Error document empty.", 49 | "Error null (0) or unexpected EOF found in input stream.", 50 | "Error parsing CDATA.", 51 | "Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.", 52 | }; 53 | -------------------------------------------------------------------------------- /log_server/Log_DB.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_DB.cpp 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Common_Func.h" 9 | #include "Log.h" 10 | #include "Server_Config.h" 11 | #include "Log_DB.h" 12 | 13 | Log_DB::Log_DB(void) : 14 | mysql_conn_(nullptr), 15 | db_struct_id_map_(get_hash_table_size(512)), 16 | db_struct_name_map_(get_hash_table_size(512)) { } 17 | 18 | Log_DB::~Log_DB(void) { 19 | MYSQL_MANAGER->rel_mysql_conn(mysql_conn_); 20 | } 21 | 22 | Log_DB *Log_DB::instance_; 23 | 24 | Log_DB *Log_DB::instance(void) { 25 | if (instance_ == 0) 26 | instance_ = new Log_DB; 27 | return instance_; 28 | } 29 | 30 | int Log_DB::init(void) { 31 | const Json::Value &server_misc = SERVER_CONFIG->server_misc(); 32 | if (server_misc == Json::Value::null) { 33 | LOG_FATAL("db init, server_misc null"); 34 | return -1; 35 | } 36 | 37 | //连接mysql 38 | std::string ip(server_misc["mysql_log"]["ip"].asString()); 39 | int port = server_misc["mysql_log"]["port"].asInt(); 40 | std::string user(server_misc["mysql_log"]["user"].asString()); 41 | std::string password(server_misc["mysql_log"]["password"].asString()); 42 | std::string dbname(server_misc["mysql_log"]["dbname"].asString()); 43 | std::string dbpoolname(server_misc["mysql_log"]["dbpoolname"].asString()); 44 | MYSQL_MANAGER->init(ip, port, user, password, dbname, dbpoolname, 16); 45 | mysql_conn_ = MYSQL_MANAGER->get_mysql_conn(dbpoolname); 46 | 47 | load_struct(server_misc["log_db_struct_path"].asString().c_str(), LOG_STRUCT, db_struct_id_map_, db_struct_name_map_); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /log_server/Log_DB.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_DB.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_DB_H_ 9 | #define LOG_DB_H_ 10 | 11 | #include "Mysql_Conn.h" 12 | #include "Public_Struct.h" 13 | 14 | class Log_DB { 15 | public: 16 | static Log_DB *instance(void); 17 | Mysql_Conn *connection(void) { return mysql_conn_; } 18 | 19 | int init(void); 20 | 21 | inline Struct_Id_Map& db_struct_id_map() { return db_struct_id_map_; } 22 | inline Struct_Name_Map& db_struct_name_map() { return db_struct_name_map_; } 23 | 24 | private: 25 | Log_DB(void); 26 | virtual ~Log_DB(void); 27 | Log_DB(const Log_DB &); 28 | const Log_DB &operator=(const Log_DB &); 29 | 30 | private: 31 | static Log_DB *instance_; 32 | Mysql_Conn* mysql_conn_; 33 | 34 | Struct_Id_Map db_struct_id_map_; 35 | Struct_Name_Map db_struct_name_map_; 36 | }; 37 | 38 | #define LOG_DB Log_DB::instance() 39 | 40 | #endif /* LOG_DB_H_ */ 41 | -------------------------------------------------------------------------------- /log_server/Log_File.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_File.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_FILE_H_ 9 | #define LOG_FILE_H_ 10 | 11 | #include "Time_Value.h" 12 | #include "boost/unordered_map.hpp" 13 | #include "Public_Struct.h" 14 | 15 | struct File_Info { 16 | int type; 17 | int sub_type; 18 | Time_Value tv; 19 | std::string file_path; 20 | FILE *fp; 21 | }; 22 | 23 | class Log_File { 24 | public: 25 | typedef boost::unordered_map File_Info_Map; 26 | 27 | Log_File(void); 28 | virtual ~Log_File(void); 29 | 30 | int process_log_file_block(Block_Buffer &buf); 31 | 32 | private: 33 | int get_key_val(int type, int sub_type); 34 | 35 | void make_log_dir(void); 36 | int make_log_filepath(File_Info &file_info); 37 | int logging_file(int log_type, int log_sub_type, std::string &log_str); 38 | 39 | private: 40 | File_Info_Map file_info_map_; 41 | }; 42 | 43 | #endif /* LOG_FILE_H_ */ 44 | -------------------------------------------------------------------------------- /log_server/Log_Manager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Manager.cpp 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log_Timer.h" 9 | #include "Log_Manager.h" 10 | #include "Log_Server.h" 11 | #include "Log_DB.h" 12 | 13 | Log_Manager::Log_Manager(void) { } 14 | 15 | Log_Manager::~Log_Manager(void) { } 16 | 17 | Log_Manager *Log_Manager::instance_; 18 | 19 | Log_Manager *Log_Manager::instance(void) { 20 | if (! instance_) 21 | instance_ = new Log_Manager; 22 | return instance_; 23 | } 24 | 25 | int Log_Manager::init(int server_id) { 26 | LOG_DB->init(); 27 | LOG_TIMER->thr_create(); 28 | 29 | return 0; 30 | } 31 | 32 | void Log_Manager::run_handler(void) { 33 | process_list(); 34 | return ; 35 | } 36 | 37 | int Log_Manager::push_data_block(Block_Buffer *buf) { 38 | if (! buf) 39 | return -1; 40 | 41 | block_list_.push_back(buf); 42 | return 0; 43 | } 44 | 45 | int Log_Manager::push_tick(int x) { 46 | tick_list_.push_back(x); 47 | return 0; 48 | } 49 | 50 | int Log_Manager::process_list(void) { 51 | int32_t cid = 0; 52 | Block_Buffer *buf = 0; 53 | 54 | while (1) { 55 | bool all_empty = true; 56 | if ((buf = block_list_.pop_front()) != 0) { 57 | all_empty = false; 58 | if (buf->is_legal()) { 59 | buf->peek_int32(cid); 60 | process_block(*buf); 61 | } else { 62 | buf->reset(); 63 | } 64 | LOG_SERVER->push_block(cid, buf); 65 | } 66 | if (! tick_list_.empty()) { 67 | all_empty = false; 68 | tick_list_.pop_front(); 69 | tick(); 70 | } 71 | if (all_empty) { 72 | Time_Value::sleep(Time_Value(0,100)); 73 | } 74 | } 75 | 76 | return 0; 77 | } 78 | 79 | int Log_Manager::process_block(Block_Buffer &buf) { 80 | int32_t cid = 0; 81 | int16_t len = 0; 82 | int32_t msg_id = 0; 83 | int32_t status = 0; 84 | buf.read_int32(cid); 85 | buf.read_int16(len); 86 | buf.read_int32(msg_id); 87 | buf.read_int32(status); 88 | 89 | switch (msg_id) { 90 | case 999999: { 91 | log_file_.process_log_file_block(buf); 92 | break; 93 | } 94 | default: { 95 | Struct_Id_Map::iterator iter = LOG_DB->db_struct_id_map().find(msg_id); 96 | if(iter != LOG_DB->db_struct_id_map().end()){ 97 | iter->second->save_data(buf); 98 | } 99 | break; 100 | } 101 | } 102 | return 0; 103 | } 104 | 105 | int Log_Manager::tick(void) { 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /log_server/Log_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Manager.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_MANAGER_H_ 9 | #define LOG_MANAGER_H_ 10 | 11 | #include "Log_File.h" 12 | #include "Thread.h" 13 | #include "List.h" 14 | #include "Block_List.h" 15 | 16 | class Log_Manager: public Thread { 17 | public: 18 | typedef Block_List Data_List; 19 | typedef List Int_List; 20 | 21 | static Log_Manager *instance(void); 22 | int init(int server_id); 23 | void run_handler(void); 24 | 25 | int push_data_block(Block_Buffer *buf); 26 | int push_tick(int x); 27 | 28 | int process_list(void); 29 | int process_block(Block_Buffer &buf); 30 | 31 | int tick(void); 32 | 33 | private: 34 | Log_Manager(void); 35 | virtual ~Log_Manager(void); 36 | 37 | private: 38 | static Log_Manager *instance_; 39 | Data_List block_list_; 40 | Int_List tick_list_; 41 | 42 | Log_File log_file_; 43 | }; 44 | 45 | #define LOG_MANAGER Log_Manager::instance() 46 | 47 | #endif /* LOG_MANAGER_H_ */ 48 | -------------------------------------------------------------------------------- /log_server/Log_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Server.cpp 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log_Server.h" 9 | #include "Log_Manager.h" 10 | 11 | Log_Server::Log_Server(void) { } 12 | 13 | Log_Server::~Log_Server(void) { } 14 | 15 | Log_Server *Log_Server::instance_ = 0; 16 | 17 | Log_Server *Log_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new Log_Server; 20 | return instance_; 21 | } 22 | 23 | void Log_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | if (!block_list_.empty()) { 27 | buf = block_list_.pop_front(); 28 | LOG_MANAGER->push_data_block(buf); 29 | } else { 30 | //没有数据时候延迟 31 | Time_Value::sleep(Time_Value(0,100)); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /log_server/Log_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Server.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_SERVER_H_ 9 | #define LOG_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class Log_Server: public Server { 14 | public: 15 | static Log_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Log_Server(void); 21 | virtual ~Log_Server(void); 22 | Log_Server(const Log_Server &); 23 | const Log_Server &operator=(const Log_Server &); 24 | 25 | private: 26 | static Log_Server *instance_; 27 | }; 28 | 29 | #define LOG_SERVER Log_Server::instance() 30 | 31 | #endif /* LOG_SERVER_H_ */ 32 | -------------------------------------------------------------------------------- /log_server/Log_Struct.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Struct.cpp 3 | * 4 | * Created on: Jul 28, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log.h" 9 | #include "Log_DB.h" 10 | #include "Log_Struct.h" 11 | 12 | Log_Struct::Log_Struct(Xml &xml, TiXmlNode *node) : Base_Struct(xml, node) {} 13 | 14 | Log_Struct::~Log_Struct() {} 15 | 16 | void Log_Struct::save_data(Block_Buffer &buffer) { 17 | std::string str_name; 18 | std::string str_value; 19 | int64_t key_index = 0; 20 | buffer.peek_int64(key_index); 21 | for(std::vector::const_iterator iter = field_vec().begin(); 22 | iter != field_vec().end(); iter++) { 23 | if(iter->field_label == "arg") { 24 | build_sql_arg(*iter, buffer, str_name, str_value); 25 | } 26 | else { 27 | LOG_ERROR("Can not find the field_label: %s", iter->field_label.c_str()); 28 | return; 29 | } 30 | } 31 | 32 | str_name = str_name.substr(0, str_name.length()-1); 33 | str_value = str_value.substr(0, str_value.length()-1); 34 | char str_sql[512] = {0}; 35 | sprintf(str_sql, "INSERT INTO %s (%s) VALUES (%s)", table_name().c_str(), str_name.c_str(), str_value.c_str()); 36 | LOG_DB->connection()->execute(str_sql); 37 | 38 | LOG_INFO("table %s save key_index:%ld", table_name().c_str(), key_index); 39 | } 40 | 41 | void Log_Struct::build_sql_arg(const Field_Info &field_info, Block_Buffer &buffer, std::string &str_name, std::string &str_value) { 42 | std::stringstream stream; 43 | stream.str(""); 44 | stream << field_info.field_name << "," ; 45 | str_name += stream.str(); 46 | 47 | stream.str(""); 48 | if(field_info.field_type == "int8") { 49 | int8_t value = 0; 50 | buffer.read_int8(value); 51 | stream << value << "," ; 52 | } 53 | else if(field_info.field_type == "int16") { 54 | int16_t value = 0; 55 | buffer.read_int16(value); 56 | stream << value << "," ; 57 | } 58 | else if(field_info.field_type == "int32") { 59 | int32_t value = 0; 60 | buffer.read_int32(value); 61 | stream << value << "," ; 62 | } 63 | else if(field_info.field_type == "int64") { 64 | int64_t value = 0; 65 | buffer.read_int64(value); 66 | stream << value << "," ; 67 | } 68 | else if(field_info.field_type == "double") { 69 | double value = 0; 70 | buffer.read_double(value); 71 | stream << value << "," ; 72 | } 73 | else if(field_info.field_type == "bool") { 74 | bool value = false; 75 | buffer.read_bool(value); 76 | stream << value << "," ; 77 | } 78 | else if(field_info.field_type == "string") { 79 | std::string value = ""; 80 | buffer.read_string(value); 81 | stream << "\'" << value << "\'" << "," ; 82 | } 83 | else { 84 | LOG_ERROR("Can not find the field_type:%s, struct_name:%s", field_info.field_type.c_str(), struct_name().c_str()); 85 | } 86 | str_value += stream.str(); 87 | } 88 | -------------------------------------------------------------------------------- /log_server/Log_Struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Struct.h 3 | * 4 | * Created on: Jul 28, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_STRUCT_H_ 9 | #define LOG_STRUCT_H_ 10 | 11 | #include "Base_Struct.h" 12 | 13 | class Log_Struct: public Base_Struct { 14 | public: 15 | Log_Struct(Xml &xml, TiXmlNode *node); 16 | virtual ~Log_Struct(); 17 | 18 | virtual void save_data(Block_Buffer &buffer); 19 | 20 | private: 21 | void build_sql_arg(const Field_Info &field_info, Block_Buffer &buffer, std::string &str_name, std::string &str_value); 22 | }; 23 | 24 | #endif /* LOG_SERVER_LOG_STRUCT_H_ */ 25 | -------------------------------------------------------------------------------- /log_server/Log_Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Timer.cpp 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log_Timer.h" 9 | #include "Log_Manager.h" 10 | 11 | int Log_Timer_Handler::handle_timeout(const Time_Value &tv) { 12 | LOG_MANAGER->push_tick(1); 13 | return 0; 14 | } 15 | 16 | Log_Timer::Log_Timer(void) { } 17 | 18 | Log_Timer::~Log_Timer(void) { } 19 | 20 | Log_Timer *Log_Timer::instance_ = 0; 21 | 22 | Log_Timer *Log_Timer::instance(void) { 23 | if (! instance_) 24 | instance_ = new Log_Timer; 25 | return instance_; 26 | } 27 | 28 | void Log_Timer::destroy(void) { 29 | if (instance_) { 30 | delete instance_; 31 | instance_ = 0; 32 | } 33 | } 34 | 35 | void Log_Timer::run_handler(void) { 36 | register_handler(); 37 | watcher_.loop(); 38 | } 39 | 40 | void Log_Timer::register_handler(void) { 41 | Time_Value timeout_tv(0, 100 * 1000); 42 | watcher_.add(&timer_handler_, EVENT_TIMEOUT, &timeout_tv); 43 | } 44 | -------------------------------------------------------------------------------- /log_server/Log_Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Log_Timer.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOG_TIMER_H_ 9 | #define LOG_TIMER_H_ 10 | 11 | #include "Thread.h" 12 | #include "Epoll_Watcher.h" 13 | 14 | class Log_Timer_Handler: public Event_Handler { 15 | public: 16 | virtual int handle_timeout(const Time_Value &tv); 17 | }; 18 | 19 | class Log_Timer: public Thread { 20 | public: 21 | static Log_Timer *instance(void); 22 | static void destroy(void); 23 | virtual void run_handler(void); 24 | void register_handler(void); 25 | 26 | private: 27 | Log_Timer(void); 28 | virtual ~Log_Timer(void); 29 | 30 | private: 31 | static Log_Timer *instance_; 32 | Epoll_Watcher watcher_; 33 | Log_Timer_Handler timer_handler_; 34 | }; 35 | 36 | #define LOG_TIMER Log_Timer::instance() 37 | 38 | #endif /* LOG_TIMER_H_ */ 39 | -------------------------------------------------------------------------------- /login_server/Login_Client_Messager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef LOGIN_CLIENT_MESSAGER_H_ 7 | #define LOGIN_CLIENT_MESSAGER_H_ 8 | 9 | #include "Block_Buffer.h" 10 | 11 | class Login_Client_Messager { 12 | public: 13 | static Login_Client_Messager *instance(void); 14 | 15 | int process_block(Block_Buffer &buf); 16 | int connect_login(int cid, int msg_id, Block_Buffer &buf); 17 | 18 | private: 19 | Login_Client_Messager(void); 20 | virtual ~Login_Client_Messager(void); 21 | Login_Client_Messager(const Login_Client_Messager &); 22 | const Login_Client_Messager &operator=(const Login_Client_Messager &); 23 | 24 | private: 25 | static Login_Client_Messager *instance_; 26 | }; 27 | 28 | #define LOGIN_CLIENT_MESSAGER Login_Client_Messager::instance() 29 | 30 | #endif /* LOGIN_CLIENT_MESSAGER_H_ */ 31 | -------------------------------------------------------------------------------- /login_server/Login_Inner_Messager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Login_Inner_Messager.h" 7 | #include "Login_Manager.h" 8 | 9 | Login_Inner_Messager::Login_Inner_Messager(void) { } 10 | 11 | Login_Inner_Messager::~Login_Inner_Messager(void) { } 12 | 13 | Login_Inner_Messager *Login_Inner_Messager::instance_; 14 | 15 | Login_Inner_Messager *Login_Inner_Messager::instance(void) { 16 | if (! instance_) 17 | instance_ = new Login_Inner_Messager; 18 | return instance_; 19 | } 20 | 21 | int Login_Inner_Messager::process_gate_block(Block_Buffer &buf) { 22 | LOGIN_MANAGER->add_recv_bytes(buf.readable_bytes()); 23 | 24 | int32_t cid = 0; 25 | int16_t len = 0; 26 | int32_t msg_id = 0; 27 | int32_t status = 0; 28 | int32_t player_cid = 0; 29 | buf.read_int32(cid); 30 | buf.read_int16(len); 31 | buf.read_int32(msg_id); 32 | buf.read_int32(status); 33 | buf.read_int32(player_cid); 34 | 35 | LOGIN_MANAGER->add_msg_count(msg_id); 36 | switch (msg_id) { 37 | case SYNC_GATE_LOGIN_PLAYER_ACCOUNT:{ 38 | MSG_140000 msg; 39 | if (msg.deserialize(buf) ==0){ 40 | gate_login_player_account(cid, player_cid, msg); 41 | } 42 | break; 43 | } 44 | default: 45 | break; 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | int Login_Inner_Messager::gate_login_player_account(int gate_cid, int32_t player_cid, MSG_140000& msg) { 52 | Block_Buffer gate_buf; 53 | Login_Player *player = dynamic_cast(LOGIN_MANAGER->find_account_player(msg.account)); 54 | if (player && player->session_info().session == msg.session 55 | && player->session_info().gate_ip == msg.gate_ip 56 | && player->session_info().gate_port == msg.gate_port) { 57 | //验证玩家session成功,关闭玩家与login的连接,开启玩家与gate的连接 58 | gate_buf.make_player_message(SYNC_LOGIN_GATE_PLAYER_ACCOUNT, 0, player_cid); 59 | LOGIN_MANAGER->close_client(0, player->player_cid(), ERROR_SESSION_SUCCESS); 60 | } else { 61 | LOG_DEBUG("login check session error, session:%s, account:%s, gate_ip:%s, gate_port:%d, gate_cid:%d, player_cid:%d", 62 | msg.session.c_str(), msg.account.c_str(), msg.gate_ip.c_str(), msg.gate_port, gate_cid, player_cid); 63 | gate_buf.make_player_message(SYNC_LOGIN_GATE_PLAYER_ACCOUNT, ERROR_CLIENT_SESSION, player_cid); 64 | } 65 | MSG_140001 gate_msg; 66 | gate_msg.account = msg.account; 67 | gate_msg.serialize(gate_buf); 68 | gate_buf.finish_message(); 69 | LOGIN_MANAGER->send_to_gate(gate_cid, gate_buf); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /login_server/Login_Inner_Messager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef LOGIN_INNER_MESSAGER_H_ 7 | #define LOGIN_INNER_MESSAGER_H_ 8 | 9 | #include "Public_Struct.h" 10 | 11 | class Login_Inner_Messager { 12 | public: 13 | static Login_Inner_Messager *instance(void); 14 | 15 | /// gate消息处理 16 | int process_gate_block(Block_Buffer &buf); 17 | 18 | int gate_login_player_account(int gate_cid, int32_t player_cid, MSG_140000& msg); 19 | 20 | private: 21 | Login_Inner_Messager(void); 22 | virtual ~Login_Inner_Messager(void); 23 | Login_Inner_Messager(const Login_Inner_Messager &); 24 | const Login_Inner_Messager &operator=(const Login_Inner_Messager &); 25 | 26 | private: 27 | static Login_Inner_Messager *instance_; 28 | }; 29 | 30 | #define LOGIN_INNER_MESSAGER Login_Inner_Messager::instance() 31 | 32 | 33 | #endif /* LOGIN_INNER_MESSAGER_H_ */ 34 | -------------------------------------------------------------------------------- /login_server/Login_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef LOGIN_MANAGER_H_ 7 | #define LOGIN_MANAGER_H_ 8 | 9 | #include "Mysql_Conn.h" 10 | #include "Server_Manager.h" 11 | #include "Login_Player.h" 12 | 13 | class Login_Manager: public Server_Manager { 14 | typedef Object_Pool Player_Pool; 15 | public: 16 | static Login_Manager *instance(void); 17 | int init(int server_id); 18 | 19 | inline Login_Player *pop_player(void) { return player_pool_.pop(); } 20 | inline int push_player(Login_Player *player) { return player_pool_.push(player); } 21 | 22 | virtual int close_client(int gate_cid, int player_cid, int error_code); 23 | virtual int recycle_player(int gate_cid, int player_cid); 24 | virtual int free_cache(void); 25 | virtual void get_server_info(void); 26 | virtual void print_server_info(void); 27 | 28 | //发送数据 29 | int send_to_client(int player_cid, Block_Buffer &buf); 30 | int send_to_gate(int gate_cid, Block_Buffer &buf); 31 | 32 | //消息处理 33 | int process_list(); 34 | void process_drop_cid(int cid); 35 | void push_drop_cid(int cid); 36 | int push_tick(int x); 37 | int push_login_client_data(Block_Buffer *buf); 38 | int push_login_gate_data(Block_Buffer *buf); 39 | 40 | int connect_mysql_db(); 41 | int client_login(std::string& account, std::string& password); 42 | void get_gate_ip(std::string &account, std::string &ip, int &port); 43 | 44 | private: 45 | Login_Manager(void); 46 | virtual ~Login_Manager(void); 47 | Login_Manager(const Login_Manager &); 48 | const Login_Manager &operator=(const Login_Manager &); 49 | 50 | private: 51 | static Login_Manager *instance_; 52 | Player_Pool player_pool_; 53 | 54 | Int_List drop_cid_list_; //掉线玩家列表 55 | Int_List tick_list_; //定时器列表 56 | Data_List login_client_data_list_; //client-->login 57 | Data_List login_gate_data_list_; //login-->connector 58 | 59 | Server_Info login_client_server_info_; 60 | Server_Info login_gate_server_info_; 61 | 62 | Mysql_Conn* mysql_conn_; 63 | }; 64 | 65 | #define LOGIN_MANAGER Login_Manager::instance() 66 | 67 | //////////////////////////////////////////////////////////////////////////////// 68 | inline void Login_Manager::push_drop_cid(int cid) { 69 | drop_cid_list_.push_back(cid); 70 | } 71 | 72 | inline int Login_Manager::push_tick(int x) { 73 | tick_list_.push_back(x); 74 | return 0; 75 | } 76 | 77 | inline int Login_Manager::push_login_client_data(Block_Buffer *buf) { 78 | login_client_data_list_.push_back(buf); 79 | return 0; 80 | } 81 | 82 | inline int Login_Manager::push_login_gate_data(Block_Buffer *buf) { 83 | login_gate_data_list_.push_back(buf); 84 | return 0; 85 | } 86 | 87 | #endif /* LOGIN_MANAGER_H_ */ 88 | -------------------------------------------------------------------------------- /login_server/Login_Player.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Login_Player.cpp 3 | * 4 | * Created on: Jan 21, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Login_Manager.h" 9 | #include "Login_Server.h" 10 | 11 | Login_Player::Login_Player(void) { } 12 | 13 | Login_Player::~Login_Player(void) { } 14 | 15 | void Login_Player::reset(void) { 16 | Player::reset(); 17 | session_info_.reset(); 18 | } 19 | 20 | int Login_Player::tick(Time_Value &now) { 21 | if (now - session_info_.session_tick > Time_Value(10)) { 22 | //session到期,断开客户端与login的连接 23 | LOG_INFO("player session timeout, drop from login, cid:%d, account:%s", player_cid(), account().c_str()); 24 | session_info_.session_tick += Time_Value(10); 25 | LOGIN_MANAGER->close_client(0, player_cid(), ERROR_SESSION_TIMEOUT); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /login_server/Login_Player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Login_Player.h 3 | * 4 | * Created on: Jan 21, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOGIN_PLAYER_H_ 9 | #define LOGIN_PLAYER_H_ 10 | 11 | #include "Player.h" 12 | 13 | class Login_Player : public Player { 14 | public: 15 | Login_Player(void); 16 | virtual ~Login_Player(void); 17 | 18 | inline void set_session_info(const Session_Info &session_info) { session_info_ = session_info; } 19 | inline const Session_Info &session_info(void) { return session_info_; } 20 | 21 | virtual void reset(void); 22 | virtual int tick(Time_Value &now); 23 | 24 | private: 25 | Session_Info session_info_; 26 | }; 27 | 28 | #endif /* LOGIN_PLAYER_H_ */ 29 | -------------------------------------------------------------------------------- /login_server/Login_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Login_Server.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Login_Server.h" 9 | #include "Login_Manager.h" 10 | 11 | Login_Client_Server::Login_Client_Server(void) { } 12 | 13 | Login_Client_Server::~Login_Client_Server(void) { } 14 | 15 | Login_Client_Server *Login_Client_Server::instance_ = 0; 16 | 17 | Login_Client_Server *Login_Client_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new Login_Client_Server; 20 | return instance_; 21 | } 22 | 23 | void Login_Client_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | if (!block_list_.empty()) { 27 | buf = block_list_.pop_front(); 28 | LOGIN_MANAGER->push_login_client_data(buf); 29 | } else { 30 | //没有数据时候延迟 31 | Time_Value::sleep(Time_Value(0,100)); 32 | } 33 | } 34 | } 35 | 36 | /////////////////////////////////////////////////////////////////////////////// 37 | Login_Gate_Server::Login_Gate_Server(void) { } 38 | 39 | Login_Gate_Server::~Login_Gate_Server(void) { } 40 | 41 | Login_Gate_Server *Login_Gate_Server::instance_ = 0; 42 | 43 | Login_Gate_Server *Login_Gate_Server::instance(void) { 44 | if (! instance_) 45 | instance_ = new Login_Gate_Server; 46 | return instance_; 47 | } 48 | 49 | void Login_Gate_Server::process_list(void) { 50 | Block_Buffer *buf = 0; 51 | int cid = 0; 52 | while (1) { 53 | bool all_empty = true; 54 | 55 | if (!drop_cid_list_.empty()) { 56 | all_empty = false; 57 | cid = drop_cid_list_.pop_front(); 58 | LOGIN_MANAGER->push_drop_cid(cid); 59 | } 60 | 61 | if (!block_list_.empty()) { 62 | all_empty = false; 63 | buf = block_list_.pop_front(); 64 | LOGIN_MANAGER->push_login_gate_data(buf); 65 | } 66 | 67 | if (all_empty) { 68 | //没有数据时候延迟 69 | Time_Value::sleep(Time_Value(0,100)); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /login_server/Login_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Login_Server.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef LOGIN_SERVER_H_ 9 | #define LOGIN_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class Login_Client_Server: public Server { 14 | public: 15 | static Login_Client_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Login_Client_Server(void); 21 | virtual ~Login_Client_Server(void); 22 | Login_Client_Server(const Login_Client_Server &); 23 | const Login_Client_Server &operator=(const Login_Client_Server &); 24 | 25 | private: 26 | static Login_Client_Server *instance_; 27 | }; 28 | 29 | #define LOGIN_CLIENT_SERVER Login_Client_Server::instance() 30 | 31 | /////////////////////////////////////////////////////////////////////////// 32 | 33 | class Login_Gate_Server: public Server { 34 | public: 35 | static Login_Gate_Server *instance(void); 36 | 37 | virtual void process_list(void); 38 | 39 | private: 40 | Login_Gate_Server(void); 41 | virtual ~Login_Gate_Server(void); 42 | Login_Gate_Server(const Login_Gate_Server &); 43 | const Login_Gate_Server &operator=(const Login_Gate_Server &); 44 | 45 | private: 46 | static Login_Gate_Server *instance_; 47 | }; 48 | 49 | #define LOGIN_GATE_SERVER Login_Gate_Server::instance() 50 | 51 | #endif /* LOGIN_SERVER_H_ */ 52 | -------------------------------------------------------------------------------- /login_server/Login_Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Login_Manager.h" 7 | #include "Login_Timer.h" 8 | 9 | Login_Timer_Handler::Login_Timer_Handler(void) { } 10 | 11 | Login_Timer_Handler::~Login_Timer_Handler(void) { } 12 | 13 | int Login_Timer_Handler::handle_timeout(const Time_Value &tv) { 14 | return LOGIN_MANAGER->push_tick(tv.sec()); 15 | } 16 | 17 | Login_Timer::Login_Timer(void) { } 18 | 19 | Login_Timer::~Login_Timer(void) { } 20 | 21 | Login_Timer *Login_Timer::instance_; 22 | 23 | Login_Timer *Login_Timer::instance(void) { 24 | if (instance_ == 0) 25 | instance_ = new Login_Timer; 26 | return instance_; 27 | } 28 | 29 | void Login_Timer::run_handler(void) { 30 | register_handler(); 31 | watcher_.loop(); 32 | } 33 | 34 | Epoll_Watcher &Login_Timer::watcher(void) { 35 | return watcher_; 36 | } 37 | 38 | void Login_Timer::register_handler(void) { 39 | Time_Value timeout_tv(0, 100 * 1000); 40 | watcher_.add(&timer_handler_, EVENT_TIMEOUT, &timeout_tv); 41 | } 42 | -------------------------------------------------------------------------------- /login_server/Login_Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef LOGIN_TIMER_H_ 7 | #define LOGIN_TIMER_H_ 8 | 9 | #include "Thread.h" 10 | #include "Epoll_Watcher.h" 11 | 12 | class Login_Timer_Handler: public Event_Handler { 13 | public: 14 | Login_Timer_Handler(void); 15 | virtual ~Login_Timer_Handler(void); 16 | 17 | virtual int handle_timeout(const Time_Value &tv); 18 | }; 19 | 20 | class Login_Timer: public Thread { 21 | public: 22 | static Login_Timer *instance(void); 23 | virtual void run_handler(void); 24 | 25 | Epoll_Watcher &watcher(void); 26 | void register_handler(void); 27 | 28 | private: 29 | Login_Timer(void); 30 | virtual ~Login_Timer(void); 31 | Login_Timer(const Login_Timer &); 32 | const Login_Timer &operator=(const Login_Timer &); 33 | 34 | private: 35 | Epoll_Watcher watcher_; 36 | Login_Timer_Handler timer_handler_; 37 | static Login_Timer *instance_; 38 | }; 39 | 40 | #define LOGIN_TIMER Login_Timer::instance() 41 | 42 | #endif /* LOGIN_TIMER_H_ */ 43 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * main.cpp 3 | * Created on: Dec 18, 2015 4 | * Author: zhangyalei 5 | */ 6 | 7 | #include 8 | #include "Time_Value.h" 9 | #include "Debug_Server.h" 10 | #include "Daemon_Server.h" 11 | #include "Server_Config.h" 12 | #include "Public_Struct.h" 13 | #include "Log.h" 14 | 15 | static void sighandler(int sig_no) { exit(0); } /// for gprof need normal exit 16 | 17 | int main(int argc, char *argv[]) { 18 | srand(Time_Value::gettimeofday().sec() + Time_Value::gettimeofday().usec()); 19 | srandom(Time_Value::gettimeofday().sec() + Time_Value::gettimeofday().usec()); 20 | signal(SIGPIPE, SIG_IGN); 21 | signal(SIGUSR1, sighandler); 22 | 23 | SERVER_CONFIG->load_server_config(); 24 | int server_type = SERVER_CONFIG->server_misc()["server_type"].asInt(); 25 | if (server_type == MULTI_PROCESS) { 26 | DAEMON_SERVER->init(argc, argv); 27 | DAEMON_SERVER->start(argc, argv); 28 | } else if (server_type == MULTI_THREAD) { 29 | DEBUG_SERVER->init(argc, argv); 30 | DEBUG_SERVER->start(argc, argv); 31 | } else { 32 | LOG_FATAL("server start type = %d error abort", server_type); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /master_server/Master_Connector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Master_Connector.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Master_Connector.h" 9 | #include "Master_Manager.h" 10 | 11 | Master_DB_Connector::Master_DB_Connector(void) { } 12 | 13 | Master_DB_Connector::~Master_DB_Connector(void) { } 14 | 15 | Master_DB_Connector *Master_DB_Connector::instance_ = 0; 16 | 17 | Master_DB_Connector *Master_DB_Connector::instance(void) { 18 | if (! instance_) 19 | instance_ = new Master_DB_Connector; 20 | return instance_; 21 | } 22 | 23 | void Master_DB_Connector::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | if (!block_list_.empty()) { 27 | buf = block_list_.pop_front(); 28 | MASTER_MANAGER->push_master_db_data(buf); 29 | } else { 30 | //没有数据时候延迟 31 | Time_Value::sleep(Time_Value(0,100)); 32 | } 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /master_server/Master_Connector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Master_Connector.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MASTER_CONNECTOR_H_ 9 | #define MASTER_CONNECTOR_H_ 10 | 11 | #include "Connector.h" 12 | 13 | class Master_DB_Connector: public Connector { 14 | public: 15 | static Master_DB_Connector *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Master_DB_Connector(void); 21 | virtual ~Master_DB_Connector(void); 22 | Master_DB_Connector(const Master_DB_Connector &); 23 | const Master_DB_Connector &operator=(const Master_DB_Connector &); 24 | 25 | private: 26 | static Master_DB_Connector *instance_; 27 | }; 28 | 29 | #define MASTER_DB_CONNECTOR Master_DB_Connector::instance() 30 | 31 | #endif /* MASTER_CONNECTOR_H_ */ 32 | -------------------------------------------------------------------------------- /master_server/Master_Player.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Master_Manager.h" 7 | 8 | Master_Player::Master_Player(void) { } 9 | 10 | Master_Player::~Master_Player(void) { } 11 | -------------------------------------------------------------------------------- /master_server/Master_Player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef MASTER_PLAYER_H_ 7 | #define MASTER_PLAYER_H_ 8 | 9 | #include "Player.h" 10 | 11 | class Master_Player : public Player { 12 | public: 13 | Master_Player(void); 14 | virtual ~Master_Player(void); 15 | }; 16 | 17 | #endif /* MASTER_PLAYER_H_ */ 18 | -------------------------------------------------------------------------------- /master_server/Master_Server.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Master_Server.cpp 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Master_Server.h" 9 | #include "Master_Manager.h" 10 | 11 | Master_Gate_Server::Master_Gate_Server(void) { } 12 | 13 | Master_Gate_Server::~Master_Gate_Server(void) { } 14 | 15 | Master_Gate_Server *Master_Gate_Server::instance_ = 0; 16 | 17 | Master_Gate_Server *Master_Gate_Server::instance(void) { 18 | if (! instance_) 19 | instance_ = new Master_Gate_Server; 20 | return instance_; 21 | } 22 | 23 | void Master_Gate_Server::process_list(void) { 24 | Block_Buffer *buf = 0; 25 | while (1) { 26 | bool all_empty = true; 27 | if (!drop_cid_list_.empty()) { 28 | all_empty = false; 29 | int cid = drop_cid_list_.pop_front(); 30 | MASTER_MANAGER->push_drop_gate_cid(cid); 31 | } 32 | if (!block_list_.empty()) { 33 | all_empty = false; 34 | buf = block_list_.pop_front(); 35 | MASTER_MANAGER->push_master_gate_data(buf); 36 | } 37 | if (all_empty) { 38 | //没有数据时候延迟 39 | Time_Value::sleep(Time_Value(0,100)); 40 | } 41 | } 42 | } 43 | 44 | //////////////////////////////////////////////////////////////////////////////// 45 | Master_Game_Server::Master_Game_Server(void) { } 46 | 47 | Master_Game_Server::~Master_Game_Server(void) { } 48 | 49 | Master_Game_Server *Master_Game_Server::instance_ = 0; 50 | 51 | Master_Game_Server *Master_Game_Server::instance(void) { 52 | if (! instance_) 53 | instance_ = new Master_Game_Server; 54 | return instance_; 55 | } 56 | 57 | void Master_Game_Server::process_list(void) { 58 | Block_Buffer *buf = 0; 59 | while (1) { 60 | if (!block_list_.empty()) { 61 | buf = block_list_.pop_front(); 62 | MASTER_MANAGER->push_master_game_data(buf); 63 | } else { 64 | //没有数据时候延迟 65 | Time_Value::sleep(Time_Value(0,100)); 66 | } 67 | } 68 | } 69 | 70 | //////////////////////////////////////////////////////////////////////////////// 71 | Master_Http_Server::Master_Http_Server(void) { } 72 | 73 | Master_Http_Server::~Master_Http_Server(void) { } 74 | 75 | Master_Http_Server *Master_Http_Server::instance_ = 0; 76 | 77 | Master_Http_Server *Master_Http_Server::instance(void) { 78 | if (! instance_) 79 | instance_ = new Master_Http_Server; 80 | return instance_; 81 | } 82 | 83 | void Master_Http_Server::process_list(void) { 84 | Block_Buffer *buf = 0; 85 | while (1) { 86 | if (!block_list_.empty()) { 87 | buf = block_list_.pop_front(); 88 | MASTER_MANAGER->push_master_http_data(buf); 89 | } else { 90 | //没有数据时候延迟 91 | Time_Value::sleep(Time_Value(0,100)); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /master_server/Master_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Master_Server.h 3 | * 4 | * Created on: Jan 13, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MASTER_SERVER_H_ 9 | #define MASTER_SERVER_H_ 10 | 11 | #include "Server.h" 12 | 13 | class Master_Gate_Server: public Server { 14 | public: 15 | static Master_Gate_Server *instance(void); 16 | 17 | virtual void process_list(void); 18 | 19 | private: 20 | Master_Gate_Server(void); 21 | virtual ~Master_Gate_Server(void); 22 | Master_Gate_Server(const Master_Gate_Server &); 23 | const Master_Gate_Server &operator=(const Master_Gate_Server &); 24 | 25 | private: 26 | static Master_Gate_Server *instance_; 27 | }; 28 | 29 | #define MASTER_GATE_SERVER Master_Gate_Server::instance() 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | class Master_Game_Server: public Server { 34 | public: 35 | static Master_Game_Server *instance(void); 36 | 37 | virtual void process_list(void); 38 | 39 | private: 40 | Master_Game_Server(void); 41 | virtual ~Master_Game_Server(void); 42 | Master_Game_Server(const Master_Game_Server &); 43 | const Master_Game_Server &operator=(const Master_Game_Server &); 44 | 45 | private: 46 | static Master_Game_Server *instance_; 47 | }; 48 | 49 | #define MASTER_GAME_SERVER Master_Game_Server::instance() 50 | 51 | //////////////////////////////////////////////////////////////////////// 52 | 53 | class Master_Http_Server: public Server { 54 | public: 55 | static Master_Http_Server *instance(void); 56 | 57 | virtual void process_list(void); 58 | 59 | private: 60 | Master_Http_Server(void); 61 | virtual ~Master_Http_Server(void); 62 | Master_Http_Server(const Master_Http_Server &); 63 | const Master_Http_Server &operator=(const Master_Http_Server &); 64 | 65 | private: 66 | static Master_Http_Server *instance_; 67 | }; 68 | 69 | #define MASTER_HTTP_SERVER Master_Http_Server::instance() 70 | 71 | #endif /* MASTER_SERVER_H_ */ 72 | -------------------------------------------------------------------------------- /master_server/Master_Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #include "Master_Manager.h" 7 | #include "Master_Timer.h" 8 | 9 | Master_Timer_Handler::Master_Timer_Handler(void) { } 10 | 11 | Master_Timer_Handler::~Master_Timer_Handler(void) { } 12 | 13 | int Master_Timer_Handler::handle_timeout(const Time_Value &tv) { 14 | MASTER_MANAGER->push_tick(tv.sec()); 15 | MASTER_TIMER->v8_tick(tv); 16 | return 0; 17 | } 18 | 19 | Master_Timer::Master_Timer(void) { } 20 | 21 | Master_Timer::~Master_Timer(void) { } 22 | 23 | Master_Timer *Master_Timer::instance_; 24 | 25 | Master_Timer *Master_Timer::instance(void) { 26 | if (instance_ == 0) 27 | instance_ = new Master_Timer; 28 | return instance_; 29 | } 30 | 31 | void Master_Timer::run_handler(void) { 32 | register_handler(); 33 | watcher_.loop(); 34 | } 35 | 36 | Epoll_Watcher &Master_Timer::watcher(void) { 37 | return watcher_; 38 | } 39 | 40 | void Master_Timer::register_handler(void) { 41 | Time_Value timeout_tv(0, 100 * 1000); 42 | watcher_.add(&timer_handler_, EVENT_TIMEOUT, &timeout_tv); 43 | } 44 | 45 | void Master_Timer::register_v8_handler(int timer_id, int internal, int first_tick_internal) { 46 | V8_Timer_Handler *handler = v8_timer_pool_.pop(); 47 | handler->timer_id = timer_id; 48 | handler->interval = internal; 49 | handler->next_tick = Time_Value::gettimeofday() + Time_Value(first_tick_internal); 50 | v8_timer_queue_.push(handler); 51 | } 52 | 53 | int Master_Timer::v8_tick(const Time_Value &now){ 54 | while(!v8_timer_queue_.empty() && (now > v8_timer_queue_.top()->next_tick)) { 55 | V8_Timer_Handler *handler = v8_timer_queue_.top(); 56 | v8_timer_queue_.pop(); 57 | v8_timer_list_.push_back(handler->timer_id); 58 | handler->next_tick += Time_Value(handler->interval / 1000, handler->interval % 1000 * 1000); 59 | v8_timer_queue_.push(handler); 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /master_server/Master_Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Created on: Dec 21, 2015 3 | * Author: zhangyalei 4 | */ 5 | 6 | #ifndef MASTER_TIMER_H_ 7 | #define MASTER_TIMER_H_ 8 | 9 | #include "Thread.h" 10 | #include "Epoll_Watcher.h" 11 | #include "Priority_Queue.h" 12 | #include "List.h" 13 | #include "Public_Struct.h" 14 | 15 | class Master_Timer_Handler: public Event_Handler { 16 | public: 17 | Master_Timer_Handler(void); 18 | virtual ~Master_Timer_Handler(void); 19 | 20 | virtual int handle_timeout(const Time_Value &tv); 21 | }; 22 | 23 | class Master_Timer: public Thread { 24 | public: 25 | typedef Object_Pool V8_Timer_Pool; 26 | typedef Priority_Queue V8_Timer_Queue; 27 | typedef List Timer_List; 28 | 29 | static Master_Timer *instance(void); 30 | virtual void run_handler(void); 31 | Epoll_Watcher &watcher(void); 32 | 33 | int v8_tick(const Time_Value &now); 34 | 35 | void register_handler(void); 36 | void register_v8_handler(int timer_id, int internal, int first_tick_internal); //注册js层定时器 37 | int pop_v8_timer(void); //js层获取超时定时器编号 38 | 39 | private: 40 | Master_Timer(void); 41 | virtual ~Master_Timer(void); 42 | Master_Timer(const Master_Timer &); 43 | const Master_Timer &operator=(const Master_Timer &); 44 | 45 | private: 46 | Epoll_Watcher watcher_; 47 | Master_Timer_Handler timer_handler_; 48 | V8_Timer_Pool v8_timer_pool_; 49 | V8_Timer_Queue v8_timer_queue_; 50 | Timer_List v8_timer_list_; 51 | 52 | static Master_Timer *instance_; 53 | }; 54 | 55 | #define MASTER_TIMER Master_Timer::instance() 56 | 57 | inline int Master_Timer::pop_v8_timer(void){ 58 | if (v8_timer_list_.empty()) { 59 | return 0; 60 | } 61 | return v8_timer_list_.pop_front(); 62 | } 63 | 64 | #endif /* MASTER_TIMER_H_ */ 65 | -------------------------------------------------------------------------------- /misc/Daemon_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Daemon_Server.h 3 | * 4 | * Created on: Mar 26, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DAEMON_SERVER_H_ 9 | #define DAEMON_SERVER_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | #include "boost/unordered_map.hpp" 15 | 16 | class Epoll_Watcher; 17 | class Daemon_Server { 18 | public: 19 | typedef boost::unordered_map Int_Process_Map; 20 | typedef boost::unordered_map Int_Int_Map; 21 | 22 | const static int max_core_dump_num = 2500; 23 | 24 | static Daemon_Server *instance(void); 25 | 26 | int init(int argc, char *argv[]); 27 | int start(int argc, char *argv[]); 28 | 29 | Server_Conf &server_conf(void) { return server_conf_; } 30 | 31 | private: 32 | Daemon_Server(void); 33 | ~Daemon_Server(void); 34 | Daemon_Server(const Daemon_Server &); 35 | const Daemon_Server &operator=(const Daemon_Server &); 36 | 37 | int parse_cmd_arguments(int argc, char *argv[]); 38 | int fork_exec_args(const char *exec_str, int server_type, int server_id = 0); 39 | 40 | static void sigcld_handle(int signo); 41 | void restart_process(int pid); 42 | void record_pid_file(void); 43 | 44 | void run_daemon_server(void); 45 | void run_log_server(int server_id); 46 | void run_db_server(int server_id); 47 | void run_login_server(int server_id); 48 | void run_master_server(int server_id); 49 | void run_game_server(int server_id); 50 | void run_gate_server(int server_id); 51 | 52 | private: 53 | static std::string exec_name_; 54 | static Daemon_Server *instance_; 55 | static struct option long_options[]; 56 | std::string server_label_; 57 | 58 | Int_Process_Map process_map_; 59 | Int_Int_Map core_dump_num_; 60 | 61 | Epoll_Watcher *wait_watcher_; 62 | Server_Conf server_conf_; 63 | }; 64 | 65 | #define DAEMON_SERVER Daemon_Server::instance() 66 | 67 | #endif /* DAEMON_SERVER_H_ */ 68 | -------------------------------------------------------------------------------- /misc/Debug_Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Debug_Server.h 3 | * 4 | * Created on: Dec 18, 2015 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef DEBUG_SERVER_H_ 9 | #define DEBUG_SERVER_H_ 10 | 11 | #include "Event_Handler.h" 12 | #include "Public_Struct.h" 13 | 14 | class Epoll_Watcher; 15 | class Debug_Server: public Event_Handler { 16 | public: 17 | static Debug_Server *instance(void); 18 | static void destroy(void); 19 | 20 | int init(int argc, char *argv[]); 21 | int start(int argc, char *argv[]); 22 | virtual int handle_timeout(const Time_Value &tv) { return 0; } 23 | 24 | Server_Conf &server_conf(void) { return server_conf_; } 25 | 26 | private: 27 | Debug_Server(void); 28 | ~Debug_Server(void); 29 | Debug_Server(const Debug_Server &); 30 | const Debug_Server &operator=(const Debug_Server &); 31 | 32 | private: 33 | static Debug_Server *instance_; 34 | Epoll_Watcher *wait_watcher_; 35 | Server_Conf server_conf_; 36 | }; 37 | 38 | #define DEBUG_SERVER Debug_Server::instance() 39 | 40 | #endif /* DEBUG_SERVER_H_ */ 41 | -------------------------------------------------------------------------------- /misc/Inner_Msg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Inner_Msg.h 3 | * 4 | * Created on: Aug 2, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef INNER_MSG_H_ 9 | #define INNER_MSG_H_ 10 | 11 | #include "Block_Buffer.h" 12 | 13 | struct Create_Role_Info { 14 | std::string account; 15 | std::string role_name; 16 | int8_t gender; 17 | int8_t career; 18 | 19 | Create_Role_Info(void); 20 | ~Create_Role_Info(); 21 | void serialize(Block_Buffer &buffer) const; 22 | int deserialize(Block_Buffer &buffer); 23 | void reset(void); 24 | }; 25 | 26 | struct Create_Guild_Info { 27 | int64_t guild_id; 28 | std::string guild_name; 29 | int64_t chief_id; 30 | 31 | Create_Guild_Info(void); 32 | ~Create_Guild_Info(); 33 | void serialize(Block_Buffer &buffer) const; 34 | int deserialize(Block_Buffer &buffer); 35 | void reset(void); 36 | }; 37 | 38 | struct MSG_100001 { 39 | std::string account; 40 | std::string password; 41 | 42 | MSG_100001(void); 43 | ~MSG_100001(); 44 | void serialize(Block_Buffer &buffer) const; 45 | int deserialize(Block_Buffer &buffer); 46 | void reset(void); 47 | }; 48 | 49 | struct MSG_500001 { 50 | std::string gate_ip; 51 | int32_t gate_port; 52 | std::string session; 53 | 54 | MSG_500001(void); 55 | ~MSG_500001(); 56 | void serialize(Block_Buffer &buffer) const; 57 | int deserialize(Block_Buffer &buffer); 58 | void reset(void); 59 | }; 60 | 61 | struct MSG_100101 { 62 | std::string account; 63 | std::string session; //登录验证的seesion 64 | 65 | MSG_100101(void); 66 | ~MSG_100101(); 67 | void serialize(Block_Buffer &buffer) const; 68 | int deserialize(Block_Buffer &buffer); 69 | void reset(void); 70 | }; 71 | 72 | struct MSG_500101 { 73 | std::string account; 74 | 75 | MSG_500101(void); 76 | ~MSG_500101(); 77 | void serialize(Block_Buffer &buffer) const; 78 | int deserialize(Block_Buffer &buffer); 79 | void reset(void); 80 | }; 81 | 82 | struct MSG_100102 { 83 | int32_t client_time; 84 | 85 | MSG_100102(void); 86 | ~MSG_100102(); 87 | void serialize(Block_Buffer &buffer) const; 88 | int deserialize(Block_Buffer &buffer); 89 | void reset(void); 90 | }; 91 | 92 | struct MSG_500102 { 93 | int32_t client_time; 94 | int32_t server_time; 95 | 96 | MSG_500102(void); 97 | ~MSG_500102(); 98 | void serialize(Block_Buffer &buffer) const; 99 | int deserialize(Block_Buffer &buffer); 100 | void reset(void); 101 | }; 102 | 103 | struct MSG_140000 { 104 | std::string account; 105 | std::string session; 106 | std::string gate_ip; 107 | int32_t gate_port; 108 | 109 | MSG_140000(void); 110 | ~MSG_140000(); 111 | void serialize(Block_Buffer &buffer) const; 112 | int deserialize(Block_Buffer &buffer); 113 | void reset(void); 114 | }; 115 | 116 | struct MSG_140001 { 117 | std::string account; 118 | 119 | MSG_140001(void); 120 | ~MSG_140001(); 121 | void serialize(Block_Buffer &buffer) const; 122 | int deserialize(Block_Buffer &buffer); 123 | void reset(void); 124 | }; 125 | 126 | #endif /* INNER_MSG_H_ */ 127 | -------------------------------------------------------------------------------- /misc/Message.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Message.h 3 | * 4 | * Created on: Aug 2, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MESSAGE_H_ 9 | #define MESSAGE_H_ 10 | 11 | enum Message_Bound { 12 | CLIENT_GATE_MESSAGE_START = 100000, 13 | CLIENT_GATE_MESSAGE_END = 109999, 14 | CLIENT_MASTER_MESSAGE_START = 110000, 15 | CLIENT_MASTER_MESSAGE_END = 119999, 16 | CLIENT_GAME_MESSAGE_START = 120000, 17 | CLIENT_GAME_MESSAGE_END = 129999, 18 | }; 19 | 20 | enum Client_Login_Message { 21 | REQ_CONNECT_LOGIN = 100001, //客户端登录login 22 | REQ_CONNECT_GATE = 100101, //客户端登录gate 23 | REQ_SEND_HEARTBEAT = 100102, //发送心跳到gate 24 | }; 25 | 26 | enum Login_Client_Message { 27 | RES_CONNECT_LOGIN = 500001, //客户端登录login(返回) 28 | RES_CONNECT_GATE = 500101, //客户端登录gate(返回) 29 | RES_SEND_HEARTBEAT = 500102, //发送心跳到gate(返回) 30 | }; 31 | 32 | enum Client_Game_Message { 33 | REQ_FETCH_ROLE_INFO = 120001, //获取角色信息 34 | }; 35 | 36 | enum Game_Client_Message { 37 | RES_FETCH_ROLE_INFO = 520001, //获取角色信息(返回) 38 | }; 39 | 40 | enum Gate_Message { 41 | SYNC_GATE_LOGIN_PLAYER_ACCOUNT = 140000, //gate向login校验玩家session 42 | SYNC_LOGIN_GATE_PLAYER_ACCOUNT = 140001, //login返回gate校验session结果 43 | SYNC_GATE_GAME_PLAYER_LOGOUT = 140100, //gate通知玩家下线到game 44 | SYNC_GATE_MASTER_PLAYER_LOGOUT = 140200, //gate通知玩家下线到master 45 | SYNC_GATE_MASTER_PLAYER_LOGIN = 140201, //gate通知玩家上线到master 46 | SYNC_MASTER_GATE_PLAYER_CHANGE_SCENE = 140202, //master通知gate玩家改变场景 47 | }; 48 | 49 | enum Game_DB_Message { 50 | SYNC_GAME_DB_LOAD_PLAYER = 150001, //加载玩家信息 51 | SYNC_GAME_DB_CREATE_PLAYER = 150002, //创建玩家 52 | SYNC_GAME_DB_SAVE_PLAYER = 150003, //保存玩家信息 53 | SYNC_DB_GAME_LOAD_PLAYER = 550001, //加载玩家信息(返回) 54 | SYNC_DB_GAME_CREATE_PLAYER = 550002, //创建玩家(返回) 55 | SYNC_DB_GAME_SAVE_PLAYER = 550003, //保存玩家信息(返回) 56 | }; 57 | 58 | enum Master_DB_Message { 59 | SYNC_MASTER_DB_CREATE_GUILD = 150100, //创建公会 60 | SYNC_MASTER_DB_LOAD_DATA = 150101, //加载数据库信息 61 | SYNC_MASTER_DB_DELETE_DATA = 150102, //删除数据库信息 62 | SYNC_MASTER_DB_SAVE_GUILD = 150103, //保存公会信息 63 | SYNC_MASTER_DB_SAVE_OFFLINE = 150104, //保存离线信息 64 | SYNC_MASTER_DB_SAVE_RANK = 150105, //保存排行榜信息 65 | SYNC_DB_MASTER_CREATE_GUILD = 550100, //创建公会(返回) 66 | SYNC_DB_MASTER_LOAD_GUILD = 550103, //加载公会信息 67 | SYNC_DB_MASTER_LOAD_OFFLINE = 550104, //加载离线信息 68 | SYNC_DB_MASTER_LOAD_RANK = 550105, //加载排行榜信息 69 | }; 70 | 71 | enum Game_Master_Message { 72 | SYNC_GAME_MASTER_SCENE_INFO = 160000, //game通知master场景信息 73 | }; 74 | 75 | enum Active_Message { 76 | ACTIVE_DISCONNECT = 300000, //服务器主动断开连接 77 | }; 78 | 79 | #endif /* MESSAGE_H_ */ 80 | -------------------------------------------------------------------------------- /misc/Msg_Manager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Msg_Manager.cpp 3 | * 4 | * Created on: Aug 4, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log.h" 9 | #include "Common_Func.h" 10 | #include "Server_Config.h" 11 | #include "Msg_Manager.h" 12 | 13 | Msg_Manager::Msg_Manager(void) : 14 | msg_struct_id_map_(get_hash_table_size(512)), 15 | msg_struct_name_map_(get_hash_table_size(512)) { } 16 | 17 | Msg_Manager::~Msg_Manager(void) { } 18 | 19 | Msg_Manager *Msg_Manager::instance_; 20 | 21 | Msg_Manager *Msg_Manager::instance(void) { 22 | if (instance_ == 0) 23 | instance_ = new Msg_Manager; 24 | return instance_; 25 | } 26 | 27 | int Msg_Manager::init(void) { 28 | const Json::Value &server_misc = SERVER_CONFIG->server_misc(); 29 | if (server_misc == Json::Value::null) { 30 | LOG_FATAL("msg_struct init, server_misc null"); 31 | return -1; 32 | } 33 | 34 | load_struct(server_misc["msg_struct_path"].asString().c_str(), MSG_STRUCT, msg_struct_id_map_, msg_struct_name_map_); 35 | load_struct(server_misc["game_db_struct_path"].asString().c_str(), MSG_STRUCT, msg_struct_id_map_, msg_struct_name_map_); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /misc/Msg_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Msg_Manager.h 3 | * 4 | * Created on: Aug 4, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MSG_MANAGER_H_ 9 | #define MSG_MANAGER_H_ 10 | 11 | #include "Msg_Struct.h" 12 | #include "Public_Struct.h" 13 | 14 | class Msg_Manager { 15 | public: 16 | static Msg_Manager *instance(void); 17 | 18 | int init(void); 19 | 20 | inline Struct_Id_Map& msg_struct_id_map() { return msg_struct_id_map_; } 21 | inline Struct_Name_Map& msg_struct_name_map() { return msg_struct_name_map_; } 22 | 23 | private: 24 | Msg_Manager(void); 25 | virtual ~Msg_Manager(void); 26 | Msg_Manager(const Msg_Manager &); 27 | const Msg_Manager &operator=(const Msg_Manager &); 28 | 29 | private: 30 | static Msg_Manager *instance_; 31 | 32 | Struct_Id_Map msg_struct_id_map_; 33 | Struct_Name_Map msg_struct_name_map_; 34 | }; 35 | 36 | #define MSG_MANAGER Msg_Manager::instance() 37 | 38 | #endif /* MSG_MANAGER_H_ */ 39 | -------------------------------------------------------------------------------- /misc/Msg_Struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Msg_Struct.h 3 | * 4 | * Created on: Aug 2, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef MSG_STRUCT_H_ 9 | #define MSG_STRUCT_H_ 10 | 11 | #include "include/v8.h" 12 | #include "json/json.h" 13 | #include "Base_Struct.h" 14 | 15 | using namespace v8; 16 | 17 | class Msg_Struct: public Base_Struct { 18 | public: 19 | Msg_Struct(Xml &xml, TiXmlNode *node); 20 | virtual ~Msg_Struct(); 21 | 22 | v8::Local build_http_msg_object(Isolate* isolate, int cid, int msg_id, const Json::Value &value); 23 | void build_http_msg_buffer(Isolate* isolate, v8::Local object, std::string &str); 24 | 25 | v8::Local build_msg_object(Isolate* isolate, int cid, int player_cid, int msg_id, int status, Block_Buffer &buffer); 26 | void build_msg_buffer(Isolate* isolate, v8::Local object, Block_Buffer &buffer); 27 | 28 | private: 29 | v8::Local build_object_arg(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate); 30 | v8::Local build_object_vector(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate); 31 | v8::Local build_object_map(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate); 32 | v8::Local build_object_struct(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate); 33 | 34 | void build_buffer_arg(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate, v8::Local value); 35 | void build_buffer_vector(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate, v8::Local value); 36 | void build_buffer_map(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate, v8::Local value); 37 | void build_buffer_struct(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate, v8::Local value); 38 | }; 39 | 40 | #endif /* MSG_STRUCT_H_ */ 41 | -------------------------------------------------------------------------------- /misc/Player.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Player.cpp 3 | * 4 | * Created on: Sep 7, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Player.h" 9 | 10 | Player::Player(void): 11 | gate_cid_(0), 12 | game_cid_(0), 13 | player_cid_(0), 14 | role_id_(0), 15 | account_("") 16 | { } 17 | 18 | Player::~Player(void) { } 19 | 20 | void Player::reset(void) { 21 | gate_cid_ = 0; 22 | game_cid_ = 0; 23 | player_cid_ = 0; 24 | role_id_ = 0; 25 | account_.clear(); 26 | } 27 | 28 | int Player::tick(Time_Value &now) { 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /misc/Player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Player.h 3 | * 4 | * Created on: Sep 7, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef PLAYER_H_ 9 | #define PLAYER_H_ 10 | 11 | #include "Public_Struct.h" 12 | 13 | class Player { 14 | public: 15 | Player(void); 16 | virtual ~Player(void); 17 | 18 | inline void set_gate_cid(int gate_cid) { gate_cid_ = gate_cid; } 19 | inline int gate_cid(void) { return gate_cid_; } 20 | 21 | inline void set_game_cid(int game_cid) { game_cid_ = game_cid; } 22 | inline int game_cid(void) { return game_cid_; } 23 | 24 | inline void set_player_cid(int player_cid) { player_cid_ = player_cid; } 25 | inline int player_cid(void) { return player_cid_; } 26 | 27 | inline void set_role_id(int64_t role_id) { role_id_ = role_id; } 28 | inline int64_t role_id(void) { return role_id_; } 29 | 30 | inline void set_account(std::string &account) { account_ = account; } 31 | inline std::string account(void) { return account_; } 32 | 33 | virtual void reset(void); 34 | virtual int tick(Time_Value &now); 35 | 36 | private: 37 | int gate_cid_; //gate连接game的cid 38 | int game_cid_; //game连接master的cid 39 | int player_cid_; //player连接gate的cid 40 | int64_t role_id_; //player role_id 41 | std::string account_; //player account 42 | }; 43 | 44 | #endif /* PLAYER_H_ */ 45 | -------------------------------------------------------------------------------- /misc/Server_Config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Server_Config.cpp 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Server_Config.h" 9 | 10 | Server_Config::Server_Config(void) { } 11 | 12 | Server_Config::~Server_Config(void) { } 13 | 14 | Server_Config * Server_Config::instance_; 15 | 16 | Server_Config *Server_Config::instance() { 17 | if (! instance_) 18 | instance_ = new Server_Config; 19 | return instance_; 20 | } 21 | 22 | void Server_Config::load_server_config(void) { 23 | GUARD_WRITE(Config_Lock, mon, server_config_.lock); 24 | { 25 | std::string path("./config/server/server_conf.json"); 26 | load_json_file(path.c_str(), server_config_.server_conf.get_cur_json()); 27 | } 28 | { 29 | std::string path("./config/server/server_misc.json"); 30 | load_json_file(path.c_str(), server_config_.server_misc.get_cur_json()); 31 | } 32 | } 33 | 34 | const Json::Value &Server_Config::server_conf(void) { 35 | return get_json_value_with_rlock(server_config_.lock, server_config_.server_conf); 36 | } 37 | 38 | const Json::Value &Server_Config::server_misc(void) { 39 | return get_json_value_with_rlock(server_config_.lock, server_config_.server_misc); 40 | } 41 | -------------------------------------------------------------------------------- /misc/Server_Config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Server_Config.h 3 | * 4 | * Created on: Mar 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef SERVER_CONFIG_H_ 9 | #define SERVER_CONFIG_H_ 10 | 11 | #include "Config.h" 12 | 13 | class Server_Config : public Config { 14 | public: 15 | struct Server_Config_Entry { 16 | Config_Entry server_conf; 17 | Config_Entry server_misc; 18 | 19 | Config_Lock lock; 20 | }; 21 | 22 | static Server_Config *instance(); 23 | 24 | /// server 25 | void load_server_config(void); 26 | const Json::Value &server_conf(void); 27 | const Json::Value &server_misc(void); 28 | 29 | private: 30 | Server_Config(void); 31 | virtual ~Server_Config(void); 32 | Server_Config(const Server_Config &); 33 | const Server_Config &operator=(const Server_Config &); 34 | 35 | private: 36 | static Server_Config *instance_; 37 | Server_Config_Entry server_config_; 38 | }; 39 | 40 | #define SERVER_CONFIG Server_Config::instance() 41 | 42 | #endif /* SERVER_CONFIG_H_ */ 43 | -------------------------------------------------------------------------------- /v8/Entity_Wrap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Entity_Wrap.h 3 | * 4 | * Created on: Sep 3, 2016 5 | * Author: lijunliang 6 | */ 7 | 8 | #ifndef ENTITY_WRAP_H_ 9 | #define ENTITY_WRAP_H_ 10 | 11 | #include "include/v8.h" 12 | #include "Scene_Entity.h" 13 | 14 | using namespace v8; 15 | 16 | Local wrap_scene_entity(Isolate* isolate, Scene_Entity *entity); 17 | Scene_Entity *unwrap_scene_entity(Local obj); 18 | 19 | void enter_scene(const FunctionCallbackInfo& args); 20 | void update_position(const FunctionCallbackInfo& args); 21 | void leave_scene(const FunctionCallbackInfo& args); 22 | void get_aoi_list(const FunctionCallbackInfo& args); 23 | void broadcast_msg_to_all(const FunctionCallbackInfo& args); 24 | void broadcast_msg_to_all_without_self(const FunctionCallbackInfo& args); 25 | void reclaim_scene_entity(const FunctionCallbackInfo& args); 26 | 27 | void get_entity_x(Local property, const PropertyCallbackInfo& args); 28 | void set_entity_x(Local property, Local value, const PropertyCallbackInfo& args); 29 | void get_entity_y(Local property, const PropertyCallbackInfo& args); 30 | void set_entity_y(Local property, Local value, const PropertyCallbackInfo& args); 31 | void get_entity_z(Local property, const PropertyCallbackInfo& args); 32 | void set_entity_z(Local property, Local value, const PropertyCallbackInfo& args); 33 | void get_scene_id(Local property, const PropertyCallbackInfo& args); 34 | void set_scene_id(Local property, Local value, const PropertyCallbackInfo& args); 35 | 36 | #endif /* ENTITY_WRAP_H_ */ 37 | -------------------------------------------------------------------------------- /v8/V8_Manager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * V8_Manager.cpp 3 | * 4 | * Created on: Jan 22, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #include "Log.h" 9 | #include "V8_Manager.h" 10 | #include "V8_Wrap.h" 11 | #include "Server_Config.h" 12 | #include "Public_Struct.h" 13 | 14 | V8_Manager::V8_Manager(void):platform_(nullptr), isolate_(nullptr) { } 15 | 16 | V8_Manager::~V8_Manager(void) { 17 | fini(); 18 | } 19 | 20 | int V8_Manager::init(const char *server_path) { 21 | //初始化V8 22 | V8::InitializeICU(); 23 | V8::InitializeExternalStartupData(""); 24 | platform_ = platform::CreateDefaultPlatform(); 25 | V8::InitializePlatform(platform_); 26 | V8::Initialize(); 27 | 28 | //初始化v8虚拟机 29 | ArrayBufferAllocator allocator; 30 | Isolate::CreateParams create_params; 31 | create_params.array_buffer_allocator = &allocator; 32 | isolate_ = Isolate::New(create_params); 33 | //进入v8的Isolate内部,才能使用V8引擎 34 | Isolate::Scope isolate_scope(isolate_); 35 | //创建V8执行环境 36 | HandleScope handle_scope(isolate_); 37 | Local context = Create_Context(isolate_); 38 | context_.Reset(isolate_, context); 39 | //进入V8执行环境内部 40 | Context::Scope context_scope(context); 41 | //根据不同的服务器加载不同的脚本 42 | const Json::Value &server_misc = SERVER_CONFIG->server_misc(); 43 | Run_Script(isolate_, server_misc[server_path].asCString()); 44 | return 0; 45 | } 46 | 47 | int V8_Manager::fini(void) { 48 | //释放V8资源 49 | isolate_->LowMemoryNotification(); 50 | context_.Reset(); 51 | V8::Dispose(); 52 | V8::ShutdownPlatform(); 53 | delete platform_; 54 | return 0; 55 | } 56 | 57 | ////////////////////////////////////////////////////////////////////////////////////////// 58 | Game_V8_Manager::Game_V8_Manager(void) { } 59 | 60 | Game_V8_Manager::~Game_V8_Manager(void) { } 61 | 62 | Game_V8_Manager *Game_V8_Manager::instance_ = 0; 63 | 64 | Game_V8_Manager *Game_V8_Manager::instance(void) { 65 | if (! instance_) 66 | instance_ = new Game_V8_Manager; 67 | return instance_; 68 | } 69 | 70 | void Game_V8_Manager::run_handler(void) { 71 | init("game_server_path"); 72 | } 73 | 74 | ////////////////////////////////////////////////////////////////////////////////////////// 75 | Master_V8_Manager::Master_V8_Manager(void) { } 76 | 77 | Master_V8_Manager::~Master_V8_Manager(void) { } 78 | 79 | Master_V8_Manager *Master_V8_Manager::instance_ = 0; 80 | 81 | Master_V8_Manager *Master_V8_Manager::instance(void) { 82 | if (! instance_) 83 | instance_ = new Master_V8_Manager; 84 | return instance_; 85 | } 86 | 87 | void Master_V8_Manager::run_handler(void) { 88 | //多进程模式开启master v8虚拟机 89 | if (SERVER_CONFIG->server_misc()["server_type"].asInt() == MULTI_PROCESS) { 90 | init("master_server_path"); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /v8/V8_Manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * V8_Manager.h 3 | * 4 | * Created on: Jan 22, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef V8_MANAGER_H_ 9 | #define V8_MANAGER_H_ 10 | 11 | #include 12 | #include "include/v8.h" 13 | #include "include/libplatform/libplatform.h" 14 | #include "Thread.h" 15 | 16 | using namespace v8; 17 | 18 | class Block_Buffer; 19 | class ArrayBufferAllocator : public ArrayBuffer::Allocator { 20 | public: 21 | virtual void* Allocate(size_t length) { 22 | void* data = AllocateUninitialized(length); 23 | return data == NULL ? data : memset(data, 0, length); 24 | } 25 | virtual void* AllocateUninitialized(size_t length) { return malloc(length); } 26 | virtual void Free(void* data, size_t) { free(data); } 27 | }; 28 | 29 | class V8_Manager: public Thread { 30 | public: 31 | V8_Manager(void); 32 | virtual ~V8_Manager(void); 33 | 34 | int init(const char *server_path); 35 | int fini(void); 36 | 37 | private: 38 | static V8_Manager *instance_; 39 | Platform* platform_; 40 | Isolate* isolate_; 41 | Global context_; 42 | }; 43 | 44 | /////////////////////////////////////////////////////////////////////////////// 45 | class Game_V8_Manager: public V8_Manager { 46 | public: 47 | static Game_V8_Manager *instance(void); 48 | virtual void run_handler(void); 49 | 50 | private: 51 | Game_V8_Manager(void); 52 | virtual ~Game_V8_Manager(void); 53 | Game_V8_Manager(const Game_V8_Manager &); 54 | const Game_V8_Manager &operator=(const Game_V8_Manager &); 55 | 56 | private: 57 | static Game_V8_Manager *instance_; 58 | }; 59 | 60 | #define GAME_V8_MANAGER Game_V8_Manager::instance() 61 | 62 | ////////////////////////////////////////////////////////////////////////////////////// 63 | class Master_V8_Manager: public V8_Manager { 64 | public: 65 | static Master_V8_Manager *instance(void); 66 | virtual void run_handler(void); 67 | 68 | private: 69 | Master_V8_Manager(void); 70 | virtual ~Master_V8_Manager(void); 71 | Master_V8_Manager(const Master_V8_Manager &); 72 | const Master_V8_Manager &operator=(const Master_V8_Manager &); 73 | 74 | private: 75 | static Master_V8_Manager *instance_; 76 | }; 77 | 78 | #define MASTER_V8_MANAGER Master_V8_Manager::instance() 79 | 80 | #endif /* V8_MANAGER_H_ */ 81 | -------------------------------------------------------------------------------- /v8/V8_Wrap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * V8_Wrap.h 3 | * 4 | * Created on: Feb 1, 2016 5 | * Author: zhangyalei 6 | */ 7 | 8 | #ifndef V8_WRAP_H_ 9 | #define V8_WRAP_H_ 10 | 11 | #include "include/v8.h" 12 | 13 | using namespace v8; 14 | 15 | Local Create_Context(Isolate* isolate); 16 | const char* ToCString(const String::Utf8Value& value); 17 | int Run_Script(Isolate* isolate, const char* file_path); 18 | MaybeLocal Read_File(Isolate* isolate, const char* file_path); 19 | void Report_Exception(Isolate* isolate, TryCatch* handler, const char* file_path = NULL); 20 | 21 | //函数说明:引用js文件 参数:1,文件路径 返回值:无 22 | void require(const FunctionCallbackInfo& args); 23 | //函数说明:读取json配置文件 参数:1,文件路径 返回值:文件内容字符串对象 24 | void read_json(const FunctionCallbackInfo& args); 25 | //函数说明:打印参数到控制台 参数:可变参数列表 返回值:无 26 | void print(const FunctionCallbackInfo& args); 27 | //函数说明:程序睡眠Time_Value(0,100) 参数:无 返回值:无 28 | void sleep(const FunctionCallbackInfo& args); 29 | 30 | #endif /* V8_WRAP_H_ */ 31 | --------------------------------------------------------------------------------