-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabSocket.lua
More file actions
517 lines (432 loc) · 12.5 KB
/
tabSocket.lua
File metadata and controls
517 lines (432 loc) · 12.5 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
--tabSocket is a simple tab interface for lua socket, the implementation
--demonstrates the correct use of tabProxy
--author cs
--email 04nycs@gmail.com
--https://github.com/ThinEureka/tabMachine
--created on Jan 11, 2021
local socket = require "socket"
local tabSocket = nil
local STATUS_CLOSED = "closed"
local STATUS_NOT_CONNECTED = "Socket is not connected"
local STATUS_ALREADY_CONNECTED = "already connected"
local STATUS_ALREADY_IN_PROGRESS = "Operation already in progress"
local STATUS_TIMEOUT = "timeout"
--------------------------------------------------------------------------------
-- main flow
-- private:
tabSocket = _{
tabName = "tabSocket",
STATE = {
DISCONNECTED = 1,
CONNECTING = 2,
CONNECTED = 3,
},
s1 = function(c, isUsingIpV6)
c._isUsingIpV6 = isUsingIpV6
c._closeDelay = 0.1
c._tcp = nil
c._buffer = nil
c._state = tabSocket.STATE.DISCONNECTED
c._nickName = "SOCKET->等待连接指令"
c:suspend("s2")
end,
s2 = function(c, mode, ip, port, timeout, tcp)
c._nickName = tostring(ip)..":"..tostring(port)
if mode == 1 then
c._state = tabSocket.STATE.CONNECTING
c._nickName = "SOCKET->连接中"
c._buffer = {}
c._tcp = nil
c:call(tabSocket.tabConnectting(ip, port, timeout) >> "isSuccess" >> "err", "s3")
else
--only for server mode
c._nickName = "SOCKET->已连接"
c._tcp = tcp
c._tcp:settimeout(0.000000000001)
c._buffer = {}
c._state = tabSocket.STATE.CONNECTED
c._connected = c:call(tabSocket.tabConnectted(), "s5")
end
end,
s4 = function(c)
if c.isSuccess then
c._nickName = "SOCKET->已连接"
c._state = tabSocket.STATE.CONNECTED
c._connected = c:call(tabSocket.tabConnectted(), "s5")
else
c:start("s1")
end
end,
s6 = function(c)
c._connected = nil
c:start("s1")
end,
final = function(c)
if c._tcp then
c._tcp:close()
end
end,
-- inner
inner = {
tabSocket = function(c)
return c
end,
buffer = function(c)
return c._buffer
end,
isUsingIpV6 = function(c)
return c._isUsingIpV6
end,
tcp = function(c)
return c._tcp
end,
setTcp = function(c, tcp)
c._tcp = tcp
end,
closeDelay = function(c)
return c._closeDelay
end,
},
--public:
connect = function(c, ip, port, timeout)
c:resume("s2", 1, ip, port, timeout)
return c:tabProxy("s3")
end,
disconnect = function(c)
c:notify("tabSocket.disconnect")
end,
--for server
acceptSocket = function(c, tcp)
assert(tcp ~= nil)
assert(c._state == tabSocket.STATE.DISCONNECTED)
c:resume("s2", 2, nil, nil, nil, tcp)
end,
send = function(c, data)
if c._connected == nil then
return STATUS_NOT_CONNECTED
end
return c._connected:send(data)
end,
getState = function(c)
return c._state
end,
--the progress when socket is the specified state
tabInState = _{
s1 = function(c, self, state)
c._nickName = "tabSocket#tabInState->".. state
if self._state ~= state then
c:stop()
else
c:call(self:tabState(state), "s2")
end
end,
},
--private:
tabState = function(c, scName)
if scName == tabSocket.STATE.CONNECTING then
return c:tabProxy("s2")
elseif scName == tabSocket.STATE.CONNECTED then
return c:tabProxy("s5")
elseif scName == tabSocket.STATE.DISCONNECTED then
return c:tabSuspend("s2")
end
end,
}
--private:
-- 2nd level flow
tabSocket.tabConnectting = _{
tabName = "tabConnectting",
s1 = function(c, ip, port, timeout)
if timeout == nil then
timeout = 3
end
c.timeout = timeout
c.port = port
local ipV6 = tabSocket.getIpV6Address(ip)
if c:_("isUsingIpV6") and ipV6 ~= nil then
c.ip = ipV6
c.tcp = socket.tcp6()
else
c.ip = ip
c.tcp = socket.tcp()
end
c.tcp:settimeout(0.000000000001)
c:_("setTcp", c.tcp)
c.time = 0
c.isConnected = c:_tryToConnect()
if c.isConnected then
c:output(true)
c:stop("s1")
end
end,
s1_update = function(c, dt)
c.isConnected = c:_tryToConnect()
if c.isConnected then
c:output(true)
c:stop()
return
end
c.time = c.time + dt
if c.time >= c.timeout then
c:output(false, "tabSocket.connectting.timeout")
c:stop()
end
end,
s2 = function(c)
c:call(g_t.skipFrames(1), "s3")
end,
s4 = function(c)
c:stop()
end,
event = {
["tabSocket.disconnect"] = function(c)
c:output(false, "tabSocket.disconnect")
c:stop()
end
},
final = function(c)
if not c.isConnected then
c.tcp:close()
c.tcp = nil
end
end,
--private:
_tryToConnect = function(c)
local isSuccess, status = c.tcp:connect(c.ip, c.port)
if not isSuccess then
if status == STATUS_ALREADY_CONNECTED then
isSuccess = true
end
end
return isSuccess
end,
}
tabSocket.tabConnectted = _{
tabName = "tabConnectted",
s1 = function(c)
c._sendQueue = {}
end,
s1_update = function(c)
local body, status, partical = c:_("tcp"):receive("*a") -- read the package body
if partical and partical:len() > 0 then
local buffer = c:_("buffer")
table.insert(buffer, partical)
else
if body and body:len() > 0 then
local buffer = c:_("buffer")
table.insert(buffer, body)
end
end
if status == STATUS_CLOSED or status == STATUS_NOT_CONNECTED then
c.status = status
c:stop("s1")
return
end
local queueSize = #c._sendQueue
while queueSize > 0 do
local data = table.remove(c._sendQueue, 1)
c:send(data, true)
if queueSize == #c._sendQueue then
break
end
queueSize = #c._sendQueue
end
end,
s2 = function(c)
-- 在socket连接关闭后,给个默认的延迟是外部有一定的时间读取buffer里内容
c:call(g_t.delay, "s3", nil, c:_("closeDelay"))
end,
s4 = function(c)
c:output(false, c.status)
c:stop()
end,
event = {
["tabSocket.disconnect"] = function(c)
c:output(false, "tabSocket.disconnect")
c:stop()
end
},
final = function(c)
local tcp = c:_("tcp")
tcp:shutdown()
tcp:close()
c:_("setTcp", nil)
end,
-- public:
send = function(c, data, isRent)
if not isRent then
if #c._sendQueue > 0 then
table.insert(c._sendQueue, data)
return nil, nil, #data
end
end
local size, err, sent = c:_("tcp"):send(data)
if err == nil then
return size, nil, nil
elseif err == "timeout" then
table.insert(c._sendQueue, 1 , data:sub(sent+1))
return nil, nil, sent
else
--assert(false)
return nil, err, sent
end
end,
}
--------------------------------------------------------------------------------
--public:
--read a segment of which the boundary is determined by funDecode
tabSocket.tabReadOneSegment = _{
tabName = "tabSocket#tabReadOneSegment",
s1 = function(c, self, funDecode, updateInterval, timeout, disconnectWhenTimeout)
c.buffer = self:_("buffer")
c.tabSocket = self
c.funDecode = funDecode
c.disconnectWhenTimeout = disconnectWhenTimeout
c.lastBuffLen = 0
local segment = c:_decode(c.buffer, c.funDecode)
if segment ~= nil then
c:output(segment)
c:stop()
end
if timeout then
c:call(g_t.delay, "d1", nil, timeout)
end
c:setDynamics("s2", "updateInterval", updateInterval)
end,
s2 = g_t.empty_fun,
s2_update = function(c, dt)
local buffer = c.buffer
local newBuffLen = #buffer
local funDecode = c.funDecode
if newBuffLen ~= c.lastBuffLen and newBuffLen > 0 then
local segment = c:_decode(buffer, funDecode)
if segment ~= nil then
c:output(segment)
c:stop()
return
end
c.lastBuffLen = #buffer
end
end,
--override by setDynamics
s2_updateInterval = nil,
d2 = function(c)
if c.disconnectWhenTimeout then
c:stop("s2")
c.tabSocket:disconnect()
else
c:output(nil)
c:stop()
end
end,
event = g_t.empty_event,
-- private:
_decode = function(c, buffer, funDecode)
local newBuffLen = #buffer
if newBuffLen > 0 then
local stream
if newBuffLen > 1 then
stream = table.concat(buffer)
else
stream = buffer[1]
end
local segment, remain = funDecode(stream)
while (#buffer > 0) do
table.remove(buffer)
end
if remain and remain:len() > 0 then
table.insert(buffer, remain)
end
return segment
else
return nil
end
end,
}
--pull msgs repeatly
tabSocket.tabPullSegments = _{
tabName = "tabSocket#tabPullSegments",
s1 = function(c, self, funDecode, segmentHandler, updateInterval)
c.funDecode = funDecode
c.buffer = self:_("buffer")
c.segmentHandler = segmentHandler
c.lastBuffLen = 0
c.lastBreak = false
c:setDynamics("s2", "updateInterval", updateInterval)
end,
s2 = function(c)
c:_pullSegment()
end,
s2_update = function(c, dt)
c:_pullSegment()
end,
--override by setDynamics
s2_updateInterval = nil,
--private:
_pullSegment = function(c, frameTime)
local buffer = c.buffer
local newBuffLen = #buffer
local socket = require("socket")
local startTime = socket:gettime()
if not frameTime then
frameTime = 0.001
end
if (newBuffLen ~= c.lastBuffLen and newBuffLen > 0) or c.lastBreak then
c.lastBreak = false
local funDecode = c.funDecode
local segmentHandler = c.segmentHandler
while true do
local segment = c:_decode(buffer, funDecode)
if segment ~= nil then
segmentHandler(segment)
else
break
end
local endTime = socket:gettime()
if (endTime - startTime > frameTime) then
c.lastBreak = true
break
end
end
c.lastBuffLen = #buffer
end
end,
_decode = function(c, buffer, funDecode)
local newBuffLen = #buffer
if newBuffLen > 0 then
local stream
if newBuffLen > 1 then
stream = table.concat(buffer)
else
stream = buffer[1]
end
local segment, remain = funDecode(stream)
while (#buffer > 0) do
table.remove(buffer)
end
if remain and remain:len() > 0 then
table.insert(buffer, remain)
return segment
end
return segment
else
return nil
end
end,
}
--------------------------------------------------------------------------------
-- static private:
tabSocket.getIpV6Address = function(ip)
local result = socket.dns.getaddrinfo(ip)
local addr = nil
if result then
for k,v in pairs(result) do
if v.family == "inet6" then
addr = v.addr
break
end
end
end
return addr
end
return tabSocket