forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.lua
355 lines (321 loc) · 6.98 KB
/
socket.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
local driver = require "socketdriver"
local skynet = require "skynet"
local assert = assert
local socket = {} -- api
local buffer_pool = {} -- store all message buffer object
local socket_pool = setmetatable( -- store all socket object
{},
{ __gc = function(p)
for id,v in pairs(p) do
driver.close(id)
-- don't need clear v.buffer, because buffer pool will be free at the end
p[id] = nil
end
end
}
)
local socket_message = {}
local function wakeup(s)
local co = s.co
if co then
s.co = nil
skynet.wakeup(co)
end
end
local function suspend(s)
assert(not s.co)
s.co = coroutine.running()
skynet.wait()
-- wakeup closing corouting every time suspend,
-- because socket.close() will wait last socket buffer operation before clear the buffer.
if s.closing then
skynet.wakeup(s.closing)
end
end
-- read skynet_socket.h for these macro
-- SKYNET_SOCKET_TYPE_DATA = 1
socket_message[1] = function(id, size, data)
local s = socket_pool[id]
if s == nil then
skynet.error("socket: drop package from " .. id)
driver.drop(data, size)
return
end
local sz = driver.push(s.buffer, buffer_pool, data, size)
local rr = s.read_required
local rrt = type(rr)
if rrt == "number" then
-- read size
if sz >= rr then
s.read_required = nil
wakeup(s)
end
else
if s.buffer_limit and sz > s.buffer_limit then
skynet.error(string.format("socket buffer overlow: fd=%d size=%d", id , sz))
driver.clear(s.buffer,buffer_pool)
driver.close(id)
return
end
if rrt == "string" then
-- read line
if driver.readline(s.buffer,nil,rr) then
s.read_required = nil
wakeup(s)
end
end
end
end
-- SKYNET_SOCKET_TYPE_CONNECT = 2
socket_message[2] = function(id, _ , addr)
local s = socket_pool[id]
if s == nil then
return
end
-- log remote addr
s.connected = true
wakeup(s)
end
-- SKYNET_SOCKET_TYPE_CLOSE = 3
socket_message[3] = function(id)
local s = socket_pool[id]
if s == nil then
return
end
s.connected = false
wakeup(s)
end
-- SKYNET_SOCKET_TYPE_ACCEPT = 4
socket_message[4] = function(id, newid, addr)
local s = socket_pool[id]
if s == nil then
driver.close(newid)
return
end
s.callback(newid, addr)
end
-- SKYNET_SOCKET_TYPE_ERROR = 5
socket_message[5] = function(id)
local s = socket_pool[id]
if s == nil then
skynet.error("socket: error on unknown", id)
return
end
if s.connected then
skynet.error("socket: error on", id)
end
s.connected = false
wakeup(s)
end
skynet.register_protocol {
name = "socket",
id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6
unpack = driver.unpack,
dispatch = function (_, _, t, n1, n2, data)
socket_message[t](n1,n2,data)
end
}
local function connect(id, func)
local newbuffer
if func == nil then
newbuffer = driver.buffer()
end
local s = {
id = id,
buffer = newbuffer,
connected = false,
read_require = false,
co = false,
callback = func,
}
socket_pool[id] = s
suspend(s)
if s.connected then
return id
end
end
function socket.open(addr, port)
local id = driver.connect(addr,port)
return connect(id)
end
function socket.bind(os_fd)
local id = driver.bind(os_fd)
return connect(id)
end
function socket.stdin()
return socket.bind(0)
end
function socket.start(id, func)
driver.start(id)
return connect(id, func)
end
function socket.shutdown(id)
local s = socket_pool[id]
if s then
if s.buffer then
driver.clear(s.buffer,buffer_pool)
end
if s.connected then
driver.close(id)
end
end
end
function socket.close(id)
local s = socket_pool[id]
if s == nil then
return
end
if s.connected then
driver.close(s.id)
-- notice: call socket.close in __gc should be carefully,
-- because skynet.wait never return in __gc, so driver.clear may not be called
if s.co then
-- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediatel
-- wait reading coroutine read the buffer.
assert(not s.closing)
s.closing = coroutine.running()
skynet.wait()
else
suspend(s)
end
s.connected = false
end
socket.shutdown(id)
assert(s.lock_set == nil or next(s.lock_set) == nil)
socket_pool[id] = nil
end
function socket.read(id, sz)
local s = socket_pool[id]
assert(s)
if sz == nil then
-- read some bytes
local ret = driver.readall(s.buffer, buffer_pool)
if ret ~= "" then
return ret
end
if not s.connected then
return false, ret
end
assert(not s.read_required)
s.read_required = 0
suspend(s)
ret = driver.readall(s.buffer, buffer_pool)
if ret ~= "" then
return ret
else
return false, ret
end
end
local ret = driver.pop(s.buffer, buffer_pool, sz)
if ret then
return ret
end
if not s.connected then
return false, driver.readall(s.buffer, buffer_pool)
end
assert(not s.read_required)
s.read_required = sz
suspend(s)
ret = driver.pop(s.buffer, buffer_pool, sz)
if ret then
return ret
else
return false, driver.readall(s.buffer, buffer_pool)
end
end
function socket.readall(id)
local s = socket_pool[id]
assert(s)
if not s.connected then
local r = driver.readall(s.buffer, buffer_pool)
return r ~= "" and r
end
assert(not s.read_required)
s.read_required = true
suspend(s)
assert(s.connected == false)
return driver.readall(s.buffer, buffer_pool)
end
function socket.readline(id, sep)
sep = sep or "\n"
local s = socket_pool[id]
assert(s)
local ret = driver.readline(s.buffer, buffer_pool, sep)
if ret then
return ret
end
if not s.connected then
return false, driver.readall(s.buffer, buffer_pool)
end
assert(not s.read_required)
s.read_required = sep
suspend(s)
if s.connected then
return driver.readline(s.buffer, buffer_pool, sep)
else
return false, driver.readall(s.buffer, buffer_pool)
end
end
function socket.block(id)
local s = socket_pool[id]
if not s or not s.connected then
return false
end
assert(not s.read_required)
s.read_required = 0
suspend(s)
return s.connected
end
socket.write = assert(driver.send)
socket.lwrite = assert(driver.lsend)
socket.header = assert(driver.header)
function socket.invalid(id)
return socket_pool[id] == nil
end
function socket.listen(host, port, backlog)
if port == nil then
host, port = string.match(host, "([^:]+):(.+)$")
port = tonumber(port)
end
return driver.listen(host, port, backlog)
end
function socket.lock(id)
local s = socket_pool[id]
assert(s)
local lock_set = s.lock
if not lock_set then
lock_set = {}
s.lock = lock_set
end
if #lock_set == 0 then
lock_set[1] = true
else
local co = coroutine.running()
table.insert(lock_set, co)
skynet.wait()
end
end
function socket.unlock(id)
local s = socket_pool[id]
assert(s)
local lock_set = assert(s.lock)
table.remove(lock_set,1)
local co = lock_set[1]
if co then
skynet.wakeup(co)
end
end
-- abandon use to forward socket id to other service
-- you must call socket.start(id) later in other service
function socket.abandon(id)
local s = socket_pool[id]
if s and s.buffer then
driver.clear(s.buffer,buffer_pool)
end
socket_pool[id] = nil
end
function socket.limit(id, limit)
local s = assert(socket_pool[id])
s.buffer_limit = limit
end
return socket