ServerQueryThread.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "ServerQueryThread.h"
  16. #include <QReadWriteLock>
  17. #include <QThreadPool>
  18. #include <QThread>
  19. class Sleeper: public QThread
  20. {
  21. public:
  22. static void msleep(unsigned long msec)
  23. {
  24. QThread::msleep(msec);
  25. }
  26. };
  27. ServerQueryThread::ServerQueryThread():
  28. QRunnable(),
  29. myStopMutex(new QReadWriteLock()),
  30. myServerInfoMutex(new QReadWriteLock()),
  31. myStopThreadFlag(true),
  32. myThreadRunningFlag(false),
  33. myServerPlayerCount(0)
  34. {
  35. setAutoDelete(false);
  36. myStopMutex->unlock();
  37. myServerInfoMutex->unlock();
  38. }
  39. ServerQueryThread::~ServerQueryThread()
  40. {
  41. delete myStopMutex;
  42. delete myServerInfoMutex;
  43. }
  44. void ServerQueryThread::setAddress(const QHostAddress &address, quint16 port)
  45. {
  46. myServerAddress = address;
  47. myServerPort = port;
  48. return;
  49. }
  50. bool ServerQueryThread::startQueryThread()
  51. {
  52. if(!isRunning())
  53. {
  54. myStopThreadFlag = false;
  55. myThreadRunningFlag = true;
  56. QThreadPool::globalInstance()->start(this);
  57. return true;
  58. }
  59. return false;
  60. }
  61. void ServerQueryThread::stopQueryThread()
  62. {
  63. myStopMutex->lockForWrite();
  64. myStopThreadFlag = true;
  65. myStopMutex->unlock();
  66. }
  67. quint8 ServerQueryThread::playerCount() const
  68. {
  69. myServerInfoMutex->lockForRead();
  70. quint8 c = myServerPlayerCount;
  71. myServerInfoMutex->unlock();
  72. return c;
  73. }
  74. bool ServerQueryThread::isRunning() const
  75. {
  76. myStopMutex->lockForRead();
  77. bool r = myThreadRunningFlag;
  78. myStopMutex->unlock();
  79. return r;
  80. }
  81. const QHostAddress& ServerQueryThread::serverAddress() const
  82. {
  83. return myServerAddress;
  84. }
  85. quint16 ServerQueryThread::serverPort() const
  86. {
  87. return myServerPort;
  88. }
  89. void ServerQueryThread::run()
  90. {
  91. ServerQuery* query = new ServerQuery();
  92. query->setAddress(myServerAddress, myServerPort);
  93. forever
  94. {
  95. /* Handle thread stop request */
  96. myStopMutex->lockForRead();
  97. if(myStopThreadFlag)
  98. {
  99. myStopMutex->unlock();
  100. myStopMutex->lockForWrite();
  101. myThreadRunningFlag = false;
  102. myStopMutex->unlock();
  103. delete query;
  104. return;
  105. }
  106. myStopMutex->unlock();
  107. if(query->query())
  108. {
  109. myServerInfoMutex->lockForWrite();
  110. myServerPlayerCount = query->playerList().size();
  111. myServerInfoMutex->unlock();
  112. }
  113. // else
  114. // {
  115. // /* On fail set to 0xff to indicate bad read, so that the client don't act on this */
  116. // myServerInfoMutex->lockForWrite();
  117. // myServerPlayerCount = 0xff;
  118. // myServerInfoMutex->unlock();
  119. // }
  120. Sleeper::msleep(1000);
  121. }
  122. }