forked from Melelery/c-binance-futures-quant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsServer.cpp
259 lines (207 loc) · 20.2 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
//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= "";
bool toClient(string MSG,websocketpp::connection_hdl hdl)
{
server.send(hdl,MSG, websocketpp::frame::opcode::text);
return true;
}
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;
}