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.
42 lines
727 B
Go
42 lines
727 B
Go
2 years ago
|
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(_ interface{}, 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
|
||
|
}
|