internal/pipewire: specify opcode and file count with message
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m39s
Test / Sandbox (race detector) (push) Successful in 4m41s
Test / Hakurei (push) Successful in 4m56s
Test / Hpkg (push) Successful in 4m57s
Test / Hakurei (race detector) (push) Successful in 6m31s
Test / Flake checks (push) Successful in 1m30s

This adds checking of FileCount while writing a message. Message encoding is relocated to an exported method to be used externally, probably for test stubbing.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-12-07 13:54:11 +09:00
parent 246e04214a
commit b9b9705b52
5 changed files with 156 additions and 29 deletions

View File

@@ -191,36 +191,30 @@ func (ctx *Context) queueFiles(fds ...int) (offset Fd) {
return
}
// InconsistentFilesError describes an implementation error where an incorrect amount
// of files is queued between two messages.
type InconsistentFilesError [2]Int
func (e *InconsistentFilesError) Error() string {
return "queued " + strconv.Itoa(int(e[0])) + " files instead of the expected " + strconv.Itoa(int(e[1]))
}
// writeMessage appends the POD representation of v and an optional footer to buf.
func (ctx *Context) writeMessage(
Id Int, opcode byte,
v KnownSize,
) (err error) {
func (ctx *Context) writeMessage(Id Int, v Message) (err error) {
if fileCount := Int(len(ctx.pendingFiles) - ctx.headerFiles); fileCount != v.FileCount() {
return &InconsistentFilesError{fileCount, v.FileCount()}
}
if ctx.pendingFooter == nil && ctx.deferredPendingFooter != nil {
ctx.pendingFooter, ctx.deferredPendingFooter = ctx.deferredPendingFooter, nil
}
size := v.Size()
if ctx.pendingFooter != nil {
size += ctx.pendingFooter.Size()
}
if size&^SizeMax != 0 {
return ErrSizeRange
}
ctx.buf = slices.Grow(ctx.buf, int(SizeHeader+size))
ctx.buf = (&Header{
ID: Id, Opcode: opcode, Size: size,
Sequence: ctx.sequence,
FileCount: Int(len(ctx.pendingFiles) - ctx.headerFiles),
}).append(ctx.buf)
ctx.headerFiles = len(ctx.pendingFiles)
ctx.buf, err = MarshalAppend(ctx.buf, v)
if err == nil && ctx.pendingFooter != nil {
ctx.buf, err = MarshalAppend(ctx.buf, ctx.pendingFooter)
ctx.buf, err = MessageEncoder{v}.AppendMessage(ctx.buf, Id, ctx.sequence, ctx.pendingFooter)
if err == nil {
ctx.headerFiles = len(ctx.pendingFiles)
ctx.pendingFooter = nil
ctx.sequence++
}
ctx.sequence++
return
}