A length prefixed varint stream ReadWriter for Go.
func handler(s network.Stream) {
lps := lpstream.New(s)
buf, err := lps.Read()
if err != nil {
fmt.Printf("Error reading from stream: %v\n", err)
s.Reset()
return
}
// echo the received buf
err = lps.Write(buf)
if err != nil {
fmt.Printf("Error writing to stream: %v\n", err)
s.Reset()
return
}
s.Close()
}
func handler(s network.Stream) {
lps := lpstream.New(s)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
buf, err := lps.Read(lpstream.AbortOptions{Context: ctx})
if err != nil {
fmt.Printf("Error reading from stream: %v\n", err)
s.Reset()
return
}
// echo the received buf
err = lps.Write(buf, lpstream.AbortOptions{Context: ctx})
if err != nil {
fmt.Printf("Error writing to stream: %v\n", err)
s.Reset()
return
}
s.Close()
}
See tests for examples of writing multiple buffers with WriteV and setting the max length.
go test -v ./...
go test -bench=^Benchmark -benchmem ./...
./fuzz.sh