[lua] future模式*协程
以下是lua实现的future模式。基于cocos客户端
local function param_pack( params, callback )
table.insert(params, callback)
return params
end local function future( ... )
local order = {result = true}
local function callback( ... )
order.res = { ... }
if order.co then
coroutine.resume(order.co)
end
end
function order:result()
if (self.res == nil) then
local co, main = coroutine.running()
if not main then self.co = co else return end
coroutine.yield()
end
return unpack(self.res)
end
local params = {...}
local host, service = params[], table.remove(params, )
if type(params[#params]) == 'function' then
params = table.remove(params)(params, callback)
else
params = param_pack(params, callback)
end
if type(host[service]) == 'function' then
host[service](unpack(params))
return order
else
print('service:'..service..' not implement at '..tostring(host))
end
end
local function asyncall( ... )
return future(...):result()
--[[
local co, main = coroutine.running()
if main then
print('Please use .call(...) in .run(func) context')
return
end
local function callback( ... )
return coroutine.resume(co, ...)
end
local params = {...}
local host, service = params[1], table.remove(params, 2)
if type(params[#params]) == 'function' then
params = table.remove(params)(params, callback)
else
params = param_pack(params, callback)
end
if type(host[service]) == 'function' then
return coroutine.yield(host[service](unpack(params)))
else
print('service:'..service..' not implement at '..tostring(host))
end
--]]
end local function http(...)
-- local order = http([method,] url [, params])
-- status, resp = order:result()
local method, url, params
if select('#', ...) < then
method, url, params = 'GET', ...
else
method, url, params = ...
end
method = string.upper(method)
local support = {GET = true, POST = true}
if not support[method] then return end local request = cc.XMLHttpRequest:new()
request:registerScriptHandler(function()
local callback = request.callback
if callback then
callback(request.status, request.responseText)
end
end)
function request:script(params, callback)
self.callback = callback
self.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON
local handler = {}
function handler.GET()
self:open(method, url..'?'..params, true)
self:send()
end
function handler.POST()
self:open(method, url, true)
self:setRequestHeader("Content-Type","application/x-www-form-urlencoded;")
self:send(params)
end
if (handler[method]) then
handler[method]()
end
end
if params~=nil then
local function url_encode(params)
if type(params) ~= 'table' then
return string.urlencode(tostring(params))
end local pp = {}
for k, v in pairs(params) do
pp[#pp+] = k..'='..string.urlencode(tostring(v))
end
return table.concat(pp, '&')
end
return future(request, 'script', url_encode(params))
end
return future(request, 'script')
end local function runProcess( ... )
local func = select(-, ...)
assert(type(func)=='function', 'the last argument must be a function for coroutine process')
local co = coroutine.create(func) local function process( ... )
coroutine.resume(co, ...)
end
process(...)
return process
end local target = {
call = asyncall,
book = future,
http = http,
run = runProcess
} return target --[[
-- example
local Plugin = plugin.AgentManager:getUserPlugin()
target.run(function ( ... )
local code, msg, info = target.call(Plugin, 'queryThirdAccountBindState', 'weixin')
if code == ThirdAccountStatus.kBinded then
code, msg, info = target.call(Plugin, 'queryThirdInfo', 'weixin')
if code == AsyncQueryStatus.kSuccess then
dump(info)
else
print(msg)
end
end
-- following using future model
local order = target.http('http://huodong.uc108.org:922/CheckIn/AddCheckIn', {ActId=‘123’, UserId=77681})---
local checkin = order
order = target.http('http://huodong.uc108.org:922/floorreward/GetFloorRewardConfig', {actid='666'})
local code, resp = order:result()
code, resp = checkin:result()
end) --]]
[lua] future模式*协程的更多相关文章
- Lua 5.3 协程简单示例
Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...
- lua中的协程
lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...
- [转]skynet Lua中的协程
Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...
- lua编程之协程介绍
一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...
- [lua] 游戏客户端逻辑使用lua协程
我们的游戏有这样一种情景:客户端中角色需要用到一些公会的数据,但服务器不会在玩家(创角后)一进入到游戏里就推送给玩家,而是需要客户端自己在需要的时候向服务器请求公会的数据,之前的实现就是在请求消息的时 ...
- 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程
流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...
- Openresty Lua协程调度机制
写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...
- Lua协程的一个例子
很久没记录笔记了,还是养成不了记录的习惯 下面是来自 programming in lua的一个协程的例(生产者与用户的例子) 帖代码,慢慢理解 -- Programming in Lua Corou ...
- 写个百万级别full-stack小型协程库——原理介绍
其实说什么百万千万级别都是虚的,下面给出实现原理和测试结果,原理很简单,我就不上图了: 原理:为了简单明了,只支持单线程,每个协程共享一个4K的空间(你可以用堆,用匿名内存映射或者直接开个数组也都是可 ...
随机推荐
- C# 遍历文本框
#region 文本框指定位置加入回车符 private void button1_Click(object sender, EventArgs e) { #region // 查询首字母位置 //s ...
- Hadoop-2.2.0集群部署时live nodes数目不对的问题
关于防火墙,hadoop本身配置都确定没任何问题,集群启动不报错,但打开50070页面,始终live nodes数目不对,于是我尝试/etc/hosts文件配置是否存在逻辑的错误: 127.0.0.1 ...
- 实现Modbus ASCII多主站应用
1.更新设计关于原来的协议栈在Modbus ASCII主站应用时所存在的局限性与Modbus RTU也是一样的,所以我们不分析它的不足,只讨论更新设计.我们将主站及其所访问的从站定义为通用的对象,而当 ...
- null值处理
一,在实体类的上面加注解 import com.fasterxml.jackson.annotation.JsonInclude @JsonInclude(JsonInclude.Include.NO ...
- Jmeter的参数签名测试
简介 参数签名可以保证开发的者的信息被冒用后,信息不会被泄露和受损.原因在于接入者和提供者都会对每一次的接口访问进行签名和验证. 签名sign的方式是目前比较常用的方式. 第1步:接入者把需求访问的接 ...
- TensorFlow技术解析与实战学习笔记(15)-----MNIST识别(LSTM)
一.任务:采用基本的LSTM识别MNIST图片,将其分类成10个数字. 为了使用RNN来分类图片,将每张图片的行看成一个像素序列,因为MNIST图片的大小是28*28像素,所以我们把每一个图像样本看成 ...
- 洛谷P1583 魔法照片【模拟+排序】
一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从大到小进行排序,每人就有 ...
- bpm被攻击事件
bpm登录不上,服务器是windows2008,从深信服上面设置了ddos每秒钟连接超5000次封锁,阻断后面的IP连接,,深信服DDOS日志没有记录 在bpm服务器上面通过netstat -a查看发 ...
- 1.VMware虚拟机的安装
1.找到安装软件 2.使用如下操作安装 3.选择接受协议 4.修改安装目录 5.如果上一步有修改,此步骤不用改路径 7.安装后创建桌面快捷方式 8.安装成功可以看到桌面上有快捷方式图标 安装结束 声明 ...
- phpcms_完整版
{pc:content action="category" catid="0" num="6" siteid="$siteid&q ...