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

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. model2 "github.com/seata/seata-go/pkg/protocol/branch"
  23. "github.com/seata/seata-go/pkg/protocol/message"
  24. )
  25. func init() {
  26. GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRegisterRequestCodec{})
  27. }
  28. type BranchRegisterRequestCodec struct {
  29. }
  30. func (g *BranchRegisterRequestCodec) Decode(in []byte) interface{} {
  31. buf := goetty.NewByteBuf(len(in))
  32. buf.Write(in)
  33. msg := message.BranchRegisterRequest{}
  34. length := ReadUInt16(buf)
  35. if length > 0 {
  36. bytes := make([]byte, length)
  37. msg.Xid = string(Read(buf, bytes))
  38. }
  39. msg.BranchType = model2.BranchType(ReadByte(buf))
  40. length = ReadUInt16(buf)
  41. if length > 0 {
  42. bytes := make([]byte, length)
  43. msg.ResourceId = string(Read(buf, bytes))
  44. }
  45. length32 := ReadUInt32(buf)
  46. if length > 0 {
  47. bytes := make([]byte, length32)
  48. msg.LockKey = string(Read(buf, bytes))
  49. }
  50. length32 = ReadUInt32(buf)
  51. if length > 0 {
  52. bytes := make([]byte, length32)
  53. msg.ApplicationData = Read(buf, bytes)
  54. }
  55. return msg
  56. }
  57. func (c *BranchRegisterRequestCodec) Encode(in interface{}) []byte {
  58. buf := goetty.NewByteBuf(0)
  59. req, _ := in.(message.BranchRegisterRequest)
  60. Write16String(req.Xid, buf)
  61. buf.WriteByte(byte(req.BranchType))
  62. Write16String(req.ResourceId, buf)
  63. Write32String(req.LockKey, buf)
  64. Write32String(string(req.ApplicationData), buf)
  65. return buf.RawBuf()
  66. }
  67. func (g *BranchRegisterRequestCodec) GetMessageType() message.MessageType {
  68. return message.MessageType_BranchRegister
  69. }

Go Implementation For Seata