av/codec/codecutil/lex.go

144 lines
3.3 KiB
Go

/*
NAME
lex.go
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package codecutil
import (
"fmt"
"io"
"time"
)
// ByteLexer is used to lex bytes using a buffer size which is configured upon construction.
type ByteLexer struct {
bufSize int
}
// NewByteLexer returns a pointer to a ByteLexer with the given buffer size.
func NewByteLexer(s int) (*ByteLexer, error) {
if s <= 0 {
return nil, fmt.Errorf("invalid buffer size: %v", s)
}
return &ByteLexer{bufSize: s}, nil
}
// zeroTicks can be used to create an instant ticker.
var zeroTicks chan time.Time
func init() {
zeroTicks = make(chan time.Time)
close(zeroTicks)
}
// Lex reads l.bufSize bytes from src and writes them to dst every d seconds.
func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, d time.Duration) error {
if d < 0 {
return fmt.Errorf("invalid delay: %v", d)
}
var ticker *time.Ticker
if d == 0 {
ticker = &time.Ticker{C: zeroTicks}
} else {
ticker = time.NewTicker(d)
defer ticker.Stop()
}
buf := make([]byte, l.bufSize)
for {
<-ticker.C
off, err := src.Read(buf)
// The only error that will stop the lexer is an EOF.
if err == io.EOF {
return err
} else if err != nil {
continue
}
_, err = dst.Write(buf[:off])
if err != nil {
return err
}
}
}
// Noop assumes that for writing to src is blocked until the entire previous
// write to src is read, i.e. src is expected to connected to a pipe-like structure.
func Noop(dst io.Writer, src io.Reader, d time.Duration) error {
if d < 0 {
return fmt.Errorf("invalid delay: %v", d)
}
if d == 0 {
d = 40 * time.Millisecond
}
ticker := time.NewTicker(d)
defer ticker.Stop()
const checkDur = 500 * time.Millisecond
rateChkTicker := time.NewTicker(checkDur)
frameCh := make(chan []byte, 1000)
errCh := make(chan error)
go func() {
for {
toWrite := <-frameCh
_, err := dst.Write(toWrite)
if err != nil {
errCh <- fmt.Errorf("could not write to dst: %w", err)
}
<-ticker.C
select {
case <-rateChkTicker.C:
var adj int
const equilibrium = 500
if len(frameCh) > equilibrium {
adj = -2
} else if len(frameCh) < equilibrium {
adj = 2
}
d += time.Millisecond * time.Duration(adj)
ticker.Reset(d)
default:
}
}
}()
const maxFrameSize = 250000 // = 20kB
buf := make([]byte, maxFrameSize)
for {
n, err := src.Read(buf)
if err != nil {
return fmt.Errorf("could not read src: %w", err)
}
newFrame := make([]byte, n)
copy(newFrame, buf[:n])
frameCh <- newFrame
select {
case err := <-errCh:
return fmt.Errorf("error from output routine: %w", err)
default:
}
}
}