QTfrontend/tcpBase.cpp
author displacer
Sat, 14 Oct 2006 13:13:51 +0000
changeset 197 8a7123b84d1b
parent 186 7157b34a24d6
child 291 afa378f1e961
permissions -rw-r--r--
multiple classes accept signal receive bugfix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     2
 * Hedgewars, a worms-like game
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     3
 * Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     4
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * along with this program; if not, write to the Free Software
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    17
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
#include "tcpBase.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
#include <QMessageBox>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    22
#include <QList>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    23
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
#include <QImage>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    25
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
#include "hwconsts.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    27
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
QList<TCPBase*> srvsList;
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    29
QTcpServer* TCPBase::IPCServer(0);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    30
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
TCPBase::TCPBase(bool demoMode) :
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    32
  m_isDemoMode(demoMode),
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    33
  IPCSocket(0)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    34
{
186
7157b34a24d6 unneded variable removed
displacer
parents: 185
diff changeset
    35
  if(!IPCServer) {
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    36
    IPCServer = new QTcpServer(this);
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    37
    IPCServer->setMaxPendingConnections(1);
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    38
    if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    39
      QMessageBox::critical(0, tr("Error"),
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    40
			    tr("Unable to start the server: %1.")
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    41
			    .arg(IPCServer->errorString()));
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    42
    }
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    43
  }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    44
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    45
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    46
void TCPBase::NewConnection()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    47
{
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    48
  if(IPCSocket) {
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    49
    // connection should be already finished
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    50
    return;
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    51
  }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
  QTcpSocket * client = IPCServer->nextPendingConnection();
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    53
  if(!client) return;
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    54
  IPCSocket = client;
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    55
  connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    56
  connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    57
  SendToClientFirst();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    58
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    59
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    60
void TCPBase::RealStart()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    61
{
197
8a7123b84d1b multiple classes accept signal receive bugfix
displacer
parents: 186
diff changeset
    62
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    63
  IPCSocket = 0;
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    64
  
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    65
  QProcess * process;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    66
  process = new QProcess;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    67
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    68
  QStringList arguments=setArguments();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    69
  process->start(bindir->absolutePath() + "/hwengine", arguments);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    70
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    71
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
void TCPBase::ClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    73
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
  IPCSocket->close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    75
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    76
  onClientDisconnect();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    77
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    78
  readbuffer.clear();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    79
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    80
  if(srvsList.size()==1) srvsList.pop_front();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    81
  emit isReadyNow();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    82
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    83
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    84
void TCPBase::ClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    85
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    86
  readbuffer.append(IPCSocket->readAll());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    87
  onClientRead();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    88
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    89
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    90
void TCPBase::StartProcessError(QProcess::ProcessError error)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    91
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    92
  QMessageBox::critical(0, tr("Error"),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    93
			tr("Unable to run engine: %1 (")
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
			.arg(error) + bindir->absolutePath() + "/hwengine)");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    96
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    97
void TCPBase::tcpServerReady()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    98
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    99
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   100
  srvsList.pop_front();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   101
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
   102
  RealStart();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   103
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   105
void TCPBase::Start()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   106
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
  if(srvsList.isEmpty()) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   108
    srvsList.push_back(this);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   109
  } else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   110
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   111
    srvsList.push_back(this);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   112
    return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   113
  }
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
   114
  
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   115
  RealStart();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   116
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   117
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   118
void TCPBase::onClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   119
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   120
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
void TCPBase::onClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   123
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   125
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
void TCPBase::SendToClientFirst()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   129
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
void TCPBase::SendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
	if (buf.size() > MAXMSGCHARS) return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
	quint8 len = buf.size();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   134
	RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   135
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   136
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   137
void TCPBase::RawSendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   138
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   139
	if (!IPCSocket)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   140
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   141
		toSendBuf += buf;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   142
	} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   143
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   144
		if (toSendBuf.size() > 0)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   145
		{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   146
			IPCSocket->write(toSendBuf);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   147
			if(m_isDemoMode) demo->append(toSendBuf);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   148
			toSendBuf.clear();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   149
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   150
		IPCSocket->write(buf);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   151
		if(m_isDemoMode) demo->append(buf);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   152
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   153
}