dissertation-2-code/udp/packet.go
Jake Hillion 3ac4d1f618
Some checks failed
continuous-integration/drone/push Build is failing
initial udp code
2020-11-26 18:55:29 +00:00

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...)
}