-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreq_handle.go
205 lines (190 loc) · 4.37 KB
/
req_handle.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
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
package jaguar
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"reflect"
)
type ReqHandle interface {
//Read byte stream data
ReadStream(...interface{}) error
//Read string data, need to specify length
ReadStreamByString(int, *string) error
//Write byte stream data
WriteStream(...interface{})
//Reply to a request
Respone() error
//Read byte stream
ReadStreamBytes() []byte
//Requested data
Reader() *bytes.Buffer
//Data to reply to
Writer() *bytes.Buffer
}
func newReqHandle(protocol uint16, conn *tcpConn, inBuffer *bytes.Buffer) *reqHandle {
h := new(reqHandle)
h.protocol = protocol
h.inBuffer = inBuffer
h.conn = conn
return h
}
type reqHandle struct {
conn *tcpConn
inBuffer *bytes.Buffer
outBuffer *bytes.Buffer
writeError error
protocol uint16
}
// ReadStream
func (rh *reqHandle) ReadStream(values ...interface{}) error {
for index := 0; index < len(values); index++ {
if err := toType(values[index]); err != nil {
return err
}
if err := binary.Read(rh.inBuffer, _opt.ByteOrder, values[index]); err != nil {
return err
}
}
return nil
}
func (rh *reqHandle) ReadStreamBytes() []byte {
return rh.inBuffer.Bytes()
}
func (rh *reqHandle) ReadStreamByString(byteLen int, value *string) error {
data := rh.inBuffer.Next(byteLen)
*value = string(data)
if len(data) == byteLen {
return nil
}
return errors.New("Unknown error")
}
func (rh *reqHandle) WriteStream(values ...interface{}) {
if rh.outBuffer == nil {
rh.outBuffer = new(bytes.Buffer)
proBuf, _ := toBytes(rh.protocol)
rh.outBuffer.Write(proBuf)
}
if rh.writeError != nil {
return
}
for index := 0; index < len(values); index++ {
data, err := toBytes(values[index])
if err != nil {
rh.writeError = err
break
}
_, err = rh.outBuffer.Write(data)
if err != nil {
rh.writeError = err
break
}
}
}
func (rh *reqHandle) Respone() error {
if rh.writeError != nil {
return rh.writeError
}
for _, f := range rh.conn.hook.respone {
f(rh.protocol, rh)
}
rh.conn.respone(rh.protocol, rh.outBuffer)
return nil
}
func (rh *reqHandle) di(obj interface{}) {
structFields(obj, func(sf reflect.StructField, value reflect.Value) {
if sf.Type.Kind() != reflect.Interface {
return
}
inject, ok := sf.Tag.Lookup("inject")
if !ok {
return
}
if inject == "req_handle" {
value.Set(reflect.ValueOf(rh))
return
}
if inject != "" {
obj := rh.conn.attach.Interface("inject%_%" + inject)
if obj != nil {
value.Set(reflect.ValueOf(obj))
}
return
}
obj := rh.conn.attach.Interface(value.String())
if obj != nil {
value.Set(reflect.ValueOf(obj))
}
})
}
func (rh *reqHandle) Reader() *bytes.Buffer {
return rh.inBuffer
}
func (rh *reqHandle) Writer() *bytes.Buffer {
return rh.outBuffer
}
func toBytes(dest interface{}) (result []byte, e error) {
switch data := dest.(type) {
case string:
result = []byte(data)
case []byte:
result = []byte(data)
case int8:
result = append(result, byte(data))
case uint8:
result = append(result, byte(data))
case uint16:
result = make([]byte, 2)
_opt.ByteOrder.PutUint16(result, uint16(data))
case int16:
result = make([]byte, 2)
_opt.ByteOrder.PutUint16(result, uint16(data))
case uint32:
result = make([]byte, 4)
_opt.ByteOrder.PutUint32(result, uint32(data))
case int32:
result = make([]byte, 4)
_opt.ByteOrder.PutUint32(result, uint32(data))
case uint64:
result = make([]byte, 8)
_opt.ByteOrder.PutUint64(result, uint64(data))
case int64:
result = make([]byte, 8)
_opt.ByteOrder.PutUint64(result, uint64(data))
case float32:
result = make([]byte, 4)
bits := math.Float32bits(float32(data))
_opt.ByteOrder.PutUint32(result, bits)
case float64:
result = make([]byte, 8)
bits := math.Float64bits(float64(data))
_opt.ByteOrder.PutUint64(result, bits)
default:
e = errors.New("This type is not supported " + fmt.Sprint(dest) + "(" + fmt.Sprint(reflect.TypeOf(dest).Kind()) + ")")
return
}
return
}
func toType(dest interface{}) (e error) {
switch dest.(type) {
case *[]byte:
if reflect.ValueOf(dest).Elem().Len() == 0 {
e = errors.New("ReadStream sbyte length is 0")
}
case *int8:
case *uint8:
case *uint16:
case *int16:
case *uint32:
case *int32:
case *uint64:
case *int64:
case *float32:
case *float64:
default:
e = errors.New("ReadStream type is not supported " + fmt.Sprint(reflect.TypeOf(dest).Kind()))
return
}
return nil
}