real_encoder.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package protocol
  2. import "encoding/binary"
  3. type realEncoder struct {
  4. raw []byte
  5. off int
  6. stack []pushEncoder
  7. }
  8. // primitives
  9. func (re *realEncoder) putInt8(in int8) {
  10. re.raw[re.off] = byte(in)
  11. re.off += 1
  12. }
  13. func (re *realEncoder) putInt16(in int16) {
  14. binary.BigEndian.PutUint16(re.raw[re.off:], uint16(in))
  15. re.off += 2
  16. }
  17. func (re *realEncoder) putInt32(in int32) {
  18. binary.BigEndian.PutUint32(re.raw[re.off:], uint32(in))
  19. re.off += 4
  20. }
  21. func (re *realEncoder) putInt64(in int64) {
  22. binary.BigEndian.PutUint64(re.raw[re.off:], uint64(in))
  23. re.off += 8
  24. }
  25. // arrays
  26. func (re *realEncoder) putInt32Array(in []int32) {
  27. re.putArrayCount(len(in))
  28. for _, val := range in {
  29. re.putInt32(val)
  30. }
  31. }
  32. func (re *realEncoder) putArrayCount(in int) {
  33. re.putInt32(int32(in))
  34. }
  35. // misc
  36. func (re *realEncoder) putError(in KError) {
  37. re.putInt16(int16(in))
  38. }
  39. func (re *realEncoder) putString(in string) {
  40. re.putInt16(int16(len(in)))
  41. copy(re.raw[re.off:], in)
  42. re.off += len(in)
  43. }
  44. func (re *realEncoder) putBytes(in []byte) {
  45. if in == nil {
  46. re.putInt32(-1)
  47. return
  48. }
  49. re.putInt32(int32(len(in)))
  50. re.putRaw(in)
  51. }
  52. func (re *realEncoder) putRaw(in []byte) {
  53. copy(re.raw[re.off:], in)
  54. re.off += len(in)
  55. }
  56. // stackable
  57. func (re *realEncoder) push(in pushEncoder) {
  58. in.saveOffset(re.off)
  59. re.off += in.reserveLength()
  60. re.stack = append(re.stack, in)
  61. }
  62. func (re *realEncoder) pushLength32() {
  63. re.push(&length32Encoder{})
  64. }
  65. func (re *realEncoder) pushCRC32() {
  66. re.push(&crc32Encoder{})
  67. }
  68. func (re *realEncoder) pop() {
  69. // this is go's ugly pop pattern (the inverse of append)
  70. in := re.stack[len(re.stack)-1]
  71. re.stack = re.stack[:len(re.stack)-1]
  72. in.run(re.off, re.raw)
  73. }