-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (48 loc) · 869 Bytes
/
server.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
package rcom
import (
"io"
"os"
"os/signal"
"syscall"
)
type reader struct {
upstream io.Reader
}
func (r reader) Read(p []byte) (n int, err error) {
n, err = r.upstream.Read(p)
if err == nil {
Logger.Printf("Read %d bytes: %x", n, p[0:n])
}
return n, err
}
func Server(linkname string, force bool) error {
Logger.Printf("Connecting server to %s", linkname)
p, err := newPort(linkname, force)
if err != nil {
return err
}
done := make(chan interface{}, 3)
go func() {
io.Copy(p, os.Stdin)
done <- true
}()
go func() {
io.Copy(os.Stdout, p)
done <- true
}()
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
closed := false
go func() {
sig := <-ch
Logger.Printf("Server received %v", sig)
p.CloseTTY()
closed = true
done <- true
}()
<-done
if !closed {
p.CloseTTY()
}
return nil
}