QTfrontend/net/tcpBase.cpp
author unc0rr
Tue, 04 Mar 2014 00:40:23 +0400
changeset 10175 c92668840ea8
parent 10108 c68cf030eded
child 10177 0f9042536c46
permissions -rw-r--r--
Remove game/preview functions split in library mode, just let normal procedure run instead: - Remove tons of HWLIBRARY ifdefs - Override ParamCount and ParamStr to let GetParams work - Small fixes here and there
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 533
diff changeset
     2
 * Hedgewars, a free turn based strategy game
486
7ea71cd3acd5 - Change proto version to 4
unc0rr
parents: 443
diff changeset
     3
 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
9998
736015b847e3 update copyright to 2014
sheepluva
parents: 9800
diff changeset
     4
 * Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com>
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
#include <QList>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
#include <QImage>
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    22
#include <QThread>
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    23
#include <QApplication>
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    25
#include "tcpBase.h"
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
#include "hwconsts.h"
8799
44e520374cfc drop QApplication in favor of HWApplication and minor refactor of MessageDialog
koda
parents: 8635
diff changeset
    27
#include "MessageDialog.h"
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    29
#ifdef HWLIBRARY
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    30
extern "C" {
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    31
    void RunEngine(int argc, char ** argv);
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    32
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    33
    int operatingsystem_parameter_argc;
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    34
    char ** operatingsystem_parameter_argv;
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    35
}
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    36
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    37
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    38
EngineInstance::EngineInstance(QObject *parent)
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    39
    : QObject(parent)
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    40
{
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    41
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    42
}
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    43
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    44
EngineInstance::~EngineInstance()
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    45
{
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    46
}
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    47
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    48
void EngineInstance::setArguments(const QStringList & arguments)
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    49
{
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    50
    m_arguments.clear();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    51
    m_arguments << qApp->arguments().at(0).toUtf8();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    52
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    53
    m_argv.resize(arguments.size() + 1);
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    54
    m_argv[0] = m_arguments.last().data();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    55
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    56
    int i = 1;
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    57
    foreach(const QString & s, arguments)
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    58
    {
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    59
        m_arguments << s.toUtf8();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    60
        m_argv[i] = m_arguments.last().data();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    61
        ++i;
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    62
    }
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    63
}
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    64
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    65
void EngineInstance::start()
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    66
{
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
    67
    RunEngine(m_argv.size(), m_argv.data());
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    68
}
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    69
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    70
#endif
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
    71
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
QList<TCPBase*> srvsList;
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
    73
QPointer<QTcpServer> TCPBase::IPCServer(0);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
419
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    75
TCPBase::~TCPBase()
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    76
{
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
    77
    // make sure this object is not in the server list anymore
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
    78
    srvsList.removeOne(this);
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
    79
7289
835bb2852579 Restore almost old TCPBase behaviour
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    80
    if (IPCSocket)
835bb2852579 Restore almost old TCPBase behaviour
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    81
        IPCSocket->deleteLater();
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
    82
419
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    83
}
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    84
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    85
TCPBase::TCPBase(bool demoMode, QObject *parent) :
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    86
    QObject(parent),
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
    87
    m_hasStarted(false),
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    88
    m_isDemoMode(demoMode),
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
    89
    m_connected(false),
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    90
    IPCSocket(0)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    91
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    92
    if(!IPCServer)
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    93
    {
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    94
        IPCServer = new QTcpServer(0);
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    95
        IPCServer->setMaxPendingConnections(1);
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    96
        if (!IPCServer->listen(QHostAddress::LocalHost))
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    97
        {
8799
44e520374cfc drop QApplication in favor of HWApplication and minor refactor of MessageDialog
koda
parents: 8635
diff changeset
    98
            MessageDialog::ShowFatalMessage(tr("Unable to start server at %1.").arg(IPCServer->errorString()));
7794
ab7b94c03bc9 QMessageBox overhaul: unify buttons, style and appearance of info dialogs, fixing a few typos and return values along the way
koda
parents: 7673
diff changeset
    99
            exit(0); // FIXME - should be graceful exit here (lower Critical -> Warning above when implemented)
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   100
        }
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
   101
    }
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   102
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   103
    ipc_port=IPCServer->serverPort();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   105
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   106
void TCPBase::NewConnection()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   108
    if(IPCSocket)
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   109
    {
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   110
        // connection should be already finished
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   111
        return;
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   112
    }
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   113
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   114
    disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   115
    IPCSocket = IPCServer->nextPendingConnection();
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   116
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   117
    if(!IPCSocket) return;
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   118
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   119
    m_connected = true;
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   120
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   121
    connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   122
    connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   123
    SendToClientFirst();
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   124
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   125
    if(simultaneousRun())
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   126
    {
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   127
        srvsList.removeOne(this);
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   128
        emit isReadyNow();
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   129
    }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
void TCPBase::RealStart()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   134
    connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   135
    IPCSocket = 0;
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
   136
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
   137
#ifdef HWLIBRARY
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
   138
    QThread *thread = new QThread();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
   139
    EngineInstance *instance = new EngineInstance();
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
   140
    instance->setArguments(getArguments());
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   141
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   142
    instance->moveToThread(thread);
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   143
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   144
    connect(thread, SIGNAL(started()), instance, SLOT(start(void)));
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   145
    connect(instance, SIGNAL(finished()), thread, SLOT(quit()));
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   146
    connect(instance, SIGNAL(finished()), instance, SLOT(deleteLater()));
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   147
    connect(instance, SIGNAL(finished()), thread, SLOT(deleteLater()));
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8082
diff changeset
   148
    thread->start();
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
   149
