Skip to content

Commit e3da1f1

Browse files
committed
Merge pull request chukong#222 from zrong/develop
add a sample for SocketTCP
2 parents 924fe3d + 0b11bc6 commit e3da1f1

File tree

15 files changed

+608
-1
lines changed

15 files changed

+608
-1
lines changed

framework/cc/utils/ByteArrayVarint.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A description: https://github.com/zrong/lua#ByteArrayVarint
99
@author zrong(zengrong.net)
1010
Creation: 2013-11-14
1111
]]
12-
local ByteArrayVarint = class("BitVaiant", require("cc.utils.ByteArray"))
12+
local ByteArrayVarint = class("BitVaiant", import(".ByteArray"))
1313

1414
require("bit")
1515

Binary file not shown.
6.34 KB
Binary file not shown.
Binary file not shown.

samples/luasocket/README.cn.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
* [README in english][10]
2+
* [README in chinese][20]
3+
4+
作者: [zrong][1]
5+
6+
这是一个 luasocket 范例。
7+
8+
为了便于使用,我封装了 luasocket 到 cc.net.SocketTCP 类中。这个范例展示如何使用 cc.net.SocketTCP。
9+
10+
同时,在本范例中还使用了 cc.utils.ByteArray 和 cc.utils.ByteArrayVarint。
11+
12+
要了解更多关于 cc.net.SocketTCP 和 cc.utils.ByteArray 的信息,请阅读 [一个LuaSocket封装][6][用lua实现ByteArray和ByteArrayVarint][7] .
13+
14+
在本范例的 net 包中,有3个 lua 类:
15+
16+
* [net.PacketBuffer][2]
17+
我们收到的数据包经常是不完整的,特别是当数据包比较大的时候。因此我们必须确保数据包完整才能使用。PacketBuffer 就是做这件事的。
18+
* [net.Protocol][3]
19+
这个类把数据包解析成人类可读的格式。它转换一个数据包到一个lua table,并且为每个数据项命名。数据项的名称定义在 [net.protocols][4] 文件中。
20+
* [net.protocols][4]
21+
这是你的协议定义文件。你可以写一个生成器程序来生成它。
22+
23+
下面是本范例运行的log文件。当然,你需要自己实现服务端程序。
24+
25+
[0.2467] [INFO] socket.getTime:1389319197.462940
26+
[0.2472] [INFO] os.gettime:1389319197.000000
27+
[0.2477] [INFO] socket._VERSION: LuaSocket 2.1-rc1
28+
[0.2530] [INFO] Scene "MainScene:onEnter()"
29+
[1.9754] [INFO] socket status: SOCKET_TCP_CONNECTED
30+
[5.8635] send 1000 packet: 86 7B 00 0B 00 00 00 B1 04 00 03 00 08 10 C0 3E 01 01
31+
[5.8859] socket receive raw data: 86 7B 00 60 00 00 00 00 7D 00 0D 00 09 11 18 20 28 30 38 40 48 50 58 60 00 3C 63 73 76 3D 34 38 32 28 30 31 30 39 2E 31 35 33 32 32 38 29 0A 66 73 3D 31 33 31 28 30 31 30 39 2E 31 35 33 31 35 31 29 0A 67 73 3D 33 35 32 28 30 31 30 39 2E 31 38 30 33 32 37 29 04 74 65 73 74 00 14 50 D6 03 96 01 00 00 00 0F 6E 103
32+
[5.8873] start analyzing... buffer len: 103, available: 103
33+
[5.8878] method:2000
34+
[5.8884] before get meta position:11
35+
[5.8897] after get meta position:25
36+
[5.8904] after get body position:104
37+
[5.8909] dump from: [string "d:/cocos2dx/quick/samples/luasocket/scripts/scenes/MainScene.lua"]:54: in function <[string "d:/cocos2dx/quick/samples/luasocket/scripts/scenes/MainScene.lua"]:48>
38+
[5.8921] - "<var>" = {
39+
[5.8924] - "body" = {
40+
[5.8931] - "ap" = 0
41+
[5.8938] - "crystal" = 150
42+
[5.8944] - "dust" = 0
43+
[5.8950] - "gold" = 470
44+
[5.8955] - "goodsMaxNum" = 110
45+
[5.8959] - "goodsNum" = 15
46+
[5.8965] - "lv" = 20
47+
[5.8971] - "mithril" = 0
48+
[5.8975] - "money" = 80
49+
[5.8981] - "flag" = 0
50+
[5.8987] - "name" = "test"
51+
[5.8993] - "sex" = 0
52+
[5.8999] - "ver" = "csv=482(0109.153228)\nfs=131(0109.153151)\ngs=352(0109.180327)"
53+
[5.9008] - }
54+
[5.9013] - "method" = 2000
55+
[5.9019] - "ver" = 0
56+
[5.9025] - }
57+
58+
[1]: http://zengrong.net
59+
[2]: scripts/net/PacketBuffer.lua
60+
[3]: scripts/net/Protocol.lua
61+
[4]: scripts/net/protocols.lua
62+
[6]: http://zengrong.net/post/1980.htm
63+
[7]: http://zengrong.net/post/1968.htm
64+
[10]: README.md
65+
[20]: README.cn.md

