forked from cloudfoundry-attic/garden-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
59 lines (48 loc) · 908 Bytes
/
writer.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
package link
import (
"encoding/gob"
"net"
)
type Input struct {
StdinData []byte
EOF bool
WindowSize *WindowSize
Msg []byte
}
type WindowSize struct {
Columns int
Rows int
}
type Writer struct {
conn net.Conn
enc *gob.Encoder
}
func NewWriter(conn net.Conn) *Writer {
return &Writer{conn: conn, enc: gob.NewEncoder(conn)}
}
func (w *Writer) TerminateConnection() error {
return w.conn.Close()
}
func (w *Writer) Write(d []byte) (int, error) {
err := w.enc.Encode(Input{StdinData: d})
if err != nil {
return 0, err
}
return len(d), nil
}
func (w *Writer) Close() error {
return w.enc.Encode(Input{EOF: true})
}
func (w *Writer) SetWindowSize(cols, rows int) error {
return w.enc.Encode(Input{
WindowSize: &WindowSize{
Columns: cols,
Rows: rows,
},
})
}
func (w *Writer) SendMsg(msg []byte) error {
return w.enc.Encode(Input{
Msg: msg,
})
}