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.

branch_register_response_codec.go 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package codec
  18. import (
  19. "github.com/fagongzi/goetty"
  20. )
  21. import (
  22. "github.com/seata/seata-go/pkg/protocol/message"
  23. "github.com/seata/seata-go/pkg/protocol/transaction"
  24. )
  25. func init() {
  26. GetCodecManager().RegisterCodec(CodeTypeSeata, &BranchRegisterResponseCodec{})
  27. }
  28. type BranchRegisterResponseCodec struct {
  29. }
  30. func (g *BranchRegisterResponseCodec) Decode(in []byte) interface{} {
  31. buf := goetty.NewByteBuf(len(in))
  32. buf.Write(in)
  33. msg := message.BranchRegisterResponse{}
  34. resultCode := ReadByte(buf)
  35. msg.ResultCode = message.ResultCode(resultCode)
  36. if msg.ResultCode == message.ResultCodeFailed {
  37. length := ReadUInt16(buf)
  38. if length > 0 {
  39. bytes := make([]byte, length)
  40. msg.Msg = string(Read(buf, bytes))
  41. }
  42. }
  43. exceptionCode := ReadByte(buf)
  44. msg.TransactionExceptionCode = transaction.TransactionExceptionCode(exceptionCode)
  45. msg.BranchId = int64(ReadUInt64(buf))
  46. return msg
  47. }
  48. func (c *BranchRegisterResponseCodec) Encode(in interface{}) []byte {
  49. buf := goetty.NewByteBuf(0)
  50. resp, _ := in.(message.BranchRegisterResponse)
  51. resultCode := ReadByte(buf)
  52. if resultCode == byte(message.ResultCodeFailed) {
  53. var msg string
  54. if len(resp.Msg) > 128 {
  55. msg = resp.Msg[:128]
  56. } else {
  57. msg = resp.Msg
  58. }
  59. Write16String(msg, buf)
  60. }
  61. buf.WriteByte(byte(resp.TransactionExceptionCode))
  62. branchID := uint64(resp.BranchId)
  63. branchIdBytes := []byte{
  64. byte(branchID >> 56),
  65. byte(branchID >> 48),
  66. byte(branchID >> 40),
  67. byte(branchID >> 32),
  68. byte(branchID >> 24),
  69. byte(branchID >> 16),
  70. byte(branchID >> 8),
  71. byte(branchID),
  72. }
  73. buf.Write(branchIdBytes)
  74. return buf.RawBuf()
  75. }
  76. func (g *BranchRegisterResponseCodec) GetMessageType() message.MessageType {
  77. return message.MessageType_BranchRegisterResult
  78. }

Go Implementation For Seata