-
Notifications
You must be signed in to change notification settings - Fork 46
/
serve.go
210 lines (176 loc) · 4.03 KB
/
serve.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"bufio"
"fmt"
"log"
"net"
"runtime/debug"
"syscall"
"github.com/aaronjanse/3mux/ecma48"
"github.com/aaronjanse/3mux/pane"
"github.com/aaronjanse/3mux/render"
"github.com/aaronjanse/3mux/wm"
"github.com/npat-efault/poller"
)
func serve(sessionInfo *SessionInfo) error {
log.Println("Booting...")
config, err := loadOrGenerateConfig()
if err != nil {
return fmt.Errorf("Failed to load or generate config: %s", err)
}
renderer := render.NewRenderer(-1)
renderer.Resize(20, 20)
go renderer.Render()
shutdown := make(chan error)
newPane := func(renderer ecma48.Renderer) wm.Node {
return pane.NewPane(renderer, true, sessionInfo.uuid)
}
u := wm.NewUniverse(renderer,
config.generalSettings.EnableHelpBar,
config.generalSettings.EnableStatusBar,
func(err error) {
go func() {
if err != nil {
shutdown <- fmt.Errorf("%s\n%s", err, debug.Stack())
} else {
shutdown <- nil
}
}()
}, wm.Rect{X: 0, Y: 0, W: 50, H: 20}, newPane)
defer u.Kill()
stdin := make(chan ecma48.Output, 64)
defer close(stdin)
var parser *ecma48.Parser
var stdinBuf *bufio.Reader
var stdinPoller *poller.FD
listenFd(sessionInfo, func(stdinFd, stdoutFd int) {
stdinPoller, _ = poller.NewFD(stdinFd)
stdinBuf = bufio.NewReader(stdinPoller)
parser = ecma48.NewParser(true)
go parser.Parse(stdinBuf, stdin)
renderer.UpdateOut(stdoutFd)
})
defer net.Dial("unix", sessionInfo.killClientPath)
listenResize(sessionInfo, func(width, height int) {
renderer.Resize(width, height)
u.SetRenderRect(0, 0, width, height)
})
go func() {
detachSocket, err := net.Listen("unix", sessionInfo.detachPath)
if err != nil {
log.Println("Detach error:", err)
panic(err)
}
for {
_, err = detachSocket.Accept()
if err != nil {
log.Println("Detach accept error:", err)
panic(err)
}
log.Println("Detaching...")
renderer.UpdateOut(-1)
parser.Shutdown <- nil
stdinPoller.Close()
log.Println("Detaching... DONE")
net.Dial("unix", sessionInfo.killClientPath)
}
}()
go func() {
detachSocket, err := net.Listen("unix", sessionInfo.killServerPath)
if err != nil {
panic(err)
}
detachSocket.Accept()
shutdown <- nil
}()
for {
select {
case next := <-stdin:
human := humanify(next)
log.Println("Keypress:", human)
if human == "Ctrl+Q" {
return nil
}
if seiveMouseEvents(u, human, next) {
break
}
if seiveConfigEvents(config, u, human) {
break
}
// if we didn't find anything special, just pass the raw data to
// the selected terminal
u.HandleStdin(next)
case err := <-shutdown:
if err != nil {
return err
}
return nil
}
}
}
func listenResize(sessionInfo *SessionInfo, callback func(width, height int)) {
socket, err := net.Listen("unix", sessionInfo.resizePath)
if err != nil {
panic(err)
}
go func() {
for {
conn, err := socket.Accept()
if err != nil {
panic(err)
}
b := make([]byte, 4)
_, err = conn.Read(b)
if err != nil {
panic(err)
}
width := (int(b[0]) << 8) + int(b[1])
height := (int(b[2]) << 8) + int(b[3])
callback(width, height)
conn.Close()
}
}()
}
func listenFd(sessionInfo *SessionInfo, callback func(stdinFd, stdoutFd int)) {
socket, err := net.Listen("unix", sessionInfo.fdPath)
if err != nil {
panic(err)
}
go func() {
for {
conn, err := socket.Accept()
if err != nil {
panic(err)
}
callback(parseFdConn(conn.(*net.UnixConn)))
conn.Close()
}
}()
}
func parseFdConn(conn *net.UnixConn) (stdinFd, stdoutFd int) {
fConn, err := conn.File()
if err != nil {
panic(err)
}
numFds := 2
buf := make([]byte, syscall.CmsgSpace(numFds*4))
_, _, _, _, err = syscall.Recvmsg(int(fConn.Fd()), nil, buf, 0)
if err != nil {
panic(err)
}
msgs, err := syscall.ParseSocketControlMessage(buf)
if err != nil {
panic(err)
}
fds := []int{}
for _, msg := range msgs {
newFds, err := syscall.ParseUnixRights(&msg)
if err != nil {
panic(err)
}
fds = append(fds, newFds...)
}
stdinFd = fds[0]
stdoutFd = fds[1]
return
}