badpacket/proto/packet/header.go

63 lines
1.2 KiB
Go
Raw Normal View History

2022-03-07 05:56:48 +00:00
package packet
import (
"io"
"tpack/proto/packet/packetflag"
"tpack/util"
)
type HeaderFlag = byte
type Header struct {
Length uint16
Opcode uint16
Sequence uint16
Secret byte
Flag packetflag.Flag
Check byte
}
func (H *Header) Decode(r io.Reader) (err error) {
if H.Length, err = util.ReadUint16(r); err != nil {
return err
}
if H.Opcode, err = util.ReadUint16(r); err != nil {
return err
}
if H.Sequence, err = util.ReadUint16(r); err != nil {
return err
}
if H.Secret, err = util.ReadByte(r); err != nil {
return err
}
if H.Flag, err = util.ReadByte(r); err != nil {
return err
}
if H.Check, err = util.ReadByte(r); err != nil {
return err
}
return nil
}
func (H *Header) Encode(w io.Writer) (err error) {
if err = util.WriteUint16(w, H.Opcode); err != nil {
return err
}
if err = util.WriteUint16(w, H.Opcode); err != nil {
return err
}
if err = util.WriteUint16(w, H.Sequence); err != nil {
return err
}
if err = util.WriteByte(w, H.Secret); err != nil {
return err
}
if err = util.WriteByte(w, H.Flag); err != nil {
return err
}
if err = util.WriteByte(w, H.Check); err != nil {
return err
}
return nil
}