handshake/fec: Encode a message from its own bytes, copying only a padded last symbol

This commit is contained in:
JackDoan committed 2026-10-15 10:42:48 -05:00
1 parent cbcf441d7c
commit 05410034e3
2 files changed
+8 -5

No files matched your search

+6 -4
View File
@@ -58,18 +58,20 @@ type Encoder struct {
data [][]byte
}
// NewEncoder copies msg into padded data symbols.
// NewEncoder splits msg into data symbols. They alias msg, all but a padded
// last symbol, so msg must not change while the Encoder is in use.
func NewEncoder(msg []byte, maxSymbolSize int) (*Encoder, error) {
l, err := NewLayout(len(msg), maxSymbolSize)
if err != nil {
return nil, err
}
buf := make([]byte, l.K*l.SymbolSize)
copy(buf, msg)
data := make([][]byte, l.K)
for i := range data {
data[i] = buf[i*l.SymbolSize : (i+1)*l.SymbolSize]
data[i] = msg[i*l.SymbolSize : min((i+1)*l.SymbolSize, len(msg))]
}
if last := data[l.K-1]; len(last) < l.SymbolSize {
data[l.K-1] = append(make([]byte, 0, l.SymbolSize), last...)[:l.SymbolSize]
}
return &Encoder{layout: l, data: data}, nil
}
+2 -1
View File
@@ -34,7 +34,8 @@ type Sender struct {
}
// NewSender prepares msg for sending. subtype, remoteIndex and counter fill the
// Nebula header of every fragment; fragID is the sender's local index.
// Nebula header of every fragment; fragID is the sender's local index. The
// Sender keeps msg, which must not change while it is in use.
func NewSender(msg []byte, subtype header.MessageSubType, remoteIndex uint32, counter uint64, fragID uint32) (*Sender, error) {
if len(msg) == 0 || len(msg) > math.MaxUint16 {
return nil, ErrMsgLen