forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_lib.go
67 lines (60 loc) · 1.33 KB
/
source_lib.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
package rulexlib
import (
"github.com/hootrhino/rulex/glogger"
"github.com/hootrhino/rulex/typex"
lua "github.com/hootrhino/gopher-lua"
)
// rulexlib:WriteInStream('INEND', rulexlib:T2J(t))
var sourceReadBuffer []byte = []byte{}
/*
*
* 向资源写入数据
*
*/
func WriteSource(rx typex.RuleX) func(l *lua.LState) int {
return func(l *lua.LState) int {
uuid := l.ToString(2)
data := l.ToString(3)
if value, ok := rx.AllInEnd().Load(uuid); ok {
n, err := value.(*typex.InEnd).Source.DownStream([]byte(data))
if err != nil {
glogger.GLogger.Error(err)
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(lua.LNumber(n))
l.Push(lua.LNil)
return 2
}
l.Push(lua.LNil)
l.Push(lua.LString("source not exists:" + uuid))
return 2
}
}
/*
*
* 从资源里面读数据出来
*
*/
func ReadSource(rx typex.RuleX) func(*lua.LState) int {
return func(l *lua.LState) int {
uuid := l.ToString(2)
InEnd := rx.GetInEnd(uuid)
if InEnd != nil {
n, err := InEnd.Source.UpStream(sourceReadBuffer)
if err != nil {
glogger.GLogger.Error(err)
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(lua.LString(sourceReadBuffer[:n]))
l.Push(lua.LNil)
return 2
}
l.Push(lua.LNil)
l.Push(lua.LString("source not exists:" + uuid))
return 2
}
}