ServerQueryThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. {
  34. setAutoDelete(false);
  35. }
  36. ServerQueryThread::~ServerQueryThread()
  37. {
  38. delete myStopMutex;
  39. delete myServerInfoMutex;
  40. }
  41. void ServerQueryThread::setAddress(const QHostAddress &address, quint16 port)
  42. {
  43. myServerAddress = address;
  44. myServerPort = port;
  45. return;
  46. }
  47. bool ServerQueryThread::startQueryThread()
  48. {
  49. if(!isRunning())
  50. {
  51. QThreadPool::globalInstance()->start(this);
  52. myStopThreadFlag = false;
  53. myThreadRunningFlag = true;
  54. return true;
  55. }
  56. return false;
  57. }
  58. void ServerQueryThread::stopQueryThread()
  59. {
  60. myStopMutex->lockForWrite();
  61. myStopThreadFlag = true;
  62. myStopMutex->unlock();
  63. }
  64. quint8 ServerQueryThread::playerCount() const
  65. {
  66. myServerInfoMutex->lockForRead();
  67. quint8 c = myServerPlayerCount;
  68. myServerInfoMutex->unlock();
  69. return c;
  70. }
  71. bool ServerQueryThread::isRunning() const
  72. {
  73. myStopMutex->lockForRead();
  74. bool r = myThreadRunningFlag;
  75. myStopMutex->unlock();
  76. return r;
  77. }
  78. const QHostAddress& ServerQueryThread::serverAddress() const
  79. {
  80. return myServerAddress;
  81. }
  82. quint16 ServerQueryThread::serverPort() const
  83. {
  84. return myServerPort;
  85. }
  86. void ServerQueryThread::run()
  87. {
  88. ServerQuery* query = new ServerQuery();
  89. query->setAddress(myServerAddress, myServerPort);
  90. forever
  91. {
  92. /* Handle thread stop request */
  93. myStopMutex->lockForRead();
  94. if(myStopThreadFlag)
  95. {
  96. myThreadRunningFlag = false;
  97. myStopMutex->unlock();
  98. delete query;
  99. return;
  100. }
  101. myStopMutex->unlock();
  102. if(query->query())
  103. {
  104. myServerInfoMutex->lockForWrite();
  105. myServerPlayerCount = query->playerList().size();
  106. myServerInfoMutex->unlock();
  107. }
  108. else
  109. {
  110. /* On fail set to 0xff to indicate bad read, so that the client don't act on this */
  111. myServerInfoMutex->lockForWrite();
  112. myServerPlayerCount = 0xff;
  113. myServerInfoMutex->unlock();
  114. }
  115. Sleeper::msleep(1000);
  116. }
  117. }