prepare uniting tcp classes
authordisplacer
Sun, 01 Oct 2006 20:14:30 +0000
changeset 177 c67c15e6fae3
parent 176 533d03041dcd
child 178 efdc2a63be8e
prepare uniting tcp classes
QTfrontend/CMakeLists.txt
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hedgewars.pro
QTfrontend/hwmap.cpp
QTfrontend/hwmap.h
--- a/QTfrontend/CMakeLists.txt	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/CMakeLists.txt	Sun Oct 01 20:14:30 2006 +0000
@@ -28,7 +28,8 @@
 	SquareLabel.cpp
 	hedgehogerWidget.cpp
 	hwmap.cpp
-	mapContainer.cpp)
+	mapContainer.cpp
+	tcpBase.cpp)
 
 if (WIN32)
 	set(hwfr_src ${hwfr_src} res/hedgewars.rc)
@@ -51,7 +52,8 @@
 	SquareLabel.h
 	hedgehogerWidget.h
 	hwmap.h
-	mapContainer.h)
+	mapContainer.h
+	tcpBase.h)
 
 
 set(hwfr_rez
--- a/QTfrontend/game.cpp	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/game.cpp	Sun Oct 01 20:14:30 2006 +0000
@@ -221,6 +221,11 @@
 void HWGame::ClientRead()
 {
 	readbuffer.append(IPCSocket->readAll());
+	onClientRead();
+}
+
+void HWGame::onClientRead()
+{
 	quint8 msglen;
 	quint32 bufsize;
 	while (((bufsize = readbuffer.size()) > 0) &&
@@ -247,9 +252,15 @@
 
 	demo = new QByteArray;
 	QProcess * process;
-	QStringList arguments;
 	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;
 	arguments << resolutions[0][config->vid_Resolution()];
 	arguments << resolutions[1][config->vid_Resolution()];
 	arguments << "16";
@@ -258,7 +269,7 @@
 	arguments << (config->isSoundEnabled() ? "1" : "0");
 	arguments << tr("en.txt");
 	arguments << "128";
-	process->start(bindir->absolutePath() + "/hwengine", arguments);
+	return arguments;
 }
 
 void HWGame::StartProcessError(QProcess::ProcessError error)
--- a/QTfrontend/game.h	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/game.h	Sun Oct 01 20:14:30 2006 +0000
@@ -45,7 +45,8 @@
 
 #include <map>
 
-#define IPC_PORT 46631
+#include "tcpBase.h"
+
 #define MAXMSGCHARS 255
 #define SENDIPC(a) SendIPC(a, sizeof(a) - 1)
 
@@ -63,6 +64,10 @@
 	void StartQuick();
 	void StartNet();
 
+ protected:
+	virtual QStringList setArguments();
+	virtual void onClientRead();
+
 signals:
 	void SendNet(const QByteArray & msg);
 
--- a/QTfrontend/hedgewars.pro	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/hedgewars.pro	Sun Oct 01 20:14:30 2006 +0000
@@ -28,7 +28,8 @@
            SquareLabel.h \
            hedgehogerWidget.h \
            hwmap.h \
-           mapContainer.h
+           mapContainer.h \
+           tcpBase.h
            
 SOURCES += game.cpp \
            main.cpp \
@@ -46,7 +47,8 @@
            SquareLabel.cpp \
            hedgehogerWidget.cpp \
            hwmap.cpp \
-           mapContainer.cpp
+           mapContainer.cpp \
+           tcpBase.cpp
 
 TRANSLATIONS += translations/hedgewars_ru.ts
 
--- a/QTfrontend/hwmap.cpp	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/hwmap.cpp	Sun Oct 01 20:14:30 2006 +0000
@@ -32,20 +32,9 @@
  */
 
 #include "hwmap.h"
-#include "hwconsts.h"
 
-#include <QMessageBox>
-#include <QMutex>
-#include <QList>
-
-QList<HWMap*> srvsList;
-
-HWMap::HWMap() :
-  m_isStarted(false)
+HWMap::HWMap()
 {
-  IPCServer = new QTcpServer(this);
-  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
-  IPCServer->setMaxPendingConnections(1);
 }
 
 HWMap::~HWMap()
@@ -58,23 +47,19 @@
   Start();
 }
 
-void HWMap::ClientDisconnect()
+QStringList HWMap::setArguments()
+{
+  QStringList arguments;
+  arguments << "46631";
+  arguments << "landpreview";
+  return arguments;
+}
+
+void HWMap::onClientDisconnect()
 {
   QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
   im.setNumColors(2);
-
-  IPCSocket->close();
-  IPCServer->close();
-
   emit ImageReceived(im);
-  readbuffer.clear();
-  if(srvsList.size()==1) srvsList.pop_front();
-  emit isReadyNow();
-}
-
-void HWMap::ClientRead()
-{
-  readbuffer.append(IPCSocket->readAll());
 }
 
 void HWMap::SendToClientFirst()
@@ -86,64 +71,3 @@
 
   IPCSocket->write("\x01!", 2);
 }
-
-void HWMap::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()));
-    SendToClientFirst();
-  } else {
-    qWarning("2nd IPC client?!");
-    client->disconnectFromHost();
-  }
-}
-
-void HWMap::StartProcessError(QProcess::ProcessError error)
-{
-  QMessageBox::critical(0, tr("Error"),
-			tr("Unable to run engine: %1 (")
-			.arg(error) + bindir->absolutePath() + "/hwengine)");
-}
-
-void HWMap::tcpServerReady()
-{
-  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
-  srvsList.pop_front();
-
-  RealStart();
-}
-
-void HWMap::Start()
-{
-  if(srvsList.isEmpty()) {
-    srvsList.push_back(this);
-  } else {
-    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
-    srvsList.push_back(this);
-    return;
-  }
-  
-  RealStart();
-}
-
-void HWMap::RealStart()
-{
-  IPCSocket = 0;
-  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
-    QMessageBox::critical(0, tr("Error"),
-			  tr("Unable to start the server: %1.")
-			  .arg(IPCServer->errorString()));
-  }
-  
-  QProcess * process;
-  QStringList arguments;
-  process = new QProcess;
-  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
-  arguments << "46631";
-  arguments << "landpreview";
-  process->start(bindir->absolutePath() + "/hwengine", arguments);
-}
--- a/QTfrontend/hwmap.h	Thu Sep 28 16:34:42 2006 +0000
+++ b/QTfrontend/hwmap.h	Sun Oct 01 20:14:30 2006 +0000
@@ -43,11 +43,11 @@
 #include <QProcess>
 #include <QImage>
 
-#include "game.h"
+#include "tcpBase.h"
 
 #include <string>
 
-class HWMap : public QObject
+class HWMap : public TCPBase
 {
   Q_OBJECT
     
@@ -56,31 +56,18 @@
   virtual ~HWMap();
   void getImage(std::string seed);
 
+ protected:
+  virtual QStringList setArguments();
+  virtual void onClientDisconnect();
+  virtual void SendToClientFirst();
+
  signals:
   void ImageReceived(const QImage newImage);
-  void isReadyNow();
 
  private:
-  void RealStart();
-
-  bool m_isStarted;
   std::string m_seed;
-  QTcpServer * IPCServer;
-  QTcpSocket * IPCSocket;
-  
-  QByteArray readbuffer;
-
-  void Start();
-
-  void SendToClientFirst();
 
  private slots:
-  void NewConnection();
-  void ClientDisconnect();
-  void ClientRead();
-  void StartProcessError(QProcess::ProcessError error);
-
-  void tcpServerReady();
 };
 
 #endif // _HWMAP_INCLUDED