-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocketstreamtask.cpp
237 lines (171 loc) · 9.03 KB
/
websocketstreamtask.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
// std
#include "rapidjson/document.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/istreamwrapper.h"
// project
#include "websocketstreamtask.h"
#include "utils.h"
using namespace std;
using namespace rapidjson;
extern void g_logger( const string _msg );
itemFromNewitems_t g_itemFromNewitems;
WebsocketStreamTask::WebsocketStreamTask( const int _responsesCount, vector<Item_t *> & _items ) :
m_done(false),
m_profileItems(_items) {
m_responesFromWebsocket.resize( _responsesCount );
}
WebsocketStreamTask::~WebsocketStreamTask(){
}
void WebsocketStreamTask::clear(){
m_done.store(false);
m_responesFromWebsocket.clear();
}
bool WebsocketStreamTask::process(){
for( string row : m_responesFromWebsocket ){
if( "pong" == row ){
g_logger( "pong received" );
continue;
}
Document doc;
if( doc.ParseInsitu<0>( & row.front() ).HasParseError() ){
g_logger( "ERROR: json parse error, message (note: modifyed by RapidJSON): " + row );
g_logger( "---------------------------------------------------" );
return false;
}
for( Value::ConstMemberIterator iter = doc.MemberBegin(); iter != doc.MemberEnd(); ++iter ){
if( ! strcmp("type", iter->name.GetString()) && ! strcmp("newitems_cs", iter->value.GetString()) ){
// -------------------------------------------
// NEWITEMS JSON Record
// -------------------------------------------
// {
// "type":"newitems_cs",
// "data":"{
// \"i_quality\":\"Genuine\",
// \"i_name_color\":\"4D7455\",
// \"i_classid\":\"519295759\",
// \"i_instanceid\":\"93973071\",
// \"i_market_hash_name\":\"Genuine Weather Rain\",
// \"i_market_name\":\"Genuine Weather Rain\",
// \"ui_price\":23.35,
// \"app\":\"cs\"
// }"
// }
++iter;
const Value & dataVal = doc[ iter->name.GetString() ];
Document dataDoc;
if( dataDoc.Parse<0>( dataVal.GetString() ).HasParseError() ){
g_logger( string("ERROR: json parse error, message (note: modifyed by RapidJSON): ") + dataVal.GetString() );
}
for( Value::ConstMemberIterator iter = dataDoc.MemberBegin(); iter != dataDoc.MemberEnd(); ++iter ){
const Value & val = dataDoc[ iter->name.GetString() ];
if( val.IsString() ){
if( ! strcmp(iter->name.GetString(), "i_market_hash_name") ){
g_itemFromNewitems.i_marketHashName = val.GetString();
}
else if( ! strcmp(iter->name.GetString(), "i_market_name") ){
g_itemFromNewitems.i_marketName = val.GetString();
}
else if( ! strcmp(iter->name.GetString(), "i_classid") ){
g_itemFromNewitems.i_classId = atoll( val.GetString() );
}
else if( ! strcmp(iter->name.GetString(), "i_instanceid") ){
g_itemFromNewitems.i_instanceId = atoll( val.GetString() );
}
}
else if( val.IsInt() ){
}
else if( val.IsFloat() ){
if( ! strcmp(iter->name.GetString(), "ui_price") ){
g_itemFromNewitems.ui_price = val.GetFloat();
}
}
else{
g_logger( string("WARN: unknown value type of key: ") + iter->name.GetString() );
}
}
for( Item_t * item : m_profileItems ){
if( ItemProcessType_en::BUY_WEBSOCKET == item->processType ){
if( item->classId == g_itemFromNewitems.i_classId &&
item->instanceId == g_itemFromNewitems.i_instanceId ){
if( item->maxPriceForBuy <= g_itemFromNewitems.ui_price ){
g_logger( "WEBSOCKET: found item (classID " + to_string( g_itemFromNewitems.i_classId ) +
", instanceID " + to_string( g_itemFromNewitems.i_instanceId ) +
", our max price " + to_string( item->maxPriceForBuy ) +
", founded item price " + to_string( g_itemFromNewitems.ui_price ) +
". TRY TO BUY");
item->catchedViaWebsocket = true;
}
}
}
}
// cout << "------------------------------------------------------------------------" << endl;
// cout << row << endl;
// cout << "------------------------------------------------------------------------" << endl;
// cout << "i_market_hash_name: " << g_itemFromNewitems.i_marketHashName << endl;
// cout << "i_market_name: " << g_itemFromNewitems.i_marketName << endl;
// cout << "i_classid: " << g_itemFromNewitems.i_classId << endl;
// cout << "i_instanceid: " << g_itemFromNewitems.i_instanceId << endl;
// cout << "ui_price: " << g_itemFromNewitems.ui_price << endl;
// cout << endl;
// TODO check item for itemProcessType_t::BUY_WEBSOCKET
// TODO check lower + upper bounds
}
else if( ! strcmp("type", iter->name.GetString()) && ! strcmp("webnotify", iter->value.GetString()) ){
++iter;
const Value & dataVal = doc[ iter->name.GetString() ];
// TODO какая та хуита. почему то не убираются escape-последовательности
string unescaped;
unescape( dataVal.GetString(), dataVal.GetStringLength(), unescaped );
unescaped.erase( unescaped.find_first_of("\""), 1 ); // TODO неэффективно
unescaped.erase( unescaped.find_last_of("\""), 1 );
Document cyrillicStrDoc;
if( cyrillicStrDoc.Parse<0>( & unescaped.front() ).HasParseError() ){
g_logger( "ERROR: json parse error, message (note: modifyed by RapidJSON): " + unescaped );
}
for( Value::ConstMemberIterator iter = cyrillicStrDoc.MemberBegin(); iter != cyrillicStrDoc.MemberEnd(); ++iter ){
const Value & a = cyrillicStrDoc[ iter->name.GetString() ];
if( a.IsString() ){
g_logger( string(iter->name.GetString()) + " : " + a.GetString() );
}
else if( a.IsInt() ){
g_logger( string(iter->name.GetString()) + " : " + to_string(a.GetInt()) );
}
else{
g_logger( string("ERROR: unknown value type of key: ") + iter->name.GetString() );
}
}
}
else if( ! strcmp("type", iter->name.GetString()) && ! strcmp("history_cs", iter->value.GetString()) ){
++iter;
const Value & dataVal = doc[ iter->name.GetString() ];
// TODO какая та хуита. почему то не убираются escape-последовательности
string unescaped;
unescape( dataVal.GetString(), dataVal.GetStringLength(), unescaped );
unescaped.erase( unescaped.find_first_of("\""), 1 ); // TODO неэффективно
unescaped.erase( unescaped.find_last_of("\""), 1 );
Document dataDoc;
if( dataDoc.Parse<0>( & unescaped.front() ).HasParseError() ){
g_logger( "ERROR: json parse error, message (note: modifyed by RapidJSON): " + unescaped );
}
for( auto & member : dataDoc.GetArray() ){
if( member.IsString() ){
// cout << member.GetString() << endl;
}
else if( member.IsInt() ){
// cout << iter->name.GetInt() << endl;
}
else{
g_logger( string("WARN: unknown value type of key: ") + iter->name.GetString() );
}
}
}
else{
g_logger( string("WARN: type of websocket is not \"webnotify\": ") + iter->name.GetString() + row );
}
}
}
m_done.store( true );
return true;
}