forked from Melelery/c-binance-futures-quant
/
wsServer.cpp
381 lines (318 loc) · 23.1 KB
/
wsServer.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//commonGame
#include<sstream>
#include<fstream> // 标准库文件IO部分的头文件
#include <iostream>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include<stdlib.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
//websocket
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
typedef websocketpp::server<websocketpp::config::asio> WebsocketServer;
typedef websocketpp::connection<websocketpp::config::asio> connection;
typedef WebsocketServer::message_ptr message_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
WebsocketServer server;
//max
boost::random::mt19937 gen;
string ONE_HOUR_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string FOUR_HOURS_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string ONE_DAY_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string ONE_WEEK_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string ONE_MONTH_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string FIFTEEN_MINS_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string ONE_MIN_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string TWO_ONE_MIN_KLINE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string TWO_ONE_MIN_KLINE_STR = "";
string ONE_DAY_POLE_STR_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string VOL_ARR[400]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
string TICK_STR = "";
int SYMBOL_COUNT = 0;
int ONE_MIN_INDEX = 0;
int SPECIAL_ONE_MIN_INDEX = 0;
int FIFTEEN_MINS_INDEX = 0;
int SPECIAL_FIFTEEN_MINS_INDEX = 0;
long TICK_UPDATE_TS = 0;
string POSITION_STR= "";
string BAN_SYMBOL_STR= "";
string ACCOUNT_BALANCE_STR= "";
long POSITION_UPDATE_TS = 0;
long ACCOUNT_BALANCE_UPDATE_TS = 0;
string GROUP_ONE_MIN_KLINE= "";
string GROUP_FIFTEEN_MINS_KLINE= "";
long TS_nowTime=0;//目前的时间戳
int randomDirection;//special food random direction
struct timeval t_start,t_end;
long LAST_SEND_TO_SUB_SERVER = 0;
bool mustSend=false;
int scanNumber=0;
int uniqueID=0;//every account have a single number ,it seems like a key ,user and sub server can use it to main server to search the corresponding uniqueIDVector vector
int sendToSubServerNumber=0;// the number of the subServer that connect to the mainSever
int nowExistSubServerNumber=0;
int nowPreparePlayerNumber;
int insufficientVector_BeginNumber=0;
vector<bool> registerPassword;
string str_thisHdl="";
/*********************************************************************************************************
**********************************************************************************************************
matching server
**********************************************************************************************************
*********************************************************************************************************/
bool isDigit(string str)
{
for(int i=0;i<str.size();i++)
{
if ((str.at(i)>'9') || (str.at(i)<'0') )
{
return false;
}
}
return true;
}
const string zero[10]={"","0","00","000","0000","00000","000000","0000000","00000000","000000000"};
string supplementZero(string originalPara,int allDigitNumber)
{
//outfile << "----------------------------void supplementZero------------------------------" <<"\r\n";
int zeroNum=allDigitNumber-originalPara.length();
if(zeroNum>0)
{
originalPara=zero[zeroNum]+originalPara;
}
else if(zeroNum<0)
{
string errorStr="";
for(int a=0;a<allDigitNumber;a++)
{
errorStr+="X";
}
originalPara=errorStr;
}
return originalPara;
}
template <class T> string beString(T originalPara)
{
stringstream oss;
oss<<originalPara;
string generateString(oss.str());
oss.str();
return generateString;
}
struct subServer
{
subServer()
{
statu=0;
}
char statu;// 0 re invaild ,1 re exist
websocketpp::connection_hdl hdl;
string str_hdl;
};
vector<subServer> subServerVector;
int random(int mixNum,int maxNum)
{
if(maxNum-mixNum>0)
{
return TS_nowTime%(maxNum-mixNum+1)+mixNum;
}
else
{
return abs((mixNum+maxNum)/2);
}
}
bool verifySubServer()
{
if(subServerVector[0].str_hdl==str_thisHdl)
{
return true;
}
else
{
return false;
}
}
//send message to sub server
void toSubServer(string MSG)
{
//cout<<"sendToSubServerNumber:"<<MSG<<endl;
try
{
server.send(subServerVector[0].hdl,MSG, websocketpp::frame::opcode::text);
}
catch(...)
{
}
}
bool toClient(string MSG,websocketpp::connection_hdl hdl)
{
//cout<<"MSG:"<<MSG<<endl;
// try
// {
server.send(hdl,MSG, websocketpp::frame::opcode::text);
return true;
// }
// catch(...)
// {
// //cout<<"8"<<endl;
// return false;
// }
}
void closeHdl(websocketpp::connection_hdl hdl)
{
try
{
server.pause_reading(hdl); // <-- THIS LINE FIXES THE ERROR
server.close(hdl, websocketpp::close::status::going_away, "");
}
catch(...)
{
}
}
bool validate(WebsocketServer *server, websocketpp::connection_hdl hdl)
{
return true;
}
/*********************************************************************************************************
**********************************************************************************************************
the operation when the websocket++ establish
**********************************************************************************************************
*********************************************************************************************************/
void OnOpen(WebsocketServer *server, websocketpp::connection_hdl hdl)
{
//outfile << "----------------------------void OnOpen------------------------------" <<"\r\n";
/*
*/
// uint8_t buffer[4] = {1,1,1,1};
// str_thisHdl=beString(server->get_con_from_hdl(hdl));
// WebsocketServer::connection_ptr con=server->get_con_from_hdl(hdl);
// websocketpp::http::parser::request rt = con->get_request();
// const string& strUri = rt.get_uri();
}
/*********************************************************************************************************
**********************************************************************************************************
the operation when the websocket++ close
**********************************************************************************************************
*********************************************************************************************************/
void OnClose(WebsocketServer *server, websocketpp::connection_hdl hdl)
{
//outfile << "----------------------------void OnClose------------------------------" <<"\r\n";
// string str_thisHdl=beString(server->get_con_from_hdl(hdl));
}
/*********************************************************************************************************
**********************************************************************************************************
the operation when client send the message
**********************************************************************************************************
*********************************************************************************************************/
void OnMessage(WebsocketServer* s,websocketpp::connection_hdl hdl, message_ptr msg)
{
string clientToServerMsg = msg->get_payload();
string firstMSG = clientToServerMsg.substr(0,1);
if (firstMSG == "B"){
string MSG_TO_CLIENT = TWO_ONE_MIN_KLINE_STR+"*"+TICK_STR+"*"+POSITION_STR+"*"+BAN_SYMBOL_STR+"*"+ACCOUNT_BALANCE_STR;
toClient(MSG_TO_CLIENT,hdl);
}
else if (firstMSG == "A"){
string sendStr = ONE_MIN_KLINE_STR_ARR[0];
for(int a=1;a<SYMBOL_COUNT;a++)
{
sendStr = sendStr+"@"+ONE_MIN_KLINE_STR_ARR[a];
}
toClient(sendStr,hdl);
}
else if (firstMSG == "E"){
string MSG_TO_CLIENT = "{\"s\":\"y\",\"d\":\""+POSITION_STR+"\",\"i\":\""+firstMSG+"\"}";
toClient(MSG_TO_CLIENT,hdl);
}
else if (firstMSG == "F"){
ONE_MIN_INDEX = ONE_MIN_INDEX+1;
if(ONE_MIN_INDEX>=SYMBOL_COUNT){
ONE_MIN_INDEX = 0;
}
string MSG_TO_CLIENT = std::to_string(ONE_MIN_INDEX);
toClient(MSG_TO_CLIENT,hdl);
}
else if (firstMSG == "G"){
SPECIAL_ONE_MIN_INDEX = SPECIAL_ONE_MIN_INDEX+1;
if(SPECIAL_ONE_MIN_INDEX>=SYMBOL_COUNT){
SPECIAL_ONE_MIN_INDEX = 0;
}
string MSG_TO_CLIENT = std::to_string(SPECIAL_ONE_MIN_INDEX);
toClient(MSG_TO_CLIENT,hdl);
}
else if(clientToServerMsg.size()>=19)
{
string mark = clientToServerMsg.substr(0,16);
string valueInfo = clientToServerMsg.substr(16);
if(mark == "sjaiyhsaoyosauio"){
int thisIndex = atoi(valueInfo.substr(0,3).c_str());
ONE_MIN_KLINE_STR_ARR[thisIndex] = valueInfo.substr(3);
}else if(mark == "sajoiyfpdufiyiry"){
int thisIndex = atoi(valueInfo.substr(0,3).c_str());
TWO_ONE_MIN_KLINE_STR_ARR[thisIndex] = valueInfo.substr(3);
TWO_ONE_MIN_KLINE_STR = TWO_ONE_MIN_KLINE_STR_ARR[0];
for(int a=1;a<SYMBOL_COUNT;a++)
{
TWO_ONE_MIN_KLINE_STR = TWO_ONE_MIN_KLINE_STR+"@"+TWO_ONE_MIN_KLINE_STR_ARR[a];
}
}else if(mark == "sjaoihsoaitowljd"){
long newTickUpdateTs = atol(valueInfo.substr(0,13).c_str());
if(newTickUpdateTs>=TICK_UPDATE_TS){
TICK_UPDATE_TS = newTickUpdateTs;
TICK_STR = valueInfo.substr(13);
}
}else if(mark == "gggoihsoaitowljd"){
long newUpdateTs = atol(valueInfo.substr(0,13).c_str());
if(newUpdateTs>=POSITION_UPDATE_TS){
POSITION_UPDATE_TS = newUpdateTs;
POSITION_STR = valueInfo.substr(13);
}
}else if(mark == "abcoihsoaitowljd"){
BAN_SYMBOL_STR = valueInfo;
}else if(mark == "fdsoihsoaitowljd"){
long newUpdateTs = atol(valueInfo.substr(0,13).c_str());
cout<<newUpdateTs<<endl;
cout<<valueInfo<<endl;
if(newUpdateTs>=ACCOUNT_BALANCE_UPDATE_TS){
ACCOUNT_BALANCE_UPDATE_TS = newUpdateTs;
ACCOUNT_BALANCE_STR = valueInfo.substr(13);
}
}else if(mark == "bbboiyfpdufiyuyu"){
SYMBOL_COUNT = atoi(valueInfo.substr(0,3).c_str());
}
}
}
int main()
{
// Set logging settings
server.set_access_channels(websocketpp::log::alevel::connect);
server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
server.init_asio();
server.set_validate_handler(bind(&validate, &server,_1));
// Register our open handler
server.set_open_handler(bind(&OnOpen, &server,_1));
// Register our close handler
server.set_close_handler(bind(&OnClose, &server, _1));
// Register our message handler
// try
// {
server.set_message_handler(bind(&OnMessage,&server, _1,_2));
// }
// catch(...)
// {
// }
//Listen on port 2152
server.listen(3698);
//Start the server accept loop
server.start_accept();
//Start the ASIO io_service run loop
server.run();
return 0;
}