#else
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   150
    QProcess * process;
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   151
    process = new QProcess();
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   152
    connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
10175
c92668840ea8 Remove game/preview functions split in library mode, just let normal procedure run instead:
unc0rr
parents: 10108
diff changeset
   153
    QStringList arguments = getArguments();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3157
diff changeset
   154
9212
1d4e1469940b use QT_DEBUG which is always added
koda
parents: 9080
diff changeset
   155
#ifdef QT_DEBUG
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   156
    // redirect everything written on stdout/stderr
8635
c1c6b815691e convert HW_DEV in ifdef DEBUG clauses
koda
parents: 8310
diff changeset
   157
    process->setProcessChannelMode(QProcess::ForwardedChannels);
c1c6b815691e convert HW_DEV in ifdef DEBUG clauses
koda
parents: 8310
diff changeset
   158
#endif
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   159
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   160
    process->start(bindir->absolutePath() + "/hwengine", arguments);
8082
675372256a01 lotsa hackery to get frontend somehow link libengine...
koda
parents: 8070
diff changeset
   161
#endif
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   162
    m_hasStarted = true;
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   163
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   164
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   165
void TCPBase::ClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   166
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   167
    disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   168
    onClientDisconnect();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   169
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   170
    if(!simultaneousRun())
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   171
        emit isReadyNow();
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   172
    IPCSocket->deleteLater();
7673
e06b83cbde74 fix a segfault related to tcp servers deleting themselves without removing themselves from the internal server list first.
sheepluva
parents: 7615
diff changeset
   173
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   174
    deleteLater();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   175
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   176
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   177
void TCPBase::ClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   178
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   179
    QByteArray readed=IPCSocket->readAll();
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   180
    if(readed.isEmpty()) return;
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   181
    readbuffer.append(readed);
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   182
    onClientRead();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   183
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   185
void TCPBase::StartProcessError(QProcess::ProcessError error)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   186
{
8799
44e520374cfc drop QApplication in favor of HWApplication and minor refactor of MessageDialog
koda
parents: 8635
diff changeset
   187
    MessageDialog::ShowFatalMessage(tr("Unable to run engine at %1\nError code: %2").arg(bindir->absolutePath() + "/hwengine").arg(error));
7587
688076d61b83 Don't leave frontend in engine waiting state when there's error occured on attempt to start engine
unc0rr
parents: 6952
diff changeset
   188
    ClientDisconnect();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   189
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   190
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   191
void TCPBase::tcpServerReady()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   192
{
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   193
    disconnect(srvsList.first(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   194
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   195
    RealStart();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   196
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   197
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   198
void TCPBase::Start(bool couldCancelPreviousRequest)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   199
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   200
    if(srvsList.isEmpty())
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   201
    {
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   202
        srvsList.push_back(this);
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   203
        RealStart();
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   204
    }
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   205
    else
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   206
    {
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
   207
        TCPBase * last = srvsList.last();
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
   208
        if(couldCancelPreviousRequest
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
   209
            && last->couldBeRemoved()
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   210
            && (last->isConnected() || !last->hasStarted())
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
   211
            && (last->parent() == parent()))
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   212
        {
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
   213
            srvsList.removeLast();
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   214
            last->deleteLater();
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   215
            Start(couldCancelPreviousRequest);
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   216
        } else
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   217
        {
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   218
            connect(last, SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   219
            srvsList.push_back(this);
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   220
        }
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   221
    }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   222
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   223
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   224
void TCPBase::onClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   225
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   226
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   227
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   228
void TCPBase::onClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   229
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   230
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   231
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   232
void TCPBase::SendToClientFirst()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   233
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   234
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   235
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   236
void TCPBase::SendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   237
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   238
    if (buf.size() > MAXMSGCHARS) return;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   239
    quint8 len = buf.size();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   240
    RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   241
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   242
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   243
void TCPBase::RawSendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   244
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   245
    if (!IPCSocket)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   246
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   247
        toSendBuf += buf;
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   248
    }
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   249
    else
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   250
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   251
        if (toSendBuf.size() > 0)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   252
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   253
            IPCSocket->write(toSendBuf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   254
            if(m_isDemoMode) demo.append(toSendBuf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   255
            toSendBuf.clear();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   256
        }
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   257
        if(!buf.isEmpty())
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   258
        {
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   259
            IPCSocket->write(buf);
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   260
            if(m_isDemoMode) demo.append(buf);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   261
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   262
    }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   263
}
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   264
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   265
bool TCPBase::couldBeRemoved()
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   266
{
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   267
    return false;
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7794
diff changeset
   268
}
9800
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   269
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   270
bool TCPBase::isConnected()
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   271
{
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   272
    return m_connected;
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   273
}
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   274
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   275
bool TCPBase::simultaneousRun()
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   276
{
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   277
    return false;
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   278
}
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   279
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   280
bool TCPBase::hasStarted()
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   281
{
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   282
    return m_hasStarted;
169fbb968bb3 No idea what this is doing, seems to allow recorder instance to not block further engine starts while it is doing its job.
unc0rr
parents: 9212
diff changeset
   283
}