123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- GNU General Public License version 3 notice
- Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see < http://www.gnu.org/licenses/ >.
- */
- #include "ServerQueryThread.h"
- #include <QReadWriteLock>
- #include <QThreadPool>
- #include <QThread>
- class Sleeper: public QThread
- {
- public:
- static void msleep(unsigned long msec)
- {
- QThread::msleep(msec);
- }
- };
- ServerQueryThread::ServerQueryThread():
- QRunnable(),
- myStopMutex(new QReadWriteLock()),
- myServerInfoMutex(new QReadWriteLock()),
- myStopThreadFlag(true),
- myThreadRunningFlag(false),
- myServerPlayerCount(0)
- {
- setAutoDelete(false);
- myStopMutex->unlock();
- myServerInfoMutex->unlock();
- }
- ServerQueryThread::~ServerQueryThread()
- {
- delete myStopMutex;
- delete myServerInfoMutex;
- }
- void ServerQueryThread::setAddress(const QHostAddress &address, quint16 port)
- {
- myServerAddress = address;
- myServerPort = port;
- return;
- }
- bool ServerQueryThread::startQueryThread()
- {
- if(!isRunning())
- {
- myStopThreadFlag = false;
- myThreadRunningFlag = true;
- QThreadPool::globalInstance()->start(this);
- return true;
- }
- return false;
- }
- void ServerQueryThread::stopQueryThread()
- {
- myStopMutex->lockForWrite();
- myStopThreadFlag = true;
- myStopMutex->unlock();
- }
- quint8 ServerQueryThread::playerCount() const
- {
- myServerInfoMutex->lockForRead();
- quint8 c = myServerPlayerCount;
- myServerInfoMutex->unlock();
- return c;
- }
- bool ServerQueryThread::isRunning() const
- {
- myStopMutex->lockForRead();
- bool r = myThreadRunningFlag;
- myStopMutex->unlock();
- return r;
- }
- const QHostAddress& ServerQueryThread::serverAddress() const
- {
- return myServerAddress;
- }
- quint16 ServerQueryThread::serverPort() const
- {
- return myServerPort;
- }
- void ServerQueryThread::run()
- {
- ServerQuery* query = new ServerQuery();
- query->setAddress(myServerAddress, myServerPort);
- forever
- {
- /* Handle thread stop request */
- myStopMutex->lockForRead();
- if(myStopThreadFlag)
- {
- myStopMutex->unlock();
- myStopMutex->lockForWrite();
- myThreadRunningFlag = false;
- myStopMutex->unlock();
- delete query;
- return;
- }
- myStopMutex->unlock();
- if(query->query())
- {
- myServerInfoMutex->lockForWrite();
- myServerPlayerCount = query->playerList().size();
- myServerInfoMutex->unlock();
- }
- // else
- // {
- // /* On fail set to 0xff to indicate bad read, so that the client don't act on this */
- // myServerInfoMutex->lockForWrite();
- // myServerPlayerCount = 0xff;
- // myServerInfoMutex->unlock();
- // }
- Sleeper::msleep(1000);
- }
- }
|