QTfrontend/game.cpp
changeset 43 e297fea1a2f3
parent 41 5d7a505875cd
child 52 ae2950c5465c
equal deleted inserted replaced
42:72ffe21f027c 43:e297fea1a2f3
    58 	{
    58 	{
    59 		IPCServer->close();
    59 		IPCServer->close();
    60 		IPCSocket = client;
    60 		IPCSocket = client;
    61 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    61 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    62 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    62 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    63 		msgsize = 0;
       
    64 		if (toSendBuf.size() > 0)
    63 		if (toSendBuf.size() > 0)
    65 			SENDIPC("?");
    64 			SENDIPC("?");
    66 	} else
    65 	} else
    67 	{
    66 	{
    68 		qWarning("2nd IPC client?!");
    67 		qWarning("2nd IPC client?!");
   103 	SENDIPC("eadd hh2 1");
   102 	SENDIPC("eadd hh2 1");
   104 	SENDIPC("eadd hh3 1");
   103 	SENDIPC("eadd hh3 1");
   105 	SENDIPC("eadd hh4 1");
   104 	SENDIPC("eadd hh4 1");
   106 }
   105 }
   107 
   106 
   108 void HWGame::ParseMessage()
   107 void HWGame::ParseMessage(const QByteArray & msg)
   109 {
   108 {
   110 	switch(msgbuf[0])
   109 	switch(msg.data()[1])
   111 	{
   110 	{
   112 		case '?':
   111 		case '?':
   113 		{
   112 		{
   114 			if (gameType == gtNet)
   113 			if (gameType == gtNet)
   115 				emit SendNet(QByteArray("\x01""?"));
   114 				emit SendNet(QByteArray("\x01""?"));
   129 				if (gameType == gtLocal)
   128 				if (gameType == gtLocal)
   130 				 	SendConfig();
   129 				 	SendConfig();
   131 			}
   130 			}
   132 			break;
   131 			break;
   133 		}
   132 		}
       
   133 		case 'E':
       
   134 		{
       
   135 			QMessageBox::critical(0,
       
   136 					"Hedgewars: error message",
       
   137 					QString().append(msg.mid(2)).left(msg.size() - 6),
       
   138 					QMessageBox::Ok,
       
   139 					QMessageBox::NoButton,
       
   140 					QMessageBox::NoButton);
       
   141 			return;
       
   142 		}
   134 		case '+':
   143 		case '+':
   135 		{
   144 		{
   136 			if (gameType == gtNet)
   145 			if (gameType == gtNet)
   137 			{
   146 			{
   138 				QByteArray tmpbuf = QByteArray::fromRawData((char *)&msgsize, 1) + QByteArray::fromRawData(msgbuf, msgsize);
   147 				emit SendNet(msg);
   139 				emit SendNet(tmpbuf);
       
   140 			}
   148 			}
   141 			break;
   149 			break;
   142 		}
   150 		}
   143 		default:
   151 		default:
   144 		{
   152 		{
   145 			QByteArray tmpbuf = QByteArray::fromRawData((char *)&msgsize, 1) + QByteArray::fromRawData(msgbuf, msgsize);
       
   146 			if (gameType == gtNet)
   153 			if (gameType == gtNet)
   147 			{
   154 			{
   148 				emit SendNet(tmpbuf);
   155 				emit SendNet(msg);
   149 			}
   156 			}
   150 			demo->append(tmpbuf);
   157 			demo->append(msg);
   151 		}
   158 		}
   152 	}
   159 	}
   153 }
   160 }
   154 
   161 
   155 void HWGame::SendIPC(const char * msg, quint8 len)
   162 void HWGame::SendIPC(const char * msg, quint8 len)
   187 	RawSendIPC(msg);
   194 	RawSendIPC(msg);
   188 }
   195 }
   189 
   196 
   190 void HWGame::ClientRead()
   197 void HWGame::ClientRead()
   191 {
   198 {
   192 	qint64 readbytes = 1;
   199 	readbuffer.append(IPCSocket->readAll());
   193 	while (readbytes > 0)
   200 	quint8 msglen;
   194 	{
   201 	quint32 bufsize;
   195 		if (msgsize == 0)
   202 	while (((bufsize = readbuffer.size()) > 0) &&
   196 		{
   203 			((msglen = readbuffer.data()[0]) < bufsize))
   197 			msgbufsize = 0;
   204 	{
   198 			readbytes = IPCSocket->read((char *)&msgsize, 1);
   205 		QByteArray msg = readbuffer.left(msglen + 1);
   199 		} else
   206 		readbuffer.remove(0, msglen + 1);
   200 		{
   207 		ParseMessage(msg);
   201 			msgbufsize +=
       
   202 			readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
       
   203 			if (msgbufsize = msgsize)
       
   204 			{
       
   205 				ParseMessage();
       
   206 				msgsize = 0;
       
   207 			}
       
   208 		}
       
   209 	}
   208 	}
   210 }
   209 }
   211 
   210 
   212 void HWGame::Start()
   211 void HWGame::Start()
   213 {
   212 {