-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStocks.cpp
183 lines (162 loc) · 7.31 KB
/
Stocks.cpp
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
//
// Created by charles.cxh on 2021/9/4.
//
#include "Stocks.h"
#include <map>
#include <iostream>
#include "sysinfo.h"
#include "Snapshot.h"
#include "UpdateLevel.h"
#include "DeleteLevel.h"
using namespace sysinfo;
using namespace std;
enum msgType {
SNAPSHOT_TYPE = (uint8_t) 1,//存在
UPDATE_TYPE = (uint8_t) 2,//执行
DELETE_TYPE = (uint8_t) 3,//写
};
/**
* 反序列化
*/
void Stocks::deserialize(std::unique_ptr<char[]> &arg, std::streamsize size) {
if (size <= 0) {
std::cout << "文件处理内容为空" << std::endl;
return;
}
size_t offset = 0;
while (offset < size) {
uint8_t type = sysinfo::ReadUInt8(arg[offset]);
//第一阶段:初始化五档行情
if (type == SNAPSHOT_TYPE) {
Snapshot snapshot;
//内存初始化
memset(&snapshot, 0, sizeof(struct Snapshot));
snapshot.msgType = sysinfo::ReadUInt8(arg[offset]);
memcpy(&snapshot.symbol, &arg[offset + msgTypeOffset], symbolOffset);
for (int i = 0; i < 5; ++i) {
snapshot.bidPrice[i] = sysinfo::ReadFloat64(
&arg[offset + msgTypeOffset + symbolOffset +
i * sizeof(FLOAT64)]);
snapshot.bidSize[i] = sysinfo::ReadUInt32(
&arg[offset + msgTypeOffset + symbolOffset +
bidPriceOffset +
i * sizeof(uint32_t)]);
snapshot.askPrice[i] = sysinfo::ReadFloat64(
&arg[offset + msgTypeOffset + symbolOffset +
bidPriceOffset + bidSizeOffset +
i * sizeof(FLOAT64)]);
snapshot.askSize[i] = sysinfo::ReadUInt32(
&arg[offset + msgTypeOffset + symbolOffset +
bidPriceOffset + bidSizeOffset + askPriceOffset +
i * sizeof(uint32_t)]);
}
offset += sizeof(Snapshot);
char *str = new char[symbolOffset + 1];
memcpy(str, snapshot.symbol, symbolOffset);
str[symbolOffset] = '\0';
snapshotMap.insert(std::map<std::string, Snapshot>::value_type(std::string(str), snapshot));
delete str;
} else if (type == UPDATE_TYPE) {
UpdateLevel updateLevel;
memset(&updateLevel, 0, sizeof(struct UpdateLevel));
updateLevel.msgType = sysinfo::ReadUInt8(arg[offset]);
memcpy(&updateLevel.symbol, &arg[offset + msgTypeOffset], symbolOffset);
updateLevel.side = sysinfo::ReadUInt8(
arg[offset + msgTypeOffset + symbolOffset]);
updateLevel.level = sysinfo::ReadUInt8(
arg[offset + msgTypeOffset + symbolOffset +
sideOffset]);
updateLevel.price = sysinfo::ReadFloat64(
&arg[offset + msgTypeOffset + symbolOffset +
sideOffset + levelOffset]);
updateLevel.size = sysinfo::ReadUInt32(
&arg[offset + msgTypeOffset + symbolOffset +
sideOffset + levelOffset + priceOffset]);
char *str = new char[symbolOffset + 1];
memcpy(str, updateLevel.symbol, symbolOffset);
str[symbolOffset] = '\0';
const std::map<std::string, Snapshot>::iterator &iterator = snapshotMap.find(
str);
if (iterator != snapshotMap.end()) {
cout << "更新" << str << (int) updateLevel.side << "(1-Bid 2-Ask) " << (int) updateLevel.level
<< "档 数量:"<< updateLevel.size << "价格:" << updateLevel.price << endl;
cout << "修改前" << endl;
iterator->second.show();
if (updateLevel.side == 1) {
iterator->second.bidPrice[updateLevel.level - 1] = updateLevel.price;
iterator->second.bidSize[updateLevel.level - 1] = updateLevel.size;
} else if (updateLevel.side == 2) {
iterator->second.askPrice[updateLevel.level - 1] = updateLevel.price;
iterator->second.askSize[updateLevel.level - 1] = updateLevel.size;
}
cout << "修改后" << endl;
iterator->second.show();
}
offset += sizeof(UpdateLevel);
delete[] str;
} else if (type == DELETE_TYPE) {
DeleteLevel deleteLevel;
memset(&deleteLevel, 0, sizeof(struct DeleteLevel));
deleteLevel.msgType = sysinfo::ReadUInt8(arg[offset]);
memcpy(&deleteLevel.symbol, &arg[offset + sizeof(deleteLevel.msgType)], symbolOffset);
deleteLevel.side = sysinfo::ReadUInt8(
arg[offset + msgTypeOffset + symbolOffset]);
deleteLevel.level = sysinfo::ReadUInt8(
arg[offset + msgTypeOffset + symbolOffset +
sideOffset]);
char *str = new char[symbolOffset + 1];
memcpy(str, deleteLevel.symbol, symbolOffset);
str[symbolOffset] = '\0';
const std::map<std::string, Snapshot>::iterator &iterator = snapshotMap.find(
str);
if (iterator != snapshotMap.end()) {
cout << "删除" << str <<"(" <<(int) deleteLevel.side << ":1-Bid 2-Ask) " << (int) deleteLevel.level
<<"档" << endl;
cout << "删除前" << endl;
iterator->second.show();
if (deleteLevel.side == 1) {
if (deleteLevel.level == 5) {
iterator->second.bidPrice[4] = 0;
iterator->second.bidSize[4] = 0;
} else {
for (int i = deleteLevel.level; i < 5; ++i) {
iterator->second.bidPrice[i - 1] = iterator->second.bidPrice[i];
iterator->second.bidSize[i - 1] = iterator->second.bidSize[i];
}
iterator->second.bidPrice[4] = 0;
iterator->second.bidSize[4] = 0;
}
} else if (deleteLevel.side == 2) {
if (deleteLevel.level == 5) {
iterator->second.askPrice[4] = 0;
iterator->second.askSize[4] = 0;
} else {
for (int i = deleteLevel.level; i < 5; ++i) {
iterator->second.askPrice[i - 1] = iterator->second.askPrice[i];
iterator->second.askSize[i - 1] = iterator->second.askSize[i];
}
iterator->second.askPrice[4] = 0;
iterator->second.askSize[4] = 0;
}
}
cout << "删除后" << endl;
iterator->second.show();
}
offset += sizeof(DeleteLevel);
delete[] str;
}
}
}
/**
* 查询股票信息
* @param symbol
* @return
*/
Snapshot *Stocks::query(std::string symbol) {
const std::map<std::string, Snapshot>::iterator &iterator = snapshotMap.find(
symbol);
if (iterator != snapshotMap.end()) {
return &iterator->second;
}
return NULL;
}