united TCP transfers for start game and receive preview
authordisplacer
Sun, 01 Oct 2006 20:25:03 +0000
changeset 178 efdc2a63be8e
parent 177 c67c15e6fae3
child 179 06e472d3f9f8
united TCP transfers for start game and receive preview
QTfrontend/game.cpp
QTfrontend/game.h
--- a/QTfrontend/game.cpp	Sun Oct 01 20:14:30 2006 +0000
+++ b/QTfrontend/game.cpp	Sun Oct 01 20:25:03 2006 +0000
@@ -53,30 +53,9 @@
 	seed = "";
 }
 
-void HWGame::NewConnection()
-{
-	QTcpSocket * client = IPCServer->nextPendingConnection();
-	if(!IPCSocket)
-	{
-		IPCServer->close();
-		IPCSocket = client;
-		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
-		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
-		if (toSendBuf.size() > 0)
-			SENDIPC("?");
-	} else
-	{
-		qWarning("2nd IPC client?!");
-		client->disconnectFromHost();
-	}
-}
-
-void HWGame::ClientDisconnect()
+void HWGame::onClientDisconnect()
 {
 	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
-    IPCSocket->deleteLater();
-	IPCSocket = 0;
-	deleteLater();
 }
 
 void HWGame::SendTeamConfig(int index)
@@ -218,12 +197,6 @@
 	RawSendIPC(msg);
 }
 
-void HWGame::ClientRead()
-{
-	readbuffer.append(IPCSocket->readAll());
-	onClientRead();
-}
-
 void HWGame::onClientRead()
 {
 	quint8 msglen;
@@ -237,27 +210,6 @@
 	}
 }
 
-void HWGame::Start()
-{
-	IPCServer = new QTcpServer(this);
-	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
-	IPCServer->setMaxPendingConnections(1);
-	IPCSocket = 0;
-	if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT))
-	{
-		QMessageBox::critical(0, tr("Error"),
-				tr("Unable to start the server: %1.")
-				.arg(IPCServer->errorString()));
-	}
-
-	demo = new QByteArray;
-	QProcess * process;
-	process = new QProcess;
-	connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
-	QStringList arguments=setArguments();
-	process->start(bindir->absolutePath() + "/hwengine", arguments);
-}
-
 QStringList HWGame::setArguments()
 {
 	QStringList arguments;
@@ -272,13 +224,6 @@
 	return arguments;
 }
 
-void HWGame::StartProcessError(QProcess::ProcessError error)
-{
-	QMessageBox::critical(0, tr("Error"),
-				tr("Unable to run engine: %1 (")
-				.arg(error) + bindir->absolutePath() + "/hwengine)");
-}
-
 void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs)
 {
 	if (TeamCount == 5) return;
@@ -333,6 +278,7 @@
 	demofile.close();
 
 	// run engine
+	demo = new QByteArray;
 	Start();
 }
 
@@ -348,6 +294,7 @@
 	gameType = gtLocal;
 	if (TeamCount < 2) return;
 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
+	demo = new QByteArray;
 	Start();
 }
 
@@ -355,6 +302,7 @@
 {
 	gameType = gtQLocal;
 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
+	demo = new QByteArray;
 	Start();
 }
 
--- a/QTfrontend/game.h	Sun Oct 01 20:14:30 2006 +0000
+++ b/QTfrontend/game.h	Sun Oct 01 20:25:03 2006 +0000
@@ -53,7 +53,7 @@
 class GameUIConfig;
 class GameCFGWidget;
 
-class HWGame : public QObject
+class HWGame : public TCPBase
 {
 	Q_OBJECT
 public:
@@ -67,6 +67,7 @@
  protected:
 	virtual QStringList setArguments();
 	virtual void onClientRead();
+	virtual void onClientDisconnect();
 
 signals:
 	void SendNet(const QByteArray & msg);
@@ -83,10 +84,7 @@
         gtDemo   = 3,
         gtNet    = 4
     };
-    QTcpServer * IPCServer;
-	QTcpSocket * IPCSocket;
 	char msgbuf[MAXMSGCHARS];
-	QByteArray readbuffer;
 	QString teams[5];
 	std::map<QString, unsigned char> hdNum;
 	QString seed;
@@ -97,7 +95,6 @@
 	GameCFGWidget * gamecfg;
 	GameType gameType;
 
-	void Start();
 	void SendConfig();
 	void SendQuickConfig();
 	void SendTeamConfig(int index);
@@ -107,12 +104,6 @@
 	void SendIPC(const QString & buf);
 	void RawSendIPC(const QByteArray & buf);
 	void SaveDemo(const QString & filename);
-
-private slots:
-	void NewConnection();
-	void ClientDisconnect();
-	void ClientRead();
-	void StartProcessError(QProcess::ProcessError error);
 };
 
 #endif