This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgproxy.cpp
1164 lines (967 loc) · 32.9 KB
/
qgproxy.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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2011 Gene Chen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
With code derived from gproxy: http://code.google.com/p/gproxyplusplus/
*/
#include "qgproxy.h"
#include "gpsprotocol.h"
#include "gameprotocol.h"
#include "commandpacket.h"
#include <QSettings>
#include <QSound>
#include <QtEndian>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#endif
#ifndef WIN32
#include <sys/time.h>
#endif
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
quint32 GetTicks( )
{
#ifdef WIN32
return timeGetTime( );
#elif __APPLE__
uint64_t current = mach_absolute_time( );
static mach_timebase_info_data_t info = { 0, 0 };
// get timebase info
if( info.denom == 0 )
mach_timebase_info( &info );
uint64_t elapsednano = current * ( info.numer / info.denom );
// convert ns to ms
return elapsednano / 1e6;
#else
uint32_t ticks;
struct timespec t;
clock_gettime( 1, &t );
ticks = t.tv_sec * 1000;
ticks += t.tv_nsec / 1000000;
return ticks;
#endif
}
quint32 GetTime( )
{
return GetTicks( ) / 1000;
}
CGProxy::CGProxy(QWidget* parent) : QObject(parent), m_LocalSocket(0)
{
m_LocalServer = new QTcpServer(this);
m_ListenPort = 6125;
while(!m_LocalServer->listen(QHostAddress::LocalHost, m_ListenPort))
m_ListenPort++;
connect(m_LocalServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
m_RequesterSocket = new QUdpSocket(this);
m_RequesterSocket->bind(m_ListenPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
connect(m_RequesterSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
m_GameProtocol = new CGameProtocol();
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(30);
m_TotalPacketsReceivedFromLocal = 0;
m_TotalPacketsReceivedFromRemote = 0;
m_LastConnectionAttemptTime = 0;
m_GameIsReliable = false;
m_GameStarted = false;
m_LeaveGameSent = false;
m_ActionReceived = false;
m_Synchronized = true;
m_ReconnectPort = 0;
m_PID = 255;
m_ChatPID = 255;
m_ReconnectKey = 0;
m_NumEmptyActions = 0;
m_NumEmptyActionsUsed = 0;
m_LastAckTime = 0;
m_LastActionTime = 0;
m_LastBroadcastTime = 0;
m_RemoteSocket = new QTcpSocket(this);
connect(m_RemoteSocket, SIGNAL(disconnected()),
this, SLOT(remoteDisconnected()));
connect(m_RemoteSocket, SIGNAL(readyRead()), this, SLOT(readServerPackets()));
}
CGProxy::~CGProxy()
{
m_LocalServer->close();
for( QVector<CGameInfo*>::iterator i = games.begin( ); i != games.end( ); ++i )
m_RequesterSocket->writeDatagram(m_GameProtocol->SEND_W3GS_DECREATEGAME((*i)->GetUniqueGameID()), QHostAddress::Broadcast, 6112);
while( !games.empty( ) )
{
delete games.front( );
games.pop_front( );
}
while( !m_LocalPackets.empty( ) )
{
delete m_LocalPackets.front( );
m_LocalPackets.pop_front( );
}
while( !m_RemotePackets.empty( ) )
{
delete m_RemotePackets.front( );
m_RemotePackets.pop_front( );
}
while( !m_PacketBuffer.empty( ) )
{
delete m_PacketBuffer.front( );
m_PacketBuffer.pop_front( );
}
delete m_RemoteSocket;
delete m_GameProtocol;
}
void CGProxy::update()
{
processLocalPackets();
processServerPackets();
if( !m_RemoteServerIP.isEmpty( ) && m_LocalSocket != NULL && m_LocalSocket->state() == QTcpSocket::ConnectedState )
{
if( m_GameIsReliable && m_ActionReceived && GetTime( ) - m_LastActionTime >= 60 )
{
if( m_NumEmptyActionsUsed < m_NumEmptyActions )
{
SendEmptyAction( );
m_NumEmptyActionsUsed++;
}
else
{
SendLocalChat( "GProxy++ ran out of time to reconnect, Warcraft III will disconnect soon." );
qDebug() << ( "[GPROXY] ran out of time to reconnect" );
m_LocalSocket->disconnectFromHost( );
m_RemoteServerIP.clear( );
}
m_LastActionTime = GetTime( );
}
if( m_RemoteSocket->state() == QTcpSocket::ConnectedState )
{
if( m_GameIsReliable && m_ActionReceived && m_ReconnectPort > 0 && GetTime( ) - m_LastAckTime >= 10 )
{
m_RemoteSocket->write( m_GPSProtocol->SEND_GPSC_ACK( m_TotalPacketsReceivedFromRemote ) );
m_LastAckTime = GetTime( );
}
}
if( m_RemoteSocket->state() != QTcpSocket::ConnectingState && m_RemoteSocket->state() != QTcpSocket::ConnectedState )
{
if( m_GameIsReliable && m_ActionReceived && m_ReconnectPort > 0 )
{
if(GetTime( ) - m_LastConnectionAttemptTime >= 10)
{
SendLocalChat( "You have been disconnected from the server." );
quint32 TimeRemaining = ( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 - ( GetTime( ) - m_LastActionTime );
if( GetTime( ) - m_LastActionTime > quint32( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 )
TimeRemaining = 0;
SendLocalChat( tr("GProxy++ is attempting to reconnect... (%1 seconds remain)").arg(TimeRemaining) );
qDebug() << ( "[GPROXY] attempting to reconnect" );
m_RemoteSocket->reset();
m_RemoteSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
m_RemoteSocket->connectToHost( m_RemoteServerIP, m_ReconnectPort );
m_LastConnectionAttemptTime = GetTime( );
}
}
else
{
m_LocalSocket->disconnectFromHost( );
m_RemoteServerIP.clear( );
}
}
if( m_RemoteSocket->state() == QTcpSocket::ConnectingState )
{
// we are currently attempting to connect
if( m_RemoteSocket->waitForConnected(10000) )
{
// the connection attempt completed
if( m_GameIsReliable && m_ActionReceived )
{
// this is a reconnection, not a new connection
// if the server accepts the reconnect request it will send a GPS_RECONNECT back requesting a certain number of packets
SendLocalChat( "GProxy++ reconnected to the server!" );
SendLocalChat( "==================================================" );
qDebug() << ( "[GPROXY] reconnected to remote server" );
// note: even though we reset the socket when we were disconnected, we haven't been careful to ensure we never queued any data in the meantime
// therefore it's possible the socket could have data in the send buffer
// this is bad because the server will expect us to send a GPS_RECONNECT message first
// so we must clear the send buffer before we continue
// note: we aren't losing data here, any important messages that need to be sent have been put in the packet buffer
// they will be requested by the server if required
//m_RemoteSocket->;
m_RemoteSocket->write( m_GPSProtocol->SEND_GPSC_RECONNECT( m_PID, m_ReconnectKey, m_TotalPacketsReceivedFromRemote ) );
// we cannot permit any forwarding of local packets until the game is synchronized again
// this will disable forwarding and will be reset when the synchronization is complete
m_Synchronized = false;
}
else
qDebug() << ( "[GPROXY] connected to remote server" );
}
else if( GetTime( ) - m_LastConnectionAttemptTime >= 10 )
{
// the connection attempt timed out (10 seconds)
qDebug() << ( "[GPROXY] connect to remote server timed out" );
if( m_GameIsReliable && m_ActionReceived && m_ReconnectPort > 0 )
{
quint32 TimeRemaining = ( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 - ( GetTime( ) - m_LastActionTime );
if( GetTime( ) - m_LastActionTime > quint32( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 )
TimeRemaining = 0;
SendLocalChat( tr("GProxy++ is attempting to reconnect... (%1 seconds remain)").arg(TimeRemaining) );
qDebug() << ( "[GPROXY] attempting to reconnect" );
m_RemoteSocket->connectToHost( m_RemoteServerIP, m_ReconnectPort );
m_LastConnectionAttemptTime = GetTime( );
}
else
{
m_LocalSocket->disconnectFromHost( );
m_RemoteServerIP.clear( );
}
}
}
}
if(!m_LocalSocket || m_LocalSocket->state() != QTcpSocket::ConnectedState || m_RemoteSocket->state() != QTcpSocket::ConnectedState)
{
if( GetTime( ) - m_LastBroadcastTime >= 3 )
{
for(QVector<CGameInfo*>::const_iterator i = games.constBegin(); i != games.constEnd(); ++i)
m_RequesterSocket->writeDatagram((*i)->GetPacket(m_ListenPort), QHostAddress::LocalHost, 6112);
m_LastBroadcastTime = GetTime( );
}
}
}
void CGProxy::newConnection()
{
QTcpSocket* newClient = m_LocalServer->nextPendingConnection();
if(m_LocalSocket)
{
newClient->disconnectFromHost();
return;
}
while( !m_LocalPackets.empty( ) )
{
delete m_LocalPackets.front( );
m_LocalPackets.pop_front( );
}
while( !m_RemotePackets.empty( ) )
{
delete m_RemotePackets.front( );
m_RemotePackets.pop_front( );
}
while( !m_PacketBuffer.empty( ) )
{
delete m_PacketBuffer.front( );
m_PacketBuffer.pop_front( );
}
newClient->setSocketOption(QAbstractSocket::LowDelayOption, 1);
m_TotalPacketsReceivedFromLocal = 0;
m_TotalPacketsReceivedFromRemote = 0;
m_LastConnectionAttemptTime = 0;
m_GameIsReliable = false;
m_GameStarted = false;
m_LeaveGameSent = false;
m_ActionReceived = false;
m_Synchronized = true;
m_ReconnectPort = 0;
m_PID = 255;
m_ChatPID = 255;
m_ReconnectKey = 0;
m_NumEmptyActions = 0;
m_NumEmptyActionsUsed = 0;
m_LastAckTime = 0;
m_LastActionTime = 0;
m_LocalSocket = newClient;
connect(m_LocalSocket, SIGNAL(disconnected()),
this, SLOT(localDisconnected()));
connect(m_LocalSocket, SIGNAL(disconnected()),
m_LocalSocket, SLOT(deleteLater()));
connect(m_LocalSocket, SIGNAL(readyRead()), this, SLOT(readLocalPackets()));
}
void CGProxy::localDisconnected()
{
if( m_GameIsReliable && !m_LeaveGameSent && m_RemoteSocket->state() == QTcpSocket::ConnectedState)
{
// note: we're not actually 100% ensuring the leavegame message is sent, we'd need to check that DoSend worked, etc...
QByteArray LeaveGame;
QDataStream ds(&LeaveGame, QIODevice::ReadWrite);
ds.setByteOrder(QDataStream::LittleEndian);
ds << (quint8)W3GS_HEADER_CONSTANT;
ds << (quint8)CGameProtocol::W3GS_LEAVEGAME;
ds << (quint16)0x08;
ds << (quint32)PLAYERLEAVE_GPROXY;
m_RemoteSocket->write( LeaveGame );
m_RemoteSocket->waitForBytesWritten();
m_LeaveGameSent = true;
}
m_RemoteSocket->disconnectFromHost();
m_LocalSocket = NULL;
m_RemoteBytes.clear();
m_LocalBytes.clear();
while( !m_LocalPackets.empty( ) )
{
delete m_LocalPackets.front( );
m_LocalPackets.pop_front( );
}
while( !m_RemotePackets.empty( ) )
{
delete m_RemotePackets.front( );
m_RemotePackets.pop_front( );
}
while( !m_PacketBuffer.empty( ) )
{
delete m_PacketBuffer.front( );
m_PacketBuffer.pop_front( );
}
}
void CGProxy::remoteDisconnected()
{
while( !m_LocalPackets.empty( ) )
{
delete m_LocalPackets.front( );
m_LocalPackets.pop_front( );
}
while( !m_RemotePackets.empty( ) )
{
delete m_RemotePackets.front( );
m_RemotePackets.pop_front( );
}
if(m_LeaveGameSent)
return;
if(m_LocalSocket == NULL || m_LocalSocket->state() != QTcpSocket::ConnectedState)
{
return;
}
qDebug() << ( "[GPROXY] disconnected from remote server due to socket error" );
if( m_GameIsReliable && m_ActionReceived && m_ReconnectPort > 0 )
{
SendLocalChat( "You have been disconnected from the server due to a socket error." );
quint32 TimeRemaining = ( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 - ( GetTime( ) - m_LastActionTime );
if( GetTime( ) - m_LastActionTime > quint32( m_NumEmptyActions - m_NumEmptyActionsUsed + 1 ) * 60 )
TimeRemaining = 0;
SendLocalChat( tr("GProxy++ is attempting to reconnect... (%1 seconds remain)").arg(TimeRemaining) );
qDebug() << ( "[GPROXY] attempting to reconnect" );
m_RemoteSocket->reset();
m_RemoteSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
m_RemoteSocket->connectToHost( m_RemoteServerIP, m_ReconnectPort );
m_LastConnectionAttemptTime = GetTime( );
}
else
{
m_LocalSocket->disconnectFromHost( );
m_RemoteServerIP.clear( );
}
}
void CGProxy::readLocalPackets()
{
QByteArray bytes = m_LocalSocket->readAll();
m_LocalBytes += bytes;
while( m_LocalBytes.size( ) >= 4 )
{
if( m_LocalBytes.at(0) == W3GS_HEADER_CONSTANT )
{
QByteArray LengthBytes;
LengthBytes.push_back( m_LocalBytes[2] );
LengthBytes.push_back( m_LocalBytes[3] );
quint16 length = qFromLittleEndian<quint16>((uchar*)LengthBytes.data());
if(length >= 4)
{
if( m_LocalBytes.size( ) >= length )
{
QByteArray data = QByteArray(m_LocalBytes, length);
QDataStream ds(data);
bool forward = true;
if( m_LocalBytes.at(1) == CGameProtocol :: W3GS_CHAT_TO_HOST )
{
if( data.size( ) >= 5 )
{
int i = 5;
char Total = data[4];
if( Total > 0 && data.size( ) >= i + Total )
{
i += Total;
unsigned char Flag = data[i + 1];
i += 2;
QString MessageString;
if( Flag == 16 && data.size( ) >= i + 1 )
{
ds.skipRawData(i);
MessageString = CGameProtocol :: ExtractString(ds);
}
else if( Flag == 32 && data.size( ) >= i + 5 )
{
ds.skipRawData(i+4);
MessageString = CGameProtocol :: ExtractString(ds);
}
QString Command = MessageString.toLower();
if( Command.size( ) >= 1 && Command.at(0) == '/' )
{
forward = false;
if( Command.size( ) >= 5 && Command.mid( 0, 4 ) == "/re " )
{
// if( m_BNET->GetLoggedIn( ) )
// {
// //if( !m_BNET->GetReplyTarget( ).empty( ) )
// {
// //m_BNET->QueueChatCommand( MessageString.substr( 4 ), m_BNET->GetReplyTarget( ), true );
// SendLocalChat( "Whispered to " + m_BNET->GetReplyTarget( ) + ": " + MessageString.substr( 4 ) );
// }
// else
// SendLocalChat( "Nobody has whispered you yet." );
// }
// else
// SendLocalChat( "You are not connected to battle.net." );
}
else if( Command == "/status" )
{
if( m_LocalSocket )
{
if( m_GameIsReliable && m_ReconnectPort > 0 )
SendLocalChat( "GProxy++ disconnect protection: enabled" );
else
SendLocalChat( "GProxy++ disconnect protection: disabled" );
}
}
}
}
}
}
if( forward )
{
m_LocalPackets.push_back( new CCommandPacket( W3GS_HEADER_CONSTANT, m_LocalBytes[1], data ) );
m_PacketBuffer.push_back( new CCommandPacket( W3GS_HEADER_CONSTANT, m_LocalBytes[1], data ) );
m_TotalPacketsReceivedFromLocal++;
}
m_LocalBytes = QByteArray(m_LocalBytes.begin( ) + length, m_LocalBytes.size() - length);
}
else
return;
}
else
{
qDebug() << "[GPROXY] received invalid packet from local player (bad length)";
m_LeaveGameSent = true;
m_LocalSocket->disconnectFromHost( );
return;
}
}
else
{
qDebug() << ( "[GPROXY] received invalid packet from local player (bad header constant)" );
m_LeaveGameSent = true;
m_LocalSocket->disconnectFromHost( );
return;
}
}
processLocalPackets();
}
void CGProxy::processLocalPackets()
{
if(!m_LocalSocket || m_LocalSocket->state() != QTcpSocket::ConnectedState)
return;
while( !m_LocalPackets.empty( ) )
{
CCommandPacket *Packet = m_LocalPackets.front( );
m_LocalPackets.pop_front( );
QByteArray Data = Packet->GetData( );
QDataStream ds(Data);
ds.setByteOrder(QDataStream::LittleEndian);
if( Packet->GetPacketType( ) == W3GS_HEADER_CONSTANT )
{
if( Packet->GetID( ) == CGameProtocol :: W3GS_REQJOIN )
{
if( Data.size( ) >= 20 )
{
quint32 HostCounter;
quint32 EntryKey;
quint8 Unknown;
quint16 ListenPort;
quint32 PeerKey;
QString Name;
QByteArray Remainder;
ds.skipRawData(4);
ds >> HostCounter;
ds >> EntryKey;
ds >> Unknown;
ds >> ListenPort;
ds >> PeerKey;
Name = CGameProtocol::ExtractString(ds);
Remainder = QByteArray(Data.begin() + Name.size() + 20, Data.size() - (Name.size() + 20));
if(Remainder.size( ) == 18)
{
bool GameFound = false;
for(QVector<CGameInfo*>::iterator i = games.begin( ); i != games.end( ); ++i)
{
if((*i)->GetUniqueGameID() == EntryKey)
{
m_RemoteSocket->reset();
m_RemoteSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
m_RemoteSocket->connectToHost((*i)->GetIP(), (*i)->GetPort());
if(!m_RemoteSocket->waitForConnected(3000))
{
qDebug() << "[GPROXY] player requested to join an expired game. removing from list.";
m_RequesterSocket->writeDatagram(m_GameProtocol->SEND_W3GS_DECREATEGAME((*i)->GetUniqueGameID()), QHostAddress::Broadcast, 6112);
delete (*i);
games.erase(i);
break;
}
m_LastConnectionAttemptTime = GetTime();
m_RemoteServerIP = (*i)->GetIP();
m_GameIsReliable = (*i)->GetReliable();
m_GameStarted = false;
QByteArray DataRewritten;
QDataStream ds(&DataRewritten, QIODevice::ReadWrite);
ds.setByteOrder(QDataStream::LittleEndian);
ds << (quint8)W3GS_HEADER_CONSTANT;
ds << (quint8)Packet->GetID();
ds << (quint16)0;
ds << (*i)->GetHostCounter();
ds << (*i)->GetEntryKey();
ds << (quint8)Unknown;
ds << (quint16)ListenPort;
ds << (quint32)PeerKey;
ds.writeRawData(Name.toUtf8(), Name.length());
ds << (quint8)0;
ds.writeRawData(Remainder.data(), Remainder.length());
CGameProtocol::AssignLength(DataRewritten);
Data = DataRewritten;
GameFound = true;
emit joinedGame((*i)->GetIP(), (*i)->GetGameName());
break;
}
}
if(!GameFound)
{
qDebug() << "[GPROXY] local player requested unknown game (expired?) sending decreate.";
m_RequesterSocket->writeDatagram(m_GameProtocol->SEND_W3GS_DECREATEGAME(EntryKey), QHostAddress::Broadcast, 6112);
m_LocalSocket->disconnectFromHost();
}
}
else
qDebug() << "[GPROXY] received invalid join request from local player (invalid remainder)";
}
else
qDebug() << "[GPROXY] received invalid join request from local player (too short)";
}
else if(Packet->GetID( ) == CGameProtocol :: W3GS_LEAVEGAME)
{
QByteArray LeaveGame;
QDataStream ds(&LeaveGame, QIODevice::ReadWrite);
ds.setByteOrder(QDataStream::LittleEndian);
ds << (quint8)W3GS_HEADER_CONSTANT;
ds << (quint8)CGameProtocol::W3GS_LEAVEGAME;
ds << (quint16)0x08;
ds << (quint32)0x00;
m_RemoteSocket->write( LeaveGame );
m_RemoteSocket->waitForBytesWritten();
m_LeaveGameSent = true;
m_LocalSocket->disconnectFromHost();
}
}
if( m_RemoteSocket && m_Synchronized )
m_RemoteSocket->write(Data);
delete Packet;
}
}
void CGProxy::readServerPackets()
{
QByteArray bytes = m_RemoteSocket->readAll();
m_RemoteBytes += bytes;
while( m_RemoteBytes.size( ) >= 4 )
{
if( m_RemoteBytes.at(0) == W3GS_HEADER_CONSTANT || m_RemoteBytes.at(0) == GPS_HEADER_CONSTANT )
{
QByteArray LengthBytes;
LengthBytes.push_back( m_RemoteBytes[2] );
LengthBytes.push_back( m_RemoteBytes[3] );
quint16 length = qFromLittleEndian<quint16>((uchar*)LengthBytes.data());
if( length >= 4 )
{
if( m_RemoteBytes.size( ) >= length )
{
QByteArray data = QByteArray(m_RemoteBytes.begin(), length);
m_RemotePackets.push_back(new CCommandPacket( m_RemoteBytes.at(0), m_RemoteBytes.at(1), data ));
if( m_RemoteBytes.at(0) == W3GS_HEADER_CONSTANT )
m_TotalPacketsReceivedFromRemote++;
m_RemoteBytes = QByteArray(m_RemoteBytes.begin( ) + length, m_RemoteBytes.size() - length);
}
else
return;
}
else
{
qDebug() << "[GPROXY] received invalid packet from remote server (bad length)";
m_RemoteSocket->disconnectFromHost();
return;
}
}
else
{
qDebug() << "[GPROXY] received invalid packet from remote server (bad header constant)";
m_RemoteSocket->disconnectFromHost();
return;
}
}
processServerPackets();
}
void CGProxy::processServerPackets()
{
if(!m_LocalSocket || m_LocalSocket->state() != QTcpSocket::ConnectedState || m_RemoteSocket->state() != QTcpSocket::ConnectedState)
return;
while( !m_RemotePackets.empty( ) )
{
CCommandPacket *Packet = m_RemotePackets.front( );
m_RemotePackets.pop_front( );
QByteArray Data = Packet->GetData( );
QDataStream ds(Data);
ds.setByteOrder(QDataStream::LittleEndian);
ds.skipRawData(4);
if( Packet->GetPacketType( ) == W3GS_HEADER_CONSTANT )
{
if( Packet->GetID( ) == CGameProtocol :: W3GS_SLOTINFOJOIN )
{
if( Data.size( ) >= 6 )
{
quint16 SlotInfoSize;
ds >> SlotInfoSize;
if( Data.size( ) >= 7 + SlotInfoSize )
m_ChatPID = Data[6 + SlotInfoSize];
}
// send a GPS_INIT packet
// if the server doesn't recognize it (e.g. it isn't GHost++) we should be kicked
qDebug() << ( "[GPROXY] join request accepted by remote server" );
if( m_GameIsReliable )
{
qDebug() << ( "[GPROXY] detected reliable game, starting GPS handshake" );
m_RemoteSocket->write( m_GPSProtocol->SEND_GPSC_INIT( 1 ) );
}
else
qDebug() << ( "[GPROXY] detected standard game, disconnect protection disabled" );
}
else if( Packet->GetID( ) == CGameProtocol :: W3GS_COUNTDOWN_END )
{
if( m_GameIsReliable && m_ReconnectPort > 0 )
qDebug() << ( "[GPROXY] game started, disconnect protection enabled" );
else
{
if( m_GameIsReliable )
qDebug() << ( "[GPROXY] game started but GPS handshake not complete, disconnect protection disabled" );
else
qDebug() << ( "[GPROXY] game started" );
}
for( QVector<CGameInfo*>::iterator i = games.begin( ); i != games.end( ); ++i )
m_RequesterSocket->writeDatagram(m_GameProtocol->SEND_W3GS_DECREATEGAME((*i)->GetUniqueGameID()), QHostAddress::Broadcast, 6112);
while( !games.empty( ) )
{
delete games.front( );
games.pop_front( );
}
QSettings settings("DotaCash", "DCClient X");
if(settings.value("GameStartedSound", true).toBool())
QSound::play("./sounds/GameStarted.wav");
m_GameStarted = true;
}
else if( Packet->GetID( ) == CGameProtocol :: W3GS_INCOMING_ACTION )
{
if( m_GameIsReliable )
{
// we received a game update which means we can reset the number of empty actions we have to work with
// we also must send any remaining empty actions now
// note: the lag screen can't be up right now otherwise the server made a big mistake, so we don't need to check for it
QByteArray EmptyAction;
EmptyAction.append( (char)0xF7 );
EmptyAction.append( (char)0x0C );
EmptyAction.append( (char)0x06 );
EmptyAction.append( (char)0x00 );
EmptyAction.append( (char)0x00 );
EmptyAction.append( (char)0x00 );
for( unsigned char i = m_NumEmptyActionsUsed; i < m_NumEmptyActions; i++ )
m_LocalSocket->write( EmptyAction );
m_NumEmptyActionsUsed = 0;
}
m_ActionReceived = true;
m_LastActionTime = GetTime( );
}
else if( Packet->GetID( ) == CGameProtocol :: W3GS_START_LAG )
{
if( m_GameIsReliable )
{
QByteArray Data = Packet->GetData( );
if( Data.size( ) >= 5 )
{
quint8 NumLaggers = Data[4];
if( Data.size( ) == 5 + NumLaggers * 5 )
{
for( quint8 i = 0; i < NumLaggers; i++ )
{
bool LaggerFound = false;
for( QVector<quint8>::iterator j = m_Laggers.begin( ); j != m_Laggers.end( ); j++ )
{
if( *j == Data[5 + i * 5] )
LaggerFound = true;
}
if( LaggerFound )
qDebug() << ( "[GPROXY] warning - received start_lag on known lagger" );
else
m_Laggers.push_back( Data[5 + i * 5] );
}
}
else
qDebug() << ( "[GPROXY] warning - unhandled start_lag (2)" );
}
else
qDebug() << ( "[GPROXY] warning - unhandled start_lag (1)" );
}
}
else if( Packet->GetID( ) == CGameProtocol :: W3GS_STOP_LAG )
{
if( m_GameIsReliable )
{
QByteArray Data = Packet->GetData( );
if( Data.size( ) == 9 )
{
bool LaggerFound = false;
for( QVector<quint8>::iterator i = m_Laggers.begin( ); i != m_Laggers.end( ); )
{
if( *i == Data[4] )
{
i = m_Laggers.erase( i );
LaggerFound = true;
}
else
i++;
}
if( !LaggerFound )
qDebug() << ( "[GPROXY] warning - received stop_lag on unknown lagger" );
}
else
qDebug() << ( "[GPROXY] warning - unhandled stop_lag" );
}
}
else if( Packet->GetID( ) == CGameProtocol :: W3GS_INCOMING_ACTION2 )
{
if( m_GameIsReliable )
{
// we received a fractured game update which means we cannot use any empty actions until we receive the subsequent game update
// we also must send any remaining empty actions now
// note: this means if we get disconnected right now we can't use any of our buffer time, which would be very unlucky
// it still gives us 60 seconds total to reconnect though
// note: the lag screen can't be up right now otherwise the server made a big mistake, so we don't need to check for it
QByteArray EmptyAction;
EmptyAction.append( (char)0xF7 );
EmptyAction.append( (char)0x0C );
EmptyAction.append( (char)0x06 );
EmptyAction.append( (char)0x00 );
EmptyAction.append( (char)0x00 );
EmptyAction.append( (char)0x00 );
for( unsigned char i = m_NumEmptyActionsUsed; i < m_NumEmptyActions; i++ )
m_LocalSocket->write( EmptyAction );
m_NumEmptyActionsUsed = m_NumEmptyActions;
}
}
// forward the data
m_LocalSocket->write( Packet->GetData( ) );
// we have to wait until now to send the status message since otherwise the slotinfojoin itself wouldn't have been forwarded
if( Packet->GetID( ) == CGameProtocol :: W3GS_SLOTINFOJOIN )
{
if( m_GameIsReliable )
SendLocalChat( tr("This is a reliable game. Requesting GProxy++ disconnect protection from server...") );
else
SendLocalChat( tr("This is an unreliable game. GProxy++ disconnect protection is disabled.") );
}
}
else if( Packet->GetPacketType( ) == GPS_HEADER_CONSTANT )
{
if( m_GameIsReliable )
{
QByteArray Data = Packet->GetData( );
if( Packet->GetID( ) == CGPSProtocol :: GPS_INIT && Data.size( ) == 12 )
{
ds >> m_ReconnectPort;
ds >> m_PID;
ds >> m_ReconnectKey;
ds >> m_NumEmptyActions;
SendLocalChat( tr("GProxy++ disconnect protection is ready (%1 second buffer).").arg( ( m_NumEmptyActions + 1 ) * 60 ) );
}
else if( Packet->GetID( ) == CGPSProtocol :: GPS_RECONNECT && Data.size( ) == 8 )
{
quint32 LastPacket;
ds >> LastPacket;
quint32 PacketsAlreadyUnqueued = m_TotalPacketsReceivedFromLocal - m_PacketBuffer.size( );
if( LastPacket > PacketsAlreadyUnqueued )
{
int PacketsToUnqueue = LastPacket - PacketsAlreadyUnqueued;
if( PacketsToUnqueue > m_PacketBuffer.size( ) )
{
qDebug() << ( "[GPROXY] received GPS_RECONNECT with last packet > total packets sent" );
PacketsToUnqueue = m_PacketBuffer.size( );
}
while( PacketsToUnqueue > 0 )
{
delete m_PacketBuffer.front( );
m_PacketBuffer.pop_front( );
PacketsToUnqueue--;
}
}
// send remaining packets from buffer, preserve buffer
// note: any packets in m_LocalPackets are still sitting at the end of this buffer because they haven't been processed yet
// therefore we must check for duplicates otherwise we might (will) cause a desync
QQueue<CCommandPacket *> TempBuffer;
while( !m_PacketBuffer.empty( ) )
{
if( m_PacketBuffer.size( ) > m_LocalPackets.size( ) )
m_RemoteSocket->write( m_PacketBuffer.front( )->GetData( ) );
TempBuffer.push_back( m_PacketBuffer.front( ) );
m_PacketBuffer.pop_front( );
}
m_PacketBuffer = TempBuffer;
// we can resume forwarding local packets again
// doing so prior to this point could result in an out-of-order stream which would probably cause a desync
m_Synchronized = true;
}
else if( Packet->GetID( ) == CGPSProtocol :: GPS_ACK && Data.size( ) == 8 )
{
quint32 LastPacket;
ds >> LastPacket;
quint32 PacketsAlreadyUnqueued = m_TotalPacketsReceivedFromLocal - m_PacketBuffer.size( );
if( LastPacket > PacketsAlreadyUnqueued )
{
int PacketsToUnqueue = LastPacket - PacketsAlreadyUnqueued;
if( PacketsToUnqueue > m_PacketBuffer.size( ) )
{
qDebug() << ( "[GPROXY] received GPS_ACK with last packet > total packets sent" );
PacketsToUnqueue = m_PacketBuffer.size( );