-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathloader.go
77 lines (70 loc) · 1.74 KB
/
loader.go
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
package tcp
import (
lua "github.com/yuin/gopher-lua"
"time"
)
// Preload adds tcp to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
// local tcp = require("tcp")
func Preload(L *lua.LState) {
L.PreloadModule("tcp", Loader)
}
// Loader is the module loader function.
func Loader(L *lua.LState) int {
tcp_client_ud := L.NewTypeMetatable(`tcp_client_ud`)
L.SetGlobal(`tcp_client_ud`, tcp_client_ud)
funcs := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"write": Write,
"close": Close,
"read": Read,
})
L.SetFuncs(tcp_client_ud, map[string]lua.LGFunction{
"__index": func(state *lua.LState) int {
conn := checkLuaTCPClient(L, 1)
k := L.CheckString(2)
var duration time.Duration
switch k {
case "dialTimeout":
duration = conn.dialTimeout
case "writeTimeout":
duration = conn.writeTimeout
case "readTimeout":
duration = conn.readTimeout
case "closeTimeout":
duration = conn.closeTimeout
default:
L.Push(L.GetField(funcs, k))
return 1
}
L.Push(lua.LNumber(duration) / lua.LNumber(time.Second))
return 1
},
"__newindex": func(state *lua.LState) int {
conn := checkLuaTCPClient(L, 1)
k := L.CheckString(2)
var pDuration *time.Duration
switch k {
case "dialTimeout":
pDuration = &conn.dialTimeout
case "writeTimeout":
pDuration = &conn.writeTimeout
case "readTimeout":
pDuration = &conn.readTimeout
case "closeTimeout":
pDuration = &conn.closeTimeout
default:
return 0
}
*pDuration = time.Duration(L.CheckNumber(3) * lua.LNumber(time.Second))
return 0
},
})
t := L.NewTable()
L.SetFuncs(t, api)
L.Push(t)
return 1
}
var api = map[string]lua.LGFunction{
"open": Open,
}