cocos2dx_lua_skynet_client

Welcome to the cocos2dx_lua_skynet_client wiki!

cocos2dx 集成lpack和pbc。使用luasocket建立网络链接,和后台通信。 cocos2dx的版本为3.10. cocos创建的项目,没有自带cocos2dx目录,只需要创建cocos项目把 cocos2d-x放在frameworks下就可以了。

最近使用skynet作为服务器开发前端使用cocos2dx-lua。由于前后端都是用lua编写。 在搭建双方的通信环节是有一个步骤就是选择通信的协议。通用选择是protobuf+socket的方式。由于protobuf对lua的支持问题。我们选择了pbc这个proto解析库。

本文解决的问题

前端cocos2dx-lua引擎,使用luasocket+protobuf 和skynet进行通信?

前端使用: luasocket pbc lpack
后端使用: socket pbc string.pack string.unpack

skynet在数据发送时使用的是string.pack() string.unpack() 进行打包解包。然后这两个函数是lua5.3的。前端的cocos是lua5.1没有这个API。 因此才有了lpack这个库。

pbc: https://github.com/cloudwu/pbc.git
lpack: https://github.com/LuaDist/lpack.git
skynet: https://github.com/cloudwu/skynet.git

已经定好协议并且可通信的skynet服务器和cocos2dx客户端:https://github.com/gameloses/cocos2dx_lua_skynet_client.git git clonehttps://github.com/gameloses/skynet_pbc.git

接下来就是进行一个前后端的对接逻辑。

cocos2dx集成pbc和lpack

这是一个已经集成好的cocos2dx的例子,代码开源在githubhttps://github.com/gameloses/cocos2dx_lua_skynet_client.git 这里例子已经可以可skynet进行通信。 进入这个链接会有u详细的教程。 下面是详细的集成教程。

集成pbc

  1. 编译pbc pbc下的src拷贝到编译目录。假设vs。环境下你得建一个pbc目录然后把src下的代码copy到pbc下。编译。 发现pbc的.c在VS中不能按C代码编译,而应该按照C++编译,在所有.c的属性页中的“C/C++ => 高级”中,设置“编译为C++代码”后编译通过。因为pbc全是c代码索引得设置一下。
  2. 添加luabindings代码 将pbc目录下binding/lua/pbc-lua.c文件copy到项目中。 添加头文件:pbc-lua.h。代码如下:
#ifndef __PBC_LUA_H__
#define __PBC_LUA_H__

#ifdef __cplusplus
extern "C" {
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
#endif
	int luaopen_protobuf_c(lua_State *L);
#ifdef __cplusplus
}
#endif

#endif

编写桥接代码添加到工程:lua_cocos2dx_pbc_manual.h,lua_cocos2dx_pbc_manual.cpp

//lua_cocos2dx_pbc_manual.h
#ifndef lua_cocos2dx_pbc_manual_h__
#define lua_cocos2dx_pbc_manual_h__

#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

TOLUA_API int  register_pbc_module(lua_State* L);

#endif
//lua_cocos2dx_pbc_manual.cpp
#include "lua_bindings/lua_cocos2dx_pbc_manual.h"

#include "platform/CCPlatformConfig.h"
#include "base/ccConfig.h"
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"

#include "lua_bindings/pbc-lua.h"

#include "cocos/platform/CCFileUtils.h"

int read_protobuf_file(lua_State *L){
	const char *buff = luaL_checkstring(L, -1);
	Data data = cocos2d::FileUtils::getInstance()->getDataFromFile(buff);
	lua_pushlstring(L, (const char*)data.getBytes(), data.getSize());
	return 1;
}

TOLUA_API int register_pbc_module(lua_State* L)
{
	lua_getglobal(L, "_G");
	if (lua_istable(L, -1))//stack:...,_G,
	{
		lua_register(L, "read_protobuf_file_c", read_protobuf_file);
		luaopen_protobuf_c(L);
	}
	lua_pop(L, 1);
	return 1;
}
  1. AppDelegate中添加一下代码
