-
Notifications
You must be signed in to change notification settings - Fork 15
/
traysvr.go
161 lines (140 loc) · 2.77 KB
/
traysvr.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
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
package systray
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
"net"
"os/exec"
"path/filepath"
"strconv"
"sync"
)
func (p *_SystraySvr) Run() error {
go func() {
if len(p.clientPath) == 0 {
return
}
_, err := exec.Command(p.clientPath, "-xilp_systray_port", strconv.Itoa(p.port)).Output()
if err != nil {
panic(err)
}
}()
return p.serve()
}
func (p *_SystraySvr) Stop() error {
cmd := map[string]string{"action": "exit"}
return p.send(cmd)
}
func (p *_SystraySvr) Show(file string, hint string) error {
path, err := filepath.Abs(filepath.Join(p.iconPath, file))
if err != nil {
return err
}
cmd := map[string]string{"action": "show", "path": path, "hint": hint}
return p.send(cmd)
}
func (p *_SystraySvr) OnClick(fun func()) {
p.fclicked = fun
}
func _NewSystraySvr(iconPath string, clientPath string, port int) *_SystraySvr {
return &_SystraySvr{iconPath, clientPath, port, make(map[net.Conn]bool), nil, func(){}, sync.Mutex{}}
}
func (p *_SystraySvr) serve() error {
ln, err := net.Listen("tcp", ":" + strconv.Itoa(p.port))
if err != nil {
return err
}
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
p.lock.Lock()
p.conns[conn] = true
p.lock.Unlock()
p.resend(conn)
for {
n := uint32(0)
err := binary.Read(conn, binary.LittleEndian, &n)
if err != nil {
break
}
buf := new(bytes.Buffer)
_, err = io.CopyN(buf, conn, int64(n))
if err != nil {
break
}
data := buf.Bytes()
kvs := map[string]string{}
err = json.Unmarshal(data, &kvs)
if err != nil {
continue
}
p.received(kvs)
}
p.lock.Lock()
delete(p.conns, conn)
p.lock.Unlock()
conn.Close()
}(conn)
}
}
func (p *_SystraySvr) resend(conn net.Conn) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := conn.Write(p.lastest)
return err
}
func (p *_SystraySvr) send(cmd map[string]string) error {
p.lock.Lock()
defer p.lock.Unlock()
data, err := json.Marshal(cmd)
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, uint32(len(data)))
if err != nil {
return err
}
err = binary.Write(buf, binary.LittleEndian, data)
if err != nil {
return err
}
data = buf.Bytes()
p.lastest = data
ok := 0
for conn, _ := range p.conns {
_, ret := conn.Write(data)
if ret != nil {
err = ret
} else {
ok += 1
}
}
if ok == 0 && err != nil {
return err
}
return nil
}
func (p *_SystraySvr) received(cmd map[string]string) {
action := cmd["action"]
if len(action) == 0 {
return
}
switch action {
case "clicked":
p.fclicked()
}
}
type _SystraySvr struct {
iconPath string
clientPath string
port int
conns map[net.Conn]bool
lastest []byte
fclicked func()
lock sync.Mutex
}