inject d test/inject_fuck.lua  -- d 是服务的 handle

拿 simpledb.lua 举例,修改如下

 local skynet = require "skynet"
require "skynet.manager" -- import skynet.register
local db = {} local command = {} local i =
local str = "abc"
local t = {} function command.fuck()
return i, str, t
end skynet.info_func(function()
return command.fuck()
end) function command.GET(key)
return db[key]
end function command.SET(key, value)
local last = db[key]
db[key] = value
return last
end skynet.start(function()
skynet.dispatch("lua", function(session, address, cmd, ...)
local f = command[string.upper(cmd)]
if f then
skynet.ret(skynet.pack(f(...)))
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end)
skynet.register "SIMPLEDB"
end)

添加了 fuck 函数,并设置 info_func。

设置 info_func 主要用来在 debug console 中方便用 info d 来查看 inject 的结果

下面是 inject_fuck.lua

 if not _P then
print "inject error!"
return
end local function getuv(f)
local i =
local ups = {}
while true do
local name, value = debug.getupvalue(f, i)
if not name then
break
end
ups[name] = { i, value }
i = i +
end
return ups
end local function uvjoin(f, old_f, old_uv)
local i =
while true do
local name, value = debug.getupvalue(f, i)
if not name then
break
end
if old_uv[name] then
debug.upvaluejoin(f, i, old_f, old_uv[name][])
end
i = i +
end
end local command = _P.lua.command local func = command.fuck
local old_uv = getuv(func)
for k,v in pairs(old_uv) do
print(k,v[])
end print(" --- ") local t
local i
local str
function command.fuck()
return str, t, i
end uvjoin(command.fuck, func, old_uv)
-- debug.setupvalue( command.fuck, 1, 35 )

1. 取出原 fuck 函数的所有 upvalue

2. 然后关联到新的 fuck 函数

_P 内存的 key/value 其实就是 skynet.lua 里 14 行 proto 中的 name 和 dispatch 的 upvalue

 local c = require "skynet.core"
local tostring = tostring
local tonumber = tonumber
local coroutine = coroutine
local assert = assert
local pairs = pairs
local pcall = pcall local profile = require "profile" local coroutine_resume = profile.resume
local coroutine_yield = profile.yield local proto = {}
local skynet = {
-- read skynet.h
PTYPE_TEXT = ,
PTYPE_RESPONSE = ,
PTYPE_MULTICAST = ,
PTYPE_CLIENT = ,
PTYPE_SYSTEM = ,
PTYPE_HARBOR = ,
PTYPE_SOCKET = ,
PTYPE_ERROR = ,
PTYPE_QUEUE = , -- used in deprecated mqueue, use skynet.queue instead
PTYPE_DEBUG = ,
PTYPE_LUA = ,
PTYPE_SNAX = ,
} ---

再看 simpledb.lua 的例子,文件中 disptch 的 upvalue 有两个,command 和 skynet

所以 _P.lua.command 就是文件中的 command,_P.lua.skynet 就是 skynet

在 debug console 用 inject 命令时要格外小心。。

skynet inject address file.lua的更多相关文章

  1. skynet 控制台管理使用技巧

    skynet 自带了一个控制台服务.能够非常方便获取和调试 skynet 执行数据,并且能够热更新代码,所以.弄明确skynet控制台管理能够让你更好地使用skynet,甚至改进这个控制台服务.以满足 ...

  2. skynet debug console 使用

    预读 关于如何使用 skynet 可以参考 wiki 文档 更多实战内容见 https://www.lanqiao.cn/courses/2770 优惠码:2CZ2UA5u 环境测试搭建 使用示例代码 ...

  3. [转]Skynet之斗转星移 - 将控制权交给Lua

      Skynet之斗转星移 - 将控制权交给Lua http://www.outsky.org/code/skynet-lua.html Sep 7, 2014 在我看来,Skynet的一个重要优势是 ...

  4. 第6月第19天 lua动态链接库(luaopen_*函数的使用) skynet

    1. 给这个测试库取名为dylib,它包含一个函数add.lua中这样使用: local dylib = require "dylib.test"    local c = dyl ...

  5. skynet newservice API参考

    local skynet = require("skynet") skynet.start(start_func) c服务snlua启动后执行的第一个lua文件里面的主逻辑必定是s ...

  6. 白话skynet第一篇

    当你走过一个坐在自己店门前的杂货商面前.走过一个吸着烟斗的守门人面前,走过一个马车夫面前时,请你给我描绘一下这个杂货商.守门人和马车夫,他们的姿态,他们的外貌,要用画家那样的细节描绘出他们的精神本质, ...

  7. skynet1.0阅读笔记2_skynet的消息投递skynet.call

    为了了解 skynet.call 的调用过程,需要先看看 skynet的队列是如何把包分到不同工作线程的.看下图 查看 global_queue 的skynet_globalmq_push和skyne ...

  8. Skynet服务器框架(八) 任务和消息调度机制

    引言: 在我看来,消息和任务调度应该是skynet的核心,整个skynet框架的核心其实就是一个消息管理系统.在skynet中可以把每个功能都当做一个服务,整个skynet工程在执行过程中会创建很多个 ...

  9. skynet源码阅读<5>--协程调度模型

    注:为方便理解,本文贴出的代码部分经过了缩减或展开,与实际skynet代码可能会有所出入.    作为一个skynet actor,在启动脚本被加载的过程中,总是要调用skynet.start和sky ...

随机推荐

  1. C# 单例模式的五种写法

    1.简单实现           C#   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public sealed c ...

  2. 24、JSON与OC互相转化

    一. JSON: 1. 01.JSON是一种轻量级的数据格式,一般用于数据交互 02.服务器返回给客户端的数据,一般都是JSON格式活着XML格式(文件下载除外) JSON的格式很像OC中的字典和数组 ...

  3. 680. Valid Palindrome II

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  4. 2019.01.03 bzoj3456: 城市规划(生成函数+多项式取对)

    传送门 生成函数好题. 题意:求n个点的简单(无重边无自环)无向连通图数目 思路: 对简单无向图构造生成函数f(x)=∑n2Cn2xnn!f(x)=\sum_n2^{C_n^2}\frac{x^n}{ ...

  5. mysql学习之路_视图

    视图 视图:view是一种有结构的但是没有结构来源的虚拟表,虚拟表的结构来源不是自己定义的而是从对应的基表中产生(来源) 创建视图 基本语法: Create view 视图名字 as select 语 ...

  6. Ng第十二课:支持向量机(Support Vector Machines)(三)

    11 SMO优化算法(Sequential minimal optimization) SMO算法由Microsoft Research的John C. Platt在1998年提出,并成为最快的二次规 ...

  7. Write Markdown Syntax Online Document with Sphinx and Pandoc

    There is no doubt that we have to write doc while we are developing software. But How do you write d ...

  8. java读取excel文件(.xls,xlsx,csv)

    前提,maven工程通过poi读写excel文件,需要在pom.xml中配置依赖关系: 在<dependencies>中添加如下代码 <dependency> <grou ...

  9. Mybatis类型转换介绍

    1.1     目录 1.2     建立TypeHandler 1.2.1    TypeHandler接口 1.2.2    BaseTypeHandler抽象类 1.3     注册TypeHa ...

  10. Android内容提供者(Content provider)

    使用ContentProvider共享数据 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据,但数据访 ...