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.

codec.go 3.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "bytes"
  20. "sync"
  21. )
  22. import (
  23. "vimagination.zapto.org/byteio"
  24. )
  25. import (
  26. "github.com/seata/seata-go/pkg/common/log"
  27. "github.com/seata/seata-go/pkg/protocol/message"
  28. )
  29. type CodecType byte
  30. // TODO 待重构
  31. const (
  32. CodeTypeSeata = CodecType(0x1)
  33. CodeTypeProtobuf = CodecType(0x2)
  34. CodeTypeKRYO = CodecType(0x4)
  35. CodeTypeFST = CodecType(0x8)
  36. )
  37. type Codec interface {
  38. Encode(in interface{}) []byte
  39. Decode(in []byte) interface{}
  40. GetMessageType() message.MessageType
  41. }
  42. var (
  43. codecManager *CodecManager
  44. onceCodecManager = &sync.Once{}
  45. )
  46. func GetCodecManager() *CodecManager {
  47. if codecManager == nil {
  48. onceCodecManager.Do(func() {
  49. codecManager = &CodecManager{
  50. codecMap: make(map[CodecType]map[message.MessageType]Codec, 0),
  51. }
  52. })
  53. }
  54. return codecManager
  55. }
  56. type CodecManager struct {
  57. mutex sync.Mutex
  58. codecMap map[CodecType]map[message.MessageType]Codec
  59. }
  60. func (c *CodecManager) RegisterCodec(codecType CodecType, codec Codec) {
  61. c.mutex.Lock()
  62. defer c.mutex.Unlock()
  63. codecTypeMap := c.codecMap[codecType]
  64. if codecTypeMap == nil {
  65. codecTypeMap = make(map[message.MessageType]Codec, 0)
  66. c.codecMap[codecType] = codecTypeMap
  67. }
  68. codecTypeMap[codec.GetMessageType()] = codec
  69. }
  70. func (c *CodecManager) GetCodec(codecType CodecType, msgType message.MessageType) Codec {
  71. if m := c.codecMap[codecType]; m != nil {
  72. return m[msgType]
  73. }
  74. return nil
  75. }
  76. func (c *CodecManager) Decode(codecType CodecType, in []byte) interface{} {
  77. r := byteio.BigEndianReader{Reader: bytes.NewReader(in)}
  78. typeCode, _, _ := r.ReadInt16()
  79. codec := c.GetCodec(codecType, message.MessageType(typeCode))
  80. if codec == nil {
  81. log.Errorf("This message type [%v] has no codec to decode", typeCode)
  82. return nil
  83. }
  84. return codec.Decode(in[2:])
  85. }
  86. func (c *CodecManager) Encode(codecType CodecType, in interface{}) []byte {
  87. var result = make([]byte, 0)
  88. msg := in.(message.MessageTypeAware)
  89. typeCode := msg.GetTypeCode()
  90. codec := c.GetCodec(codecType, typeCode)
  91. if codec == nil {
  92. log.Errorf("This message type [%v] has no codec to encode", typeCode)
  93. return nil
  94. }
  95. body := codec.Encode(in)
  96. typeC := uint16(typeCode)
  97. result = append(result, []byte{byte(typeC >> 8), byte(typeC)}...)
  98. result = append(result, body...)
  99. return result
  100. }

Go Implementation For Seata