packet_decoder.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package sarama
  2. import "github.com/rcrowley/go-metrics"
  3. // PacketDecoder is the interface providing helpers for reading with Kafka's encoding rules.
  4. // Types implementing Decoder only need to worry about calling methods like GetString,
  5. // not about how a string is represented in Kafka.
  6. type packetDecoder interface {
  7. // Primitives
  8. getInt8() (int8, error)
  9. getInt16() (int16, error)
  10. getInt32() (int32, error)
  11. getInt64() (int64, error)
  12. getVarint() (int64, error)
  13. getUVarint() (uint64, error)
  14. getFloat64() (float64, error)
  15. getArrayLength() (int, error)
  16. getCompactArrayLength() (int, error)
  17. getBool() (bool, error)
  18. getEmptyTaggedFieldArray() (int, error)
  19. // Collections
  20. getBytes() ([]byte, error)
  21. getVarintBytes() ([]byte, error)
  22. getCompactBytes() ([]byte, error)
  23. getRawBytes(length int) ([]byte, error)
  24. getString() (string, error)
  25. getNullableString() (*string, error)
  26. getCompactString() (string, error)
  27. getCompactNullableString() (*string, error)
  28. getCompactInt32Array() ([]int32, error)
  29. getInt32Array() ([]int32, error)
  30. getInt64Array() ([]int64, error)
  31. getStringArray() ([]string, error)
  32. // Subsets
  33. remaining() int
  34. getSubset(length int) (packetDecoder, error)
  35. peek(offset, length int) (packetDecoder, error) // similar to getSubset, but it doesn't advance the offset
  36. peekInt8(offset int) (int8, error) // similar to peek, but just one byte
  37. // Stacks, see PushDecoder
  38. push(in pushDecoder) error
  39. pop() error
  40. // To record metrics when provided
  41. metricRegistry() metrics.Registry
  42. }
  43. // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
  44. // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
  45. // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
  46. // depend upon have been decoded.
  47. type pushDecoder interface {
  48. // Saves the offset into the input buffer as the location to actually read the calculated value when able.
  49. saveOffset(in int)
  50. // Returns the length of data to reserve for the input of this encoder (e.g. 4 bytes for a CRC32).
  51. reserveLength() int
  52. // Indicates that all required data is now available to calculate and check the field.
  53. // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
  54. // of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
  55. check(curOffset int, buf []byte) error
  56. }
  57. // dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the
  58. // fields itself is unknown until its value was decoded (for instance varint encoded length
  59. // fields).
  60. // During push, dynamicPushDecoder.decode() method will be called instead of reserveLength()
  61. type dynamicPushDecoder interface {
  62. pushDecoder
  63. decoder
  64. }