// Parse will read as many packets as possible, outputting them on the channel. func (s *PacketParser) Parse() (parsed int) { for { if s.buf.Len() < 3 { return } b := s.buf.Bytes() if b[0] != 0x02 && b[0] != 0x04 { // observed packets always begin with 0x02, assume a desync fmt.Printf("desync; truncating with buf: %s\n", humanhex.String(b, 3)) s.buf.Reset() return } l := (int(b[1]) << 8) + int(b[2]) if s.buf.Len() < (l + 4) { return } // header is 3 bytes, plus 1 byte checksum d := make([]byte, l+4) s.buf.Read(d) s.ch <- NewRawPacket(d) parsed++ } }
// String returns a dump of the packet data func (p *RawPacket) String() string { return fmt.Sprintf("%v -> %v %v %s", p.From, p.To, p.Kind, humanhex.String(p.Payload, 3)) }