Ticket #5765: qt4networkcontrol.diff

File qt4networkcontrol.diff, 6.6 KB (added by Xavier Hervy <xavier.hervy@…>, 11 years ago)
  • main.cpp

     
    13561356    if (gContext->GetNumSetting("NetworkControlEnabled", 0))
    13571357    {
    13581358        int networkPort = gContext->GetNumSetting("NetworkControlPort", 6545);
    1359         networkControl = new NetworkControl(networkPort);
    1360         if (!networkControl->ok())
     1359        networkControl = new NetworkControl();
     1360        if (!networkControl->listen(QHostAddress::Any,networkPort))
    13611361            VERBOSE(VB_IMPORTANT,
    13621362                    QString("NetworkControl failed to bind to port %1.")
    13631363                    .arg(networkPort));
  • networkcontrol.h

     
    33
    44#include <pthread.h>
    55
    6 #include <q3serversocket.h>
    7 #include <q3textstream.h>
    8 #include <q3socket.h>
    9 #include <q3valuelist.h>
     6#include <qtcpserver.h>
     7#include <qtextstream.h>
     8#include <qtcpsocket.h>
     9#include <qlinkedlist.h>
    1010#include <qobject.h>
    1111#include <qmutex.h>
    1212#include <qwaitcondition.h>
    1313
    1414class MainServer;
    1515
    16 class NetworkControl : public Q3ServerSocket
     16class NetworkControl : public QTcpServer
    1717{
    1818    Q_OBJECT
    1919  public:
    20     NetworkControl(int port);
     20    NetworkControl();
    2121    ~NetworkControl();
     22    bool listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 );
    2223
    23     void newConnection(int socket);
    24 
    2524  private slots:
     25    void newConnection();
    2626    void readClient();
    27     void discardClient();
     27   
    2828
    2929  protected:
    3030    static void *SocketThread(void *param);
     
    5757    QMap <QString, int> keyMap;
    5858
    5959    QMutex clientLock;
    60     Q3Socket *client;
    61     Q3TextStream *cs;
     60    QTcpSocket *client;
     61    QTextStream *cs;
    6262
    63     Q3ValueList<QString> networkControlCommands;
     63    QLinkedList<QString> networkControlCommands;
    6464    QMutex ncLock;
    6565    QWaitCondition ncCond;
    6666
    67     Q3ValueList<QString> networkControlReplies;
     67    QLinkedList<QString> networkControlReplies;
    6868    QMutex nrLock;
    6969
    7070    pthread_t command_thread;
  • networkcontrol.cpp

     
    55#include <QApplication>
    66#include <QRegExp>
    77#include <QStringList>
    8 #include <q3textstream.h>
     8#include <qtextstream.h>
    99#include <QDir>
    1010#include <QKeyEvent>
    1111#include <QEvent>
     
    4545        return test.lower() == command.left(test.length()).lower();
    4646}
    4747
    48 NetworkControl::NetworkControl(int port)
    49           : Q3ServerSocket(port, 1),
     48NetworkControl::NetworkControl()
     49          : QTcpServer(),
    5050            prompt("# "),
    5151            gotAnswer(false), answer(""),
    5252            client(NULL), cs(NULL)
    5353{
    54     VERBOSE(VB_IMPORTANT, LOC +
    55             QString("Listening for remote connections on port %1").arg(port));
    56 
    5754    // Eventually this map should be in the jumppoints table
    5855    jumpMap["channelpriorities"]     = "Channel Recording Priorities";
    5956    jumpMap["livetv"]                = "Live TV";
     
    196193    pthread_create(&command_thread, NULL, CommandThread, this);
    197194
    198195    gContext->addListener(this);
     196   
     197    connect(this, SIGNAL(newConnection()), this, SLOT(newConnection()));
    199198}
    200199
    201200NetworkControl::~NetworkControl(void)
     
    214213    pthread_join(command_thread, NULL);
    215214}
    216215
     216bool NetworkControl::listen(const QHostAddress & address, quint16 port)
     217{
     218    if (QTcpServer::listen(address,port))
     219    {
     220        VERBOSE(VB_IMPORTANT, LOC +
     221            QString("Listening for remote connections on port %1").arg(port));
     222        return true;
     223    }
     224    return false;
     225}
     226
    217227void *NetworkControl::CommandThread(void *param)
    218228{
    219229    NetworkControl *networkControl = (NetworkControl *)param;
     
    278288    }
    279289}
    280290
    281 void NetworkControl::newConnection(int socket)
     291void NetworkControl::newConnection()
    282292{
    283293    QString welcomeStr = "";
    284294    bool closedOldConn = false;
    285     Q3Socket *s = new Q3Socket(this);
     295    QTcpSocket *s = this->nextPendingConnection();
    286296    connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
    287     connect(s, SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()));
    288     connect(s, SIGNAL(connectionClosed()), this, SLOT(discardClient()));
    289     s->setSocket(socket);
     297    connect(s, SIGNAL(disconnected()), s, SLOT(deleteLater()));
    290298
    291299    VERBOSE(VB_IMPORTANT, LOC +
    292             QString("New connection established. (%1)").arg(socket));
     300            QString("New connection established."));
    293301
    294     Q3TextStream *os = new Q3TextStream(s);
    295     os->setEncoding(Q3TextStream::UnicodeUTF8);
    296 
    297302    QMutexLocker locker(&clientLock);
     303    if (cs)
     304    {
     305        cs->setDevice(s);
     306    }
     307    else
     308    {
     309        cs = new QTextStream(s);
     310        cs->setEncoding(QTextStream::UnicodeUTF8);
     311    }
     312   
    298313    if (client)
    299314    {
    300315        closedOldConn = true;
    301316        client->close();
    302         delete client;
    303         delete cs;
    304317    }
    305 
    306318    client = s;
    307     cs = os;
    308319
    309320    ncLock.lock();
    310321    networkControlCommands.clear();
     
    334345
    335346void NetworkControl::readClient(void)
    336347{
    337     Q3Socket *socket = (Q3Socket *)sender();
     348    QTcpSocket *socket = (QTcpSocket *)sender();
    338349    if (!socket)
    339350        return;
    340351
     
    359370    }
    360371}
    361372
    362 void NetworkControl::discardClient(void)
    363 {
    364     Q3Socket *socket = (Q3Socket *)sender();
    365     if (!socket)
    366         return;
    367 
    368     QMutexLocker locker(&clientLock);
    369     if (client == socket)
    370     {
    371         delete cs;
    372         delete client;
    373         client = NULL;
    374         cs = NULL;
    375     }
    376     else
    377         delete socket;
    378 }
    379 
    380373QString NetworkControl::processJump(QStringList tokens)
    381374{
    382375    QString result = "OK";
     
    899892        nrLock.lock();
    900893        replies = networkControlReplies.size();
    901894        while (client && cs && replies > 0 &&
    902                client->state() == Q3Socket::Connected)
     895               client->state() == QTcpSocket::Connected)
    903896        {
    904897            reply = networkControlReplies.front();
    905898            networkControlReplies.pop_front();
    906899            *cs << reply;
    907900            if (!reply.contains(crlfRegEx) || reply.contains(crlfcrlfRegEx))
    908901                *cs << "\r\n" << prompt;
     902            cs->flush();
    909903            client->flush();
    910904
    911905            replies = networkControlReplies.size();
     
    914908    }
    915909    else if (e->type() == kNetworkControlCloseEvent)
    916910    {
    917         if (client && client->state() == Q3Socket::Connected)
     911        if (client && client->state() == QTcpSocket::Connected)
    918912        {
    919913            clientLock.lock();
    920914            client->close();