-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_cmdio2.go
66 lines (56 loc) · 1.01 KB
/
ex_cmdio2.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
package main
import (
"fmt"
"io"
"log"
"os"
"os/signal"
"syscall"
"github.com/l4go/cmdio"
"github.com/l4go/lineio"
"github.com/l4go/task"
)
func main() {
log.Println("START")
defer log.Println("END")
m := task.NewMission()
signal_ch := make(chan os.Signal, 1)
signal.Notify(signal_ch, syscall.SIGINT, syscall.SIGTERM)
std_rw, err := cmdio.StdDup()
if err != nil {
defer log.Println("Error:", err)
return
}
go func(cm *task.Mission) {
defer std_rw.Close()
echo_worker(cm, std_rw)
}(m.New())
select {
case <-m.Recv():
case <-signal_ch:
m.Cancel()
}
}
func echo_worker(m *task.Mission, rw io.ReadWriter) {
defer m.Done()
log.Println("start: echo worker")
defer log.Println("end: echo worker")
line_r := lineio.NewReader(rw)
for {
var ln []byte
var ok bool
select {
case ln, ok = <-line_r.Recv():
case <-m.RecvCancel():
return
}
if !ok {
break
}
fmt.Fprintln(rw, ">", string(ln))
}
if err := line_r.Err(); err != nil {
log.Println("Error:", err)
return
}
}