Luakit的前世今生-深蓝源码网
导读 Luakit的历史渊源最近发布了一个跨平台的app开发框架Luakit(https://github.com/williamwen1986/Luakit)。那怎么会想到做这样一个东西呢?这要先说一下我参与过的一些项目,和在项目中接触到的一些技术点和对项目开发体检了,因为Luakit是集合了几个重要技术才能做到用Lua脚本……
Luakit的历史渊源
最近发布了一个跨平台的app开发框架Luakit(https://github.com/williamwen1986/Luakit) 。那怎么会想到做这样一个东西呢?这要先说一下我参与过的一些项目,和在项目中接触到的一些技术点和对项目开发体检了,因为Luakit是集合了几个重要技术才能做到用Lua脚本来实现跨平台app开发的。
我主要参与的项目是QQMail的IOS版。在2017年下半年,由于机缘巧合,我参与开发了企业微信的一个分支版本,appstore上叫政务微信。QQMail的历史比较悠久了,在QQMail项目里我们使用了两项技术是比较特殊的,其他项目团队接触得比较少,一个是Lua脚本化技术,一个是orm技术。而在政务微信开发过程中是企业微信团队的跨平台开发技术给我留下很深印象,下面我首先简单介绍这几项技术。
- 当时QQMail的Lua脚本化技术我们是基于wax来做的,只能在IOS上跑,不具备跨平台的能力。QQMail里面有几个版本中,整个记事本模块从底层逻辑层到界面渲染全部都用Lua来实现,脚本化过程中我们也克服了很多技术难点,例如如何在Lua脚本实现竞争式多线程,如何高效方便地在Lua环境实现数据存储之类的这些业界难题,当然了,脚本化之后我们也第一次吃到脚本化的甜头,最大的优点就是对代码的掌控能力大大提升,一个是可以随时上线,另外就是可以给不同的用户下发不同的代码。这个对于发现问题有很大的好处,当有用户投诉的时候,给用户下发特殊的debug代码,基本没有发现不了的问题。 wax链接: https://github.com/alibaba/wax
- orm技术我们组内同事的研究成果GYDataCenter这个orm框架确实简单易用,可以大大减少数据库相关的开发量,当我们后来做政务微信的时候,项目里没有引入GYDataCenter,我们对直接裸写sql都非常的不适应,也极大的抵触。 GYDataCenter链接: https://github.com/Zepo/GYDataCenter
- 在深入接触政务微信后,我们感到企业微信客户端团队最有价值的技术是跨平台开发的技术,企业微信是基于chromium这套google开源的跨平台开发框架实现的业务跨平台的。跨平台的业务代码包括,线程模型,http短连接请求,请求调度,tcp长链接,数据库存储,数据包加解密等等,基本上除了界面,其他都放到了底层c++来实现了。当我们刚接触这种c++写的业务代码时,我们十分抵触,因为用c++开发会使复杂度大大提高,内存管理问题也是使用其他高级语言开发所不会碰到的。但是当项目继续下去,我们做了几个版本的业务的时候,慢慢的我们感觉到跨平台带来的好处了,虽然开发复杂,但是参考其他业务的代码,我们修改一下做新业务也不是太大的问题,最大的好处是只要开发一次,IOS和android就都work了,确实很高效。业务代码只有一份,bug也只有一份,一个平台修复了,另一个平台也可以享受到。 chromium链接: http://www.chromium.org/
深入接触这几个框架后,我发现Lua跟chromium真是绝配,chromium提供跨平台的消息循环机制可以完美解决lua实现竞争式多线程的问题,在lua环境实现竞争式多线程(注意,不是单单线程安全)是使用lua开发的一个普遍性的难题,cocos2d-x的lua-binding也没解决这个问题,所以基于cocos2d-x lua版开发的游戏也很难做到全脚本化,因为Lua只能单线程。有了Luakit后,这类问题都有解决方案了。而lua的内存管理机制也可以很好的解决chromium用c++开发,内存管理和不适合函数式编程的最大的弊端,两者解合可以产生很好的效果。有了lua的多线程模型后,参考GYDataCenter的实现原理,我们可以实现一套lua版的orm框架,GYDataCenter只能在ios使用,现在lua版的orm框架可以具有跨平台的特性。
Luakit的功能简介
Luakit提供的很多强大的功能,这些功能都是可以跨平台运行的,Luakit主要包括以下功能接口
- 多线程接口
- orm模型接口
- 文件操作接口
- http请求
- 异步socket接口
- 全局通知机制
- Lua代码加解密
下面简单介绍多线程接口,orm接口,http请求,异步socket接口和全局通知接口。
多线程模型
如何在Lua实现竞争式多线程我会再发一篇文章专门讲讲,因为这个问题是Lua领域的普遍存在的问题,有一定的技术意义。这里我先简单带过一下实现思路,一个lua解析器本身是不具备多线程能力,甚至不是线程安全的,但是在服务器开发上已经有人尝试起多条线程然后给每条线程配置独立的Lua解析器,然后多条线程通过一定的数据通道传输数据,通过这样的方式实现真正的多线程,但是这个思路一直没有延伸到客户端开发,主要原因是因为客户端通常把真正的线程隐藏起来,无论IOS或者android,都不能轻易地接触真正的线程,但是由于chromium提供了开源的线程模型,通过修改chromium的底层源码,生成消息循环时的给每个消息循环配置独立的lua解析器,这样最大的问题就得到了解决,下面看一下Luakit 提供的多线程接口。
创建线程
-- Parma1 is the thread type ,there are five types of thread you can create. -- BusinessThreadUI -- BusinessThreadDB -- BusinessThreadLOGIC -- BusinessThreadFILE -- BusinessThreadIO -- Param2 is the thread name -- Result is new threadId which is the token you should hold to do further action local newThreadId = lua.thread.createThread(BusinessThreadLOGIC,"newThread")
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
异步调用方法,类似IOS gcd中的 dispatch_async
-- Parma1 is the threadId for which you want to perform method -- Parma2 is the modelName -- Parma3 is the methodName -- The result is just like you run the below code on a specified thread async -- require(modelName).methodName("params", 1.1, {1,2,3}, function (p) -- end) lua.thread.postToThread(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p) -- do something here end)
demo code链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
同步调用方法,类似IOS gcd中的 dispatch_sync
-- Parma1 is the threadId for which you want to perform method -- Parma2 is the modelName -- Parma3 is the methodName -- The result is just like you run the below code on a specified thread sync -- local result = require(modelName).methodName("params", 1.1, {1,2,3}, function (p) -- end) local result = lua.thread.postToThreadSync(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p) -- do something here end)
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
orm接口
orm 模型的实现方法是参考IOS orm 开源库GYDataCenter的实现方法,GYDataCenter很依赖IOS gcd 的机制,Luakit中可以用新的lua多线程接口取代,可以做到同样的效果,下面罗列一下 orm demo code
Luakit 提供的orm框架有如下特征
- 面向对象接口
- 自动建表自动更新表结构和索引
- 自带cache功能
- 定时transaction
- 线程安全,可以在任何线程发起数据库操作
定义数据模型
-- Add the define table to dbData.lua -- Luakit provide 7 colum types -- IntegerField to sqlite integer -- RealField to sqlite real -- BlobField to sqlite blob -- CharField to sqlite varchar -- TextField to sqlite text -- BooleandField to sqlite bool -- DateTimeField to sqlite integer user = { __dbname__ = "test.db", __tablename__ = "user", username = {"CharField",{max_length = 100, unique = true, primary_key = true}}, password = {"CharField",{max_length = 50, unique = true}}, age = {"IntegerField",{null = true}}, job = {"CharField",{max_length = 50, null = true}}, des = {"TextField",{null = true}}, time_create = {"DateTimeField",{null = true}} }, -- when you use, you can do just like below local Table = require('orm.class.table') local userTable = Table("user")
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/db_test.lua
插入数据
local userTable = Table("user") local user = userTable({ username = "user1", password = "abc", time_create = os.time() }) user:save()
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/db_test.lua
更新数据
local userTable = Table("user") local user = userTable.get:primaryKey({"user1"}):first() user.password = "efg"user.time_create = os.time() user:save()
select 数据
local userTable = Table("user") local users = userTable.get:all() print("select all -----------") local user = userTable.get:first() print("select first -----------") users = userTable.get:limit(3):offset(2):all() print("select limit offset -----------") users = userTable.get:order_by({desc('age'), asc('username')}):all() print("select order_by -----------") users = userTable.get:where({ age__lt = 30, age__lte = 30, age__gt = 10, age__gte = 10, username__in = {"first", "second", "creator"}, password__notin = {"testpasswd", "new", "hello"}, username__null = false }):all() print("select where -----------") users = userTable.get:where({"scrt_tw",30},"password = ? AND age < ?"):all() print("select where customs -----------") users = userTable.get:primaryKey({"first","randomusername"}):all() print("select primaryKey -----------")
联表查询
local userTable = Table("user") local newsTable = Table("news") local user_group = newsTable.get:join(userTable):all() print("join foreign_key") user_group = newsTable.get:join(userTable,"news.create_user_id = user.username AND user.age < ?", {20}):all() print("join where ") user_group = newsTable.get:join(userTable,nil,nil,nil,{create_user_id = "username", title = "username"}):all() print("join matchColumns ")
http请求 Luakit提供了http请求接口,包括了请求队列调度控制
-- url , the request url -- isPost, boolean value represent post or get -- uploadContent, string value represent the post data -- uploadPath, string value represent the file path to post -- downloadPath, string value to tell where to save the response -- headers, tables to tell the http header -- socketWatcherTimeout, int value represent the socketTimeout -- onResponse, function value represent the response callback -- onProgress, function value represent the onProgress callback lua.http.request({ url = "http://tj.nineton.cn/Heart/index/all?city=CHSH000000", onResponse = function (response) end})
实现代码链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/network/async_task_dispatcher.h
demo code链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/WeatherManager.lua
异步socket接口 Luakit 提供了非阻塞的socket调用接口
local socket = lua.asyncSocket.create("127.0.0.1",4001) socket.connectCallback = function (rv) if rv >= 0 then print("Connected") socket:read() end end socket.readCallback = function (str) print(str) timer = lua.timer.createTimer(0) timer:start(2000,function (
