-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_udp.lua
228 lines (228 loc) · 6.24 KB
/
client_udp.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
package.cpath = ";./?51.dll;./debug/?.dll;" .. package.cpath
package.path = ";./socket/?.lua;" .. package.path
require("iuplua")
local iup
iup = _G.iup
socket = require('socket')
local dumpPrint, lenTbl, cmpStartsString, clearTable, getTimeStamp
do
local _obj_0 = require("utils")
dumpPrint, lenTbl, cmpStartsString, clearTable, getTimeStamp = _obj_0.dumpPrint, _obj_0.lenTbl, _obj_0.cmpStartsString, _obj_0.clearTable, _obj_0.getTimeStamp
end
local arg
arg = _G.arg
clientSocket = socket.udp()
bindname = nil
bindport = nil
peername = nil
peerport = nil
form = nil
username = "unknown"
queueInput = nil
tsTbl = { }
local ClientSpawn
do
local _class_0
local _base_0 = {
setForm = function(self, f)
form = f
end,
setTimestampList = function(self, listTbl)
tsTbl = listTbl
end,
setBindname = function(self, bn)
bindname = bn
end,
setBindport = function(self, bp)
bindport = tonumber(bp)
end,
setPeername = function(self, pn)
peername = pn
end,
setPeerport = function(self, pp)
peerport = tonumber(pp)
end,
setUsername = function(self, usr)
username = usr
end,
setInputQueue = function(self, queue)
queueInput = queue
end,
getSocketInfo = function(self)
print("Using sockname: " .. bindname .. ":" .. bindport)
return print("Using peername: " .. peername .. ":" .. peerport)
end,
initSocket = function(self)
clientSocket:setsockname(bindname, bindport)
clientSocket:settimeout(44)
clientSocket:setpeername(peername, peerport)
self:getSocketInfo()
end,
testRun = function(self, inputTbl)
table.insert(inputTbl, "confirm:" .. getTimeStamp() .. ";;usr: test run")
queueInput = inputTbl
self:run()
queueInput = nil
end,
addInputQueue = function(self, msg)
return table.insert(queueInput, msg)
end,
run = function(self)
if (queueInput ~= nil) then
for i, msg in ipairs(queueInput) do
local _continue_0 = false
repeat
if msg == nil then
print("msg is nil")
_continue_0 = true
break
end
local packet = getTimeStamp() .. ";;" .. msg
print("packet to send: " .. packet)
clientSocket:send(packet)
_continue_0 = true
until true
if not _continue_0 then
break
end
end
clearTable(queueInput)
end
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self)
return print("new")
end,
__base = _base_0,
__name = "ClientSpawn"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0
self.fnTick = function()
self:start()
return iup.DEFAULT
end
self.start = function()
local canread = socket.select({
clientSocket
}, nil, 0)
for i, inSocket in ipairs(canread) do
local _continue_0 = false
repeat
local line, err = inSocket:receive()
dumpPrint(tsTbl)
if line == nil or line == "nil" then
print("Received an empty message.")
if err then
print("error: " .. tostring(err))
end
_continue_0 = true
break
end
local inSockValue = tostring(line) .. ""
print("inSock: " .. inSockValue)
local splitSock = { }
for str in string.gmatch(inSockValue, "([^;;]+)") do
splitSock[lenTbl(splitSock)] = tostring(str)
end
local ts, rcptValue = splitSock[0], splitSock[1]
local maxCheckout = 10
local isInvalid = false
for i = 0, (#tsTbl or maxCheckout), 1 do
if tsTbl[i] == ts then
print("Received a duplicated message at TS: " .. ts)
isInvalid = true
end
end
if isInvalid == true then
_continue_0 = true
break
end
if #tsTbl > 10 then
tsTbl[9] = nil
end
print("tsTbl content:")
dumpPrint(tsTbl)
table.insert(tsTbl, 1, ts)
print("received: " .. rcptValue)
if (rcptValue ~= nil and rcptValue ~= "nil") then
if not cmpStartsString(rcptValue, "confirm") then
local confirmMsg = getTimeStamp() .. ";;confirm: " .. rcptValue
print("sending: " .. confirmMsg)
clientSocket:send(confirmMsg)
if form ~= nil then
form:updateClient(rcptValue)
end
end
if form ~= nil then
form:updateStatus("Connected")
end
print("added " .. rcptValue)
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
end
self.startTimer = function()
print("start timer")
local timerTick = 100
print("timer set at: " .. timerTick)
local timer = iup.timer({
time = timerTick,
action_cb = self.fnTick
})
timer.run = "YES"
end
ClientSpawn = _class_0
end
local cmdUsage
cmdUsage = function()
if arg ~= nil and arg[1] ~= nil and arg[2] ~= nil then
print("cmd mode")
local bp = arg[1]
local pp = arg[2]
local pn
if arg[3] ~= nil then
pn = arg[3]
else
pn = "localhost"
end
local clientConnector = ClientSpawn
clientConnector:setBindname("*")
clientConnector:setBindport(bp)
clientConnector:setPeername(pn)
clientConnector:setPeerport(pp)
clientConnector:initSocket()
clientConnector:setUsername("bob")
clientConnector:testRun({ })
clientConnector:setInputQueue({ })
clientConnector:setTimestampList({ })
clientConnector:startTimer()
local tsDup = getTimeStamp()
print("sending dup")
clientSocket:send(tsDup .. ";;" .. username .. ": " .. "hey")
clientSocket:send(tsDup .. ";;" .. username .. ": " .. "hey")
while true do
local _, msg = iup.GetParam("Title", nil, "Msg to reply: %s\n", "")
clientSocket:send(getTimeStamp() .. ";;" .. username .. ": " .. msg)
end
return iup.MainLoop()
end
end
cmdUsage()
return {
ClientSpawn = ClientSpawn,
clientSocket = clientSocket
}