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
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:
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -573,3 +574,48 @@ func (d *SPADict) UnmarshalPOD(data []byte) (Word, error) {
|
||||
}
|
||||
return wireSize, nil
|
||||
}
|
||||
|
||||
// A Message is a value that can be transmitted as a message over PipeWire protocol native.
|
||||
type Message interface {
|
||||
// Opcode returns the opcode of this message.
|
||||
Opcode() byte
|
||||
// FileCount returns the number of files associated with this message.
|
||||
FileCount() Int
|
||||
|
||||
KnownSize
|
||||
}
|
||||
|
||||
// A MessageEncoder provides methods for encoding a [Message].
|
||||
type MessageEncoder struct{ Message }
|
||||
|
||||
// SizeMessage returns the size of Message transmitted over protocol native.
|
||||
func (m MessageEncoder) SizeMessage(footer KnownSize) (size Word) {
|
||||
size = SizeHeader + m.Message.Size()
|
||||
if footer != nil {
|
||||
size += footer.Size()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AppendMessage appends the protocol native encoding of Message to dst and returns the appended slice.
|
||||
func (m MessageEncoder) AppendMessage(dst []byte, Id, sequence Int, footer KnownSize) (data []byte, err error) {
|
||||
size := m.SizeMessage(footer)
|
||||
if size&^SizeMax != 0 {
|
||||
return dst, ErrSizeRange
|
||||
}
|
||||
|
||||
data = slices.Grow(dst, int(size))
|
||||
data = (&Header{
|
||||
ID: Id,
|
||||
Opcode: m.Message.Opcode(),
|
||||
Size: size - SizeHeader,
|
||||
Sequence: sequence,
|
||||
FileCount: m.Message.FileCount(),
|
||||
}).append(data)
|
||||
data, err = MarshalAppend(data, m.Message)
|
||||
if err == nil && footer != nil {
|
||||
data, err = MarshalAppend(data, footer)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user