samples/luasocket/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
* [README in english][10]
2+
* [README in chinese][20]
3+
4+
Author: [zrong][1]
5+
6+
This is a luasocket sample. I encapsulate luasocket into cc.net.SocketTCP class.
7+
8+
This sample will show you how to use the cc.net.SocketTCP.
9+
10+
This sample also needs cc.utils.ByteArray and cc.utils.ByteArrayVarint.
11+
12+
For more information about cc.net.SocketTCP and cc.utils.ByteArray, see [lua SocketTCP][6] and [ByteArray in lua][7] .
13+
14+
There are 3 lua files in the net package:
15+
16+
* [net.PacketBuffer][2]
17+
The data package may be incomplete, especially it is larger. So we must ensure to get a intact data to use it. PacketBuffer can do it.
18+
* [net.Protocol][3]
19+
Protocol splits a complete data package to human-readable format. It convert a data stream to a lua table and names all data item. The names defined in [net.protocols][4] .
20+
* [net.protocols][4]
21+
Your all protocols. It is generated by a generator.
22+
23+
There is a log file that run this sample. Of cause, you can implement a server application by youself.
24+
25+
[0.2467] [INFO] socket.getTime:1389319197.462940
26+
[0.2472] [INFO] os.gettime:1389319197.000000
27+
[0.2477] [INFO] socket._VERSION: LuaSocket 2.1-rc1
28+
[0.2530] [INFO] Scene "MainScene:onEnter()"
29+
[1.9754] [INFO] socket status: SOCKET_TCP_CONNECTED
30+
[5.8635] send 1000 packet: 86 7B 00 0B 00 00 00 B1 04 00 03 00 08 10 C0 3E 01 01
31+
[5.8859] socket receive raw data: 86 7B 00 60 00 00 00 00 7D 00 0D 00 09 11 18 20 28 30 38 40 48 50 58 60 00 3C 63 73 76 3D 34 38 32 28 30 31 30 39 2E 31 35 33 32 32 38 29 0A 66 73 3D 31 33 31 28 30 31 30 39 2E 31 35 33 31 35 31 29 0A 67 73 3D 33 35 32 28 30 31 30 39 2E 31 38 30 33 32 37 29 04 74 65 73 74 00 14 50 D6 03 96 01 00 00 00 0F 6E 103
32+
[5.8873] start analyzing... buffer len: 103, available: 103
33+
[5.8878] method:2000
34+
[5.8884] before get meta position:11
35+
[5.8897] after get meta position:25
36+
[5.8904] after get body position:104
37+
[5.8909] dump from: [string "d:/cocos2dx/quick/samples/luasocket/scripts/scenes/MainScene.lua"]:54: in function <[string "d:/cocos2dx/quick/samples/luasocket/scripts/scenes/MainScene.lua"]:48>
38+
[5.8921] - "<var>" = {
39+
[5.8924] - "body" = {
40+
[5.8931] - "ap" = 0
41+
[5.8938] - "crystal" = 150
42+
[5.8944] - "dust" = 0
43+
[5.8950] - "gold" = 470
44+
[5.8955] - "goodsMaxNum" = 110
45+
[5.8959] - "goodsNum" = 15
46+
[5.8965] - "lv" = 20
47+
[5.8971] - "mithril" = 0
48+
[5.8975] - "money" = 80
49+
[5.8981] - "flag" = 0
50+
[5.8987] - "name" = "test"
51+
[5.8993] - "sex" = 0
52+
[5.8999] - "ver" = "csv=482(0109.153228)\nfs=131(0109.153151)\ngs=352(0109.180327)"
53+
[5.9008] - }
54+
[5.9013] - "method" = 2000
55+
[5.9019] - "ver" = 0
56+
[5.9025] - }
57+
58+
[1]: http://zengrong.net
59+
[2]: scripts/net/PacketBuffer.lua
60+
[3]: scripts/net/Protocol.lua
61+
[4]: scripts/net/protocols.lua
62+
[6]: http://zengrong.net/post/1980.htm
63+
[7]: http://zengrong.net/post/1968.htm
64+
[10]: README.md
65+
[20]: README.cn.md

samples/luasocket/main.bat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@echo off
2+
set QUICK=%QUICK_COCOS2DX_ROOT%\player\bin\win32\quick-x-player.exe
3+
set PROJECT=%cd%
4+
set SIZE=960x640
5+
start "Quick X Player " "%QUICK%" -workdir %PROJECT% -file %PROJECT%\scripts\main.lua -size %SIZE%

samples/luasocket/scripts/config.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
-- 0 - disable debug info, 1 - less debug info, 2 - verbose debug info
3+
DEBUG = 2
4+
DEBUG_FPS = true
5+
6+
-- design resolution
7+
CONFIG_SCREEN_WIDTH = 640
8+
CONFIG_SCREEN_HEIGHT = 960
9+
10+
-- auto scale mode
11+
CONFIG_SCREEN_AUTOSCALE = "FIXED_WIDTH"

samples/luasocket/scripts/game.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
require("config")
3+
require("framework.init")
4+
5+
-- define global module
6+
game = {}
7+
8+
function game.startup()
9+
CCFileUtils:sharedFileUtils():addSearchPath("res/")
10+
11+
game.enterMainScene()
12+
end
13+
14+
function game.exit()
15+
CCDirector:sharedDirector():endToLua()
16+
end
17+
18+
function game.enterMainScene()
19+
display.replaceScene(require("scenes.MainScene").new(), "fade", 0.6, display.COLOR_WHITE)
20+
end

samples/luasocket/scripts/main.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
function __G__TRACKBACK__(errorMessage)
3+
print("----------------------------------------")
4+
print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
5+
print(debug.traceback("", 2))
6+
print("----------------------------------------")
7+
end
8+
9+
require("game")
10+
game.startup()

0 commit comments

Comments
 (0)