-
Notifications
You must be signed in to change notification settings - Fork 3
/
response.go
110 lines (96 loc) · 2.61 KB
/
response.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
package exposed
import (
"bufio"
"fmt"
"sync"
)
// response is a TLV response.
type response struct {
payload []byte
error []byte
sizeBuf [4]byte
}
// Reset resets the given response.
func (resp *response) Reset() {
resp.payload = resp.payload[:0]
resp.error = resp.error[:0]
}
// Write appends p to the response payload.
//
// It implements io.Writer.
func (resp *response) Write(p []byte) (int, error) {
resp.AppendPayload(p)
return len(p), nil
}
// AppendPayload appends p to the response payload.
func (resp *response) AppendPayload(p []byte) {
resp.payload = append(resp.payload, p...)
}
// SwapPayload swaps the given payload with the response's payload.
//
// It is forbidden accessing the swapped payload after the call.
func (resp *response) SwapPayload(value []byte) {
resp.payload = value
return
}
// SwapError swaps the given payload with the response's payload.
//
// It is forbidden accessing the swapped payload after the call.
func (resp *response) SwapError(value []byte) []byte {
v := resp.error
resp.error = value
return v
}
// Payload returns response payload.
//
// The returned payload is valid until the next response method call.
// or until ReleaseResponse is called.
func (resp *response) Payload() []byte {
return resp.payload
}
// Payload returns response error.
//
// The returned error is valid until the next response method call.
// or until ReleaseResponse is called.
func (resp *response) Error() []byte {
return resp.error
}
// writeResponse writes the response to bw.
func (resp *response) WriteResponse(bw *bufio.Writer) error {
if err := writeBytes(bw, resp.payload, resp.sizeBuf[:]); err != nil {
return fmt.Errorf("cannot write response payload: %s", err)
}
if err := writeBytes(bw, resp.error, resp.sizeBuf[:]); err != nil {
return fmt.Errorf("cannot write response error: %s", err)
}
return nil
}
// ReadResponse reads the response from br.
//
// It implements exposed.ReadResponse.
func (resp *response) ReadResponse(br *bufio.Reader) error {
var err error
resp.payload, err = readBytes(br, resp.payload[:0], resp.sizeBuf[:])
if err != nil {
return fmt.Errorf("cannot read response payload: %s", err)
}
resp.error, err = readBytes(br, resp.error[:0], resp.sizeBuf[:])
if err != nil {
return fmt.Errorf("cannot read response error: %s", err)
}
return nil
}
// AcquireResponse acquires new response.
func AcquireResponse() *response {
v := responsePool.Get()
if v == nil {
v = &response{}
}
return v.(*response)
}
// ReleaseResponse releases the given response.
func ReleaseResponse(resp *response) {
resp.Reset()
responsePool.Put(resp)
}
var responsePool sync.Pool