-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmess.lua
225 lines (181 loc) · 4.61 KB
/
mess.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
--go@ plink d10 -t -batch sdk/bin/linux/luajit sdk/lua/mess.lua
--[[
Simple TCP protocol for sending and receiving Lua values.
Written by Cosmin Apreutesei. Public Domain.
Features:
- fast binary de/serialization of Lua objects
- full-duplex
- raising & catching errors in event handlers
SERVER
mess_listen([tcp, ]host, port, onaccept, [onerror], [server_name]) -> server
onaccept(server, channel)
onerror(server, error)
server:stop()
CLIENT
[try_]mess_connect([tcp, ]host, port, [timeout]) -> channel
CHANNEL
channel:[try_]send(msg, [expires]) -> ok | false,err
channel:[try_]recv([expires]) -> ok,msg | nil,err
channel:recvall(onmessage, [onerror])
onmessage(channel, msg)
onerror(channel, err)
channel:[try_]close()
channel:closed()
channel:onclose(fn)
channel:wait_job()
channel:wait_until(expires)
channel:wait(timeout)
PROTOCOL
mess_protocol(tcp) -> channel
]]
require'glue'
require'sock'
local
cast, u32p =
cast, u32p
local channel = {maxlen = 16 * 1024^2}
function mess_protocol(tcp)
local chan = update({tcp = tcp}, channel)
local buf = string_buffer()
tcp:onclose(function()
buf:free()
buf = nil
end)
function chan:send(msg, exp)
buf:reset()
buf:reserve(4)
buf:commit(4)
buf:encode(msg)
local p, len = buf:ref()
cast(u32p, p)[0] = len - 4
tcp:send(p, len, exp)
return true
end
chan.try_send = protect_io(chan.send)
local buf = string_buffer()
tcp:onclose(function()
buf:free()
buf = nil
end)
function chan:recv()
buf:reset()
local plen = buf:reserve(4)
tcp:recvn(plen, 4)
local len = cast(u32p, plen)[0]
tcp:checkp(len <= self.maxlen, 'message too big: %d', len)
local p = buf:reset():reserve(len)
tcp:recvn(p, len)
buf:commit(len)
return buf:decode()
end
chan.try_recv = protect_io(chan.recv)
return chan
end
local function wrapfn(event, fn, onerror, self)
return function(...)
local ok, err = pcall(fn, ...)
if not ok then
if onerror then
onerror(self, err)
end
log('ERROR', 'mess', event, '%s', err)
end
end
end
function mess_listen(s, host, port, onaccept, onerror, server_name)
if not issocket(s) then
return mess_listen(tcp(), s, host, port, onaccept, onerror)
end
server_name = server_name or 'mess'
local server = {tcp = s}
s:setopt('reuseaddr', true)
s:listen(host, port)
liveadd(s, server_name)
local stop
function server:stop()
stop = true
s:shutdown'r'
s:close()
end
local onaccept = wrapfn('accept', onaccept, onerror, server)
resume(thread(function()
while not stop do
local ctcp, err, retry = s:try_accept()
if not ctcp then
if s:closed() then --stop() called.
break
end
s:check_io(retry, err)
--temporary network error. retry without killing the CPU.
wait(0.2)
goto skip
end
liveadd(ctcp, server_name)
resume(thread(function()
local chan = mess_protocol(ctcp)
onaccept(server, chan)
ctcp:close()
end, server_name..'-accepted %s', ctcp))
::skip::
end
end, server_name..'-listen %s', s))
return server
end
function mess_connect(s, host, port, timeout)
if not issocket(s) then
return mess_connect(tcp(), s, host, port)
end
s:settimeout(timeout)
s:connect(host, port)
s:settimeout(nil)
return mess_protocol(s)
end
try_mess_connect = protect_io(mess_connect)
function channel:try_close () return self.tcp:try_close() end
function channel:close () return self.tcp:close() end
function channel:onclose (fn) return self.tcp:onclose(fn) end
function channel:closed () return self.tcp:closed() end
function channel:wait_job () return self.tcp:wait_job() end
function channel:wait_until (expires) return self.tcp:wait_until(expires) end
function channel:wait (timeout) return self.tcp:wait(timeout) end
function channel:try_recvall(onmessage, onerror)
local onmessage = wrapfn('recvall', onmessage, onerror, self)
while not self:closed() do
local msg, err = self:try_recv()
if not msg and err then
if iserror(err, 'io') and err.message == 'eof' then
break
else
return nil, err
end
else
onmessage(self, msg)
end
end
return true
end
function channel:recvall(...)
return assert(self:try_recvall(...))
end
if not ... then
logging.verbose = true
logging.debug = true
local server = mess_listen('127.0.0.1', '5555', function(self, chan)
chan:recvall(function(self, msg)
self:send(msg)
end)
chan:close()
self:stop()
end)
resume(thread(function()
local chan = mess_connect('127.0.0.1', '5555', 1)
for i = 1, 20 do
chan:send{a = i, b = 2*i, s = tostring(i)}
local t = chan:recv()
pr(t)
end
chan:close()
end, 'client'))
start()
end
return M