🔧 build: 调整库
parent
b6fc0646f7
commit
35676fc89b
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,165 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local N = 1
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
print("memory:", w:memory())
|
||||||
|
|
||||||
|
w:register{
|
||||||
|
name = "vector",
|
||||||
|
"x:float",
|
||||||
|
"y:float",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register{
|
||||||
|
name = "mark",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register{
|
||||||
|
name = "id",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register{
|
||||||
|
name = "object",
|
||||||
|
type = "lua",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:new{
|
||||||
|
object = "Hello",
|
||||||
|
}
|
||||||
|
|
||||||
|
local t = {}
|
||||||
|
for i = 1, N do
|
||||||
|
w:new{
|
||||||
|
vector = {
|
||||||
|
x = 1,
|
||||||
|
y = 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
t[i] = {
|
||||||
|
x = 1,
|
||||||
|
y = 2,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
local function swap_c()
|
||||||
|
for v in w:select "vector:update" do
|
||||||
|
local vec = v.vector
|
||||||
|
vec.x, vec.y = vec.y, vec.x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function swap_lua()
|
||||||
|
for _, v in ipairs(t) do
|
||||||
|
v.x, v.y = v.y, v.x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function timing(f)
|
||||||
|
local c = os.clock()
|
||||||
|
for i = 1, 100 do
|
||||||
|
f()
|
||||||
|
end
|
||||||
|
return os.clock() - c
|
||||||
|
end
|
||||||
|
|
||||||
|
print("memory:", w:memory())
|
||||||
|
|
||||||
|
print("CSWAP", timing(swap_c))
|
||||||
|
print("LUASWAP", timing(swap_lua))
|
||||||
|
|
||||||
|
w:new{
|
||||||
|
vector = {
|
||||||
|
x = 3,
|
||||||
|
y = 4,
|
||||||
|
},
|
||||||
|
id = 100,
|
||||||
|
}
|
||||||
|
|
||||||
|
table.insert(t, {
|
||||||
|
x = 3,
|
||||||
|
y = 4,
|
||||||
|
})
|
||||||
|
|
||||||
|
w:new{
|
||||||
|
vector = {
|
||||||
|
x = 5,
|
||||||
|
y = 6,
|
||||||
|
},
|
||||||
|
mark = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
table.insert(t, {
|
||||||
|
x = 5,
|
||||||
|
y = 6,
|
||||||
|
})
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
local context = w:context{"vector", "mark", "id"}
|
||||||
|
local test = require "ecs.ctest"
|
||||||
|
local function csum()
|
||||||
|
return test.sum(context)
|
||||||
|
end
|
||||||
|
print("csum = ", csum())
|
||||||
|
|
||||||
|
local function luasum()
|
||||||
|
local s = 0
|
||||||
|
for v in w:select "vector:in" do
|
||||||
|
s = s + v.vector.x + v.vector.y
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
print("luasum = ", luasum())
|
||||||
|
|
||||||
|
local function luanativesum()
|
||||||
|
local s = 0
|
||||||
|
for _, v in ipairs(t) do
|
||||||
|
s = s + v.x + v.y
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
print("lnative sum = ", luanativesum())
|
||||||
|
|
||||||
|
print("CSUM", timing(csum))
|
||||||
|
print("LUASUM", timing(luasum))
|
||||||
|
print("LNATIVESUM", timing(luanativesum))
|
||||||
|
|
||||||
|
print "vector:update"
|
||||||
|
for v in w:select "vector:update" do
|
||||||
|
local vec = v.vector
|
||||||
|
print(vec.x, vec.y)
|
||||||
|
vec.x, vec.y = vec.y, vec.x
|
||||||
|
end
|
||||||
|
|
||||||
|
print "vector:in id?out"
|
||||||
|
for v in w:select "vector:in id?out" do
|
||||||
|
print(v.vector.x, v.vector.y, v.id)
|
||||||
|
if v.id then
|
||||||
|
v.id = 200
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print "vector:in id:in"
|
||||||
|
|
||||||
|
for v in w:select "vector:in id:in" do
|
||||||
|
print(v.vector.x, v.vector.y, v.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
print "object:update"
|
||||||
|
|
||||||
|
for v in w:select "object:update" do
|
||||||
|
print(v.object)
|
||||||
|
v.object = v.object .. " world"
|
||||||
|
end
|
||||||
|
|
||||||
|
print "object:in"
|
||||||
|
|
||||||
|
for v in w:select "object:in" do
|
||||||
|
print(v.object)
|
||||||
|
end
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
local socket = require 'socket'
|
||||||
|
local crypt = require "skynet.crypt"
|
||||||
|
|
||||||
|
local function encode_token(token)
|
||||||
|
return string.format("%s@%s:%s:%s", crypt.base64encode(token.user), crypt.base64encode(token.worldId),
|
||||||
|
crypt.base64encode(token.pass), crypt.base64encode(token.pf))
|
||||||
|
end
|
||||||
|
|
||||||
|
local token = {
|
||||||
|
pf = '1010',
|
||||||
|
user = 'teefe',
|
||||||
|
pwd = '123456789',
|
||||||
|
worldId = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(login_host, login_port)
|
||||||
|
local sock = socket.connect(login_host or '127.0.0.1', login_port or 9527)
|
||||||
|
local challenge = crypt.base64decode(sock:receive('*l'))
|
||||||
|
local clientkey = crypt.randomkey()
|
||||||
|
sock:send(crypt.base64encode(crypt.dhexchange(clientkey)) .. '\n')
|
||||||
|
|
||||||
|
local line = crypt.base64decode(sock:receive '*l')
|
||||||
|
local secret = crypt.dhsecret(line, clientkey)
|
||||||
|
|
||||||
|
local hmac = crypt.hmac64(challenge, secret)
|
||||||
|
-- 5. 回应服务器的握手挑战码,确认握手正常
|
||||||
|
sock:send(crypt.base64encode(hmac) .. '\n')
|
||||||
|
|
||||||
|
-- 6. DES加密发送 token串
|
||||||
|
local etoken = crypt.desencode(secret, encode_token(token))
|
||||||
|
sock:send(crypt.base64encode(etoken) .. '\n')
|
||||||
|
-- 服务器解密后调用定制的auth和login handler处理客户端请求
|
||||||
|
|
||||||
|
-- 7. 从服务器读取 登录结果: 状态码 和subid
|
||||||
|
local result = sock:receive '*l'
|
||||||
|
local code = tonumber(string.sub(result, 1, 3))
|
||||||
|
assert(code == 200, "auth error:" .. tostring(code))
|
||||||
|
sock:close()
|
||||||
|
|
||||||
|
local substr = crypt.base64decode(string.sub(result, 5))
|
||||||
|
local ip, port, uid, subid = substr:match("([^:]+):([^:]+)@([^@]+)@(.+)")
|
||||||
|
return {
|
||||||
|
ip = ip,
|
||||||
|
port = port,
|
||||||
|
uid = uid,
|
||||||
|
worldId = token.worldId,
|
||||||
|
secret = secret,
|
||||||
|
subid = subid,
|
||||||
|
}
|
||||||
|
end
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
assert(_VERSION == "Lua 5.4")
|
||||||
|
|
||||||
|
package.path = "lualib/?.lua;skynet/lualib/?.lua;simulation/?.lua"
|
||||||
|
package.cpath = "skynet/luaclib/?.so;luaclib/?.so"
|
||||||
|
|
||||||
|
local auth = require "auth"
|
||||||
|
local ok, atoken = pcall(auth)
|
||||||
|
if not ok then
|
||||||
|
print("please check: login auth err", atoken)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local termfx = require "termfx"
|
||||||
|
local ui = require "simpleui"
|
||||||
|
|
||||||
|
termfx.init()
|
||||||
|
termfx.inputmode(termfx.input.ALT + termfx.input.MOUSE)
|
||||||
|
termfx.outputmode(termfx.output.COL256)
|
||||||
|
|
||||||
|
local function main(token)
|
||||||
|
local sock = require ("network")()
|
||||||
|
local sok = sock:connect(token.ip, token.port)
|
||||||
|
assert(sok)
|
||||||
|
|
||||||
|
local output = {}
|
||||||
|
|
||||||
|
local mok, err = pcall(function()
|
||||||
|
local msg_list = {}
|
||||||
|
local input
|
||||||
|
local string = {}
|
||||||
|
|
||||||
|
while true do
|
||||||
|
termfx.clear(termfx.color.WHITE, termfx.color.BLACK)
|
||||||
|
|
||||||
|
local w, h = termfx.size()
|
||||||
|
local width = math.floor(w / 2)
|
||||||
|
|
||||||
|
ui.box(2, 2, width, h - 10)
|
||||||
|
ui.box(w / 2, 2, width, h - 10)
|
||||||
|
|
||||||
|
termfx.printat(2, h - 1, input)
|
||||||
|
termfx.printat(2, h, table.concat(string, ""))
|
||||||
|
termfx.present()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local evt = termfx.pollevent(100)
|
||||||
|
if evt then
|
||||||
|
if evt.type == 'key' then
|
||||||
|
if evt.key == termfx.key.CTRL_C then
|
||||||
|
break
|
||||||
|
elseif evt.key == termfx.key.BACKSPACE2 then
|
||||||
|
table.remove(string)
|
||||||
|
elseif evt.key == termfx.key.ENTER then
|
||||||
|
input = table.concat(string, '')
|
||||||
|
string = {}
|
||||||
|
pcall(act_when_input, input)
|
||||||
|
elseif evt.key == termfx.key.SPACE then
|
||||||
|
table.insert(string, ' ')
|
||||||
|
else
|
||||||
|
table.insert(string, evt.char)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local success, err = sock:update()
|
||||||
|
assert(success, err)
|
||||||
|
|
||||||
|
local r, msgid, msg = recv_msg()
|
||||||
|
if r == true then
|
||||||
|
table.insert(msg_list, {MN[msgid] or msgid, inspect(msg)})
|
||||||
|
elseif r == false then
|
||||||
|
p('网络断开,结束客户端', msgid)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
termfx.shutdown()
|
||||||
|
if not mok then
|
||||||
|
print("Error: ", err)
|
||||||
|
end
|
||||||
|
print(':\n', table.concat(output, '\n'))
|
||||||
|
end
|
||||||
|
|
||||||
|
main(atoken)
|
||||||
Loading…
Reference in New Issue