File tree 1 file changed +23
-13
lines changed
1 file changed +23
-13
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ type BufferflowTimed 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
@@ -23,22 +24,31 @@ func (b *BufferflowTimed) Init() {
23
24
log .Println ("Initting timed buffer flow (output once every 16ms)" )
24
25
bufferedOutput = ""
25
26
26
- go func () {
27
- for data := range b .Input {
28
- bufferedOutput = bufferedOutput + data
29
- }
30
- }()
31
-
32
27
go func () {
33
28
b .ticker = time .NewTicker (16 * time .Millisecond )
34
- for _ = range b .ticker .C {
35
- if bufferedOutput != "" {
36
- m := SpPortMessage {bufferedOutput }
37
- buf , _ := json .Marshal (m )
38
- b .Output <- []byte (buf )
39
- bufferedOutput = ""
29
+ b .done = make (chan bool )
30
+ Loop:
31
+ for {
32
+ select {
33
+ case data := <- b .Input :
34
+ bufferedOutput = bufferedOutput + data
35
+ case <- b .ticker .C :
36
+ if bufferedOutput != "" {
37
+ m := SpPortMessage {bufferedOutput }
38
+ buf , _ := json .Marshal (m )
39
+ // data is now encoded in base64 format
40
+ // need a decoder on the other side
41
+ b .Output <- []byte (buf )
42
+ bufferedOutput = ""
43
+ }
44
+ case <- b .done :
45
+ break Loop
40
46
}
41
47
}
48
+
49
+ close (b .Input )
50
+ close (b .done )
51
+
42
52
}()
43
53
44
54
}
@@ -97,5 +107,5 @@ func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {
97
107
98
108
func (b * BufferflowTimed ) Close () {
99
109
b .ticker .Stop ()
100
- close ( b . Input )
110
+ b . done <- true
101
111
}
You can’t perform that action at this time.
0 commit comments