-
Notifications
You must be signed in to change notification settings - Fork 2
/
notify_generic.go
55 lines (47 loc) · 1.12 KB
/
notify_generic.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
package liblpc
import (
"github.com/gen-iot/std"
"golang.org/x/sys/unix"
)
type GenericLoopNotify struct {
*FdWatcher
wakeupCb func()
readBuf []byte
sendData []byte
wfd int
}
func NewGenericLoopNotify(loop EventLoop, wakeupCb func()) (LoopNotify, error) {
pairSock, err := MakeIpcSockpair(true)
if err != nil {
return nil, err
}
watcher := new(GenericLoopNotify)
watcher.FdWatcher = NewFdWatcher(loop, pairSock[0], watcher)
watcher.wakeupCb = wakeupCb
watcher.readBuf = make([]byte, 64)
watcher.sendData = make([]byte, 4)
watcher.wfd = pairSock[1]
return watcher, nil
}
func (this *GenericLoopNotify) OnEvent(event EventSizeType) {
_, err := unix.Read(this.GetFd(), this.readBuf)
if err != nil {
return
}
if this.wakeupCb != nil {
this.wakeupCb()
}
}
func (this *GenericLoopNotify) GetEvent() EventSizeType {
return Readable
}
func (this *GenericLoopNotify) SetEvent(event EventSizeType) {
}
func (this *GenericLoopNotify) Notify() {
_, _ = unix.Write(this.wfd, this.sendData)
}
func (this *GenericLoopNotify) Close() error {
std.CloseIgnoreErr(this.FdWatcher)
_ = unix.Close(this.wfd)
return nil
}