-
Notifications
You must be signed in to change notification settings - Fork 905
/
package.go
171 lines (143 loc) · 3.83 KB
/
package.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
/*
* 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 impl
import (
"bufio"
"bytes"
"fmt"
"time"
)
import (
"github.com/pkg/errors"
)
type PackageType int
// enum part
const (
PackageError = PackageType(0x01)
PackageRequest = PackageType(0x02)
PackageResponse = PackageType(0x04)
PackageHeartbeat = PackageType(0x08)
PackageRequest_TwoWay = PackageType(0x10)
PackageResponse_Exception = PackageType(0x20)
PackageType_BitSize = 0x2f
)
type DubboHeader struct {
SerialID byte
Type PackageType
ID int64
BodyLen int
ResponseStatus byte
}
// Service defines service instance
type Service struct {
Path string
Interface string
Group string
Version string
Method string
Timeout time.Duration // request timeout
}
type DubboPackage struct {
Header DubboHeader
Service Service
Body interface{}
Err error
Codec *ProtocolCodec
}
func (p DubboPackage) String() string {
return fmt.Sprintf("HessianPackage: Header-%v, Path-%v, Body-%v", p.Header, p.Service, p.Body)
}
func (p *DubboPackage) ReadHeader() error {
return p.Codec.ReadHeader(&p.Header)
}
func (p *DubboPackage) Marshal() (*bytes.Buffer, error) {
if p.Codec == nil {
return nil, errors.New("Codec is nil")
}
pkg, err := p.Codec.Encode(*p)
if err != nil {
return nil, errors.WithStack(err)
}
return bytes.NewBuffer(pkg), nil
}
func (p *DubboPackage) Unmarshal() error {
if p.Codec == nil {
return errors.New("Codec is nil")
}
return p.Codec.Decode(p)
}
func (p DubboPackage) IsHeartBeat() bool {
return p.Header.Type&PackageHeartbeat != 0
}
func (p DubboPackage) IsRequest() bool {
return p.Header.Type&(PackageRequest_TwoWay|PackageRequest) != 0
}
func (p DubboPackage) IsResponse() bool {
return p.Header.Type == PackageResponse
}
func (p DubboPackage) IsResponseWithException() bool {
flag := PackageResponse | PackageResponse_Exception
return p.Header.Type&flag == flag
}
func (p DubboPackage) GetBodyLen() int {
return p.Header.BodyLen
}
func (p DubboPackage) GetLen() int {
return HEADER_LENGTH + p.Header.BodyLen
}
func (p DubboPackage) GetBody() interface{} {
return p.Body
}
func (p *DubboPackage) SetBody(body interface{}) {
p.Body = body
}
func (p *DubboPackage) SetHeader(header DubboHeader) {
p.Header = header
}
func (p *DubboPackage) SetService(svc Service) {
p.Service = svc
}
func (p *DubboPackage) SetID(id int64) {
p.Header.ID = id
}
func (p DubboPackage) GetHeader() DubboHeader {
return p.Header
}
func (p DubboPackage) GetService() Service {
return p.Service
}
func (p *DubboPackage) SetResponseStatus(status byte) {
p.Header.ResponseStatus = status
}
func (p *DubboPackage) SetSerializer(serializer Serializer) {
p.Codec.SetSerializer(serializer)
}
func NewDubboPackage(data *bytes.Buffer) *DubboPackage {
var codec *ProtocolCodec
if data == nil {
codec = NewDubboCodec(nil)
} else {
codec = NewDubboCodec(bufio.NewReaderSize(data, len(data.Bytes())))
}
return &DubboPackage{
Header: DubboHeader{},
Service: Service{},
Body: nil,
Err: nil,
Codec: codec,
}
}