-
Notifications
You must be signed in to change notification settings - Fork 267
/
codec.go
146 lines (124 loc) · 4.8 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package codec
import (
"bytes"
"sync"
"vimagination.zapto.org/byteio"
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/seata/seata-go/pkg/util/log"
)
type CodecType byte
const (
CodecTypeSeata = CodecType(0x1)
CodecTypeProtobuf = CodecType(0x2)
CodecTypeKRYO = CodecType(0x4)
CodecTypeFST = CodecType(0x8)
)
type Codec interface {
Encode(in interface{}) []byte
Decode(in []byte) interface{}
GetMessageType() message.MessageType
}
var (
codecManager *CodecManager
onceCodecManager = &sync.Once{}
)
func GetCodecManager() *CodecManager {
if codecManager == nil {
onceCodecManager.Do(func() {
codecManager = &CodecManager{
codecMap: make(map[CodecType]map[message.MessageType]Codec, 0),
}
})
}
return codecManager
}
type CodecManager struct {
mutex sync.Mutex
codecMap map[CodecType]map[message.MessageType]Codec
}
func (c *CodecManager) RegisterCodec(codecType CodecType, codec Codec) {
c.mutex.Lock()
defer c.mutex.Unlock()
codecTypeMap := c.codecMap[codecType]
if codecTypeMap == nil {
codecTypeMap = make(map[message.MessageType]Codec, 0)
c.codecMap[codecType] = codecTypeMap
}
codecTypeMap[codec.GetMessageType()] = codec
}
func (c *CodecManager) GetCodec(codecType CodecType, msgType message.MessageType) Codec {
if m := c.codecMap[codecType]; m != nil {
return m[msgType]
}
return nil
}
func (c *CodecManager) Decode(codecType CodecType, in []byte) interface{} {
r := byteio.BigEndianReader{Reader: bytes.NewReader(in)}
typeCode, _, _ := r.ReadInt16()
codec := c.GetCodec(codecType, message.MessageType(typeCode))
if codec == nil {
log.Errorf("This message type [%v] has no codec to decode", typeCode)
return nil
}
return codec.Decode(in[2:])
}
func (c *CodecManager) Encode(codecType CodecType, in interface{}) []byte {
result := make([]byte, 0)
msg := in.(message.MessageTypeAware)
typeCode := msg.GetTypeCode()
codec := c.GetCodec(codecType, typeCode)
if codec == nil {
log.Errorf("This message type [%v] has no codec to encode", typeCode)
return nil
}
body := codec.Encode(in)
typeC := uint16(typeCode)
result = append(result, []byte{byte(typeC >> 8), byte(typeC)}...)
result = append(result, body...)
return result
}
func Init() {
// Global
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalReportResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalBeginRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalBeginResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalCommitRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalCommitResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalLockQueryRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalLockQueryResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalRollbackRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalRollbackResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalStatusRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalStatusResponseCodec{})
// Branch
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchCommitRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchCommitResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRegisterRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRegisterResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchReportRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRollbackRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRollbackResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchReportResponseCodec{})
// RM
GetCodecManager().RegisterCodec(CodecTypeSeata, &RegisterRMRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &RegisterRMResponseCodec{})
// TM
GetCodecManager().RegisterCodec(CodecTypeSeata, &RegisterTMRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &RegisterTMResponseCodec{})
}