Skip to content

Commit 895cc03

Browse files
committed
update bufferflow_timedraw as bufferflow_timed
1 parent 44614b3 commit 895cc03

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

bufferflow_timedraw.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,46 @@ type BufferflowTimedRaw struct {
1212
Port string
1313
Output chan []byte
1414
Input chan string
15+
done chan bool
1516
ticker *time.Ticker
1617
}
1718

1819
var (
1920
bufferedOutputRaw []byte
21+
sPortRaw string
2022
)
2123

2224
func (b *BufferflowTimedRaw) Init() {
23-
log.Println("Initting timed buffer flow (output once every 16ms)")
24-
25-
go func() {
26-
for data := range b.Input {
27-
bufferedOutputRaw = append(bufferedOutputRaw, []byte(data)...)
28-
}
29-
}()
25+
log.Println("Initting timed buffer raw flow (output once every 16ms)")
26+
bufferedOutputRaw = nil
27+
sPortRaw = ""
3028

3129
go func() {
3230
b.ticker = time.NewTicker(16 * time.Millisecond)
33-
for _ = range b.ticker.C {
34-
if len(bufferedOutputRaw) != 0 {
35-
m := SpPortMessageRaw{b.Port, bufferedOutputRaw}
36-
buf, _ := json.Marshal(m)
37-
// data is now encoded in base64 format
38-
// need a decoder on the other side
39-
b.Output <- []byte(buf)
40-
bufferedOutputRaw = nil
31+
b.done = make(chan bool)
32+
Loop:
33+
for {
34+
select {
35+
case data := <-b.Input:
36+
bufferedOutputRaw = append(bufferedOutputRaw, []byte(data)...)
37+
sPortRaw = b.Port
38+
case <-b.ticker.C:
39+
if bufferedOutputRaw != nil {
40+
m := SpPortMessageRaw{sPortRaw, bufferedOutputRaw}
41+
buf, _ := json.Marshal(m)
42+
// data is now encoded in base64 format
43+
// need a decoder on the other side
44+
b.Output <- []byte(buf)
45+
bufferedOutputRaw = nil
46+
sPortRaw = ""
47+
}
48+
case <-b.done:
49+
break Loop
4150
}
4251
}
43-
}()
4452

53+
close(b.Input)
54+
}()
4555
}
4656

4757
func (b *BufferflowTimedRaw) BlockUntilReady(cmd string, id string) (bool, bool) {

0 commit comments

Comments
 (0)