-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.lua
More file actions
227 lines (167 loc) · 3.83 KB
/
Copy pathserver.lua
File metadata and controls
227 lines (167 loc) · 3.83 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
local socket = require("socket")
local host = "175.10.100.161"
local port = 8111
local script = "main.lua"
local eventList = { "recv", "send", "timeout" }
-- global functions
function sock_connect(sock, address, port, timeout)
sock:settimeout(0)
sock:connect(address, port)
end
function sock_recv(sock, n, timeout)
sock:settimeout(0)
local time
if timeout then
time = os.time() + timeout
end
while true do
local e = coroutine.yield({ sock = sock, time = time, recv = true })
if e.timeout then
return nil, "timeout"
end
local ok, err = sock:receive(n)
if not ok and err ~= "timeout" then
return nil, err
elseif ok then
return ok
end
end
end
function sock_send(sock, s, timeout)
local time
if timeout then
time = os.time() + timeout
end
while true do
local e = coroutine.yield({ sock = sock, time = time, send = true })
if e.timeout then
return nil, "timeout"
end
local l, err = sock:send(s)
if not l then
return nil, err
else
if l < #s then
s = s:sub(l + 1)
else
return true
end
end
end
end
function sleep(timeout)
timeout = timeout or 1
coroutine.yield({ time = os.time() + timeout })
end
local process = loadfile(script)
local function accept(sock, events)
coroutine.yield({ sock = sock, recv = true })
while true do
local newSock, err = sock:accept()
if newSock then
local newCo = coroutine.create(process)
_, events[newCo] = coroutine.resume(newCo, newSock)
end
coroutine.yield({ sock = sock, recv = true })
end
end
local function scanArgs(events)
local recvt = {}
local sendt = {}
local timeout = -1
local sock2co = {}
local time2co = {}
local currentTime = os.time()
local minWait = -1
for co, e in pairs(events) do
if e.sock then
if e.recv then
table.insert(recvt, e.sock)
end
if e.send then
table.insert(sendt, e.sock)
end
sock2co[e.sock] = co
end
if e.time then
if timeout == -1 then
table.insert(time2co, co)
minWait = e.time
else
local wait = e.time - currentTime
if wait < 0 then
wait = 0
end
if wait < minWait then
minWait = wait
time2co = {}
table.insert(time2co, co)
elseif wait == minWait then
table.insert(time2co, co)
end
end
end
end
if minWait ~= -1 then
timeout = minWait - currentTime
end
return recvt, sendt, timeout, sock2co, time2co
end
local function getReadyCoroutines(recv, send, err, sock2co, time2co)
local ready = {}
for _, sock in ipairs(recv) do
local co = sock2co[sock]
if not ready[co] then
ready[co] = {}
end
ready[co].recv = true
end
for _, sock in ipairs(send) do
local co = sock2co[sock]
if not ready[co] then
ready[co] = {}
end
ready[co].send = true
end
if err == "timeout" then
for _, co in ipairs(time2co) do
if not ready[co] then
ready[co] = {}
end
ready[co].timeout = true
end
end
return ready
end
local function executeCoroutines(ready, events)
for co, args in pairs(ready) do
local ok, event = coroutine.resume(co, args)
if not ok then
events[co] = nil
elseif coroutine.status(co) == "dead" then
i = i + 1
print("close the thread ", i, co)
events[co] = nil
else
events[co] = event
end
end
end
local events = {}
local server = socket.bind(host, port, 1024)
server:settimeout(0)
server:setoption("reuseaddr", true)
local acceptCo = coroutine.create(accept)
_, events[acceptCo] = coroutine.resume(acceptCo, server, events)
while true do
-- 1. 收集要检测的事件
local recvt, sendt, timeout, sock2co, time2co = scanArgs(events)
-- 2. 检测事件
local recv, send, err = socket.select(recvt, sendt, timeout)
-- 3. 获取等待事件已经好了的协程
local ready = getReadyCoroutines(recv, send, err, sock2co, time2co)
-- 4. 执行协程
executeCoroutines(ready, events)
-- 5. 执行垃圾收集,回收资源
collectgarbage()
end