Skip to content

Commit

Permalink
1.update skynet submodule 2.update gateserver 3.fix shared_memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzhu1990 committed Aug 16, 2017
1 parent 579ef21 commit b231b9d
Show file tree
Hide file tree
Showing 41 changed files with 1,135 additions and 186 deletions.
8 changes: 8 additions & 0 deletions config/config_db
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---------------------------public-----------------------
include("config_public")
---------------------------------------------------------

start = "main"
logfile = "db.log"
debugPort = 7001
luaservice = luaservice .. "./service/dbserver/?.lua;"
4 changes: 3 additions & 1 deletion config/config_gate
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ include("config_public")
start = "main"
logfile = "gate.log"
debugPort = 7001
luaservice = luaservice .. "./service/gateserver/?.lua;"
luaservice = luaservice .. "./service/gateserver/?.lua;"

websocket_test = true
52 changes: 36 additions & 16 deletions logic/common/dbmgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ local skynet = require "skynet"
local redis = require "skynet.db.redis"
local mysql = require "skynet.db.mysql"
local mongo = require "skynet.db.mongo"


local db_config = require "config.db_config"
local mysql_config = require "config.mysql_config"
local redis_config = require "config.redis_config"
local dbmgr = {}

local _redisdb = nil
local _mongodb = nil
local _mysqldb = nil
Expand All @@ -18,24 +20,42 @@ function dbmgr.get_redis_db()
return _redisdb
end

function dbmgr.get_mongo_db()
return _mongodb
end

function dbmgr.init_mysql_db(conf)
_mysqldb = mysql.connect(conf)
assert(_mysqldb, string.format("mysql connect %s:%d failed", conf.host, conf.port))
db:query("set charset utf8")
function dbmgr.init_mysql_db(mysql_id)
local conf = mysql_config[mysql_id]
if not conf then
return
end
local mysql_conf = {
host = conf.mysql_host,
port = conf.mysql_port,
database = conf.mysql_database,
user = conf.mysql_user,
password = conf.mysql_password,
max_packet_size = conf.mysql_max_packet_size
}
_mysqldb = mysql.connect(mysql_conf)
assert(_mysqldb, string.format("mysql connect %s:%d failed", mysql_conf.host, mysql_conf.port))
_mysqldb:query("set charset utf8")
end

function dbmgr.init_redis_db(conf)
_redisdb = redis.connect(conf)
assert(_redisdb, string.format("redis connect %s:%d failed", conf.host, conf.port))
function dbmgr.init_redis_db(redis_id)
local conf = redis_config[redis_id]
if not conf then
return
end
local redis_conf = {
host = conf.redis_host,
port = conf.redis_port,
db = conf.redis_db
}
_redisdb = redis.connect(redis_conf)
assert(_redisdb, string.format("redis connect %s:%d failed", redis_conf.host, redis_conf.port))
end

function dbmgr.init_mongo_db(conf)
_mongodb = mongo.connect(conf)
assert(_mongodb, string.format("mongo connect %s:%d failed", conf.host, conf.port))
function dbmgr.init(svc_name)
local conf = assert(db_config[svc_name], "not found svc_name")
dbmgr.init_mysql_db(conf.mysql_id)
dbmgr.init_redis_db(conf.redis_id)
end

return dbmgr
2 changes: 1 addition & 1 deletion logic/common/role_base.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--
-- Author: Kuzhu1990
-- Date: 2013-12-16 18:52:11
-- Date: 2017-12-16 18:52:11
-- 有序的玩家类
--

Expand Down
47 changes: 47 additions & 0 deletions logic/config/db_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--[[
连接名, mysql连接id, redis连接id, service服务名, 连接个数, 注释
svc_name, mysql_id, redis_id, service_name, svc_count, desc
]]
local db_config = {
["unique_db"] = {
svc_name = [[unique_db]],
mysql_id = 1,
redis_id = 1,
service_name = ".unique_db",
svc_count = 1,
desc = [[该db用于惟一id, 玩家在线状态]],
},
["account_db"] = {
svc_name = [[account_db]],
mysql_id = 1,
redis_id = 0,
service_name = ".account_db",
svc_count = 4,
desc = [[该db用于登陆(查询tb_account, tb_player)]],
},
["lobby_db"] = {
svc_name = [[lobby_db]],
mysql_id = 1,
redis_id = 2,
service_name = ".lobby_db",
svc_count = 1,
desc = [[该db用于缓存玩家在线状态,玩家登录状态]],
},
["game_db"] = {
svc_name = [[game_db]],
mysql_id = 1,
redis_id = 3,
service_name = ".game_db",
svc_count = 1,
desc = [[该db用于游戏房间注册等]],
},
["agent_db"] = {
svc_name = [[agent_db]],
mysql_id = 1,
redis_id = 4,
service_name = ".agent_db",
svc_count = 4,
desc = [[该db用于缓存玩家数据(每个玩家id对应一个db)]],
},
}
return db_config
27 changes: 27 additions & 0 deletions logic/config/mysql_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--[[
mysql连接id, mysql地址, mysql端口, mysql库名, mysql用户名, mysql密码, mysql包最大长度, 注释
mysql_id, mysql_host, mysql_port, mysql_database, mysql_user, mysql_password, mysql_max_packet_size, desc
]]
local mysql_config = {
[1] = {
mysql_id = 1,
mysql_host = "127.0.0.1",
mysql_port = 3306,
mysql_database = "game",
mysql_user = "game",
mysql_password = "game!Zsq1214",
mysql_max_packet_size = 1048576,
desc = [[game库]],
},
[2] = {
mysql_id = 2,
mysql_host = "127.0.0.1",
mysql_port = 3306,
mysql_database = "gamelog",
mysql_user = "gamelog",
mysql_password = "gamelog!Zsq1214",
mysql_max_packet_size = 1048576,
desc = [[gamelog库]],
},
}
return mysql_config
47 changes: 47 additions & 0 deletions logic/config/redis_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--[[
redis连接id, redis地址, redis端口, redis库名, redis验证, 注释
redis_id, redis_host, redis_port, redis_db, redis_auth, desc
]]
local redis_config = {
[1] = {
redis_id = 1,
redis_host = "127.0.0.1",
redis_port = 6379,
redis_db = 0,
redis_auth = nil,
desc = [[全局数据缓存]],
},
[2] = {
redis_id = 2,
redis_host = "127.0.0.1",
redis_port = 6379,
redis_db = 1,
redis_auth = nil,
desc = [[lobby数据缓存]],
},
[3] = {
redis_id = 3,
redis_host = "127.0.0.1",
redis_port = 6379,
redis_db = 2,
redis_auth = nil,
desc = [[game数据缓存]],
},
[4] = {
redis_id = 4,
redis_host = "127.0.0.1",
redis_port = 6379,
redis_db = 3,
redis_auth = nil,
desc = [[玩家数据缓存]],
},
[5] = {
redis_id = 5,
redis_host = "127.0.0.1",
redis_port = 6379,
redis_db = 4,
redis_auth = nil,
desc = [[gamelog数据缓存]],
},
}
return redis_config
96 changes: 88 additions & 8 deletions logic/gate/client_msg.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,98 @@
local skynet = require "skynet"

