forked from thejinchao/AxTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAT4_Message.cpp
658 lines (515 loc) · 19.2 KB
/
AT4_Message.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/***************************************************
AXIA|Trace4
(C) Copyright thecodeway.com 2019
***************************************************/
#include "stdafx.h"
#include "AT4_Message.h"
//--------------------------------------------------------------------------------------------
Message::Message(SessionPtr session, const axtrace_time_s& traceTime)
: m_session(session)
{
memcpy(&m_time, &traceTime, sizeof(axtrace_time_s));
}
//--------------------------------------------------------------------------------------------
Message::~Message()
{
}
//-------------------------------------------------------------------------------------
int Message::_lua_get_type(lua_State *L)
{
const Message* msg = (const Message*)lua_touserdata(L, 1);
lua_pushinteger(L, msg->getType());
return 1;
}
//-------------------------------------------------------------------------------------
int Message::_lua_get_process_id(lua_State *L)
{
const Message* msg = (const Message*)lua_touserdata(L, 1);
lua_pushinteger(L, msg->getSession()->getProcessID());
return 1;
}
//-------------------------------------------------------------------------------------
int Message::_lua_get_thread_id(lua_State *L)
{
const Message* msg = (const Message*)lua_touserdata(L, 1);
lua_pushinteger(L, msg->getSession()->getThreadID());
return 1;
}
//--------------------------------------------------------------------------------------------
QQueue<ShakehandMessage*> ShakehandMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
ShakehandMessage::ShakehandMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
{
}
//--------------------------------------------------------------------------------------------
ShakehandMessage::~ShakehandMessage()
{
}
//--------------------------------------------------------------------------------------------
bool ShakehandMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
axtrace_shakehand_s shakehand;
size_t len = ringBuf->peek(0, &shakehand, sizeof(axtrace_shakehand_s));
assert(len == sizeof(axtrace_shakehand_s));
//check version
if (shakehand.ver != AXTRACE_PROTO_VERSION) return false;
//check name length
if (shakehand.sname_len > AXTRACE_MAX_PROCESSNAME_LENGTH) return false;
//receive session name
char sessionName[AXTRACE_MAX_PROCESSNAME_LENGTH] = { 0 };
len = ringBuf->peek(sizeof(axtrace_shakehand_s), sessionName, shakehand.sname_len);
assert(len == shakehand.sname_len);
sessionName[shakehand.sname_len - 1] = 0; //make sure last char is '\0'
m_sessionName = QString::fromUtf8(sessionName);
m_version = shakehand.ver;
m_processID = shakehand.pid;
m_threadID = shakehand.tid;
//shakehand
if (!(m_session->onSessionShakehand(this))) return false;
//ok!
ringBuf->discard(shakehand.head.length);
return true;
}
//--------------------------------------------------------------------------------------------
QQueue<LogMessage*> LogMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
LogMessage::LogMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
{
}
//--------------------------------------------------------------------------------------------
LogMessage::~LogMessage()
{
}
//--------------------------------------------------------------------------------------------
bool LogMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
static QThreadStorage<QByteArray> memoryCache;
if (!(m_session->isHandshaked())) return false;
axtrace_log_s logHead;
size_t len = ringBuf->memcpy_out(&logHead, sizeof(axtrace_log_s));
assert(len == sizeof(axtrace_log_s));
m_logType = logHead.log_type;
int logLength = logHead.length;
QByteArray& cache = memoryCache.localData();
if (cache.size() < logLength + 2)
cache.resize(logLength + 2);
len = ringBuf->memcpy_out(cache.data(), logLength);
assert(len == logLength);
cache.data()[logLength] = 0;
cache.data()[logLength+1] = 0;
switch (logHead.code_page)
{
case ATC_UTF16:
m_log = QString::fromUtf16((const ushort*)cache.data());
break;
case ATC_UTF8:
m_log = QString::fromUtf8(cache);
break;
case ATC_ACP:
m_log = QString::fromLocal8Bit(cache);
break;
}
return true;
}
//-------------------------------------------------------------------------------------
int LogMessage::_lua_get_log_type(lua_State *L)
{
const LogMessage* msg = (const LogMessage*)lua_touserdata(L, 1);
lua_pushinteger(L, msg->getLogType());
return 1;
}
//-------------------------------------------------------------------------------------
int LogMessage::_lua_get_log(lua_State *L)
{
const LogMessage* msg = (const LogMessage*)lua_touserdata(L, 1);
QByteArray msgUtf8 = msg->getLog().toUtf8();
lua_pushstring(L, msgUtf8.data());
return 1;
}
//--------------------------------------------------------------------------------------------
const char* LogMessage::MetaName = "AxTrace.LogMessage";
void LogMessage::_luaopen(lua_State *L)
{
static luaL_Reg msg_data_meta[] =
{
{ "get_type", Message::_lua_get_type },
{ "get_pid", Message::_lua_get_process_id },
{ "get_tid", Message::_lua_get_thread_id },
{ "get_log_type", LogMessage::_lua_get_log_type },
{ "get_log", LogMessage::_lua_get_log },
{ 0, 0 }
};
//PlayerData meta table
luaL_newmetatable(L, LogMessage::MetaName);
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, msg_data_meta); /* file methods */
}
//--------------------------------------------------------------------------------------------
QQueue<ValueMessage*> ValueMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
ValueMessage::ValueMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
, m_valueBuf(nullptr)
{
}
//--------------------------------------------------------------------------------------------
ValueMessage::~ValueMessage()
{
if (m_valueBuf && m_valueBuf != m_standValueBuf)
{
delete[] m_valueBuf;
}
}
//--------------------------------------------------------------------------------------------
bool ValueMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
if (!(m_session->isHandshaked())) return false;
axtrace_value_s value_head;
size_t len = ringBuf->peek(0, &value_head, sizeof(axtrace_value_s));
assert(len == sizeof(value_head));
m_valueType = value_head.value_type;
m_valueSize = value_head.value_len;
//copy name
char tempName[AXTRACE_MAX_VALUENAME_LENGTH];
int name_length = value_head.name_len;
//check name length
if (name_length > AXTRACE_MAX_VALUENAME_LENGTH) return false;
//ok
ringBuf->discard(sizeof(axtrace_value_s));
len = ringBuf->memcpy_out(tempName, name_length);
assert(len == name_length);
tempName[name_length - 1] = 0; //make sure last char is '\0'
m_name = QString::fromUtf8(tempName);
//value
if (m_valueSize > STANDARD_VALUE_SIZE)
{
//big value
m_valueBuf = new char[m_valueSize];
memset(m_valueBuf, 0, m_valueSize);
}
else
{
m_valueBuf = m_standValueBuf;
}
//value
len = ringBuf->memcpy_out(m_valueBuf, m_valueSize);
assert(len == m_valueSize);
//make sure '\0' ended
if (m_valueType == AXV_STR_ACP || m_valueType == AXV_STR_UTF8)
{
((char*)m_valueBuf)[m_valueSize - 1] = 0;
}
else if (m_valueType == AXV_STR_UTF16)
{
((char*)m_valueBuf)[m_valueSize - 1] = 0;
((char*)m_valueBuf)[m_valueSize - 2] = 0;
}
else if (m_valueType == AXV_STR_UTF32)
{
((char*)m_valueBuf)[m_valueSize - 1] = 0;
((char*)m_valueBuf)[m_valueSize - 2] = 0;
((char*)m_valueBuf)[m_valueSize - 3] = 0;
((char*)m_valueBuf)[m_valueSize - 4] = 0;
}
return true;
}
//--------------------------------------------------------------------------------------------
void ValueMessage::getValueAsString(QString& value) const
{
const static QString ERROR_VALUE("<ERR>");
switch (m_valueType)
{
case AXV_INT8:
value = QString::number(*((int8_t*)m_valueBuf));
break;
case AXV_UINT8:
value = QString::number(*((uint8_t*)m_valueBuf));
break;
case AXV_INT16:
value = QString::number(*((int16_t*)m_valueBuf));
break;
case AXV_UINT16:
value = QString::number(*((uint16_t*)m_valueBuf));
break;
case AXV_INT32:
value = QString::number(*((int32_t*)m_valueBuf));
break;
case AXV_UINT32:
value = QString::number(*((uint32_t*)m_valueBuf));
break;
case AXV_INT64:
value = QString::number(*((int64_t*)m_valueBuf));
break;
case AXV_UINT64:
value = QString::number(*((uint64_t*)m_valueBuf));
break;
case AXV_FLOAT32:
value = QString::number(*((float*)m_valueBuf));
break;
case AXV_FLOAT64:
value = QString::number(*((double*)m_valueBuf));
break;
case AXV_STR_UTF32:
value = QString::fromUcs4((const uint*)m_valueBuf);
break;
case AXV_STR_UTF16:
value = QString::fromUtf16((const ushort*)m_valueBuf);
break;
case AXV_STR_UTF8:
value = QString::fromUtf8((const char*)m_valueBuf);
break;
case AXV_STR_ACP:
value = QString::fromLocal8Bit((const char*)m_valueBuf);
break;
default:
value = ERROR_VALUE;
break;
}
}
//-------------------------------------------------------------------------------------
int ValueMessage::_lua_get_value(lua_State *L)
{
const ValueMessage* msg = (const ValueMessage*)lua_touserdata(L, 1);
QString value_as_string;
msg->getValueAsString(value_as_string);
QByteArray msgUtf8 = value_as_string.toUtf8();
lua_pushstring(L, msgUtf8.data());
return 1;
}
//--------------------------------------------------------------------------------------------
const char* ValueMessage::MetaName = "AxTrace.ValueMessage";
void ValueMessage::_luaopen(lua_State *L)
{
static luaL_Reg msg_data_meta[] =
{
{ "get_type", Message::_lua_get_type },
{ "get_pid", Message::_lua_get_process_id },
{ "get_tid", Message::_lua_get_thread_id },
{ "get_value", ValueMessage::_lua_get_value },
{ 0, 0 }
};
//PlayerData meta table
luaL_newmetatable(L, ValueMessage::MetaName);
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, msg_data_meta); /* file methods */
}
//--------------------------------------------------------------------------------------------
QQueue<Begin2DSceneMessage*> Begin2DSceneMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
Begin2DSceneMessage::Begin2DSceneMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
{
}
//--------------------------------------------------------------------------------------------
Begin2DSceneMessage::~Begin2DSceneMessage()
{
}
//--------------------------------------------------------------------------------------------
bool Begin2DSceneMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
if (!(m_session->isHandshaked())) return false;
axtrace_2d_begin_scene_s value_head;
size_t len = ringBuf->memcpy_out(&value_head, sizeof(value_head));
assert(len == sizeof(value_head));
m_sceneRect = QRectF(value_head.left, value_head.top, value_head.right-value_head.left, value_head.bottom-value_head.top);
//copy name
char tempBuf[AXTRACE_MAX_SCENE_DEFINE_LENGTH] = { 0 };
int name_length = value_head.name_len;
if (name_length > AXTRACE_MAX_SCENE_NAME_LENGTH) name_length = AXTRACE_MAX_SCENE_NAME_LENGTH;
if (name_length > 0) {
len = ringBuf->memcpy_out(tempBuf, name_length);
assert(len == name_length);
tempBuf[name_length - 1] = 0; //make sure last char is '\0'
m_sceneName = QString::fromUtf8(tempBuf);
}
//copy define
int define_length = value_head.define_len;
if (define_length > AXTRACE_MAX_SCENE_DEFINE_LENGTH) define_length = AXTRACE_MAX_SCENE_DEFINE_LENGTH;
if (define_length > 0) {
len = ringBuf->memcpy_out(tempBuf, define_length);
assert(len == define_length);
tempBuf[define_length - 1] = 0;
//make json object
QJsonParseError jerror;
QJsonDocument jsonDocument = QJsonDocument::fromJson(tempBuf, &jerror);
if (jerror.error == QJsonParseError::NoError)
{
m_sceneDefine = jsonDocument.object();
}
}
return true;
}
//--------------------------------------------------------------------------------------------
const char* Begin2DSceneMessage::MetaName = "AxTrace.Begin2DScene";
void Begin2DSceneMessage::_luaopen(lua_State *L)
{
static luaL_Reg msg_data_meta[] =
{
{ "get_type", Message::_lua_get_type },
{ "get_pid", Message::_lua_get_process_id },
{ "get_tid", Message::_lua_get_thread_id },
{ 0, 0 }
};
//PlayerData meta table
luaL_newmetatable(L, Begin2DSceneMessage::MetaName);
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, msg_data_meta); /* file methods */
}
//--------------------------------------------------------------------------------------------
QQueue<Update2DActorMessage*> Update2DActorMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
Update2DActorMessage::Update2DActorMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
{
}
//--------------------------------------------------------------------------------------------
Update2DActorMessage::~Update2DActorMessage()
{
}
//--------------------------------------------------------------------------------------------
bool Update2DActorMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
if (!(m_session->isHandshaked())) return false;
axtrace_2d_actor_s value_head;
size_t len = ringBuf->memcpy_out(&value_head, sizeof(value_head));
assert(len == sizeof(value_head));
m_actorID = (qint64)value_head.actor_id;
m_position = QPointF((qreal)value_head.x, (qreal)value_head.y);
m_dir = (qreal)value_head.dir;
m_style = (quint32)value_head.style;
//copy name
char tempName[AXTRACE_MAX_SCENE_NAME_LENGTH] = { 0 };
int name_length = value_head.name_len;
if (name_length > AXTRACE_MAX_SCENE_NAME_LENGTH) name_length = AXTRACE_MAX_SCENE_NAME_LENGTH;
if (name_length > 0) {
len = ringBuf->memcpy_out(tempName, name_length);
assert(len == name_length);
tempName[name_length - 1] = 0; //make sure last char is '\0'
m_sceneName = QString::fromUtf8(tempName);
}
//copy info
char tempInfo[AXTRACE_MAX_ACTOR_INFO_LENGTH] = { 0 };
int info_length = value_head.info_len;
if (info_length > AXTRACE_MAX_ACTOR_INFO_LENGTH) info_length = AXTRACE_MAX_ACTOR_INFO_LENGTH;
if (info_length > 0) {
len = ringBuf->memcpy_out(tempInfo, info_length);
assert(len == info_length);
tempInfo[info_length - 1] = 0; //make sure last char is '\0'
m_actorInfo = QString::fromUtf8(tempInfo);
}
return true;
}
//-------------------------------------------------------------------------------------
int Update2DActorMessage::_lua_get_actor_id(lua_State *L)
{
const Update2DActorMessage* msg = (const Update2DActorMessage*)lua_touserdata(L, 1);
QString id = QString("%1").arg(msg->getActorID());
lua_pushstring(L, id.toUtf8().toStdString().c_str());
return 1;
}
//-------------------------------------------------------------------------------------
int Update2DActorMessage::_lua_get_actor_position(lua_State *L)
{
const Update2DActorMessage* msg = (const Update2DActorMessage*)lua_touserdata(L, 1);
const QPointF& pos = msg->getActorPosition();
lua_pushnumber(L, pos.x());
lua_pushnumber(L, pos.y());
return 2;
}
//-------------------------------------------------------------------------------------
int Update2DActorMessage::_lua_get_actor_dir(lua_State *L)
{
const Update2DActorMessage* msg = (const Update2DActorMessage*)lua_touserdata(L, 1);
lua_pushnumber(L, msg->getActorDir());
return 1;
}
//-------------------------------------------------------------------------------------
int Update2DActorMessage::_lua_get_actor_style(lua_State *L)
{
const Update2DActorMessage* msg = (const Update2DActorMessage*)lua_touserdata(L, 1);
lua_pushinteger(L, msg->getActorStyle());
return 1;
}
//-------------------------------------------------------------------------------------
int Update2DActorMessage::_lua_get_actor_info(lua_State *L)
{
const Update2DActorMessage* msg = (const Update2DActorMessage*)lua_touserdata(L, 1);
QString actorInfo = msg->getActorInfo();
QByteArray msgUtf8 = actorInfo.toUtf8();
lua_pushstring(L, msgUtf8.data());
return 1;
}
//--------------------------------------------------------------------------------------------
const char* Update2DActorMessage::MetaName = "AxTrace.Actor2DMessage";
void Update2DActorMessage::_luaopen(lua_State *L)
{
static luaL_Reg msg_data_meta[] =
{
{ "get_type", Message::_lua_get_type },
{ "get_pid", Message::_lua_get_process_id },
{ "get_tid", Message::_lua_get_thread_id },
{ "get_actor_id", Update2DActorMessage::_lua_get_actor_id },
{ "get_actor_position", Update2DActorMessage::_lua_get_actor_position },
{ "get_actor_dir", Update2DActorMessage::_lua_get_actor_dir },
{ "get_actor_style", Update2DActorMessage::_lua_get_actor_style },
{ "get_actor_info", Update2DActorMessage::_lua_get_actor_info },
{ 0, 0 }
};
//PlayerData meta table
luaL_newmetatable(L, Update2DActorMessage::MetaName);
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, msg_data_meta); /* file methods */
}
//--------------------------------------------------------------------------------------------
QQueue<End2DSceneMessage*> End2DSceneMessage::s_messagePool;
//--------------------------------------------------------------------------------------------
End2DSceneMessage::End2DSceneMessage(SessionPtr session, const axtrace_time_s& traceTime)
: Message(session, traceTime)
{
}
//--------------------------------------------------------------------------------------------
End2DSceneMessage::~End2DSceneMessage()
{
}
//--------------------------------------------------------------------------------------------
bool End2DSceneMessage::build(const axtrace_head_s& head, cyclone::RingBuf* ringBuf)
{
if (!(m_session->isHandshaked())) return false;
axtrace_2d_end_scene_s value_head;
size_t len = ringBuf->memcpy_out(&value_head, sizeof(value_head));
assert(len == sizeof(value_head));
//copy name
char tempName[AXTRACE_MAX_SCENE_NAME_LENGTH] = { 0 };
int name_length = value_head.name_len;
if (name_length > AXTRACE_MAX_SCENE_NAME_LENGTH) name_length = AXTRACE_MAX_SCENE_NAME_LENGTH;
if (name_length > 0) {
len = ringBuf->memcpy_out(tempName, name_length);
assert(len == name_length);
tempName[name_length - 1] = 0; //make sure last char is '\0'
m_sceneName = QString::fromUtf8(tempName);
}
return true;
}
//--------------------------------------------------------------------------------------------
const char* End2DSceneMessage::MetaName = "AxTrace.End2DScene";
void End2DSceneMessage::_luaopen(lua_State *L)
{
static luaL_Reg msg_data_meta[] =
{
{ "get_type", Message::_lua_get_type },
{ "get_pid", Message::_lua_get_process_id },
{ "get_tid", Message::_lua_get_thread_id },
{ 0, 0 }
};
//PlayerData meta table
luaL_newmetatable(L, End2DSceneMessage::MetaName);
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, msg_data_meta); /* file methods */
}