#include "lua_bindings/lua_cocos2dx_pbc_manual.h"   
register_pbc_module(L);
  1. 编译一下就ok。 android和mac平台比较简单就不说了。
  2. 将binding/lua 下的 protobuf.lua parser.lua 拷贝到lua代码目录下。
  3. 测试 添加协议 pbhead.proto
package PbHead;
message MsgHead{
    required int32 msgtype = 1;
    required string msgname = 2; 
    required int32 msgret = 3; 
}

协议生成

protoc --descriptor_set_out  proto/pbhead.pb proto/pbhead.proto

解析协议

require "protobuf"
local buffer = io.read("proto/pbhead.pb") --读出文件内容
protobuf.register(buffer)

编码解码

local msg_head={msgtype = 1, msgname = msg_name, msgret = 0};
local pb_head = protobuf.encode("PbHead.MsgHead", msg_head)
local data = protobuf.decode("PbHead.MsgHead", pb_head);

集成lpack

  1. 下载lpack.添加lpack.c到项目中。
  2. 添加一个lpack.h
#ifndef __LAPCK_H__
#define __LAPCK_H__

#ifdef __cplusplus
extern "C" {
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
#endif
	int luaopen_pack(lua_State *L);
#ifdef __cplusplus
}
#endif

#endif

3.在Appdelegate中添加代码

#include "lpack/lpack.h"
luaopen_pack(L);
  1. 测试 lpack是注册在string类里面的。 string.pack
    string.unpack
    pack(f,...) unpack(s,f,[init])
    注意unpack多返回了一个长度信息。(坑)

前后端消息格式定义

protocol buffer数据包结构如下:

12345
2字节2字节PBMsgHeadPBMsgBody't'

开头2字节存放一个short数据,表示整个数据包的长度。即:
数据包总长度 = 2 byte + 2 byte + PBMsgHead字节数 + PBMsgBody字节数 + 1 byte

随后的2字节存放一个short数据,表示PBMsgHead数据的长度。 PBMsgBody数据长度 = 数据包总长度 - 2 - 2 - 1 - PBMsgHead数据长度。

需要注意的是 PBMsgBody有可能为空,即没有这个数据。因为错误码放在PBMsgHead里面,当逻辑出错时候,只有错误码,没有PBMsgBody。

luasocket进行数据收发

local LuaSock = class("LuaSock")

function LuaSock:connect()
    local socket = require('luasocket.socket'); 
    self.m_sock = socket.tcp(); 
    self.m_sock:settimeout(0);  --非阻塞
    self.m_sock:setoption("tcp-nodelay", true) --去掉优化 不用处理粘包
    self.m_sock:connect(self.m_ip, self.m_port); 
    
    --定时检测是否可写以判断socket的状态
    self.check_ = schedule(function()
        if self:connect_is_success() then
            unschedule(self.check_)
            
            
        end
    end)
end

function LuaSock:connect_is_success( ... )
	local for_write = {};
	table.insert(for_write,self.m_sock);
	local ready_forwrite;
	_,ready_forwrite,_ = socket.select(nil,for_write,0);
	if #ready_forwrite > 0 then
		return true;
	end
	return false;
end


