@@ -12,36 +12,46 @@ type BufferflowTimedRaw struct {
12
12
Port string
13
13
Output chan []byte
14
14
Input chan string
15
+ done chan bool
15
16
ticker * time.Ticker
16
17
}
17
18
18
19
var (
19
20
bufferedOutputRaw []byte
21
+ sPortRaw string
20
22
)
21
23
22
24
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 = ""
30
28
31
29
go func () {
32
30
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
41
50
}
42
51
}
43
- }()
44
52
53
+ close (b .Input )
54
+ }()
45
55
}
46
56
47
57
func (b * BufferflowTimedRaw ) BlockUntilReady (cmd string , id string ) (bool , bool ) {
0 commit comments