-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.lua
143 lines (141 loc) · 2.38 KB
/
hook.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
local hooks={}
local timers={}
hook={
interval=nil,
sel={},
rsel={},
meta={},
created={},
hooks=hooks,
timers=timers,
}
local hook=hook
local function nxt(tbl)
local n=1
while tbl[n] do
n=n+1
end
return n
end
function tpairs(tbl)
local s={}
local c=1
for k,v in pairs(tbl) do
s[c]=k
c=c+1
end
c=0
return function()
c=c+1
return s[c],tbl[s[c]]
end
end
local ed
function hook.stop()
ed=true
end
function hook.queue(name,...)
local callback=hook.callback
hook.callback=nil
if type(name)~="table" then
name={name}
end
local p={}
for _,nme in pairs(name) do
for k,v in tpairs(hooks[nme] or {}) do
if v then
ed=false
hook.name=name
p={v(...)}
if callback then
callback(unpack(p))
end
if ed then
hook.del(v)
end
end
end
end
return unpack(p)
end
function hook.newsocket(sk)
hook.created[sk]=debug.traceback()
sk:settimeout(0)
table.insert(hook.sel,sk)
end
function hook.remsocket(sk)
for k,v in pairs(hook.sel) do
if v==sk then
table.remove(hook.sel,k)
return
end
end
end
function hook.newrsocket(sk)
hook.created[sk]=debug.traceback()
sk:settimeout(0)
table.insert(hook.rsel,sk)
end
function hook.remrsocket(sk)
for k,v in pairs(hook.rsel) do
if v==sk then
table.remove(hook.rsel,k)
return
end
end
end
function hook.new(name,func,meta)
if type(name)~="table" then
name={name}
end
for _,nme in pairs(name) do
hook.meta[nme]=meta
hooks[nme]=hooks[nme] or {}
table.insert(hooks[nme],func)
end
return func
end
function hook.timer(tme)
local n=nxt(timers)
timers[n]={tme,tme}
hook.interval=math.min(tme,hook.interval or tme)
return "timer_"..n
end
local lst=socket.gettime()
hook.new("select",function()
local dt=socket.gettime()-lst
local mn
for num,tme in tpairs(timers) do
timers[num][1]=timers[num][1]-dt
if timers[num][1]<=0 then
timers[num][1]=hook.queue("timer_"..num) and timers[num][2] or nil
if not timers[num][1] then
timers[num]=nil
end
else
mn=math.min(mn or timers[num][1],mn or math.huge)
end
end
if mn and mn+1>mn then
hook.interval=mn
end
lst=socket.gettime()
end)
function hook.del(name)
if type(name)~="table" then
name={name}
end
for _,nme in pairs(name) do
if type(nme)=="function" then
for k,v in pairs(hooks) do
for n,l in pairs(v) do
if l==nme then
hooks[k][n]=nil
end
end
end
else
hooks[nme]=nil
end
end
end