forked from apache/thrift
/
header_protocol.go
175 lines (141 loc) · 4.25 KB
/
header_protocol.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* 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 thrift
import (
"fmt"
)
type THeaderProtocol struct {
TProtocol
origTransport TTransport
trans *THeaderTransport
protoID ProtocolID
}
type THeaderProtocolFactory struct{}
func NewTHeaderProtocolFactory() *THeaderProtocolFactory {
return &THeaderProtocolFactory{}
}
func (p *THeaderProtocolFactory) GetProtocol(trans TTransport) TProtocol {
return NewTHeaderProtocol(trans)
}
func NewTHeaderProtocol(trans TTransport) *THeaderProtocol {
p := &THeaderProtocol{
origTransport: trans,
protoID: ProtocolIDCompact,
}
if et, ok := trans.(*THeaderTransport); ok {
p.trans = et
} else {
p.trans = NewTHeaderTransport(trans)
}
if err := p.ResetProtocol(); err != nil {
panic(err)
}
return p
}
func (p *THeaderProtocol) ResetProtocol() error {
if p.TProtocol != nil && p.protoID == p.trans.ProtocolID() {
return nil
}
p.protoID = p.trans.ProtocolID()
switch p.protoID {
case ProtocolIDBinary:
p.TProtocol = NewTBinaryProtocol(p.trans, false, true)
case ProtocolIDCompact:
p.TProtocol = NewTCompactProtocol(p.trans)
default:
return NewTProtocolException(fmt.Errorf("Unknown protocol id: %#x", p.protoID))
}
return nil
}
func (p *THeaderProtocol) WriteMessageBegin(name string, typeId TMessageType, seqid int32) error {
p.ResetProtocol()
if typeId == CALL || typeId == ONEWAY {
p.trans.SetSeqID(uint32(seqid))
}
return p.TProtocol.WriteMessageBegin(name, typeId, seqid)
}
func (p *THeaderProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqid int32, err error) {
if typeId == INVALID_TMESSAGE_TYPE {
if err = p.trans.ResetProtocol(); err != nil {
return name, EXCEPTION, seqid, err
}
}
err = p.ResetProtocol()
if err != nil {
return name, EXCEPTION, seqid, err
}
return p.TProtocol.ReadMessageBegin()
}
func (p *THeaderProtocol) Flush() (err error) {
return NewTProtocolException(p.trans.Flush())
}
func (p *THeaderProtocol) Skip(fieldType TType) (err error) {
return SkipDefaultDepth(p, fieldType)
}
func (p *THeaderProtocol) Transport() TTransport {
return p.origTransport
}
func (p *THeaderProtocol) THeaderTransport() TTransport {
return p.trans
}
func (p *THeaderProtocol) SetIdentity(identity string) {
p.trans.SetIdentity(identity)
}
func (p *THeaderProtocol) Identity() string {
return p.trans.Identity()
}
func (p *THeaderProtocol) PeerIdentity() string {
return p.trans.PeerIdentity()
}
func (p *THeaderProtocol) SetPersistentHeader(key, value string) {
p.trans.SetPersistentHeader(key, value)
}
func (p *THeaderProtocol) PersistentHeader(key string) (string, bool) {
return p.trans.PersistentHeader(key)
}
func (p *THeaderProtocol) PersistentHeaders() map[string]string {
return p.trans.PersistentHeaders()
}
func (p *THeaderProtocol) ClearPersistentHeaders() {
p.trans.ClearPersistentHeaders()
}
func (p *THeaderProtocol) SetHeader(key, value string) {
p.trans.SetHeader(key, value)
}
func (p *THeaderProtocol) Header(key string) (string, bool) {
return p.trans.Header(key)
}
func (p *THeaderProtocol) Headers() map[string]string {
return p.trans.Headers()
}
func (p *THeaderProtocol) ClearHeaders() {
p.trans.ClearHeaders()
}
func (p *THeaderProtocol) ReadHeader(key string) (string, bool) {
return p.trans.ReadHeader(key)
}
func (p *THeaderProtocol) ReadHeaders() map[string]string {
return p.trans.ReadHeaders()
}
func (p *THeaderProtocol) ProtocolID() ProtocolID {
return p.protoID
}
func (p *THeaderProtocol) AddTransform(trans TransformID) error {
return p.trans.AddTransform(trans)
}