You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/protocol/plain/packer_plain.go

42 lines
719 B
Go

package plain
import (
"bytes"
"git.noahlan.cn/noahlan/nnet/packet"
)
type Packer struct {
buf *bytes.Buffer
size int // 最近一次 length
typ byte // 最近一次 packet type
flag byte // 最近一次 flag
}
func NewPacker() *Packer {
p := &Packer{
buf: bytes.NewBuffer(nil),
}
p.resetFlags()
return p
}
func (d *Packer) resetFlags() {
}
func (d *Packer) Pack(_ any, data []byte) ([]byte, error) {
buf := make([]byte, len(data))
copy(buf, data)
return buf, nil
}
func (d *Packer) Unpack(data []byte) ([]packet.IPacket, error) {
d.buf.Write(data) // copy
var packets []packet.IPacket
p := newPacket()
p.Data = d.buf.Next(d.buf.Len())
packets = append(packets, p)
return packets, nil
}