-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
267 lines (197 loc) · 5.4 KB
/
main.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
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
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<thread>
#include <objbase.h>
#include <string.h>
#include <iostream>
#include "tools/MyZmqPublish.h"
#include "tools/MyLogger.h"
#include "clients/MyWebSocketMarket_Binance_f.h"
#include "clients/MyWebSocketMarket_Okx_f.h"
using namespace std;
using namespace rapidjson;
using namespace std;
Document* gcp_config = NULL;
MyLogger *gcp_mylogger = NULL;
std::map<std::string, MyZmqPublish*> gcp_map_zmqpublish;
thread * gcp_thread_zmqconsume[30];
std::vector<MyWebSocketMarket*> gcp_vec_mywebsocktmarket;
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
int gf_split3(const std::string& str, std::vector<std::string>& ret_, std::string sep)
{
ret_.clear();
if (str.empty())
{
return 0;
}
int strcount = 0;
std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = 0;
while (pos_begin != std::string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != std::string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty())
{
ret_.push_back(tmp);
strcount++;
tmp.clear();
}
}
return strcount;
}
long long gf_getlltime()
{
static long long begints = 0;
if (begints == 0)
{
SYSTEMTIME currentTime;
GetLocalTime(¤tTime);
tm temptm = { currentTime.wSecond,
currentTime.wMinute,
currentTime.wHour,
currentTime.wDay,
currentTime.wMonth - 1,
currentTime.wYear - 1900
};
long long nTimer = mktime(&temptm) * 1000 + currentTime.wMilliseconds;
begints = nTimer - GetTickCount64();
}
return GetTickCount64() + begints;
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
int load_config(const char* jsonfile)
{
std::ifstream in;
std::string stringFromStream;
in.open(jsonfile, std::ifstream::in);
if (!in.is_open())
return -1;
std::string line;
while (getline(in, line)) {
stringFromStream.append(line + "\n");
}
in.close();
Document * config = new Document();
config->Parse(stringFromStream.c_str());
if (gcp_config != NULL)
{
Document * temp = gcp_config;
delete temp;
}
gcp_config = config;
return 0;
}
MyWebSocketMarket* create_websocket_market(const char* platformname, const char* wsid)
{
MyWebSocketMarket* ws = NULL;
if (strcmp(platformname, "binance-f") == 0)
{
ws = new MyWebSocketMarket_Binance_f(wsid);
}
else if (strcmp(platformname, "okx-f") == 0)
{
ws = new MyWebSocketMarket_Okx_f(wsid);
}
return ws;
}
int init_market()
{
const Value& WSMarketConfig = (*gcp_config)["WSMarketConfig"];
for (Value::ConstMemberIterator itr = WSMarketConfig.MemberBegin(); itr != WSMarketConfig.MemberEnd(); ++itr)
{
std::string wsid = itr->name.GetString();
std::string wsclassname = itr->value["wsclass"].GetString();
MyWebSocketMarket* newws = create_websocket_market(wsclassname.c_str(), wsid.c_str());
gcp_vec_mywebsocktmarket.push_back(newws);
std::string cnstr = itr->value["zmqstr"].GetString();
gcp_map_zmqpublish[wsid] = new MyZmqPublish();
gcp_map_zmqpublish[wsid]->mf_SetCon(cnstr);
for (Value::ConstMemberIterator subitr = itr->value.MemberBegin(); subitr != itr->value.MemberEnd(); ++subitr)
{
std::string channelname = subitr->name.GetString();
if (channelname == "depth")
{
const Value& symbolarray = subitr->value["symbol"].GetArray();
size_t symbolsize = symbolarray.Size();
sscanf(subitr->value["level"].GetString(), "%d", &newws->m_subinfo_depth_level);
if (strcmp(subitr->value["type"].GetString(), "limit") == 0)
{
newws->m_subinfo_depth_type = 1;
}
else if (strcmp(subitr->value["type"].GetString(), "full") == 0)
{
newws->m_subinfo_depth_type = 2;
}
for (int i = 0; i < symbolsize; i++)
{
string fullsymbolname = wsclassname + "_" + symbolarray[i].GetString();
newws->mf_add_subinfo(fullsymbolname.c_str(),1);
}
}
else if (channelname == "trade")
{
const Value& symbolarray = subitr->value["symbol"].GetArray();
size_t symbolsize = symbolarray.Size();
for (int i = 0; i < symbolsize; i++)
{
string fullsymbolname = wsclassname + "_" + symbolarray[i].GetString();
newws->mf_add_subinfo(fullsymbolname.c_str(), 2);
}
}
}
}
return 0;
}
int start_websocket_market()
{
for (int i = 0; i < gcp_vec_mywebsocktmarket.size(); i++)
{
gcp_vec_mywebsocktmarket[i]->open();
}
return gcp_vec_mywebsocktmarket.size();
}
int start_websocket_zmq()
{
for (auto itor = gcp_map_zmqpublish.begin(); itor != gcp_map_zmqpublish.end(); itor++)
{
itor->second->mf_Connect();
}
return gcp_map_zmqpublish.size();
}
int main()
{
long long nowts = gf_getlltime();
// 创建日志对象
gcp_mylogger = new MyLogger();//创建日志对象
thread mylogger = thread(&MyLogger::mf_Run_Write_Log, gcp_mylogger);
// 加载配置文件
load_config("./config/myconfig.json");
init_market();
start_websocket_market();
start_websocket_zmq();
while (true)
{
Sleep(100000);
}
}