QxThread.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef Q_MOC_RUN
  32. #include <QxCommon/QxConfig.h> // Need to include this file for the 'moc' process
  33. #endif // Q_MOC_RUN
  34. #ifdef _QX_ENABLE_QT_NETWORK
  35. #ifndef _QX_SERVICE_THREAD_H_
  36. #define _QX_SERVICE_THREAD_H_
  37. #ifdef _MSC_VER
  38. #pragma once
  39. #endif
  40. /*!
  41. * \file QxThread.h
  42. * \author Lionel Marty
  43. * \ingroup QxService
  44. * \brief Thread to execute a transaction of QxService module
  45. */
  46. #ifdef _QX_NO_PRECOMPILED_HEADER
  47. #ifndef Q_MOC_RUN
  48. #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
  49. #endif // Q_MOC_RUN
  50. #endif // _QX_NO_PRECOMPILED_HEADER
  51. #ifdef QT_NO_OPENSSL
  52. #ifndef QT_NO_SSL
  53. #define QT_NO_SSL /* Nothing */
  54. #endif // QT_NO_SSL
  55. #endif // QT_NO_OPENSSL
  56. #include <QtNetwork/qtcpsocket.h>
  57. #ifndef QT_NO_SSL
  58. #include <QtNetwork/qsslsocket.h>
  59. #include <QtNetwork/qsslconfiguration.h>
  60. #include <QtNetwork/qsslcertificate.h>
  61. #include <QtNetwork/qsslerror.h>
  62. #include <QtNetwork/qsslkey.h>
  63. #endif // QT_NO_SSL
  64. #ifndef Q_MOC_RUN
  65. #include <QxService/QxTransaction.h>
  66. #endif // Q_MOC_RUN
  67. #if (QT_VERSION >= 0x050000)
  68. #define QX_TYPE_SOCKET_DESC qintptr
  69. #else // (QT_VERSION >= 0x050000)
  70. #define QX_TYPE_SOCKET_DESC int
  71. #endif // (QT_VERSION >= 0x050000)
  72. namespace qx {
  73. namespace service {
  74. class QxThreadPool;
  75. /*!
  76. * \ingroup QxService
  77. * \brief qx::service::QxThread : thread to execute a transaction of QxService module
  78. *
  79. * <a href="https://www.qxorm.com/qxorm_en/tutorial_2.html" target="_blank">Click here to access to a tutorial to explain how to work with QxService module.</a>
  80. */
  81. class QX_DLL_EXPORT QxThread : public QObject
  82. {
  83. Q_OBJECT
  84. protected:
  85. QX_TYPE_SOCKET_DESC m_iSocketDescriptor; //!< Socket descriptor to retrieve 'QTcpSocket'
  86. QxThreadPool * m_pThreadPool; //!< Parent thread pool to set available
  87. QThread * m_pThread; //!< Thread where this worker is executed
  88. QxTransaction_ptr m_pTransaction; //!< Current service transaction
  89. bool m_bIsStopped; //!< Set this flag to 'true' to terminate thread
  90. bool m_bIsDisconnected; //!< Socket has been disconnected
  91. QMutex m_mutex; //!< Mutex => 'QxThread' is thread-safe
  92. public:
  93. QxThread(QxThreadPool * pool, QThread * thread) : QObject(), m_iSocketDescriptor(0), m_pThreadPool(pool), m_pThread(thread), m_bIsStopped(false), m_bIsDisconnected(false) { qAssert(m_pThreadPool); qAssert(m_pThread); }
  94. virtual ~QxThread() { clearData(); }
  95. void init();
  96. void stop();
  97. void wait();
  98. bool isAvailable();
  99. void execute(QX_TYPE_SOCKET_DESC socketDescriptor);
  100. protected:
  101. void quit();
  102. void clearData();
  103. bool hasBeenStopped();
  104. void doProcess(QTcpSocket & socket);
  105. bool checkKeepAlive(QTcpSocket & socket);
  106. QX_TYPE_SOCKET_DESC getSocketDescriptor();
  107. #ifndef QT_NO_SSL
  108. bool checkSocketSSLEncrypted(QTcpSocket * socket);
  109. QSslSocket * initSocketSSL();
  110. #endif // QT_NO_SSL
  111. Q_SIGNALS:
  112. void error(const QString & err, qx::service::QxTransaction_ptr transaction);
  113. void transactionStarted(qx::service::QxTransaction_ptr transaction);
  114. void transactionFinished(qx::service::QxTransaction_ptr transaction);
  115. void customRequestHandler(qx::service::QxTransaction_ptr transaction);
  116. void incomingConnection();
  117. void finished();
  118. private Q_SLOTS:
  119. void onCustomRequestHandler();
  120. void onIncomingConnection();
  121. void onSocketDisconnected();
  122. void onSocketReadyRead();
  123. #ifndef QT_NO_SSL
  124. #ifndef QT_NO_OPENSSL
  125. void onSocketSSLEncrypted();
  126. void onSocketSSLErrors(const QList<QSslError> & errors);
  127. void onSocketSSLPeerVerifyError(const QSslError & error);
  128. #endif // QT_NO_OPENSSL
  129. #endif // QT_NO_SSL
  130. };
  131. } // namespace service
  132. } // namespace qx
  133. #endif // _QX_SERVICE_THREAD_H_
  134. #endif // _QX_ENABLE_QT_NETWORK