-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathinterface_shared.lua
More file actions
307 lines (275 loc) · 8.71 KB
/
Copy pathinterface_shared.lua
File metadata and controls
307 lines (275 loc) · 8.71 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
VFS.Include(LIB_LOBBY_DIRNAME .. "lobby.lua")
LOG_SECTION = "liblobby"
if not Spring.GetConfigInt("LuaSocketEnabled", 0) == 1 then
Spring.Log(LOG_SECTION, LOG.ERROR, "LuaSocketEnabled is disabled")
return false
end
Interface = Lobby:extends{}
function Interface:init()
-- dumpConfig()
self.messagesSentCount = 0
self.lastSentSeconds = Spring.GetTimer()
self.status = "offline"
self.finishedConnecting = false
self.listeners = {}
-- Inheritance is too shallow for interface_zerok.lua to get its own init.
if self.InheritanceIsBrokenWorkaroundInit then
self:InheritanceIsBrokenWorkaroundInit()
end
-- timeout (in seconds) until first message is received from server before disconnect is assumed
self.connectionTimeout = 50
-- private
self.buffer = ""
self:super("init")
end
function Interface:Connect(host, port, user, password, cpu, localIP, lobbyVersion)
--host = "test.zero-k.info"
--port = 8202
self:super("Connect", host, port)
if self.client then
self.client:close()
end
self.client = socket.tcp()
self.client:settimeout(0)
self.loginData = {user, password, cpu, localIP, lobbyVersion}
self._startedConnectingTime = os.clock()
local res, err = self.client:connect(host, port)
if res == nil and err == "host not found" then
self:_OnDisconnected("Host not found")
-- The socket is expected to return "timeout" immediately since timeout time is set to 0
elseif not (res == nil and err == "timeout") then
Spring.Log(LOG_SECTION, LOG.ERROR, "Error in connect: " .. err)
else
self.status = "connecting"
end
return true
end
function Interface:Disconnect()
self.status = "offline"
self.finishedConnecting = false
if self.client then
self.client:close()
self.client = nil
end
self:_OnDisconnected(nil, true)
end
function Interface:_SendCommand(command, sendMessageCount)
if sendMessageCount then
self.messagesSentCount = self.messagesSentCount + 1
command = "#" .. self.messagesSentCount .. " " .. command
end
if not self.client then
Spring.Echo("Missing self.client!!!")
return
end
local numBytes, errorCode, numActuallySent
local maxPacketSize = 32000 -- 65535 actually for TCP
local commandLength = #command
local totalSent = 0
if commandLength > maxPacketSize then
-- previously timeout was 0, which didnt allow sending large messages without timeouting
self.client:settimeout(2)
for i=1,commandLength, maxPacketSize do
local commandPart = string.sub(command, i, i + maxPacketSize -1 )
if i + maxPacketSize > commandLength and commandPart[#commandPart] ~= "\n" then
commandPart = commandPart .. '\n'
end
-- https://w3.impa.br/~diego/software/luasocket/tcp.html#send
--Spring.Echo("sending a big chunk",i, #commandpart)
numBytes, errorCode, numActuallySent = self.client:send(commandPart)
totalSent = totalSent + (numBytes or numActuallySent)
if numBytes == nil then
break
end
end
self.client:settimeout(0)
else
if command[#command] ~= "\n" then
command = command .. "\n"
end
numBytes, errorCode, numActuallySent = self.client:send(command)
totalSent = totalSent + (numBytes or numActuallySent)
end
if numBytes == nil then
Spring.Echo("Error in Interface:_SendCommand while sending", numBytes, errorCode, numActuallySent, commandLength, totalSent)
--Spring.Echo(command)
end
self:_CallListeners("OnCommandSent", command:sub(1, #command-1))
self.lastSentSeconds = Spring.GetTimer()
end
function Interface:SendCustomCommand(command)
self:_SendCommand(command, false)
end
function Interface:ProcessBuffer()
if not self.commandBuffer then
return false
end
self.bufferExecutionPos = self.bufferExecutionPos + 1
local command = self.commandBuffer[self.bufferExecutionPos]
if not self.commandBuffer[self.bufferExecutionPos + 1] then
self:CommandReceived(command)
self.commandBuffer = false
return false
end
self:CommandReceived(command)
return true
end
function Interface:SendCommandToBuffer(cmdName)
if not self.bufferBypass then
return true
end
return not self.bufferBypass[cmdName]
end
function Interface:CommandReceived(command)
local cmdId, cmdName, arguments
local argumentsPos = false
if command:sub(1,1) == "#" then
i = command:find(" ")
cmdId = command:sub(2, i - 1)
argumentsPos = command:find(" ", i + 1)
if argumentsPos ~= nil then
cmdName = command:sub(i + 1, argumentsPos - 1)
else
cmdName = command:sub(i + 1)
end
else
argumentsPos = command:find(" ")
if argumentsPos ~= nil then
cmdName = command:sub(1, argumentsPos - 1)
else
cmdName = command
end
end
if self.bufferCommandsEnabled and self:SendCommandToBuffer(cmdName) then
if not self.commandBuffer then
self.commandBuffer = {}
self.commandsInBuffer = 0
self.bufferExecutionPos = 0
end
self.commandsInBuffer = self.commandsInBuffer + 1
self.commandBuffer[self.commandsInBuffer] = command
self:_CallListeners("OnCommandBuffered", command)
return
end
if argumentsPos then
arguments = command:sub(argumentsPos + 1)
end
self:_OnCommandReceived(cmdName, arguments, cmdId)
end
function Interface:_GetCommandPattern(cmdName)
return Interface.commandPattern[cmdName]
end
function Interface:_GetCommandFunction(cmdName)
return Interface.commands[cmdName], Interface.commandPattern[cmdName]
end
function Interface:_GetJsonCommandFunction(cmdName)
return Interface.jsonCommands[cmdName]
end
-- status can be one of: "offline", "connected", "connected" and "disconnected"
function Interface:GetConnectionStatus()
return self.status
end
function Interface:_OnCommandReceived(cmdName, arguments, cmdId)
local commandFunction, pattern = self:_GetCommandFunction(cmdName)
local fullCmd
if arguments ~= nil then
fullCmd = cmdName .. " " .. arguments
else
fullCmd = cmdName
end
if commandFunction ~= nil then
local pattern = self:_GetCommandPattern(cmdName)
if pattern then
local funArgs = {arguments:match(pattern)}
if #funArgs ~= 0 then
commandFunction(self, unpack(funArgs))
else
Spring.Log(LOG_SECTION, LOG.ERROR, "Failed to match command: ", cmdName, ", args: " .. tostring(arguments) .. " with pattern: ", pattern)
end
else
--Spring.Echo("No pattern for command: " .. cmdName)
commandFunction(self)
end
else
local jsonCommandFunction = self:_GetJsonCommandFunction(cmdName)
if jsonCommandFunction ~= nil then
local success, obj = pcall(json.decode, arguments)
if not success then
Spring.Log(LOG_SECTION, LOG.ERROR, "Failed to parse JSON: " .. tostring(arguments))
end
if obj then
jsonCommandFunction(self, obj)
end
else
Spring.Log(LOG_SECTION, LOG.ERROR, "No such function: " .. cmdName .. ", for command: " .. fullCmd)
end
end
self:_CallListeners("OnCommandReceived", fullCmd)
end
function Interface:_SocketUpdate()
if self.client == nil then
return
end
-- get sockets ready for read
local readable, writeable, err = socket.select({self.client}, {self.client}, 0)
local host, port = self.client:getpeername()
-- if host == nil then
-- self.client:shutdown()
-- self.client = nil
-- self:_OnDisconnected("Cannot resolve host.")
-- return
-- end
local brec, bsent, age = self.client:getstats()
if err ~= nil then
-- some error happened in select
if err == "timeout" then
-- we've received no data after connecting for a while. assume connection cannot be established
if brec == 0 and os.clock() - self._startedConnectingTime > self.connectionTimeout then
self.client:shutdown()
self.client = nil
self:_OnDisconnected("No response from host.")
end
-- nothing to do, return
return
end
Spring.Log(LOG_SECTION, LOG.ERROR, "Error in select: " .. err)
end
for _, input in ipairs(readable) do
local s, status, commandsStr = input:receive('*a') --try to read all data
if (status == "timeout" or status == nil) and commandsStr ~= nil and commandsStr ~= "" then
Spring.Log(LOG_SECTION, LOG.DEBUG, commandsStr)
local commands = explode("\n", commandsStr)
commands[1] = self.buffer .. commands[1]
for i = 1, #commands-1 do
local command = commands[i]
if command ~= nil then
self:CommandReceived(command)
end
end
self.buffer = commands[#commands]
elseif status == "closed" then
Spring.Log(LOG_SECTION, LOG.INFO, "Disconnected from server.")
input:close()
-- if status is "offline", user initiated the disconnection
if self.status ~= "offline" then
self.status = "disconnected"
end
self:_OnDisconnected()
end
end
end
function Interface:SafeUpdate()
self:super("SafeUpdate")
self:_SocketUpdate()
-- prevent timeout with PING
if self.status == "connected" then
local currentTime = Spring.GetTimer()
if Spring.DiffTimers(currentTime, self.lastSentSeconds) > 30 then
self:Ping()
end
end
end
function Interface:Update()
xpcall(function() self:SafeUpdate() end,
function(err) self:_PrintError(err) end )
end