以下是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模式*协程的更多相关文章

  1. Lua 5.3 协程简单示例

    Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...

  2. lua中的协程

    lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...

  3. [转]skynet Lua中的协程

    Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...

  4. lua编程之协程介绍

    一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...

  5. [lua] 游戏客户端逻辑使用lua协程

    我们的游戏有这样一种情景:客户端中角色需要用到一些公会的数据,但服务器不会在玩家(创角后)一进入到游戏里就推送给玩家,而是需要客户端自己在需要的时候向服务器请求公会的数据,之前的实现就是在请求消息的时 ...

  6. 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程

    流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...

  7. Openresty Lua协程调度机制

    写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...

  8. Lua协程的一个例子

    很久没记录笔记了,还是养成不了记录的习惯 下面是来自 programming in lua的一个协程的例(生产者与用户的例子) 帖代码,慢慢理解 -- Programming in Lua Corou ...

  9. 写个百万级别full-stack小型协程库——原理介绍

    其实说什么百万千万级别都是虚的,下面给出实现原理和测试结果,原理很简单,我就不上图了: 原理:为了简单明了,只支持单线程,每个协程共享一个4K的空间(你可以用堆,用匿名内存映射或者直接开个数组也都是可 ...

随机推荐

  1. C#判断文件是否存在 //创建txt文件

    if(System.IO.File.Exists(@"")) { } if (System.IO.File.Exists(HttpRuntime.AppDomainAppPath ...

  2. jsonp的作用

    jsonp(即JSON with padding),也就是json填充. 背景: json格式的数据对比xml格式的数据,性能上已经有了很大的提升.但是json可以被本地执行仍然会导致几个重要的性能问 ...

  3. web开发——在网页中引用字体包(.ttf),即嵌入特殊字体

    在写html时,有点时候需要显示一些特殊字体,不过这些特殊字体是系统一般不自带的,这时就需要我们自行加载要用的字体.方法如下: 1.首先在style里添加: @font-face { font-fam ...

  4. Express+Nodejs 下的登录拦截实现

    Express+Nodejs 下的登录拦截实现 利用商城举例,在商城中没有登录之前,可以看商品列表.详情.登录或者注册都可以,但是购买的时候是不行的,那么这个功能在Node后台中是怎么实现的呢,这个功 ...

  5. pymysql.err.ProgrammingError: (1064)(字符串转译问题)

    代码: sql = "insert into dm_copy(演出类型,演出场馆,剧目名称,演出地点,演出时间,演出票价,演出团体,创建时间, url)values('%s','%s','% ...

  6. GDI 画刷(10)

    创建画刷 调用 CreateSoildBrush.CreateHatchBrush.CreateBrushIndirect 函数可以创建画刷. CreateSoildBrush:创建实心画刷: Cre ...

  7. 分布式系列文章 —— 从 ACID 到 CAP / BASE

    转自:https://mp.weixin.qq.com/s?amp;mid=2652037708&__biz=MzI0NDI0MTgyOA%3D%3D&idx=1&chksm= ...

  8. 原生js封装ajax,深入理解$.ajax()

    直接上代码 //封装的ajax函数 // 传一个对象,所有要用的参数都在对象中 因为不写对象 实参列表个数太多,所以像jq一样,调用ajax也是把对象当实际参数传进去 // type 请求方式 默认g ...

  9. mongodb集群搭建(分片+副本)

    原文地址:https://www.cnblogs.com/ityouknow/p/7344005.html 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...

  10. sql语句学习(NOT EXISTS 和 NOT IN )

    NOT EXISTS SELECT a.*FROM t_user aWHERE a.id_card LIKE '%3203821995100%'AND NOT EXISTS ( SELECT id F ...