@@ -18,7 +18,6 @@ package daemon
18
18
import (
19
19
"fmt"
20
20
"io"
21
- "log"
22
21
23
22
"github.com/arduino/arduino-cli/arduino/monitors"
24
23
rpc "github.com/arduino/arduino-cli/rpc/monitor"
@@ -69,42 +68,68 @@ func (s *MonitorService) StreamingOpen(stream rpc.Monitor_StreamingOpenServer) e
69
68
}
70
69
}
71
70
72
- // now we can stream the other messages and re-route to the monitor
71
+ // we'll use these channels to communicate with the goroutines
72
+ // handling the stream and the target respectively
73
+ streamClosed := make (chan error )
74
+ targetClosed := make (chan error )
75
+
76
+ // now we can read the other messages and re-route to the monitor...
73
77
go func () {
74
78
for {
75
79
msg , err := stream .Recv ()
76
80
if err == io .EOF {
77
- // connection closed, exit
81
+ // stream was closed
82
+ streamClosed <- nil
78
83
break
79
84
}
80
85
81
86
if err != nil {
82
- // error, exit
83
- log . Fatal ( err )
87
+ // error reading from stream
88
+ streamClosed <- err
84
89
break
85
90
}
86
91
87
- mon .Write (msg .GetData ())
92
+ if _ , err := mon .Write (msg .GetData ()); err != nil {
93
+ // error writing to target
94
+ targetClosed <- err
95
+ break
96
+ }
88
97
}
89
98
}()
90
99
91
- // read from the monitor and forward to the output stream
100
+ // ...and read from the monitor and forward to the output stream
101
+ go func () {
102
+ buf := make ([]byte , 8 )
103
+ for {
104
+ n , err := mon .Read (buf )
105
+ if err != nil {
106
+ // error reading from target
107
+ targetClosed <- err
108
+ break
109
+ }
92
110
93
- buf := make ([]byte , 8 )
94
- for {
95
- n , err := mon .Read (buf )
96
- if err != nil {
97
- return err
98
- }
111
+ if n == 0 {
112
+ // target was closed
113
+ targetClosed <- nil
114
+ break
115
+ }
99
116
100
- if n == 0 {
101
- // port was closed
102
- return nil
117
+ if err = stream .Send (& rpc.StreamingOpenResp {
118
+ Data : buf [:n ],
119
+ }); err != nil {
120
+ // error sending to stream
121
+ streamClosed <- err
122
+ break
123
+ }
103
124
}
125
+ }()
104
126
105
- if err = stream .Send (& rpc.StreamingOpenResp {
106
- Data : buf [:n ],
107
- }); err != nil {
127
+ for {
128
+ select {
129
+ case err := <- streamClosed :
130
+ mon .Close ()
131
+ return err
132
+ case err := <- targetClosed :
108
133
return err
109
134
}
110
135
}
0 commit comments