/
console.go
52 lines (41 loc) · 942 Bytes
/
console.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
package connect
import (
"os"
)
const CLICHUNK = 4096
type console struct {
*ConnectorStats
buf []byte
closed bool
}
func NewConsoleConnector() Connector {
return &console{&ConnectorStats{}, nil, true}
}
func (c *console) Name() string {
return "console"
}
func (c *console) Description() string {
return "Read/write from stdin/stdout"
}
func (c *console) Stats() *ConnectorStats {
return c.ConnectorStats
}
func (c *console) Connect(listen, plain bool, address string) (Connector, error) {
return &console{&ConnectorStats{}, make([]byte, CLICHUNK), false}, nil
}
func (c *console) Close() {
c.closed = true
}
func (c *console) Read() ([]byte, int, error) {
if c.closed {
return nil, 0, nil
}
length, err := os.Stdin.Read(c.buf)
c.recv += length
return c.buf, length, err
}
func (c *console) Write(buf []byte, length int) (int, error) {
wl, err := os.Stdout.Write(buf[:length])
c.send += wl
return wl, err
}