-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
300 lines (249 loc) · 6.44 KB
/
utils.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
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
290
291
292
293
294
295
296
297
298
299
300
package ibapi
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"strconv"
"time"
)
const (
delim byte = '\x00'
// MAX_MSG_LEN is the max length that receiver could take.
MAX_MSG_LEN int = 0xFFFFFF // 16Mb - 1byte
)
// MsgBuffer is the buffer that contains a whole msg.
type MsgBuffer struct {
bytes.Buffer
bs []byte
err error
}
// NewMsgBuffer create a new MsgBuffer.
func NewMsgBuffer(bs []byte) *MsgBuffer {
return &MsgBuffer{*bytes.NewBuffer(bs), nil, nil}
}
// Reset reset buffer, []byte, err.
func (m *MsgBuffer) Reset() {
m.Buffer.Reset()
m.bs = m.bs[:0]
m.err = nil
}
func (m *MsgBuffer) decodeInt64() int64 {
var i int64
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode int64 read error")
}
m.bs = m.bs[:len(m.bs)-1]
if bytes.Equal(m.bs, nil) {
return 0
}
i, m.err = strconv.ParseInt(string(m.bs), 10, 64)
if m.err != nil {
fmt.Println(string(m.bs))
log.Panic().Err(m.err).Msg("decode int64 parse error")
}
return i
}
func (m *MsgBuffer) decode() {
_, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode read error")
}
}
func (m *MsgBuffer) decodeDecimal() Decimal {
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode decimal read error")
}
d, err := StringToDecimalErr(string(m.bs[:len(m.bs)-1]))
if err != nil {
log.Panic().Err(err).Msg("decode decimal parse error")
}
return d
}
func (m *MsgBuffer) decodeInt64ShowUnset() int64 {
var i int64
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode int64ShowUnset read error")
}
m.bs = m.bs[:len(m.bs)-1]
if bytes.Equal(m.bs, nil) {
return UNSET_INT
}
i, m.err = strconv.ParseInt(string(m.bs), 10, 64)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode int64ShowUnset parse error")
}
return i
}
func (m *MsgBuffer) decodeFloat64() float64 {
var f float64
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode float64 read error")
}
m.bs = m.bs[:len(m.bs)-1]
if bytes.Equal(m.bs, nil) {
return 0.0
}
f, m.err = strconv.ParseFloat(string(m.bs), 64)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode float64 parse error")
}
return f
}
func (m *MsgBuffer) decodeFloat64ShowUnset() float64 {
var f float64
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode float64ShowUnset read error")
}
m.bs = m.bs[:len(m.bs)-1]
if bytes.Equal(m.bs, nil) || bytes.Equal(m.bs, []byte("None")) {
return UNSET_FLOAT
}
f, m.err = strconv.ParseFloat(string(m.bs), 64)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode float64ShowUnset parse error")
}
return f
}
func (m *MsgBuffer) decodeBool() bool {
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode bool read error")
}
m.bs = m.bs[:len(m.bs)-1]
if bytes.Equal(m.bs, []byte{'0'}) || bytes.Equal(m.bs, nil) {
return false
}
return true
}
func (m *MsgBuffer) decodeString() string {
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode string read error")
}
return string(m.bs[:len(m.bs)-1])
}
func (m *MsgBuffer) decodeStringUnescaped() string {
m.bs, m.err = m.ReadBytes(delim)
if m.err != nil {
log.Panic().Err(m.err).Msg("decode string read error")
}
var s string
s, m.err = strconv.Unquote(fmt.Sprint("\"", m.bs[:len(m.bs)-1], "\""))
if m.err != nil {
log.Panic().Err(m.err).Msg("decode string unmarshal error")
}
return s
}
// scanFields defines how to unpack the buf
func scanFields(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF {
return 0, nil, io.EOF
}
if len(data) < 4 {
return 0, nil, nil // will try to read more data
}
totalSize := int(binary.BigEndian.Uint32(data[:4])) + 4
if totalSize > len(data) {
return 0, nil, nil
}
// msgBytes := make([]byte, totalSize-4, totalSize-4)
// copy(msgBytes, data[4:totalSize])
// not copy here, copied by callee more reasonable
return totalSize, data[4:totalSize], nil
}
// makeFields is a universal way to make the request ,but not an efficient way
// TODO: do some test and improve!!!
func makeFields(fields ...interface{}) []byte {
msgBytes := make([]byte, 4, 8*len(fields)+4) // pre alloc memory
for _, f := range fields {
switch v := f.(type) {
case int64:
msgBytes = strconv.AppendInt(msgBytes, v, 10)
case float64:
msgBytes = strconv.AppendFloat(msgBytes, v, 'g', 10, 64)
case string:
msgBytes = append(msgBytes, []byte(v)...)
case bool:
if v {
msgBytes = append(msgBytes, '1')
} else {
msgBytes = append(msgBytes, '0')
}
case int:
msgBytes = strconv.AppendInt(msgBytes, int64(v), 10)
case []byte:
msgBytes = append(msgBytes, v...)
case Decimal:
msgBytes = append(msgBytes, []byte(DecimalToString(v))...)
default:
log.Panic().Interface("field", f).Msg("failed to covert the field") // never reach here
}
msgBytes = append(msgBytes, delim)
}
// add the size header
binary.BigEndian.PutUint32(msgBytes, uint32(len(msgBytes)-4))
return msgBytes
}
func splitMsgBytes(data []byte) [][]byte {
fields := bytes.Split(data, []byte{delim})
return fields[:len(fields)-1]
}
func handleEmpty(d interface{}) string {
switch v := d.(type) {
case int64:
if v == UNSET_INT {
return ""
}
return strconv.FormatInt(v, 10)
case float64:
if v == UNSET_FLOAT {
return ""
}
return strconv.FormatFloat(v, 'g', 10, 64)
default:
log.Panic().Interface("val", d).Msg("no handler for such type")
return "" // never reach here
}
}
func FloatMaxString(val float64) string {
if val == UNSET_FLOAT {
return ""
}
return strconv.FormatFloat(val, 'g', 10, 64)
}
func LongMaxString(val int64) string {
if val == UNSET_LONG {
return ""
}
return strconv.FormatInt(val, 10)
}
func IntMaxString(val int64) string {
if val == UNSET_INT {
return ""
}
return strconv.FormatInt(val, 10)
}
func DecimalMaxString(val Decimal) string {
if val == UNSET_DECIMAL {
return ""
}
return DecimalToString(val)
}
// CurrentTimeMillis returns the current time in milliseconds.
func currentTimeMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
// GetTimeStrFromMillis converts a timestamp in milliseconds to a formatted string.
// Returns an empty string if the input time is less than or equal to zero.
func GetTimeStrFromMillis(timestamp int64) string {
if timestamp > 0 {
return time.Unix(0, timestamp*int64(time.Millisecond)).Format("20060102-15:04:05")
}
return ""
}