package udp import ( "encoding/binary" "mpbl3p/proxy" "mpbl3p/shared" ) type Packet struct { ack uint32 nack uint32 seq uint32 data proxy.Packet } func UnmarshalPacket(b []byte) (p Packet, err error) { if len(b) < 12 { return Packet{}, shared.ErrNotEnoughBytes } 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 = proxy.SimplePacket(b[12:]) 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...) } func (p Packet) Contents() []byte { return p.data.Contents() }