- Anti-flood and anti-lag changes
authorunc0rr
Sat, 31 Dec 2005 13:46:49 +0000
changeset 36 a803a00a3272
parent 35 9367f246fb5f
child 37 2b7f2a43b999
- Anti-flood and anti-lag changes - Fixed crash on connect to net server
QTfrontend/game.cpp
QTfrontend/hwform.cpp
QTfrontend/netclient.cpp
QTfrontend/netclient.h
--- a/QTfrontend/game.cpp	Sat Dec 31 13:45:09 2005 +0000
+++ b/QTfrontend/game.cpp	Sat Dec 31 13:46:49 2005 +0000
@@ -142,6 +142,11 @@
 		}
 		case '+':
 		{
+			if (gameType == gtNet)
+			{
+				QByteArray tmpbuf = QByteArray::fromRawData((char *)&msgsize, 1) + QByteArray::fromRawData(msgbuf, msgsize);
+				emit SendNet(tmpbuf);
+			}
 			break;
 		}
 		default:
--- a/QTfrontend/hwform.cpp	Sat Dec 31 13:45:09 2005 +0000
+++ b/QTfrontend/hwform.cpp	Sat Dec 31 13:46:49 2005 +0000
@@ -306,11 +306,11 @@
 void HWForm::NetConnect()
 {
 	hwnet = new HWNet(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
-	hwnet->Connect("172.19.5.168", 6667, ui.editNetNick->text());
 	connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat()));
 	connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &)));
 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
 	connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &)));
+	hwnet->Connect("172.19.5.168", 6667, ui.editNetNick->text());
 }
 
 void HWForm::NetDisconnect()
--- a/QTfrontend/netclient.cpp	Sat Dec 31 13:45:09 2005 +0000
+++ b/QTfrontend/netclient.cpp	Sat Dec 31 13:46:49 2005 +0000
@@ -165,11 +165,26 @@
 			// its ping ("?")
 		} else
 		{
-			RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
+			if (state == nsGaming)
+			{
+				NetBuffer += buf;
+			} else
+			{
+				RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
+			}
 		}
 	}
 }
 
+void HWNet::FlushNetBuf()
+{
+	if (NetBuffer.size() > 0)
+	{
+		RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, QString(NetBuffer.toBase64())));
+		NetBuffer.clear();
+	}
+}
+
 void HWNet::ParseLine(const QString & msg)
 {
 	//QMessageBox::information(0, "", msg);
@@ -344,14 +359,27 @@
 			msg = MAGIC_CHAR "T" MAGIC_CHAR + teams[i].nick + MAGIC_CHAR + teams[i].hhs.join(MAGIC_CHAR);
 			RawSendNet(QString("PRIVMSG %1 :%2").arg(channel, msg));
 			hwp_chanmsg(mynick, msg);
-			SENDCFGSTRNET(QString("ecolor %1").arg(color));
-			SENDCFGSTRNET("eadd hh0 0");
-			SENDCFGSTRNET("eadd hh1 0");
-			SENDCFGSTRNET("eadd hh2 0");
+			QByteArray cache;
+			#define ADD(a) { \
+							QByteArray strmsg; \
+							strmsg.append(a); \
+							quint8 sz = strmsg.size(); \
+							cache.append(QByteArray((char *)&sz, 1)); \
+							cache.append(strmsg); \
+							}
+			ADD(QString("ecolor %1").arg(color));
+			ADD("eadd hh0 0");
+			ADD("eadd hh1 0");
+			ADD("eadd hh2 0");
+			ADD("eadd hh3 0");
+			ADD("eadd hh4 0");
+			#undef ADD
+			QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(cache.toBase64());
+			hwp_chanmsg(mynick, _msg);
+			RawSendNet(QString("PRIVMSG %1 :").arg(channel) + _msg);
 			color <<= 8;
 		}
 		SENDCFGSTRNET("!");
-		state = nsGaming;
 	}
 }
 
@@ -384,13 +412,16 @@
 		if (msg == MAGIC_CHAR MAGIC_CHAR "ASE=")
 		{
 			state = nsGaming;
+			TimerFlusher = new QTimer();
+			connect(TimerFlusher, SIGNAL(timeout()), this, SLOT(FlushNetBuf()));
+			TimerFlusher->start(2000);
 		}
 		if (msg.startsWith(MAGIC_CHAR"T"MAGIC_CHAR))
 		{
 			NetTeamAdded(msg.mid(3));
 		}
 	}
-	if ((state != nsGaming) && (state != nsStarting))
+	if ((state < nsStarting) || (state > nsGaming))
 	{
 		return;
 	}
--- a/QTfrontend/netclient.h	Sat Dec 31 13:45:09 2005 +0000
+++ b/QTfrontend/netclient.h	Sat Dec 31 13:46:49 2005 +0000
@@ -38,6 +38,7 @@
 #include <QTcpSocket>
 #include <QRegExp>
 #include <QStringList>
+#include <QTimer>
 #include "team.h"
 #include "rndstr.h"
 
@@ -103,6 +104,8 @@
 	RNDStr seedgen;
 	int playerscnt;
 	int configasks;
+	QByteArray NetBuffer;
+	QTimer * TimerFlusher;
 
 	void RawSendNet(const QString & buf);
 	void RawSendNet(const QByteArray & buf);
@@ -128,6 +131,7 @@
 	void OnDisconnect();
 	void Perform();
 	void displayError(QAbstractSocket::SocketError socketError);
+	void FlushNetBuf();
 };
 
 #define SENDCFGSTRNET(a)   {\
@@ -135,8 +139,9 @@
 							strmsg.append(a); \
 							quint8 sz = strmsg.size(); \
 							QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
-							emit FromNet(enginemsg); \
-							RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, QString(enginemsg.toBase64()))); \
+							QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(enginemsg.toBase64()); \
+							hwp_chanmsg(mynick, _msg); \
+							RawSendNet(QString("PRIVMSG %1 :").arg(channel) + _msg); \
 						}
 
 #define SENDCFGSTRLOC(a)   {\