telebit/mplexer/decoder.go

68 lines
1.4 KiB
Go
Raw Normal View History

2020-05-22 10:41:24 +00:00
package telebit
2020-05-19 07:06:10 +00:00
import (
"io"
)
2020-05-21 05:30:24 +00:00
// Decoder handles a Reader stream containing mplexy-encoded clients
2020-05-19 07:06:10 +00:00
type Decoder struct {
2020-05-21 05:30:24 +00:00
in io.Reader
bufferSize int
2020-05-19 07:06:10 +00:00
}
// NewDecoder returns an initialized Decoder
2020-05-21 05:30:24 +00:00
func NewDecoder(rin io.Reader) *Decoder {
2020-05-19 07:06:10 +00:00
return &Decoder{
2020-05-21 05:30:24 +00:00
in: rin,
bufferSize: defaultBufferSize,
2020-05-19 07:06:10 +00:00
}
}
2020-05-21 05:30:24 +00:00
// Decode will call WriteMessage as often as addressable data exists,
2020-05-19 07:06:10 +00:00
// reading up to bufferSize (default 8192) at a time
// (header + data, though headers are often sent separately from data).
2020-05-21 05:30:24 +00:00
func (d *Decoder) Decode(out Router) error {
p := NewParser(out)
2020-05-19 07:06:10 +00:00
rx := make(chan []byte)
rxErr := make(chan error)
go func() {
for {
2020-05-21 05:30:24 +00:00
b := make([]byte, d.bufferSize)
2020-05-19 07:06:10 +00:00
//fmt.Println("loopers gonna loop")
2020-05-21 05:30:24 +00:00
n, err := d.in.Read(b)
2020-05-19 07:06:10 +00:00
if n > 0 {
rx <- b[:n]
}
if nil != err {
rxErr <- err
return
}
}
}()
for {
//fmt.Println("poopers gonna poop")
select {
// TODO, do we actually need ctx here?
// would it be sufficient to expect the reader to be closed by the caller instead?
case b := <-rx:
_, err := p.Write(b)
if nil != err {
// an error to write represents an unrecoverable error,
// not just a downstream client error
2020-05-21 05:30:24 +00:00
//d.in.Close()
2020-05-19 07:06:10 +00:00
return err
}
case err := <-rxErr:
2020-05-21 05:30:24 +00:00
//d.in.Close()
2020-05-19 07:06:10 +00:00
if io.EOF == err {
// it can be assumed that err will close though, right
return nil
}
return err
}
}
}