forked from Tencent/UnLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
146 lines (125 loc) · 5.32 KB
/
example.lua
File metadata and controls
146 lines (125 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- yasio 3.34 demo
local proto = require 'protocol_enc'
local yasio = require 'yasio' -- constants
local io_service = yasio.io_service
local stopFlag = 0
local hostent = {host = '0.0.0.0', port = 12191}
local hostents = {
{host = '0.0.0.0', port = 12191},
{host = '0.0.0.0', port = 12192}
}
local server = io_service.new(hostents)
local transport1 = nil
local data_partial2 = nil
local packet_count = 0
server:start(function(event)
local t = event:kind()
if t == yasio.YEK_PACKET then
packet_count = packet_count + 1
elseif(t == yasio.YEK_CONNECT_RESPONSE) then -- connect responseType
if(event:status() == 0) then
local transport = event:transport()
-- local requestData = "GET /index.htm HTTP/1.1\r\nHost: www.ip138.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36\r\nAccept: */*;q=0.8\r\nConnection: Close\r\n\r\n"
local msg = {
id = 110,
value1 = 3291,
value2 = 391040,
value3 = true,
value4 = 301.32,
value6 = 13883.197,
uname = "halx99",
passwd = "x-studio Pro is a powerful IDE!"
}
local obs = proto.e101(msg)
-- separate packet to 2 partial
local data = obs:to_string()
local data_partial1 = data:sub(1, #data - 10)
data_partial2 = data:sub(#data - 10 + 1, #data)
-- write the whole packet
print('yasio - write whole packet with yasio::obstream*')
server:write(transport, obs)
-- write the partial1
server:write(transport, data_partial1)
-- write the partial2 after 3 seconds
transport1 = transport
print('yasio - the remain data will be sent after 3 seconds...')
end
elseif(t == yasio.YEK_CONNECTION_LOST) then -- connection lost event
print("yasio - the connection is lost!")
end
end)
-- enable reuse addr for tcp server
server:set_option(yasio.YOPT_C_MOD_FLAGS, 0, yasio.YCF_REUSEADDR, 0)
server:set_option(yasio.YOPT_C_MOD_FLAGS, 1, yasio.YCF_REUSEADDR, 0)
server:open(0, yasio.YCK_TCP_SERVER)
server:open(1, yasio.YCK_TCP_SERVER)
hostent.host = "127.0.0.1"
local client = io_service.new(hostent)
--tcp unpack params, TCP拆包参数设置接口:
client:set_option(yasio.YOPT_C_LFBFD_PARAMS,
0, -- channelIndex, 信道索引
65535, -- maxFrameLength, 最大包长度
0, -- lenghtFieldOffset, 长度字段偏移,相对于包起始字节
4, -- lengthFieldLength, 长度字段大小,支持1字节,2字节,3字节,4字节
0 -- lengthAdjustment:如果长度字段字节大小包含包头,则为0, 否则,这里=包头大小
)
client:start(function(event)
local t = event:kind()
if t == yasio.YEK_PACKET then
local ibs = event:packet()
local msg = proto.d101(ibs)
print(string.format('yasio - receve data from server: %s', msg.passwd))
stopFlag = stopFlag + 1
-- test buffer out_of_range exception handler
-- local _, result = pcall(ibs.read_i8, ibs)
-- print('yasio - ' .. result)
elseif(t == yasio.YEK_CONNECT_RESPONSE) then -- connect responseType
if(event:status() == 0) then
print("yasio - connect server succeed.")
-- local transport = event:transport()
-- local requestData = "GET /index.htm HTTP/1.1\r\nHost: www.ip138.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36\r\nAccept: */*;q=0.8\r\nConnection: Close\r\n\r\n"
-- client:write(transport, obs)
else
print("yasio - connect server failed!")
end
elseif(t == yasio.YEK_CONNECTION_LOST) then -- connection lost event
print("yasio - the connection is lost!")
end
end)
-- httpclient
local http_client = require 'http_client'
http_client:sendHttpGetRequest('https://tool.chinaz.com/', function(respData)
print(string.format('yasio - http request done, %d bytes transferred\n', #respData))
print(respData)
stopFlag = stopFlag + 1
end)
local elapsedTime = 0
local partial2Sent = false
local connectRequested = false
local function yasio_update(dt)
elapsedTime = elapsedTime + dt
server:dispatch(128)
client:dispatch(128)
if http_client then
http_client:update()
end
if elapsedTime > 2 and not connectRequested then
connectRequested = true
print('yasio - connecting server...')
client:open(0, yasio.YCK_TCP_CLIENT)
end
if elapsedTime > 3 and not partial2Sent then
partial2Sent = true
if transport1 then
server:write(transport1, data_partial2)
end
end
return stopFlag >= 3
end
if(yasio.loop) then
print("yasio - start loop main thread to fetch event from yasio io_service!")
yasio.loop(-1, 0.02, function()
yasio_update(0.02)
end)
end
return yasio_update