forked from cyb0124/OCRemote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
252 lines (245 loc) · 6.09 KB
/
client.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
local serverAddr, serverPort, clientName, resX, resY = ...
local encode
(function()
local encMap = {
['nil'] = function(x)
return '!'
end,
number = function(x)
return string.format("#%.17g@", x)
end,
string = function(x)
return '@' .. string.gsub(x, '@', '@.') .. '@~'
end,
boolean = function(x)
if x then return '+' else return '-' end
end,
table = function(x)
local result = '='
for k, v in pairs(x) do
result = result .. encode(k) .. encode(v)
end
return result .. '!'
end
}
function encode(x)
return encMap[type(x)](x)
end
end)()
local function decode(cb)
local s, sTable
local function sNumber()
local p, data = s, ''
function s(x)
local pos = string.find(x, '@')
if pos then
s = p
s(tonumber(data .. string.sub(x, 1, pos - 1)), string.sub(x, pos + 1))
else
data = data .. x
end
end
end
local function sString()
local p, data, escape = s, '', false
function s(x)
for i = 1, #x do
local now = string.sub(x, i, i)
if escape then
if now == '.' then
data = data .. '@'
escape = false
elseif now == '~' then
s = p
return s(data, string.sub(x, i + 1))
else
error('unknown escape: ' .. now)
end
elseif now == '@' then
escape = true
else
data = data .. now
end
end
end
end
local function sRoot()
local p = s
function s(x)
if #x > 0 then
s = p
local tag, rem = string.sub(x, 1, 1), string.sub(x, 2)
if tag == '!' then
s(nil, rem)
elseif tag == '#' then
sNumber()
s(rem)
elseif tag == '@' then
sString()
s(rem)
elseif tag == '+' then
s(true, rem)
elseif tag == '-' then
s(false, rem)
elseif tag == '=' then
sTable()
s(rem)
else
error('invalid tag: ' .. tag)
end
end
end
end
function sTable()
local p, data, key = s, {}
function s(x, rem)
if key == nil then
if x == nil then
s = p
s(data, rem)
else
key = x
sRoot()
s(rem)
end
else
data[key] = x
key = nil
sRoot()
s(rem)
end
end
sRoot()
end
function s(x, rem)
cb(x)
sRoot()
s(rem)
end
sRoot()
return function(x)
s(x)
end
end
local function resolve(short)
for addr in component.list(short) do
return addr
end
for addr in component.list() do
if string.lower(string.sub(addr, 1, #short)) == string.lower(short) then
return addr
end
end
end
local gpu = resolve("gpu")
if gpu then
gpu = component.proxy(gpu)
gpu.bind(resolve("screen"))
gpu.setResolution(resX, resY)
gpu.setDepth(gpu.maxDepth())
gpu.setBackground(0)
end
local function print(p)
if gpu then
gpu.copy(1, 1, resX, resY, 0, -1)
gpu.fill(1, resY, resX, resY, ' ')
gpu.setForeground(p.color)
gpu.set(1, resY, p.text)
end
if p.beep then
computer.beep(p.beep)
end
end
local dbAddr, db = resolve("database")
if dbAddr then
db = component.proxy(dbAddr)
end
local invCache = {}
local function getInv(invName)
if not invCache[invName] then
invCache[invName] = component.proxy(resolve(invName))
end
return invCache[invName]
end
local inet = component.proxy(resolve("internet"))
while true do
print{text = "Connecting to " .. clientName .. "@" .. serverAddr .. ":" .. serverPort, color = 0xFFFF00}
local socket = inet.connect(serverAddr, serverPort)
local timeout = computer.uptime() + 3
while socket and not socket.finishConnect() do
if computer.uptime() > timeout then
socket.close()
socket = nil
break
end
end
if socket then
local writeBuffer = encode(clientName)
print{text = "Connected", color = 0x00FF00, beep = 440}
local onRead = decode(function(p)
for _, p in ipairs(p) do
local inv, result
if p.inv then inv = getInv(p.inv) end
if p.op == "print" then
print(p)
elseif p.op == "list" then
local stacks = inv.getAllStacks(p.side)
local count = stacks.count()
result = {}
for slot = 1, count do
local item = stacks()
if item and item.name and item.size > 0 then
item.aspects = nil
result[slot] = item
elseif slot == count then
result[slot] = ''
end
end
elseif p.op == "listME" then
result = {}
for _, item in ipairs(inv.getItemsInNetwork()) do
if item and item.name and item.size > 0 then
item.aspects = nil
table.insert(result, item)
end
end
elseif p.op == "xferME" then
local me = getInv(p.me)
db.clear(1)
me.store(p.filter, dbAddr, p.entry, 1)
me.setInterfaceConfiguration(p.entry, dbAddr, p.entry, p.size)
inv.transferItem(table.unpack(p.args))
me.setInterfaceConfiguration(1)
elseif p.op == "call" then
-- transferItem (OC): fromSide, toSide, [size, [fromSlot, [toSlot]]]
result = {inv[p.fn](table.unpack(p.args))}
else
error("invalid op")
end
writeBuffer = writeBuffer .. encode(result)
end
end)
while true do
if #writeBuffer > 0 then
local n = socket.write(writeBuffer)
if not n then
print{text = "Connection closed (write)", color = 0xFF0000, beep = 880}
socket.close()
break
elseif n > 0 then
writeBuffer = string.sub(writeBuffer, n + 1)
end
end
local data = socket.read()
if data then
onRead(data)
else
print{text = "Connection closed (read)", color = 0xFF0000, beep = 880}
socket.close()
break
end
end
else
print{text = "Failed to connect", color = 0xFF0000, beep = 880}
end
end