-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.cpp
More file actions
719 lines (688 loc) · 22.7 KB
/
sender.cpp
File metadata and controls
719 lines (688 loc) · 22.7 KB
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
594
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
* sender.cpp - Sof Game Psi plugin
* Copyright (C) 2010 Aleksey Andreev
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* You can also redistribute and/or modify this program under the
* terms of the Psi License, specified in the accompanied COPYING
* file, as published by the Psi Project; either dated January 1st,
* 2005, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QtCore>
#include "sender.h"
#include "common.h"
QString alfa = "0123456789abcdefghijklmnopqrstuvwxyz";
StanzaSendingHost* sender_;
QStringList chatJids;
QList<short int> aPrefix = (QList<short int>()
<< 22 << 13 << 5 << 16 << 29 << 4 << 5 << 34 << 27 << 25
<< 4 << 5 << 16 << 27 << 29 << 1 << 9 << 17 << 2 << 3
<< 17 << 13 << 15 << 27 << 2 << 2 << 3 << 4 << 2 << 20
<< 19) ;
Sender *Sender::instanse_ = NULL;
Sender *Sender::instance()
{
if (Sender::instanse_ == NULL) {
Sender::instanse_ = new Sender();
}
return Sender::instanse_;
}
void Sender::reset()
{
if (Sender::instanse_ != NULL) {
delete Sender::instanse_;
Sender::instanse_ = NULL;
}
}
Sender::Sender() :
currentAccount(-1),
pingMirrors(false),
jidInterval(600), // Частота отправки запросов, msec.
waitForReceivePeriod(20000) // Ожидание ответа от игры в течении 20 сек.
{
prefix = "";
foreach (short int idx, aPrefix) {
prefix.append(alfa.at(idx));
}
fastSendReg.setPattern(QString::fromUtf8("Можно отсылать не чаще .+ команд.+0- обновить."));
gameSenderTimer.setSingleShot(true);
connect(&gameSenderTimer, SIGNAL(timeout()), this, SLOT(doSendGameStringJob()));
}
/**
* Деструктор
*/
Sender::~Sender()
{
disconnect(&gameSenderTimer, SIGNAL(timeout()), this, SLOT(doSendGameStringJob()));
}
void Sender::changeAccount(int accIndex, const QString &accJid)
{
currentAccount = accIndex;
if (currentAccJid != accJid) {
jidActiveCount = 0;
currGameJidIndex = -1;
lastSend = QDateTime::currentDateTime();
gameMirrorsMode = 0;
gameJidsEx.clear();
pingQueue.clear();
gameQueue.clear();
systemQueue.clear();
lastSendIndex = -1; // С какого jid-а последний раз отправляли
waitingForReceive = false;
sendCommandRetries = 0; // Количество попыток отправки команды
lastCommand = "";
}
if (currentAccJid != accJid) {
// Уведомляем другие модули
currentAccJid = accJid;
emit accountChanged(accJid);
}
}
void Sender::setAccountStatus(int curr_status)
{
// *** Смена статуса игрового аккаунта ***
// Если аккаунт отключен, то устанавливаем игровым jid-ам статус отключенных
if (curr_status == 0) {
int cnt = gameJidsEx.size();
for (int i = 0; i < cnt; i++) {
setGameJidStatus(i, 0);
}
}
}
/**
* Вставка нового джида jid на позицию pos
* Если pos == -1, то добавляем в конец
*/
void Sender::insertGameJid(const QString &jid, int pos)
{
if (jid.isEmpty())
return;
// Проверяем наличие такого зеркала
if (getGameJidIndex(jid) != -1)
return;
// Вставляем запись
struct jid_status jst;
jst.jid = jid;
jst.status = 0;
jst.last_status = QDateTime();
jst.last_send = QDateTime();
jst.last_recv_game = QDateTime();
jst.last_send_ping = QDateTime();
jst.last_recv_ping = QDateTime();
jst.probe_count = 0;
jst.resp_average = 0.0f;
if (pos < 0 || pos >= gameJidsEx.size()) {
gameJidsEx.push_back(jst);
} else {
gameJidsEx.insert(pos, jst);
}
}
/**
* Удаление джида зеркала из списка зеркал
*/
void Sender::removeGameJid(const QString &jid)
{
int i = getGameJidIndex(jid);
if (i != -1) {
gameJidsEx.remove(i);
if (lastSendIndex == i)
lastSendIndex = -1;
}
}
int Sender::getGameJidIndex(const QString &jid) const
{
int cnt = gameJidsEx.size();
for (int i = 0; i < cnt; i++) {
if (gameJidsEx.at(i).jid == jid)
return i;
}
return -1;
}
QStringList Sender::getGameJids() const
{
QStringList res;
int cnt = gameJidsEx.size();
for (int i = 0; i < cnt; i++) {
res.push_back(gameJidsEx.at(i).jid);
}
return res;
}
const struct Sender::jid_status* Sender::getGameJidInfo(int jid_index) const
{
if (jid_index < 0 || jid_index >= gameJidsEx.size())
return 0;
return &gameJidsEx.at(jid_index);
}
bool Sender::setGameJidStatus(int jid_index, qint32 status)
{
if (jid_index < 0 || jid_index >= gameJidsEx.size())
return false;
bool res = false;
int oldJidActiveCount = jidActiveCount;
struct jid_status* jstat = &gameJidsEx[jid_index];
if (status == 0) {
if (jstat->status != 0) {
jstat->probe_count = 0;
jstat->status = 0;
jstat->last_status = QDateTime::currentDateTime();
jidActiveCount--;
res = true;
// Отменяем пинг отключенного зеркала
clearPingQueue(jid_index);
}
} else {
if (jstat->status == 0) {
jstat->status = status;
jstat->last_status = QDateTime::currentDateTime();
jidActiveCount++;
res = true;
}
}
if (oldJidActiveCount == 0 && jidActiveCount != 0) {
doPingJob();
}
return res;
}
/**
* Устанавливает режим использования зеркал игры
*/
bool Sender::setGameMirrorsMode(int mirrorsMode)
{
if (gameMirrorsMode != mirrorsMode) {
gameMirrorsMode = mirrorsMode;
if (mirrorsMode == 1) {
startPingMirrors();
} else {
stopPingMirrors();
}
}
return true;
}
bool Sender::setSendDelta(int delta)
{
/**
* Установка паузы между отправками пакетов серверу игры
**/
if (delta >= 0) {
jidInterval = delta;
return true;
}
return false;
}
/**
* Возращает значение паузы между отправками пакетов серверу игры
*/
int Sender::getSendDelta() const
{
return jidInterval;
}
/**
* Устанавливает таймаут ожидания ответа от сервера, в секундах
*/
bool Sender::setServerTimeoutDuration(int duration)
{
if (duration > 5 && duration <= 120) {
waitForReceivePeriod = duration * 1000;
return true;
}
return false;
}
/**
* Возвращает длительность ожидания ответа от сервера, в секундах
*/
int Sender::getServerTimeoutDuration() const
{
return (waitForReceivePeriod / 1000);
}
/**
* Обработка ответа игры
* Возвращает true если пакет полностью обработан функцией
*/
bool Sender::doGameAsk(const QString &mirrorJid, const QString &message)
{
int jidIndex = getGameJidIndex(mirrorJid);
if (jidIndex != -1) {
const struct jid_status* jstat = getGameJidInfo(jidIndex);
if (jstat != 0) {
// Отмечаем общее время приема пакета
QDateTime lastReceive = QDateTime::currentDateTime();
if (message.trimmed() == "000") {
// Пришел пакет в результате пинга зеркала
if (jstat->last_send_ping.isValid() && (!jstat->last_recv_ping.isValid() || jstat->last_send_ping > jstat->last_recv_ping)) { // TODO Надо продумать, что делать при значительном запоздании пакета
// Похоже что ping послан нами
// Отмечаем время получения пакета
gameJidsEx[jidIndex].last_recv_ping = lastReceive;
// Обсчитываем среднее
int elapsed = gameJidsEx[jidIndex].last_send_ping.time().elapsed();
int probe_cnt = gameJidsEx[jidIndex].probe_count;
if (probe_cnt > 0) {
float average_temp = gameJidsEx[jidIndex].resp_average;
if (probe_cnt >= 10) {
gameJidsEx[jidIndex].resp_average = (average_temp * 2.0f + elapsed) / 3.0f;
probe_cnt = 3;
} else {
gameJidsEx[jidIndex].resp_average = (average_temp * (float)probe_cnt + elapsed) / ((float)probe_cnt + 1.0f);
++probe_cnt;
}
} else {
gameJidsEx[jidIndex].resp_average = elapsed;
probe_cnt = 1;
}
gameJidsEx[jidIndex].probe_count = probe_cnt;
//return true; // Это наш пинг и мы его обработали
}
return true; // блокируем вывод "000" в любом случае
} else {
// TODO сделать проверки на сообщения сервера без запроса
// Это сообщение игры
bool fDoSend = false;
bool fDoCore = true;
if (waitingForReceive && jidIndex == lastSendIndex) {
// Это ожидаемый нами пакет
waitingForReceive = false;
// Проверяем на наличие ошибки частых команд
if (fastSendReg.indexIn(message, 0) == -1) {
sendCommandRetries = 0;
// Отмечаем дату приема сообщения
gameJidsEx[jidIndex].last_recv_game = lastReceive;
} else {
fDoCore = false;
}
fDoSend = true;
}
// Отправляем текст ядру
bool notShowText = true;
if (fDoCore) {
notShowText = emit gameTextReceived(mirrorJid, message);
}
if (fDoSend) {
// Запускаем обработчик очереди
doSendGameStringJob();
}
if (notShowText)
return true;
}
}
}
return false;
}
/**
* Добавляет команду для игры в очередь
* Если других команд в очереди нет, инициирует процедуру отправки
*/
bool Sender::sendString(const QString &str)
{
if (currentAccount == -1) {
emit errorOccurred(ERROR_INCORRECT_ACCOUNT);
return false;
}
bool is_empty = gameQueue.isEmpty();
is_empty &= systemQueue.isEmpty();
gameQueue.enqueue(str);
emit queueSizeChanged(gameQueue.size());
if (is_empty && sendCommandRetries == 0) {
doSendGameStringJob();
}
return true;
}
/**
* Добавляет системную команду для игры в очередь системных команд с максимальным приоритетом
* Если других команд в очередях нет, инициирует процедуру отправки
*/
bool Sender::sendSystemString(const QString &str)
{
bool is_empty = gameQueue.isEmpty();
is_empty &= systemQueue.isEmpty();
systemQueue.enqueue(str);
if (is_empty && sendCommandRetries == 0) {
doSendGameStringJob();
}
return true;
}
void Sender::startPingMirrors()
{
/**
* Запускает таймер для пинга зеркал, если хотя бы одно из зеркал активно
**/
if (!pingMirrors) {
pingMirrors = true;
if (jidActiveCount != 0) {
doPingJob();
}
}
}
void Sender::stopPingMirrors()
{
/**
* Останавливает таймер пинга зеркал
*/
if (pingMirrors) {
pingMirrors = false;
doPingJob();
}
}
void Sender::clearPingQueue(int mirrorIndex)
{
/**
* Очищаем очередь от пинга зеркала с указанным индексом
* Пока очищается не вся очередь, а только те элементы, что у выхода
**/
while (!pingQueue.isEmpty()) {
if (pingQueue.head() != mirrorIndex) {
break;
}
pingQueue.dequeue();
}
}
void Sender::doPingJob()
{
/**
* Контролирует очередь пингов зеркал
**/
if (!pingMirrors || jidActiveCount == 0) {
pingQueue.clear();
return;
}
QDateTime currTime = QDateTime::currentDateTime();
if (pingQueue.isEmpty()) { // Новый пинг ставим в очередь, только если предыдущие отправлены
// Получаем индекс текущего jid-а
if (currGameJidIndex != -1 && gameJidsEx[currGameJidIndex].status != 0 && gameJidsEx[currGameJidIndex].last_send.isValid()) { // Игровой jid активен
int timeDelta = gameJidsEx[currGameJidIndex].last_send.secsTo(currTime);
if (timeDelta > 5 && timeDelta < 600) { // Пингуем только если простой в игре более 5 сек, но меньше чем 10 мин.
// Проверяем, подошло ли время пинга зеркал
int indexForPing = -1;
QDateTime lastPing = currTime;
int cnt = gameJidsEx.size();
for (int i = 0; i < cnt; i++) {
if (gameJidsEx[i].status != 0) { // Зеркало должно быть активно
if (!gameJidsEx[i].last_send_ping.isValid()) {
// Небыло ни одного пинга зеркала. Пингуем безусловно.
indexForPing = i;
gameJidsEx[i].probe_count = 0;
break;
}
if (indexForPing == -1 || gameJidsEx[i].last_send_ping < lastPing) {
// Это зеркало - кандидат на ping
timeDelta = gameJidsEx[i].last_send_ping.secsTo(currTime);
if (timeDelta >= 60) { // Время между пингами одного зеркала не должно быть меньше 1 мин.
if (gameJidsEx[i].probe_count < 10 || timeDelta >= 1200) { // Менее 10ти пингов или прошло 20 мин.
indexForPing = i;
lastPing = gameJidsEx[i].last_send_ping;
}
}
}
}
}
if (indexForPing != -1) {
if (gameJidsEx[indexForPing].last_send_ping.isValid()) {
if (timeDelta >= 3600) {
// Если последний замер был час назад, то сбрасываем результаты замеров
gameJidsEx[indexForPing].probe_count = 0;
} else if (!gameJidsEx[indexForPing].last_recv_ping.isValid() || gameJidsEx[indexForPing].last_recv_ping < gameJidsEx[indexForPing].last_send_ping) {
// В последний раз ответа от зеркала не было
gameJidsEx[indexForPing].probe_count = 0;
} else if (gameJidsEx[indexForPing].probe_count >= 10) {
gameJidsEx[indexForPing].probe_count = 3; // Принижаем ценность замеров
}
}
// Добавляем в очередь
pingQueue.enqueue(indexForPing);
}
}
}
}
// По-умолчанию следующая проверка для пинга через 5 сек.
int timeout = 5000;
if (!pingQueue.isEmpty()) {
if (gameQueue.isEmpty() && systemQueue.isEmpty() && sendCommandRetries == 0) {
int timeDeltaMsec = lastSend.time().msecsTo(currTime.time());
//int timeDeltaMsec2 = lastReceive.time().msecsTo(currTime.time());
//if (timeDeltaMsec2 ? timeDeltaMsec) {
// timeDeltaMsec = timeDeltaMsec2;
//}
if (timeDeltaMsec < 0) {
timeDeltaMsec += 86400000;
}
if (timeDeltaMsec >= jidInterval) {
// Можно отправлять данные
int i = pingQueue.dequeue();
if (gameJidsEx[i].status != 0) {
gameJidsEx[i].last_send_ping = currTime;
gameJidsEx[i].last_send_ping.time().start();
lastSend = currTime;
sender_->sendMessage(currentAccount, gameJidsEx[i].jid, "000", "", "chat");
}
} else {
timeout = jidInterval - timeDeltaMsec;
}
}
}
if (timeout != 0) {
QTimer::singleShot(timeout, this, SLOT(doPingJob()));
}
}
/**
* Просматривает очередь команд игры и отправляет команду из очереди серверу.
* Если прошло слишком мало времени после последней команды, то инициируется таймер.
*/
void Sender::doSendGameStringJob() {
if (gameSenderTimer.isActive())
gameSenderTimer.stop();
if (gameQueue.isEmpty() && sendCommandRetries == 0 && systemQueue.isEmpty()) {
// Выходим без инициализации таймера, т.к. нечего отсылать
return;
}
if (sendCommandRetries >= 3) {
// Команды идут слишком часто
systemQueue.clear();
if (!gameQueue.isEmpty()) {
gameQueue.clear();
emit queueSizeChanged(0);
}
sendCommandRetries = 0;
emit errorOccurred(ERROR_QUICK_COMMAND);
return;
}
// Получаем текущее время
QDateTime currTime = QDateTime::currentDateTime();
// Проверяем, пришел ли ответ на предыдущий пакет
if (waitingForReceive) {
// Ответа пока не было
int timeDeltaMsec = gameJidsEx[lastSendIndex].last_send.time().msecsTo(currTime.time());
if (timeDeltaMsec < 0) {
timeDeltaMsec += 86400000;
}
timeDeltaMsec = waitForReceivePeriod - timeDeltaMsec;
if (timeDeltaMsec > 0) {
// Продлеваем ожидание ответа от сервера
if (timeDeltaMsec < 10) {
timeDeltaMsec = 10;
}
gameSenderTimer.start(timeDeltaMsec);
return;
}
// Долго нет ответа на предыдущую посылку
systemQueue.clear();
waitingForReceive = false;
sendCommandRetries = 0;
if (!gameQueue.isEmpty()) {
gameQueue.clear();
emit queueSizeChanged(0);
}
emit errorOccurred(ERROR_SERVER_TIMEOUT);
return;
}
// Проверка, сколько прошло времени после последней отправки
int timeDeltaMsec = lastSend.time().msecsTo(currTime.time());
if (timeDeltaMsec < 0) {
timeDeltaMsec += 86400000;
}
int timeout = 0;
if (timeDeltaMsec >= jidInterval) {
// *** Ищем подходящее зеркало из доступных ***
// Получаем количество доступных зеркал
int cnt = gameJidsEx.size();
// Инициируем индекс и вес
int jidIndex = -1;
qint32 jidWeight = QINT32_MIN;
for (int i = 0; i < cnt; i++) {
if (gameJidsEx[i].status == 1) {
// Зеркало подключено
if (gameMirrorsMode == 0) {
// Используем первый доступный Джид
jidIndex = i;
break;
}
qint32 weight = 0; // Эквивалентно: статус не менялся в теч. 10мин, не играли в течении часа, нет тестов
// Учитываем когда зеркало последний раз меняло статус
if (gameJidsEx[i].last_status.isValid()) {
int timeDelta = gameJidsEx[i].last_status.secsTo(currTime);
if (timeDelta < 10) {
weight -= 600;
} else if (timeDelta < 60) {
weight -= 350;
} else if (timeDelta < 600) {
weight -= 150;
}
} else {
weight -= 20;
}
//qDebug() << "--Index: " << i << ", weight: " << weight;
// Проверяем отклик и последний пакет с зеркала
if (gameJidsEx[i].last_send.isValid()) {
if (gameJidsEx[i].last_recv_game.isValid() && gameJidsEx[i].last_recv_game >= gameJidsEx[i].last_send) {
int timeDelta = gameJidsEx[i].last_recv_game.secsTo(currTime);
if (timeDelta <= 2) {
// Активная игра, jid без особой надобности не меняем.
weight += 300;
} else if (timeDelta <= 10) {
// Игра средней скорости
weight += 200;
} else if (timeDelta <= 60) {
// Неторопливая игра с небольшими паузами
weight += 100;
} else if (timeDelta <= 600) {
// Игра с большими паузами
weight += 40;
} else if (timeDelta <= 3600) {
// Возможно отходили или ждали восстановления
weight += 10;
}
} else {
int timeDelta = gameJidsEx[i].last_send.secsTo(currTime);
weight -= 50;
if (timeDelta > 10) {
weight -= 50;
}
}
}
//qDebug() << "--Index: " << i << ", weight: " << weight;
// Учитываем результаты подсчета среднего отклика зеркала
if (gameJidsEx[i].probe_count > 0) {
float average = gameJidsEx[i].resp_average;
int weight_tmp = 0;
if (average <= 300) {
weight_tmp = 250;
} else if (average <= 500) {
weight_tmp = 130;
} else if (average <= 1000) {
weight_tmp = 20;
} else if (average <= 2000) {
weight_tmp = -30;
} else {
weight_tmp = -250;
}
if (gameJidsEx[i].probe_count < 3) {
weight_tmp = weight_tmp / 2;
} else if (gameJidsEx[i].probe_count < 5) {
weight_tmp = weight_tmp * 7 / 10 ;
}
weight += weight_tmp;
}
// Сверяем весовые коэффициенты
if (jidWeight < weight) {
jidWeight = weight;
jidIndex = i;
}
}
}
if (jidIndex == -1) {
// Нет ни одного доступного зеркала
waitingForReceive = false;
systemQueue.clear();
sendCommandRetries = 0;
if (!gameQueue.isEmpty()) {
gameQueue.clear();
emit queueSizeChanged(0);
}
emit errorOccurred(ERROR_NO_MORE_MIRRORS);
return; // И выходим
}
// Отмечаем время отправки
gameJidsEx[jidIndex].last_send = currTime;
lastSend = currTime;
// Отсылаем строку
if (sendCommandRetries == 0) {
lastCommand = prefix;
if (!systemQueue.isEmpty()) {
lastCommand.append(systemQueue.dequeue());
} else {
lastCommand.append(gameQueue.dequeue());
emit queueSizeChanged(gameQueue.size());
}
}
sender_->sendMessage(currentAccount, gameJidsEx[jidIndex].jid, lastCommand, "", "chat");
lastSendIndex = jidIndex;
waitingForReceive = true;
sendCommandRetries++;
currGameJidIndex = jidIndex;
// Проверяем наличие команд в очереди если не пустая, взводим таймер
// if (!gameQueue.isEmpty()) {
// timeout = jidInterval;
// } else {
timeout = waitForReceivePeriod;
// }
} else {
timeout = jidInterval - timeDeltaMsec;
}
if (timeout > 0) {
gameSenderTimer.start(timeout);
}
}
/**
* Возвращает длину очереди команд игры
*/
int Sender::getGameQueueLength() const
{
int res = gameQueue.size() + systemQueue.size();
if (sendCommandRetries != 0)
res++;
return res;
}
/**
* Сбрасывает очередь команд игры
*/
void Sender::resetGameQueue()
{
waitingForReceive = false;
systemQueue.clear();
sendCommandRetries = 0;
if (!gameQueue.isEmpty()) {
gameQueue.clear();
emit queueSizeChanged(0);
}
}