-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp.go
203 lines (168 loc) · 3.93 KB
/
http.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
package nmux
import (
"io"
"log"
"net"
"net/http"
"path/filepath"
"time"
"github.com/gorilla/websocket"
"github.com/tweekmonster/nmux/screen"
"github.com/tweekmonster/nmux/util"
)
type tcpKeepAliveListener struct {
*net.TCPListener
}
var proc *Process
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type WebsocketWriter struct {
Conn *websocket.Conn
}
func (w WebsocketWriter) Write(p []byte) (int, error) {
err := w.Conn.WriteMessage(websocket.BinaryMessage, p)
if err == nil {
return len(p), nil
}
return 0, err
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
func websocketHandler(ws *websocket.Conn) {
util.Print("Connection from:", ws.RemoteAddr())
if proc == nil || !proc.IsRunning() {
util.Debug("Starting nvim process")
p, err := NewProcess("/tmp", 80, 20)
if err != nil {
util.Print("Couldn't start process:", err)
return
}
proc = p
}
input := make(chan []byte)
closeInput := func() {
// Close the input without panicking.
defer func() {
recover()
}()
close(input)
}
go func() {
util.Debug("Starting input loop")
loop:
for {
msgType, data, err := ws.ReadMessage()
if msgType != websocket.BinaryMessage {
util.Print("Unsupported message type from client", ws.RemoteAddr(), msgType)
break loop
}
if err == io.EOF {
break loop
} else if err != nil {
util.Print("WebSocket Err:", err)
break loop
} else {
select {
case input <- data:
case <-time.After(time.Second):
// XXX: Needs investigation. Occurs when a client reconnects and
// sends a key event while nvim is prompting in the command line.
// e.g. "swap recovery" prompt.
util.Print("Input deadlock?")
break loop
}
}
}
util.Print("Input stopped for client", ws.RemoteAddr())
closeInput()
}()
if err := proc.Attach(WebsocketWriter{Conn: ws}); err != nil {
util.Print("Attach err:", err)
}
mainloop:
for {
select {
case _, ok := <-proc.Deadman:
if !ok {
util.Print("Process ended")
break mainloop
}
case data, ok := <-input:
if !ok {
break mainloop
}
op := screen.Op(data[0])
switch op {
case screen.OpResize:
cols := (int(data[1]) << 8) | int(data[2])
rows := (int(data[3]) << 8) | int(data[4])
// XXX: This is the origin point of the deadlock mentioned above.
if err := proc.Resize(cols, rows); err != nil {
util.Print("Couldn't resize:", err)
break mainloop
}
case screen.OpKeyboard:
if proc != nil && proc.IsRunning() {
if _, err := proc.Input(string(data[1:])); err != nil {
util.Print("Input Error:", err)
break mainloop
}
}
}
}
}
closeInput()
if err := proc.Detach(); err != nil {
util.Print("Detach err:", err)
}
util.Print("Connection stopped:", ws.RemoteAddr())
}
func WebServer(addr string) (io.Closer, error) {
http.HandleFunc("/nmux", func(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
util.Print("Couldn't upgrade connection for", r.RemoteAddr)
return
}
websocketHandler(ws)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - %s", r.Method, r.URL.Path)
switch r.URL.Path {
case "/":
r.URL.Path = "/index.html"
case "/const.js":
w.Write(screen.JSConst)
return
}
data, err := Asset(filepath.Join("web", r.URL.Path))
if err != nil {
http.NotFound(w, r)
return
}
w.Write(data)
})
server := &http.Server{
Addr: addr,
}
listener, err := net.Listen("tcp", server.Addr)
if err != nil {
return nil, err
}
util.Debug("Listening on", server.Addr)
go func() {
if err := server.Serve(tcpKeepAliveListener{listener.(*net.TCPListener)}); err != nil {
util.Print("Server Error:", err)
}
}()
return listener, nil
}