38 lines
725 B
Go
38 lines
725 B
Go
package udp
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"mpbl3p/proxy"
|
|
)
|
|
|
|
type Packet struct {
|
|
ack uint32
|
|
nack uint32
|
|
seq uint32
|
|
|
|
data proxy.Packet
|
|
}
|
|
|
|
func UnmarshalPacket(b []byte) (p Packet, err error) {
|
|
p.ack = binary.LittleEndian.Uint32(b[0:4])
|
|
p.nack = binary.LittleEndian.Uint32(b[4:8])
|
|
p.seq = binary.LittleEndian.Uint32(b[8:12])
|
|
|
|
p.data, err = proxy.UnmarshalSimplePacket(b[12:])
|
|
if err != nil {
|
|
return Packet{}, err
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (p Packet) Marshal() []byte {
|
|
data := p.data.Marshal()
|
|
header := make([]byte, 12)
|
|
|
|
binary.LittleEndian.PutUint32(header[0:4], p.ack)
|
|
binary.LittleEndian.PutUint32(header[4:8], p.nack)
|
|
binary.LittleEndian.PutUint32(header[8:12], p.seq)
|
|
|
|
return append(header, data...)
|
|
}
|