Results 1 to 2 of 2

Thread: Multithreaded Server, Timer in QThread, Thread IDs questions

  1. #1
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Multithreaded Server, Timer in QThread, Thread IDs questions

    Hello, in order to understand the multithreaded applications deeper i have some questions:

    I have an application (server accepting clients trough SSL opening new thread for every client). Every thread makes new PSQL connection cloned from main connection with unique name.

    I needed to make a QTimer to disconnect idled connections and threads so i open the new Thread with parent 0 and i call moveToThread( this ) in the thread`s constructor. The timer starts and resets in the readData() slot called from the readyRead() signal of the QSSlSocket which i open in the run() function of the inherited QThread class.

    When i call currentThreadId() after moveToThread(this) the ID is the main thread`s ID... but called from run() method it shows new thread ID. When i call quit or exit the destructor of the inherited class is never called but the slot of thread`s finished signal is called and the thread id there is again main thread`s id.

    My questions are...
    1. Why in the thread`s constructor the currentThreadID is main thread`s id after moveToThread(this). ( i read that QThread`s object lives in the thread which is calling it so i assume it`s normal showing the main thread`s ID but am i doing it right?)
    2. Why the destructor of the QThread is not called (i recall a time when i saw it running before i fiddle with the QTimer`s stuff). The thread is closing because i watch it with OllyDbg but i still don`t understand the process.
    3. Is it ok to use Qt:irectConnection in this:
    Qt Code:
    1. connect(serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSlot(QAbstractSocket::SocketError)), Qt::DirectConnection);
    2. connect(serverSocket, SIGNAL(disconnected()), this, SLOT(connectionClosed()), Qt::DirectConnection);
    3. connect(serverSocket, SIGNAL(readyRead()), this, SLOT(readData()), Qt::DirectConnection);
    4. connect(serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)), Qt::DirectConnection);
    5. connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()), Qt::DirectConnection);
    To copy to clipboard, switch view to plain text mode 
    because IF i don`t i have to use qRegisterMetaType for the types in the SLOT() and i`m getting really weird random crashes.

    Here some code:

    SSLServer.cpp

    Qt Code:
    1. #include "SSLserver.h"
    2.  
    3. SSLServer::SSLServer(quint16 port, QObject* parent)
    4. : QTcpServer(parent)
    5. {
    6. if(!listen(QHostAddress::Any, port))
    7. {
    8. QMessageBox::critical(0, "Error!", tr("Server cannot listen at port %1!").arg(port), QMessageBox::Ok);
    9. exit(5);
    10. }
    11. }
    12.  
    13. void SSLServer::incomingConnection(int socketDescriptor)
    14. {
    15. qDebug() << "OPening new Thread from sslserver TID: " << QThread::currentThreadId();// SHOW MAIN THREAD ID
    16. thread = new SSLThread(socketDescriptor, 0);// HERE OPEN NEW THREAD WITH PARENT 0
    17. connect(thread, SIGNAL(finished()), this, SLOT(fin()));
    18. connect(thread, SIGNAL(messageToGui(QString)), QObject::parent(), SLOT(writeToLogs(QString)));
    19. connect(thread, SIGNAL(addRegisteredUser(QString)), QObject::parent(), SLOT(addUserToList(QString)));
    20. connect(thread, SIGNAL(delRegisteredUser(QString)), QObject::parent(), SLOT(removeUserFromList(QString)));
    21. thread->start();
    22. }
    23.  
    24. void SSLServer::fin()
    25. {
    26. qDebug() << "Closing Thread ID (from sslserver): " << thread->currentThreadId();// HERE SHOWS MAIN THREAD ID
    27. thread->deleteLater();
    28. }
    To copy to clipboard, switch view to plain text mode 

    SSLThread.cpp

    Qt Code:
    1. #include "SSLthread.h"
    2. #include <QtNetwork>
    3.  
    4. SSLThread::SSLThread(int socketDr, QObject* parent) : QThread(parent)
    5. {
    6. moveToThread( this );
    7. socketDescriptor = socketDr;
    8. .....
    9. qDebug() << "ThreadID from constructor: " << currentThreadId();//SHOWS MAIN THREAD ID ??????
    10. }
    11.  
    12. SSLThread::~SSLThread()
    13. {
    14. qDebug() << ">>>Destructor<<<";
    15. //NEVER CALLED
    16. }
    17.  
    18. void SSLThread::run()
    19. {
    20. qDebug() << "ThreadID: " << currentThreadId();//SHOWS NEW THREAD ID
    21. serverSocket = new QSslSocket();
    22. serverSocket->setProtocol(QSsl::AnyProtocol);
    23. serverSocket->setPeerVerifyMode(QSslSocket::VerifyNone);
    24. serverSocket->setDefaultCiphers(serverSocket->supportedCiphers());
    25. connect(serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSlot(QAbstractSocket::SocketError)), Qt::DirectConnection);
    26. connect(serverSocket, SIGNAL(disconnected()), this, SLOT(connectionClosed()), Qt::DirectConnection);
    27. connect(serverSocket, SIGNAL(readyRead()), this, SLOT(readData()), Qt::DirectConnection);
    28. connect(serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)), Qt::DirectConnection);
    29. connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()), Qt::DirectConnection);
    30. //connect(serverSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)), Qt::DirectConnection);
    31. connect(serverSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)), Qt::DirectConnection);
    32. timerTimeout = new QTimer(this);timerTimeout->setSingleShot(true);
    33. connect(timerTimeout, SIGNAL(timeout()), this, SLOT(timeOut()));
    34.  
    35.  
    36. .....(reading certificates and running stuff)
    37. exec();
    38. }
    39.  
    40. void SSLThread::connectionClosed()
    41. {
    42. qDebug() << "ThreadID: " << currentThreadId();
    43. QSqlDatabase db = QSqlDatabase::database(uid);
    44. if (db.isOpen())
    45. db.close();
    46. if (serverSocket->state() == QAbstractSocket::ClosingState)
    47. {
    48. connect(serverSocket, SIGNAL(disconnected()), SLOT(deleteLater()));
    49. }
    50. else
    51. {
    52. serverSocket->deleteLater();
    53. }
    54. emit delRegisteredUser(authUserName);
    55. emit messageToGui(tr("%1 %2 quits the system.").arg(QTime::currentTime().toString()).arg(authUserName));
    56. authUserName.clear();
    57. if(timerTimeout)
    58. timerTimeout->deleteLater();
    59. qDebug("Connection closed.");
    60. exit(0);
    61. }
    62.  
    63. void SSLThread::timeOut()
    64. {
    65. if(timerTimeout->isActive())
    66. timerTimeout->stop();
    67.  
    68. serverSocket->close();
    69. qDebug() << "ThreadID: " << currentThreadId();
    70. }
    To copy to clipboard, switch view to plain text mode 

    Now the server is running fine.. it accepts connections, reads all the data, disconnects the clients fine... but i`m worried if i`m doing things properly. Also i`m worried if everything is cleaned properly... it`s important because that server should be 24/7 and memory leaks are not very welcome. Thank you in advance!
    Last edited by sadjoker; 23rd April 2010 at 12:27.

  2. #2
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreaded Server, Timer in QThread, Thread IDs questions

    Changing

    Qt Code:
    1. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 

    into SSLServer.cpp fixed a bug with not executing the timers of the next threads.
    Last edited by sadjoker; 23rd April 2010 at 13:33.

Similar Threads

  1. Multithreaded Server
    By niol1000 in forum Newbie
    Replies: 1
    Last Post: 24th February 2010, 17:33
  2. how to enable a timer in a non-gui thread?
    By zeopha in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 09:29
  3. Replies: 4
    Last Post: 26th June 2008, 18:41
  4. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  5. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 14:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.