You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_global_end_response_codec.go 1.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package codec
  2. import (
  3. "github.com/fagongzi/goetty"
  4. )
  5. import (
  6. "github.com/seata/seata-go/pkg/protocol/message"
  7. "github.com/seata/seata-go/pkg/protocol/transaction"
  8. )
  9. type CommonGlobalEndResponseCodec struct {
  10. }
  11. func (c *CommonGlobalEndResponseCodec) Encode(in interface{}) []byte {
  12. buf := goetty.NewByteBuf(0)
  13. resp := in.(message.AbstractGlobalEndResponse)
  14. buf.WriteByte(byte(resp.ResultCode))
  15. if resp.ResultCode == message.ResultCodeFailed {
  16. var msg string
  17. if len(resp.Msg) > 128 {
  18. msg = resp.Msg[:128]
  19. } else {
  20. msg = resp.Msg
  21. }
  22. Write16String(msg, buf)
  23. }
  24. buf.WriteByte(byte(resp.TransactionExceptionCode))
  25. buf.WriteByte(byte(resp.GlobalStatus))
  26. return buf.RawBuf()
  27. }
  28. func (c *CommonGlobalEndResponseCodec) Decode(in []byte) interface{} {
  29. buf := goetty.NewByteBuf(len(in))
  30. buf.Write(in)
  31. msg := message.AbstractGlobalEndResponse{}
  32. resultCode := ReadByte(buf)
  33. msg.ResultCode = message.ResultCode(resultCode)
  34. if msg.ResultCode == message.ResultCodeFailed {
  35. length := ReadUInt16(buf)
  36. if length > 0 {
  37. bytes := make([]byte, length)
  38. msg.Msg = string(Read(buf, bytes))
  39. }
  40. }
  41. exceptionCode := ReadByte(buf)
  42. msg.TransactionExceptionCode = transaction.TransactionExceptionCode(exceptionCode)
  43. globalStatus := ReadByte(buf)
  44. msg.GlobalStatus = transaction.GlobalStatus(globalStatus)
  45. return msg
  46. }

Go Implementation For Seata