forked from caronwang/OKEX_V5SDK_GO
/
BookData.go
225 lines (185 loc) · 4.53 KB
/
BookData.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
/*
订阅频道后收到的推送数据
*/
package wImpl
import (
"bytes"
"errors"
"fmt"
"hash/crc32"
"log"
"strconv"
)
// 普通推送
type MsgData struct {
Arg map[string]string `json:"arg"`
Data []interface{} `json:"data"`
}
// 深度数据
type DepthData struct {
Arg map[string]string `json:"arg"`
Action string `json:"action"`
Data []DepthDetail `json:"data"`
}
type DepthDetail struct {
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
Ts string `json:"ts"`
Checksum int32 `json:"checksum"`
}
/*
深度数据校验
*/
func (this *DepthData) CheckSum(snap *DepthDetail) (pDepData *DepthDetail, err error) {
if len(this.Data) != 1 {
err = errors.New("深度数据错误!")
return
}
if this.Action == DEPTH_SNAPSHOT {
_, cs := CalCrc32(this.Data[0].Asks, this.Data[0].Bids)
if cs != this.Data[0].Checksum {
err = errors.New("校验失败!")
return
}
pDepData = &this.Data[0]
log.Println("snapshot校验成功", this.Data[0].Checksum)
}
if this.Action == DEPTH_UPDATE {
if snap == nil {
err = errors.New("深度快照数据不可为空!")
return
}
pDepData, err = MergDepthData(*snap, this.Data[0], this.Data[0].Checksum)
if err != nil {
return
}
log.Println("update校验成功", this.Data[0].Checksum)
}
return
}
func CalCrc32(askDepths [][]string, bidDepths [][]string) (bytes.Buffer, int32) {
crc32BaseBuffer := bytes.Buffer{}
crcAskDepth, crcBidDepth := 25, 25
if len(askDepths) < 25 {
crcAskDepth = len(askDepths)
}
if len(bidDepths) < 25 {
crcBidDepth = len(bidDepths)
}
if crcAskDepth == crcBidDepth {
for i := 0; i < crcAskDepth; i++ {
if crc32BaseBuffer.Len() > 0 {
crc32BaseBuffer.WriteString(":")
}
crc32BaseBuffer.WriteString(
fmt.Sprintf("%v:%v:%v:%v",
(bidDepths)[i][0], (bidDepths)[i][1],
(askDepths)[i][0], (askDepths)[i][1]))
}
} else {
var crcArr []string
for i, j := 0, 0; i < crcBidDepth || j < crcAskDepth; {
if i < crcBidDepth {
crcArr = append(crcArr, fmt.Sprintf("%v:%v", (bidDepths)[i][0], (bidDepths)[i][1]))
i++
}
if j < crcAskDepth {
crcArr = append(crcArr, fmt.Sprintf("%v:%v", (askDepths)[j][0], (askDepths)[j][1]))
j++
}
}
crc32BaseBuffer.WriteString(strings.Join(crcArr, ":"))
}
expectCrc32 := int32(crc32.ChecksumIEEE(crc32BaseBuffer.Bytes()))
return crc32BaseBuffer, expectCrc32
}
/*
深度合并的内部方法
返回结果:
res:合并后的深度
index: 最新的 ask1/bids1 的索引
*/
func mergeDepth(oldDepths [][]string, newDepths [][]string, method string) (res [][]string, err error) {
oldIdx, newIdx := 0, 0
for oldIdx < len(oldDepths) && newIdx < len(newDepths) {
oldItem := oldDepths[oldIdx]
newItem := newDepths[newIdx]
var oldPrice, newPrice float64
oldPrice, err = strconv.ParseFloat(oldItem[0], 10)
if err != nil {
return
}
newPrice, err = strconv.ParseFloat(newItem[0], 10)
if err != nil {
return
}
if oldPrice == newPrice {
if newItem[1] != "0" {
res = append(res, newItem)
}
oldIdx++
newIdx++
} else {
switch method {
// 降序
case "bids":
if oldPrice < newPrice {
res = append(res, newItem)
newIdx++
} else {
res = append(res, oldItem)
oldIdx++
}
// 升序
case "asks":
if oldPrice > newPrice {
res = append(res, newItem)
newIdx++
} else {
res = append(res, oldItem)
oldIdx++
}
}
}
}
if oldIdx < len(oldDepths) {
res = append(res, oldDepths[oldIdx:]...)
}
if newIdx < len(newDepths) {
res = append(res, newDepths[newIdx:]...)
}
return
}
/*
深度合并,并校验
*/
func MergDepthData(snap DepthDetail, update DepthDetail, expChecksum int32) (res *DepthDetail, err error) {
newAskDepths, err1 := mergeDepth(snap.Asks, update.Asks, "asks")
if err1 != nil {
return
}
// log.Println("old Ask - ", snap.Asks)
// log.Println("update Ask - ", update.Asks)
// log.Println("new Ask - ", newAskDepths)
newBidDepths, err2 := mergeDepth(snap.Bids, update.Bids, "bids")
if err2 != nil {
return
}
// log.Println("old Bids - ", snap.Bids)
// log.Println("update Bids - ", update.Bids)
// log.Println("new Bids - ", newBidDepths)
cBuf, checksum := CalCrc32(newAskDepths, newBidDepths)
if checksum != expChecksum {
err = errors.New("校验失败!")
log.Println("buffer:", cBuf.String())
log.Fatal(checksum, expChecksum)
return
}
res = &DepthDetail{
Asks: newAskDepths,
Bids: newBidDepths,
Ts: update.Ts,
Checksum: update.Checksum,
}
return
}