refresher.h
1 //------------------------------------------------------------------------------
2 // refresher.h
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef DOOMSEEKER_REFRESHER_REFRESHER_H
24 #define DOOMSEEKER_REFRESHER_REFRESHER_H
25 
26 #include <QObject>
27 #include <QMutex>
28 
29 #define gRefresher (Refresher::instance())
30 
31 class MasterClient;
32 class Server;
33 class QHostAddress;
34 
35 class Refresher : public QObject
36 {
37  Q_OBJECT
38 
39  public:
40  ~Refresher();
41 
46  void quit();
47 
53  void registerMaster(MasterClient* pMaster);
54 
63  bool registerServer(Server* server);
64 
69  void setDelayBetweenResends(int delay);
70 
71  bool start();
72 
73  static Refresher *instance();
74  static bool isInstantiated();
75  static void deinstantiate();
76 
77  signals:
81  void block();
82 
83  void finishedQueryingMaster(MasterClient* master);
84 
89  void sleepingModeEnter();
90 
95  void sleepingModeExit();
96 
97  private:
98  class Data;
99  class MasterClientInfo;
100 
101  static const int MASTER_SERVER_TIMEOUT_DELAY = 10000;
102  static Refresher *staticInstance;
103  static QMutex instanceMutex;
104 
105  Data *d;
106 
107  Refresher();
108 
109  void concludeRefresh();
110  bool isAnythingToRefresh() const;
111  Server* findRefreshingServer(const QHostAddress& address, unsigned short port);
112 
113  void purgeNullServers();
114 
115  void readPendingDatagram();
116 
117  void startNewServerRefresh();
118  void resendCurrentServerRefreshesIfTimeout();
119 
120  // TODO: Constify 'address' and 'packet' args.
125  bool tryReadDatagramByMasterClient(QHostAddress& address,
126  unsigned short port, QByteArray& packet);
131  bool tryReadDatagramByServer(const QHostAddress& address,
132  unsigned short port, QByteArray& packet);
133 
134  void unregisterMaster(MasterClient* pMaster);
135 
136  private slots:
137  void attemptTimeoutMasters();
138  void masterFinishedRefreshing();
139  void readAllPendingDatagrams();
140  void sendMasterQueries();
141  void sendServerQueries();
142 };
143 
144 #endif
void quit()
Definition: refresher.cpp:255
void block()
void sleepingModeExit()
void setDelayBetweenResends(int delay)
Definition: refresher.cpp:377
A representation of a server for a given game.
Definition: server.h:93
void registerMaster(MasterClient *pMaster)
Definition: refresher.cpp:260
void sleepingModeEnter()
bool registerServer(Server *server)
Definition: refresher.cpp:283
Abstract base for all MasterClients.
Definition: masterclient.h:49