forked from 26F-Studio/Zenitha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.lua
374 lines (328 loc) · 9.36 KB
/
tcp.lua
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
---@alias Zenitha.TCP.sendID string 0 = server/broadcast, 1+ = client id, NUMBER ONLY
---@alias Zenitha.TCP.recvID Zenitha.TCP.sendID|Zenitha.TCP.sendID[]|nil 0 = server/broadcast, 1+ = client id, NUMBER ONLY, nil = broadcast
---@class Zenitha.TCP.Client
---@field conn LuaSocket.client
---@field id string '1'|'2'|...
---@field sockname string
---@field timestamp number
---@class Zenitha.TCP.MsgPack
---@field config? Zenitha.TCP.MsgCfg
---@field data? any
---@field sender? Zenitha.TCP.sendID
---@field receiver? Zenitha.TCP.recvID
---@field bus? Zenitha.TCP.busID
---@class Zenitha.TCP.ConfigMsg
---@field action Zenitha.TCP.ConfigMsgAction
---@field id string
---@field flag boolean
---@field count number
---@field time number
---@field bus string
local ins,rem=table.insert,table.remove
local TCP={}
local S_thread=love.thread.newThread('Zenitha/tcp_server.lua'); S_thread:start()
local S_running=false
local S_confCHN=love.thread.getChannel('tcp_s_config')
local S_sendCHN=love.thread.getChannel('tcp_s_send')
local S_recvCHN=love.thread.getChannel('tcp_s_receive')
local function S_daemonFunc()
while true do
DEBUG.yieldT(0.626)
if not S_thread:isRunning() then
print(S_thread:getError())
return
end
end
end
local function checkRecvID(id)
if type(id)=='string' then id={id} end
for i=#id,1,-1 do
if id[i]:find('[^0-9A-Za-z_]') then
rem(id,i)
end
end
return id
end
---Get client connection status
function TCP.S_isRunning()
return S_running
end
---Start server
---@param port number 0~65535
function TCP.S_start(port)
if S_running then return end
assert(type(port)=='number' and port>=1 and port<=65535 and port%1==0,"TCP.S_start(port): Need 0~65535")
TASK.removeTask_code(S_daemonFunc)
TASK.new(S_daemonFunc)
S_confCHN:push(port)
local result=S_recvCHN:demand()
if result.success then
S_running=true
else
MSG.new('error', result.message)
end
end
---Stop the TCP server
function TCP.S_stop()
if not S_running then return end
S_confCHN:push{action='close'}
S_sendCHN:clear()
S_recvCHN:clear()
S_running=false
end
---Disconnect a client
---@param id Zenitha.TCP.recvID
function TCP.S_kick(id)
if not S_running then return end
S_confCHN:push{action='kick',id=checkRecvID(id)}
end
---Set whether brodcast is allowed or not
---@param flag boolean only `true` take effect
function TCP.S_setAllowBroadcast(flag)
S_confCHN:push{action='setAllowBroadcast',flag=flag==true}
end
---Send data to client(s)
---@param data any must be lua or love object
---@param id Zenitha.TCP.recvID
function TCP.S_send(data,id)
if not S_running then return end
---@type Zenitha.TCP.MsgPack
local pack={
data=data,
receiver=checkRecvID(id),
}
S_sendCHN:push(pack)
end
---Receive data from client(s)
---@return Zenitha.TCP.MsgPack?
function TCP.S_receive()
return S_recvCHN:pop()
end
local C_thread=love.thread.newThread('Zenitha/tcp_client.lua'); C_thread:start()
local C_running=false
local C_confCHN=love.thread.getChannel('tcp_c_config')
local C_sendCHN=love.thread.getChannel('tcp_c_send')
local C_recvCHN=love.thread.getChannel('tcp_c_receive')
local function C_daemonFunc()
while true do
DEBUG.yieldT(0.626)
if not C_thread:isRunning() then
print(C_thread:getError())
return
end
end
end
---Get client connection status
function TCP.C_isRunning()
return C_running
end
---Connect to server
---@param ip string
---@param port number
function TCP.C_connect(ip,port)
if C_running then return end
TASK.removeTask_code(C_daemonFunc)
TASK.new(C_daemonFunc)
C_confCHN:push(ip)
C_confCHN:push(port)
local result=C_recvCHN:demand()
if result.success then
C_running=true
else
MSG.new('error', result.message)
end
end
---Disconnect from the server
function TCP.C_disconnect()
if not C_running then return end
C_confCHN:push{action='close'}
C_sendCHN:clear()
C_recvCHN:clear()
C_running=false
end
---Send data to server
---@param data any must be lua or love object
---@param id Zenitha.TCP.recvID
function TCP.C_send(data,id)
---@type Zenitha.TCP.MsgPack
local pack={
data=data,
receiver=checkRecvID(id),
}
C_sendCHN:push(pack)
end
---Receive data from server
---@return Zenitha.TCP.MsgPack?
function TCP.C_receive()
return C_recvCHN:pop()
end
--------------------------------------------------------------
-- Use the following pub/sub features when you need more scalable communication.
---@alias Zenitha.TCP.busID string [0-9A-Za-z_]+
---@alias Zenitha.TCP.MsgCfg
---| 'bus.get' recv: data=Bus name list
---| 'bus.join' recv: data=joined client id
---| 'bus.quit' recv: data=quited client id
---| 'bus.close' recv: data=quited client id
---@alias Zenitha.TCP.ConfigMsgAction
---| 'bus.get' send
---| 'bus.join' send: bus=Bus name
---| 'bus.quit' send
---@class Zenitha.TCP.Bus
---@field name string
---@field createTime number
---@field maxMember number
---@field maxAliveTime number
---@field startIdleTime? number
---@field members table
local S_busRecvCHN=love.thread.getChannel('tcp_s_receiveBus')
local S_busPackBuffer={} ---@type Zenitha.TCP.MsgPack[]
local function checkBusName(name)
assert(type(name)=='string' and not name:find('[^0-9A-Za-z_]'),"Need string of 0-9/A-Z/_")
end
---@param count number
function TCP.S_Bus_setMaxCount(count)
assert(type(count)=='number' and count>0 and count%1==0,"TCP.S_Bus_setMaxCount(count): Need positive int")
S_confCHN:push{action='setMaxBus',count=count}
end
---@param time number Negative numbers treated as 0
function TCP.S_Bus_setDefaultMaxAliveTime(time)
assert(type(time)=='number' and time>=0,"TCP.S_Bus_setDefaultMaxAliveTime(time): Need number")
S_confCHN:push{action='setBusMaxAliveTime',time=time}
end
---@param count number
function TCP.S_Bus_setDefaultMaxMemberCount(count)
assert(type(count)=='number' and count>0 and count%1==0,"TCP.S_Bus_setMaxCount(count): Need positive int")
S_confCHN:push{action='setBusMaxMember',count=count}
end
---@param name Zenitha.TCP.busID
---@param maxMember? number
---@param maxAliveTime? number
---@return boolean #Success or not, will fail when reached max count
function TCP.S_Bus_new(name,maxMember,maxAliveTime)
if not S_running then return false end
checkBusName(name)
S_confCHN:push{
action='bus.create',
bus=name,
maxMember=maxMember,
maxAliveTime=maxAliveTime,
}
return S_recvCHN:demand().success
end
---@return string[] #List of Bus names
function TCP.S_Bus_get()
if not S_running then return {} end
S_confCHN:push{action='bus.get'}
local list=S_recvCHN:demand()
for i=#list,1,-1 do
if not pcall(checkBusName,list[i]) then
rem(list,i)
end
end
return list
end
---@param name Zenitha.TCP.busID
function TCP.S_Bus_join(name)
if not S_running then return false end
checkBusName(name)
S_confCHN:push{
action='bus.join',
bus=name,
}
end
---@param name Zenitha.TCP.busID
function TCP.S_Bus_quit(name)
checkBusName(name)
S_confCHN:push{
action='bus.quit',
bus=name,
}
end
---@param name Zenitha.TCP.busID
function TCP.S_Bus_del(name)
if not S_running then return false end
checkBusName(name)
S_confCHN:push{
action='bus.close',
bus=name,
}
end
---@param name Zenitha.TCP.busID
---@param data any must be lua or love object
function TCP.S_Bus_Send(name,data)
checkBusName(name)
---@type Zenitha.TCP.MsgPack
local pack={
data=data,
bus=name,
}
S_sendCHN:push(pack)
end
---@param name Zenitha.TCP.busID
---@return Zenitha.TCP.MsgPack
function TCP.S_Bus_Receive(name)
checkBusName(name)
while true do
---@type Zenitha.TCP.MsgPack
local pack=S_busRecvCHN:pop()
if not pack then break end
ins(S_busPackBuffer,pack)
end
for i=1,#S_busPackBuffer do
if S_busPackBuffer[i].bus==name then
return rem(S_busPackBuffer,i)
end
end
end
local C_busRecvCHN=love.thread.getChannel("tcp_c_receiveBus")
local C_busPackBuffer={} ---@type Zenitha.TCP.MsgPack[]
---Send Bus getting request, receive data from C_receive
function TCP.C_Bus_get()
if not C_running then return false end
C_confCHN:push{action='bus.get'}
end
---@param name Zenitha.TCP.busID
function TCP.C_Bus_join(name)
checkBusName(name)
C_confCHN:push{
action='bus.join',
bus=name,
}
end
---@param name Zenitha.TCP.busID
function TCP.C_Bus_quit(name)
checkBusName(name)
C_confCHN:push{
action='bus.quit',
bus=name,
}
end
---@param name Zenitha.TCP.busID
---@param data any must be lua or love object
function TCP.C_Bus_send(name,data)
checkBusName(name)
---@type Zenitha.TCP.MsgPack
local pack={
data=data,
bus=name,
}
C_sendCHN:push(pack)
end
---@param name Zenitha.TCP.busID
---@return Zenitha.TCP.MsgPack
function TCP.C_Bus_receive(name)
checkBusName(name)
while true do
---@type Zenitha.TCP.MsgPack
local pack=C_busRecvCHN:pop()
if not pack then break end
ins(C_busPackBuffer,pack)
end
for i=1,#C_busPackBuffer do
if C_busPackBuffer[i].bus==name then
return rem(C_busPackBuffer,i)
end
end
end
return TCP