txqueue work

This commit is contained in:
soypat
2025-01-13 19:36:17 -03:00
parent 9abf0c11b9
commit 0a75ef4670
4 changed files with 56 additions and 9 deletions
+2 -2
View File
@@ -68,9 +68,9 @@ func (tx *ringTx) Write(b []byte) (int, error) {
return r.WriteLimited(b, first.off)
}
// ReadPacket reads from the unsent data ring buffer and generates a new packet segment.
// MakePacket reads from the unsent data ring buffer and generates a new packet segment.
// It fails if the sent packet queue is full.
func (tx *ringTx) NewPacketAndRead(b []byte) (int, error) {
func (tx *ringTx) MakePacket(b []byte) (int, error) {
nxtpkt := (tx.lastPkt + 1) % len(tx.packets)
if tx.firstPkt == nxtpkt {
return 0, errors.New("packet queue full")
+35
View File
@@ -0,0 +1,35 @@
package tcp
import (
"bytes"
"testing"
)
func TestTxQueueWrite(t *testing.T) {
const (
bufsize = 1024
maxPkt = 3
msg = "hello world"
)
buf := make([]byte, bufsize)
rtx := newRingTx(buf, maxPkt)
bufs := bytes.SplitAfter([]byte(msg), []byte("e"))
var data [bufsize]byte
for i, buf := range bufs {
n, err := rtx.Write(buf)
if err != nil {
t.Fatalf("writing packet %d: %s", i, err)
} else if n != len(buf) {
t.Fatalf("want %d written, got %d", len(buf), n)
}
n, err = rtx.MakePacket(data[:])
if err != nil {
t.Fatalf("making packet %d: %s", i, err)
} else if n != len(buf) {
t.Fatalf("want %d packet read, got %d", len(buf), n)
} else if !bytes.Equal(buf, data[:n]) {
t.Fatalf("want data %q, got data read %q", buf, data[:n])
}
}
}