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_commit_response_codec.go 2.3 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "math"
  20. serror "seata.apache.org/seata-go/pkg/util/errors"
  21. "seata.apache.org/seata-go/pkg/protocol/branch"
  22. "seata.apache.org/seata-go/pkg/protocol/message"
  23. "seata.apache.org/seata-go/pkg/util/bytes"
  24. )
  25. type BranchCommitResponseCodec struct{}
  26. func (g *BranchCommitResponseCodec) Decode(in []byte) interface{} {
  27. data := message.BranchCommitResponse{}
  28. buf := bytes.NewByteBuffer(in)
  29. data.ResultCode = message.ResultCode(bytes.ReadByte(buf))
  30. if data.ResultCode == message.ResultCodeFailed {
  31. data.Msg = bytes.ReadString8Length(buf)
  32. }
  33. data.TransactionErrorCode = serror.TransactionErrorCode(bytes.ReadByte(buf))
  34. data.Xid = bytes.ReadString16Length(buf)
  35. data.BranchId = int64(bytes.ReadUInt64(buf))
  36. data.BranchStatus = branch.BranchStatus(bytes.ReadByte(buf))
  37. return data
  38. }
  39. func (g *BranchCommitResponseCodec) Encode(in interface{}) []byte {
  40. data, _ := in.(message.BranchCommitResponse)
  41. buf := bytes.NewByteBuffer([]byte{})
  42. buf.WriteByte(byte(data.ResultCode))
  43. if data.ResultCode == message.ResultCodeFailed {
  44. msg := data.Msg
  45. if len(data.Msg) > math.MaxInt8 {
  46. msg = data.Msg[:math.MaxInt8]
  47. }
  48. bytes.WriteString8Length(msg, buf)
  49. }
  50. buf.WriteByte(byte(data.TransactionErrorCode))
  51. bytes.WriteString16Length(data.Xid, buf)
  52. buf.WriteInt64(data.BranchId)
  53. buf.WriteByte(byte(data.BranchStatus))
  54. return buf.Bytes()
  55. }
  56. func (g *BranchCommitResponseCodec) GetMessageType() message.MessageType {
  57. return message.MessageTypeBranchCommitResult
  58. }