local json = require "json"
local proto_map = require "proto_map"
local cluster_monitor = require "cluster_monitor"
local context = require "context"
local gate_mgr = require "gate.gate_mgr"
local sproto_helper = require "sproto_helper"
local client_msg = {}

function client_msg.dispatch(fd, msg)
local ok, msg = xpcall(function()

end, debug.traceback)
if not ok then
function client_msg.get_context(c)
local ctx = {}
ctx.gate = cluster_monitor.get_current_nodename()
ctx.watchdog = skynet.self()
ctx.fd = c.fd
ctx.ip = c.ip
ctx.session = c.session
return ctx
end

function client_msg.dispatch(c, header, msg)
if not header or not header.protoid then
return
end

local proto = proto_map.protos[header.protoid]
if not proto then
header.errorcode = SystemError.unknow_proto
client_msg.send(c.fd, header)
return
end
print("dispatch proto=", table.tostring(proto))

if proto.type ~= PROTO_TYPE.C2S then
header.errorcode = SystemError.invalid_proto
client_msg.send(c.fd, header)
return
end

if proto.service and proto.service ~= SERVICE.AUTH and not c.auth_ok then
header.errorcode = SystemError.no_auth_account
client_msg.send(c.fd, header)
return
end

if proto.server == SERVER.GAME and not header.roomproxy then
header.errorcode = SystemError.unknow_roomproxy
client_msg.send(c.fd, header)
return
end

if not proto.service and not c.agentnode and not c.agentaddr then
header.errorcode = SystemError.no_login_game
client_msg.send(c.fd, header)
return
end

local rpc_err
local ctx = client_msg.get_context(c)

--非游戏服务
if proto.service then
if proto.service == SERVICE.ROOM then
rpc_err = context.rpc_call(header.roomproxy, SERVICE.ROOM, "dispatch_client_request", ctx, msg)
else
local target_node = cluster_monitor.get_cluster_node_by_server(proto.server)
if not target_node then
header.errorcode = SystemError.service_maintance
client_msg.send(c.fd, header)
return
end
rpc_err = context.rpc_call(target_node.nodename, proto.service, "dispatch_client_request", ctx, msg)
end

else
rpc_err = context.rpc_call(c.agentnode, c.agentaddr, "dispatch_client_request", ctx, msg)
end

if rpc_err ~= RPC_ERROR.OK then
header.errorcode = SystemError.service_stoped
client_msg.send(c.fd, header)
return
end
end

function client_msg.send(fd, ...)

function client_msg.send(fd, header, data)
if skynet.getenv("websocket_test") then
local j_packet = json.encode({header = header, data = data})
skynet.send(gate_mgr.get_gate(), "lua", "send_buffer", fd, j_packet, true)
return
end

local buffer = sproto_helper.pack(header, data)
if buffer then
skynet.send(gate_mgr.get_gate(), "lua", "send_buffer", fd, buffer)
end
end

return client_msg
11 changes: 9 additions & 2 deletions logic/gate/gate_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ local skynet = require "skynet"

local gate_mgr = {}
local gate
local iswebsocket
local connections = {}
local client_count = 0


function gate_mgr.get_gate()
return gate
end

function gate_mgr.is_websocket()
return iswebsocket
end

function gate_mgr.add_connection(fd, c)
connections[fd] = c
client_count = client_count + 1
Expand All @@ -21,10 +27,11 @@ function gate_mgr.get_connection(fd)
end

function gate_mgr.close_connection(fd)

skynet.send(gate, "lua", "kick", fd)
end

function gate_mgr.init(gatename)
function gate_mgr.init(gatename, isws)
iswebsocket = isws
gate = skynet.newservice(gatename)
end

Expand Down
Loading

0 comments on commit b231b9d

Please sign in to comment.