1
0
mirror of https://github.com/fumiama/terasu.git synced 2026-06-10 21:24:46 +08:00

fix: error on different frag lens

This commit is contained in:
源文雨
2025-10-23 23:33:06 +08:00
parent 3f56d5341b
commit bda0c8de97
9 changed files with 206 additions and 33 deletions

72
relay.go Normal file
View File

@@ -0,0 +1,72 @@
package terasu
import (
"io"
"sync"
)
type relay struct {
mu sync.Mutex
buf chan []byte
rem []byte
}
func newrelay() relay {
return relay{buf: make(chan []byte, 64)}
}
// Read ...
func (r *relay) Read(p []byte) (n int, err error) {
r.mu.Lock()
defer r.mu.Unlock()
switch {
case len(p) == 0:
return
case len(p) <= len(r.rem):
n = copy(p, r.rem)
r.rem = r.rem[n:]
if len(r.rem) == 0 {
r.rem = nil
}
return
case len(r.rem) > 0:
n = copy(p, r.rem)
r.rem = nil
fallthrough
default:
for n < len(p) {
buf := <-r.buf
if len(buf) == 0 {
err = io.EOF
return
}
switch {
case len(buf) >= len(p)-n:
cnt := copy(p[n:], buf)
n += cnt
r.rem = buf[cnt:]
if len(r.rem) == 0 {
r.rem = nil
}
return
default:
n += copy(p[n:], buf)
}
}
}
panic("unexpected")
}
// Write ...
func (r *relay) Write(p []byte) (n int, err error) {
buf := make([]byte, len(p))
n = copy(buf, p)
r.buf <- p
return
}
// Close ...
func (r *relay) Close() error {
close(r.buf)
return nil
}