Skip to content

Commit

Permalink
Simplify input buffering.
Browse files Browse the repository at this point in the history
  • Loading branch information
bemasher committed May 29, 2018
1 parent 7f30323 commit 0d41d2f
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"bufio"
"bytes"
"encoding/xml"
"flag"
Expand All @@ -26,7 +27,6 @@ import (
"log"
"os"
"os/signal"
"sync"
"time"

"github.com/bemasher/rtlamr/protocol"
Expand Down Expand Up @@ -124,44 +124,29 @@ func (rcvr *Receiver) Run() {
tLimit = time.After(*timeLimit)
}

in, out := io.Pipe()

// Read blocks of samples from the receiver and write them to a pipe.
go func() {
tcpBlock := make([]byte, 16384)
for {
n, err := rcvr.Read(tcpBlock)
if err != nil {
return
}
out.Write(tcpBlock[:n])
}
}()

sampleBuf := new(bytes.Buffer)
start := time.Now()

// Allocate a channel of blocks and a sync.Pool for allocating/reusing
// sample blocks.
blockCh := make(chan []byte, 128)
blockPool := sync.Pool{
New: func() interface{} {
return make([]byte, rcvr.d.Cfg.BlockSize2)
},
}
blockCh := make(chan []byte)

// Read sample blocks from the pipe created and fed above.
go func() {
for {
block := blockPool.Get().([]byte)
buf := bufio.NewReaderSize(rcvr, 1<<20)

blockA := make([]byte, rcvr.d.Cfg.BlockSize2)
blockB := make([]byte, rcvr.d.Cfg.BlockSize2)

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

Expand Down

0 comments on commit 0d41d2f

Please sign in to comment.