2020-11-25 19:35:31 +00:00
|
|
|
package udp
|
|
|
|
|
2020-11-26 18:55:29 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"mpbl3p/proxy"
|
2020-11-28 17:15:56 +00:00
|
|
|
"mpbl3p/shared"
|
2020-11-26 18:55:29 +00:00
|
|
|
)
|
2020-11-25 19:35:31 +00:00
|
|
|
|
|
|
|
type Packet struct {
|
2020-11-26 18:55:29 +00:00
|
|
|
ack uint32
|
|
|
|
nack uint32
|
|
|
|
seq uint32
|
2020-11-25 19:35:31 +00:00
|
|
|
|
|
|
|
data proxy.Packet
|
|
|
|
}
|
2020-11-26 18:55:29 +00:00
|
|
|
|
|
|
|
func UnmarshalPacket(b []byte) (p Packet, err error) {
|
2020-11-28 17:15:56 +00:00
|
|
|
if len(b) < 12 {
|
|
|
|
return Packet{}, shared.ErrNotEnoughBytes
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:55:29 +00:00
|
|
|
p.ack = binary.LittleEndian.Uint32(b[0:4])
|
|
|
|
p.nack = binary.LittleEndian.Uint32(b[4:8])
|
|
|
|
p.seq = binary.LittleEndian.Uint32(b[8:12])
|
|
|
|
|
2020-11-28 17:15:56 +00:00
|
|
|
p.data = proxy.SimplePacket(b[12:])
|
2020-11-26 18:55:29 +00:00
|
|
|
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...)
|
|
|
|
}
|
2020-11-26 22:39:07 +00:00
|
|
|
|
|
|
|
func (p Packet) Contents() []byte {
|
|
|
|
return p.data.Contents()
|
|
|
|
}
|