http urls monitor.

packet.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package linker
  2. import (
  3. "github.com/wpajqz/linker/utils/convert"
  4. )
  5. type (
  6. Packet struct {
  7. Operator uint32
  8. Sequence int64
  9. HeaderLength uint32
  10. BodyLength uint32
  11. Header []byte
  12. Body []byte
  13. }
  14. // Packet plugin, for example debug,gzip,encrypt,decrypt
  15. PacketPlugin interface {
  16. Handle(header, body []byte) (h, b []byte)
  17. }
  18. )
  19. func NewPacket(operator uint32, sequence int64, header, body []byte, plugins []PacketPlugin) (Packet, error) {
  20. for _, plugin := range plugins {
  21. header, body = plugin.Handle(header, body)
  22. }
  23. return Packet{
  24. Operator: operator,
  25. Sequence: sequence,
  26. HeaderLength: uint32(len(header)),
  27. BodyLength: uint32(len(body)),
  28. Header: header,
  29. Body: body,
  30. }, nil
  31. }
  32. // 得到序列化后的Packet
  33. func (p Packet) Bytes() (buf []byte) {
  34. buf = append(buf, convert.Uint32ToBytes(p.Operator)...)
  35. buf = append(buf, convert.Int64ToBytes(p.Sequence)...)
  36. buf = append(buf, convert.Uint32ToBytes(p.HeaderLength)...)
  37. buf = append(buf, convert.Uint32ToBytes(p.BodyLength)...)
  38. buf = append(buf, p.Header...)
  39. buf = append(buf, p.Body...)
  40. return buf
  41. }