-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreq.go
More file actions
289 lines (280 loc) · 5.36 KB
/
req.go
File metadata and controls
289 lines (280 loc) · 5.36 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package hbtp
import (
"context"
"encoding/json"
"errors"
"net"
"net/url"
"time"
)
type Request struct {
addr string
timeout time.Duration
conn net.Conn
sended bool
control int32
cmd string
args url.Values
ctx context.Context
cncl context.CancelFunc
lmtTm *LmtTmConfig
lmtMax *LmtMaxConfig
header *Map
useVersion int
}
func (c *Request) Header() *Map {
if c.header == nil {
c.header = NewMap()
}
return c.header
}
func NewRequest(addr string, control int32, timeout ...time.Duration) *Request {
tmo := time.Second * 30
if len(timeout) > 0 {
tmo = timeout[0]
}
cli := &Request{
addr: addr,
timeout: tmo,
control: control,
lmtTm: MakeLmtTmCfg(),
lmtMax: MakeLmtMaxCfg(),
}
return cli
}
func NewConnRequest(conn net.Conn, control int32, timeout ...time.Duration) *Request {
tmo := time.Second * 30
if len(timeout) > 0 {
tmo = timeout[0]
}
cli := &Request{
conn: conn,
timeout: tmo,
control: control,
lmtTm: MakeLmtTmCfg(),
lmtMax: MakeLmtMaxCfg(),
}
return cli
}
func (c *Request) SetVersion(v int) {
c.useVersion = v
}
func (c *Request) SetLmtTm(lmt *LmtTmConfig) {
c.lmtTm = lmt
}
func (c *Request) SetContext(ctx context.Context) *Request {
if ctx == nil {
return c
}
c.ctx = ctx
c.cncl = nil
return c
}
func (c *Request) Timeout(tmo time.Duration) *Request {
c.timeout = tmo
return c
}
func (c *Request) Command(cmd string) *Request {
c.cmd = cmd
return c
}
func (c *Request) GetCommand() string {
return c.cmd
}
func (c *Request) Args(args url.Values) *Request {
c.args = args
return c
}
func (c *Request) SetArg(k, v string) *Request {
if c.args == nil {
c.args = url.Values{}
}
c.args.Set(k, v)
return c
}
func (c *Request) write(bts []byte) error {
if c.ctx == nil || c.conn == nil {
return errors.New("do is call?")
}
return TcpWrite(c.ctx, c.conn, bts)
}
func (c *Request) send(bds []byte, hds ...interface{}) error {
if c.sended {
return errors.New("already send")
}
var err error
if c.conn == nil {
if c.addr == "" {
return errors.New("addr is empty")
}
c.conn, err = net.DialTimeout("tcp", c.addr, c.lmtTm.TmOhther)
if err != nil {
return err
}
}
if c.ctx == nil {
c.ctx = context.Background()
}
c.ctx, c.cncl = context.WithTimeout(c.ctx, c.timeout)
var hd []byte
if len(hds) > 0 {
switch hds[0].(type) {
case []byte:
hd = hds[0].([]byte)
case string:
hd = []byte(hds[0].(string))
default:
hd, err = json.Marshal(hds[0])
if err != nil {
return err
}
}
} else if c.header != nil {
hd = c.header.ToBytes()
}
var args string
if c.args != nil {
args = c.args.Encode()
}
info := &msgInfo{
Version: 1,
Control: c.control,
LenCmd: uint16(len(c.cmd)),
LenArg: uint16(len(args)),
LenHead: uint32(len(hd)),
LenBody: uint32(len(bds)),
}
if c.useVersion > 0 {
info.Version = uint16(c.useVersion)
}
bts, err := FlcStruct2Byte(info)
if err != nil {
return err
}
c.sended = true
err = c.write(bts)
if err != nil {
return err
}
if info.Version >= 2 {
err = c.write([]byte{0x48, 0x42, 0x54, 0x50})
if err != nil {
return err
}
}
if info.LenCmd > 0 {
err = c.write([]byte(c.cmd))
if err != nil {
return err
}
}
if info.LenArg > 0 {
err = c.write([]byte(args))
if err != nil {
return err
}
}
if info.LenHead > 0 {
err = c.write(hd)
if err != nil {
return err
}
}
if info.LenBody > 0 {
err = c.write(bds)
if err != nil {
return err
}
}
return nil
}
func (c *Request) Res() (res *Response, rterr error) {
defer func() {
if rterr != nil && c.conn != nil {
c.conn.Close()
}
}()
if !c.sended {
return nil, errors.New("not send")
}
if c.ctx == nil || c.conn == nil {
return nil, errors.New("need do some thing")
}
info := &resInfoV1{}
infoln := FlcStructSizeof(info)
ctx, _ := context.WithTimeout(c.ctx, c.lmtTm.TmOhther)
bts, err := TcpRead(ctx, c.conn, uint(infoln))
if err != nil {
return nil, err
}
err = FlcByte2Struct(bts, info)
if err != nil {
return nil, err
}
if uint64(info.LenHead) > c.lmtMax.MaxHeads {
return nil, errors.New("bytes2 out limit!!")
}
rt := &Response{
conn: c.conn,
code: info.Code,
bdln: info.LenBody,
}
if info.LenHead > 0 {
ctx, _ = context.WithTimeout(c.ctx, c.lmtTm.TmHeads)
rt.heads, err = TcpRead(ctx, c.conn, uint(info.LenHead))
if err != nil {
return nil, err
}
}
/* if info.LenBody > 0 {
ctx, _ = context.WithTimeout(c.ctx, c.lmtTm.TmBodys)
rt.bodys, err = TcpRead(ctx, c.conn, uint(info.LenBody))
if err != nil {
return nil, err
}
} */
return rt, nil
}
func (c *Request) DoNoRes(ctx context.Context, body interface{}, hds ...interface{}) (rterr error) {
defer func() {
if rterr != nil && c.conn != nil {
c.conn.Close()
}
}()
c.ctx = ctx
var err error
var bdbts []byte
if body != nil {
switch body.(type) {
case []byte:
bdbts = body.([]byte)
case string:
bdbts = []byte(body.(string))
default:
bdbts, err = json.Marshal(body)
if err != nil {
return err
}
}
}
return c.send(bdbts, hds...)
}
func (c *Request) Do(ctx context.Context, body interface{}, hds ...interface{}) (res *Response, rterr error) {
defer func() {
if rterr != nil && c.conn != nil {
c.conn.Close()
}
}()
err := c.DoNoRes(ctx, body, hds...)
if err != nil {
return nil, err
}
return c.Res()
}
func (c *Request) Cancel() error {
if c.cncl != nil {
c.cncl()
c.cncl = nil
}
return nil
}