function LuaSock:receive() 
 	local recvt, sendt, status = socket.select({self.m_sock}, nil, 1)
 	print("input", #recvt, sendt, status)
 	if #recvt <= 0 then
 		return;
 	end

    local buffer,err = self.m_sock:receive(2);
    if buffer then 
    	--读取二进制数据流
    	local first, sencond = string.byte(buffer,1,2);
    	local len=first*256+sencond;--通过位计算长度
    	print("收到数据长度=",len)
    	local buffer,err = self.m_sock:receive(len); 
    	
    	--unpack 使用pbc decode
    	local  pb_len, pb_head,pb_body,t = string.unpack(buffer, ">PPb"); 
        local msg_head = protobuf.decode("PbHead.MsgHead", pb_head) 
        local msg_body = protobuf.decode(msg_head.msgname, pb_body)
        print("t:"..t..":"..string.char(t))
    
    end
end

function LuaSock:send()  
    --拼装头
	local msg_head={msgtype = 1, msgname = msg_name, msgret = 0};
	local pb_head = protobuf.encode("PbHead.MsgHead", msg_head)
	local pb_body = protobuf.encode(msg_name, msg_body);
	--计算长度
	local pb_head_len=#pb_head;
    local pb_body_len=#pb_body;
    local pb_len=2+pb_head_len+2+pb_body_len+1; 

	local data=string.pack(">HPPb",pb_len, pb_head, pb_body, string.byte('t'));

	--数据发送
	local _len ,_error = self.m_sock:send(data);
	if _len ~= nil and _len == #data then 
		--表示发送成功 
	end

end

至此客户端的工作是完成了。我放在了github上。

skynet 使用pbc

skynet使用pbc解析protobuf,使用socket和客户端通信。 前提是搭建编译skynet的环境。 下载pbc编译make得到protobuf.so 将pbc/binding/lua53/protobuf.lua复制到根目录的lualib目录 protobuf.so文件复制到根目录的luaclib目录。pbc就算是集成好了。 具体的步骤看下面。

服务器编译步骤

clone完整的代码 代码中引用了skynet和pbc

git clone https://github.com/gameloses/skynet_pbc.git

cd skynet_pbc git submodule init git submodule update

cd skynet git submodule init git submodule update

编译skynet

cd skynet make linux

编译pbc

cd ../3rd/pbc make

cd binding cd lua53 make

如果提示找不到lua.h则需要安装一下lua5.3. make && make install (或者修改一下Makefile文件,设置lua.h的路径)

将protobuf.lua复制到根目录的lualib目录 protobuf.so文件复制到根目录的luaclib目录

编译proto文件

回到根目录 make

如果没有安装protobuf的话会报错,因为要使用到protoc yum install protobuf-devel.x86_64 yum install protobuf.x86_64

运行

. run.sh 具体教程见wiki

使用cocos2dx 测试一下连接。

clinet的地址: https://github.com/gameloses/cocos2dx_lua_skynet_client.git

下面是使用 Lua 实现 scrollView 的无限滚动的示例代码: ```lua local scrollView = ccui.ScrollView:create() scrollView:setDirection(ccui.ScrollViewDir.horizontal) scrollView:setContentSize(cc.size(600, 200)) scrollView:setInnerContainerSize(cc.size(1200, 200)) self:addChild(scrollView) -- 添加节点 for i = 1, 5 do local node = cc.LayerColor:create(cc.c4b(0, 255, 0, 255), 200, 200) node:setPosition((i - 1) * 200, 0) scrollView:addChild(node) end -- 添加节点的复制 local firstNode = scrollView:getChildren()[1] local lastNode = scrollView:getChildren()[5] local firstNodeCopy = firstNode:clone() firstNodeCopy:setPosition(lastNode:getPositionX() + 200, 0) scrollView:addChild(firstNodeCopy) local lastNodeCopy = lastNode:clone() lastNodeCopy:setPosition(firstNode:getPositionX() - 200, 0) scrollView:addChild(lastNodeCopy) -- scrollView 的回调函数 scrollView:addEventListener(function(sender, eventType) if eventType == ccui.ScrollviewEventType.containerMoved then local position = scrollView:getInnerContainerPosition() if position.x < -200 then local lastNode = scrollView:getChildren()[6] scrollView:setInnerContainerPosition(cc.p(lastNode:getPositionX() + 200, 0)) elseif position.x > 1000 then local firstNode = scrollView:getChildren()[2] scrollView:setInnerContainerPosition(cc.p(firstNode:getPositionX() - 200, 0)) end end end) ``` 这段代码实现的效果和前面提供的 C++ 代码一样,只不过是使用 Lua 语言编写的。需要注意的是,Lua 中的数组下标从 1 开始,而不是从 0 开始,所以在获取节点时需要将下标加 1。 另外,需要注意的是,如果在 C++ 中使用了 Lua,那么在使用 Lua 中的一些类时,需要用 ccui 前缀来代替在 C++ 中使用的 ui 前缀。例如,ccui.ScrollView:create() 代替 ui::ScrollView::create()。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值