以下是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. 【Hexo】本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...

  2. 团体程序设计天梯赛-练习集-L1-035. 情人节

    L1-035. 情人节 以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家.第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”.现给出此贴下点赞的朋友名单,请你找出那两位要请客 ...

  3. Java-Class-Test:Test-1

    ylbtech-Java-Class-Test:Test-1 1.返回顶部 1.1. package com.ylbtech.api; import com.y;btech.WxApiApplicat ...

  4. eclipse导入Javaweb文件出错解决

    在项目名上右击打开properties,如图在箭头指的位置可以看出有个unbound表示导入的资源库出现 异常,需要手动导入,1.点击Server Library{Apache Tomcat v9.0 ...

  5. 25.partial update内置乐观锁并发控制

    主要知识点     (1)partial update内置乐观锁并发控制 (2)retry_on_conflict post /index/type/id/_update?retry_on_confl ...

  6. CentOS7安装Kubernetes

    CentOS7安装Kubernetes 安装Kubernetes时候需要一台机器作为管理机器,1台或者多台机器作为集群中的节点. 系统信息: Hosts: 请将IP地址换成自己环境的地址. cento ...

  7. 洛谷—— P1962 斐波那契数列

    https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...

  8. 链表快排 & 基于链表的排序

    以前只知道链表做插入(朴素.非二分)排序挺方便的.现在知道了(单)链表进行快速排序也是很好的(只是跟一般的快排的方式不一样). 参考: http://blog.csdn.net/otuhacker/a ...

  9. study reference

    CVPR2018 ReID论文简评 2017CVPR ICCV和NIPS在Person Reidentification方向的相关工作小结 CVPR 2018 Person Re-ID相关论文 pre ...

  10. pl/sql developer br 文件

    pl/sql developer br 文件 Version=1 RightMargin=72 Indent=2 UseTabCharacter=FALSE TabCharacterSize=2 Al ...