Skip to content

Commit

Permalink
Handle blocks asynchronously.
Browse files Browse the repository at this point in the history
If we don't handle blocks asynchronously and rtl_tcp isn't sending data
for some reason, this will block forever and rtlamr won't respond
to timeout or ^C.
  • Loading branch information
bemasher committed Oct 8, 2017
1 parent 5910c8f commit 8a0e86e
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,23 @@ func (rcvr *Receiver) Run() {
}
}()

block := make([]byte, rcvr.p.Cfg().BlockSize2)
sampleBuf := new(bytes.Buffer)

start := time.Now()

blockCh := make(chan []byte)
go func() {
block := make([]byte, rcvr.p.Cfg().BlockSize2)

for {
// Read new sample block.
_, err := io.ReadFull(in, block)
if err != nil {
log.Fatal("Error reading samples: ", err)
}
blockCh <- block
}
}()

for {
// Exit on interrupt or time limit, otherwise receive.
select {
Expand All @@ -131,13 +144,7 @@ func (rcvr *Receiver) Run() {
case <-tLimit:
log.Println("Time Limit Reached:", time.Since(start))
return
default:
// Read new sample block.
_, err := io.ReadFull(in, block)
if err != nil {
log.Fatal("Error reading samples: ", err)
}

case block := <-blockCh:
// If dumping samples, discard the oldest block from the buffer if
// it's full and write the new block to it.
if *sampleFilename != os.DevNull {
Expand All @@ -161,7 +168,7 @@ func (rcvr *Receiver) Run() {
msg.Length = sampleBuf.Len()
msg.Message = pkt

err = encoder.Encode(msg)
err := encoder.Encode(msg)
if err != nil {
log.Fatal("Error encoding message: ", err)
}
Expand All @@ -183,7 +190,7 @@ func (rcvr *Receiver) Run() {

if pktFound {
if *sampleFilename != os.DevNull {
_, err = sampleFile.Write(sampleBuf.Bytes())
_, err := sampleFile.Write(sampleBuf.Bytes())
if err != nil {
log.Fatal("Error writing raw samples to file:", err)
}
Expand Down

0 comments on commit 8a0e86e

Please sign in to comment.