Commit 3a646deaf18d32a8152cfb93a5f45eaae9601cf0
1 parent
a7de05c2
拆分网关
Showing
4 changed files
with
34 additions
and
11 deletions
Show diff stats
robot/robot.conf
1 | root = "./" | 1 | root = "./" |
2 | -thread = 8 | 2 | +thread = 4 |
3 | logger = nil | 3 | logger = nil |
4 | harbor = 0 | 4 | harbor = 0 |
5 | -start = "start" -- main script | 5 | +start = "robot_main" -- main script |
6 | bootstrap = "snlua bootstrap" -- The service for bootstrap | 6 | bootstrap = "snlua bootstrap" -- The service for bootstrap |
7 | 7 | ||
8 | lua_path = root .."skynet/lualib/?.lua;"..root.."robot/?.lua;"..root.."src/?.lua" | 8 | lua_path = root .."skynet/lualib/?.lua;"..root.."robot/?.lua;"..root.."src/?.lua" |
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +local skynet = require "skynet" | ||
2 | +local config = require "robot_config" | ||
3 | + | ||
4 | +local preOnlineCount = 10 | ||
5 | + | ||
6 | + | ||
7 | +skynet.start(function() | ||
8 | + local need = math.ceil(config.online / preOnlineCount) | ||
9 | + local inpre = math.ceil(config.inpre / need) | ||
10 | + local idRange = math.ceil(config.max / need) | ||
11 | + local curId = 1 | ||
12 | + for i = 1, need do | ||
13 | + skynet.newservice("start", preOnlineCount, curId, curId + idRange - 1, inpre) | ||
14 | + curId = curId + idRange | ||
15 | + end | ||
16 | + skynet.exit() | ||
17 | +end) | ||
0 | \ No newline at end of file | 18 | \ No newline at end of file |
robot/start.lua
@@ -7,11 +7,17 @@ local netpack = require "skynet.netpack" | @@ -7,11 +7,17 @@ local netpack = require "skynet.netpack" | ||
7 | local socketdriver = require "skynet.socketdriver" | 7 | local socketdriver = require "skynet.socketdriver" |
8 | local string_format = string.format | 8 | local string_format = string.format |
9 | 9 | ||
10 | +local onlineCount, startId, endId, inpre = ... | ||
11 | +onlineCount = tonumber(onlineCount) | ||
12 | +startId = tonumber(startId) | ||
13 | +endId = tonumber(endId) | ||
14 | +inpre = tonumber(inpre) | ||
15 | + | ||
10 | local queue = {} | 16 | local queue = {} |
11 | local fd2serv = {} | 17 | local fd2serv = {} |
12 | local id2fd = {} | 18 | local id2fd = {} |
13 | local MSG = {} | 19 | local MSG = {} |
14 | -local id_max = 0 -- robot id | 20 | +local id_max = startId - 1 -- robot id |
15 | 21 | ||
16 | 22 | ||
17 | function MSG.open( ... ) | 23 | function MSG.open( ... ) |
@@ -76,9 +82,9 @@ local function add_robot() | @@ -76,9 +82,9 @@ local function add_robot() | ||
76 | local fd = socketdriver.connect(config.host, config.port) | 82 | local fd = socketdriver.connect(config.host, config.port) |
77 | socketdriver.start(fd) | 83 | socketdriver.start(fd) |
78 | local id | 84 | local id |
79 | - if id_max >= config.max then | 85 | + if id_max >= endId then |
80 | while true do | 86 | while true do |
81 | - id = math.randomInt(1, config.max) | 87 | + id = math.randomInt(startId, endId) |
82 | if not id2fd[id] then | 88 | if not id2fd[id] then |
83 | break | 89 | break |
84 | end | 90 | end |
@@ -89,7 +95,7 @@ local function add_robot() | @@ -89,7 +95,7 @@ local function add_robot() | ||
89 | end | 95 | end |
90 | fd2serv[fd] = {agent = robot, id = id} | 96 | fd2serv[fd] = {agent = robot, id = id} |
91 | id2fd[id] = fd | 97 | id2fd[id] = fd |
92 | - pcall(skynet.call, robot, "lua", "start", fd, id) | 98 | + pcall(skynet.call, robot, "lua", "start", fd, id, prefix) |
93 | log(string_format("login %s", fd2serv[fd].id)) | 99 | log(string_format("login %s", fd2serv[fd].id)) |
94 | 100 | ||
95 | -- 定时下线 | 101 | -- 定时下线 |
@@ -106,8 +112,8 @@ local function online() | @@ -106,8 +112,8 @@ local function online() | ||
106 | end | 112 | end |
107 | 113 | ||
108 | -- 及时补充人数 | 114 | -- 及时补充人数 |
109 | - if curCount < config.online then | ||
110 | - for i = curCount + 1, config.online do | 115 | + if curCount < onlineCount then |
116 | + for i = curCount + 1, onlineCount do | ||
111 | add_robot() | 117 | add_robot() |
112 | end | 118 | end |
113 | end | 119 | end |
@@ -119,8 +125,8 @@ end | @@ -119,8 +125,8 @@ end | ||
119 | local function start() | 125 | local function start() |
120 | log("start testing ...") | 126 | log("start testing ...") |
121 | 127 | ||
122 | - for i = 1, config.online, config.inpre do | ||
123 | - for j = 1, config.inpre do | 128 | + for i = 1, onlineCount, inpre do |
129 | + for j = 1, inpre do | ||
124 | add_robot() | 130 | add_robot() |
125 | end | 131 | end |
126 | skynet.sleep(100) | 132 | skynet.sleep(100) |
robot/unitTest/item.lua