diff --git a/packet.go b/packet.go new file mode 100644 index 0000000..d60d7f4 --- /dev/null +++ b/packet.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + + "github.com/golang/protobuf/proto" +) + +// packet represents the top-level envelope in the dota replay format. All +// data in the replay file is packed into packets of at most 65kb. +type packet struct { + cmd packetType + tick int64 + body []byte +} + +func (p packet) String() string { + if len(p.body) > 30 { + return fmt.Sprintf("{packet cmd: %v tick: %v size: %d body: %x...}", p.cmd, p.tick, len(p.body), p.body[:27]) + } + return fmt.Sprintf("{packet cmd: %v tick: %v size: %d body: %x}", p.cmd, p.tick, len(p.body), p.body) +} + +func (p *packet) Open(m *messageFactory, pbuf *proto.Buffer) (proto.Message, error) { + msg, err := m.BuildPacket(p.cmd) + if err != nil { + return nil, err + } + pbuf.SetBuf(p.body) + if err := pbuf.Unmarshal(msg); err != nil { + return nil, err + } + return msg, nil +}