23 #include "masterclient.h" 26 #include "plugins/engineplugin.h" 27 #include "serverapi/message.h" 28 #include "serverapi/server.h" 29 #include "serverapi/playerslist.h" 30 #include "datapaths.h" 32 #include <QDataStream> 33 #include <QErrorMessage> 36 #include <QMessageBox> 47 QList<ServerPtr> servers;
51 bool isCacheOpenForReading()
const 53 return cache != NULL && cache->isReadable();
56 bool isCacheOpenForWriting()
const 58 return cache != NULL && cache->isWritable();
66 POLYMORPHIC_DEFINE_CONST(QString,
MasterClient, masterBanHelp, (), ());
68 MasterClient::MasterClient()
74 set_masterBanHelp(&MasterClient::masterBanHelp_default);
77 MasterClient::~MasterClient()
92 void MasterClient::clearServers()
99 return (d->address.toIPv4Address() == address.toIPv4Address() && d->port ==
port);
102 void MasterClient::emitBannedMessage()
106 if (!helpMsg.trimmed().isEmpty())
109 msg.
contents(), helpMsg.trimmed()));
117 foreach (ServerPtr server, d->servers)
119 server->disconnect();
130 return plugin()->data()->name;
143 QString MasterClient::masterBanHelp_default()
const 148 void MasterClient::notifyResponse(Response response)
154 case RESPONSE_BANNED:
161 "master because not enough time has past."),
true);
170 tr(
"Could not fetch a new server list. The protocol you are using is too old. " 171 "An update may be available."),
true);
176 int MasterClient::numServers()
const 178 return d->servers.size();
181 ServerPtr MasterClient::operator[](
int index)
const 183 return d->servers[index];
191 bool MasterClient::preparePacketCache(
bool write)
193 if (write ? !d->isCacheOpenForWriting() : !d->isCacheOpenForReading())
202 QString cacheFile = gDefaultDataPaths->programsDataDirectoryPath() +
"/" 203 + QString(
plugin()->data()->name).replace(
' ',
"");
204 d->cache =
new QFile(cacheFile);
211 if(!d->cache->open(write ? QIODevice::WriteOnly|QIODevice::Truncate : QIODevice::ReadOnly))
213 resetPacketCaching();
217 else if (!write && d->isCacheOpenForReading())
223 return d->cache != NULL;
226 void MasterClient::pushPacketToCache(
const QByteArray &data)
228 if(!preparePacketCache(
true))
233 QDataStream strm(d->cache);
234 strm << static_cast<quint16>(data.size());
241 if (response == RESPONSE_GOOD || response == RESPONSE_PENDING)
243 pushPacketToCache(data);
248 void MasterClient::readPacketCache()
250 if(!preparePacketCache(
false))
257 QDataStream strm(d->cache);
267 gLog << tr(
"Reloading master server results from cache for %1!").arg(
plugin()->data()->name);
268 bool hasGood =
false;
274 QByteArray data(size,
'\0');
278 if (response == RESPONSE_GOOD)
282 if(response != RESPONSE_GOOD && response != RESPONSE_PENDING)
303 server->setSelf(server.toWeakRef());
304 d->servers << server;
307 void MasterClient::resetPacketCaching()
320 resetPacketCaching();
325 if(d->address.isNull())
330 if(request.isEmpty())
332 socket->writeDatagram(request, d->address, d->port);
336 const QList<ServerPtr> &MasterClient::servers()
const 346 void MasterClient::setTimeouted(
bool b)
358 emit
message(tr(
"Master server timeout"), tr(
"Connection timeout (%1:%2).")
359 .arg(d->address.toString()).arg(d->port),
true);
370 void MasterClient::updateAddress()
376 QHostInfo info = QHostInfo::fromName(host);
377 if(info.addresses().size() == 0)
380 d->address = info.addresses().first();
381 if (d->address.protocol() != QAbstractSocket::IPv4Protocol)
383 foreach(
const QHostAddress &addr, info.addresses())
385 if(addr.protocol() == QAbstractSocket::IPv4Protocol)
Response readResponse(const QByteArray &data)
Calls readMasterResponse and handles packet caching.
unsigned short port() const
Network port of the master server.
bool sendRequest(QUdpSocket *socket)
Sends request packet through socket.
Message object used to pass messages throughout the Doomseeker's system.
void registerNewServer(ServerPtr server)
Registers new server with this MasterClient.
void masterHost(QString &host, unsigned short &port) const
void message(const QString &title, const QString &content, bool isError)
bool isTimeouted() const
Indicates that the server has timeouted recently.
virtual Response readMasterResponse(const QByteArray &data)=0
Called to read and analyze the response from the MasterServer.
QHostAddress address() const
Address of the master server.
static const unsigned BANNED_FROM_MASTERSERVER
Information indicating that current player is banned from given server.
virtual const EnginePlugin * plugin() const =0
virtual void refreshStarts()
void messageImportant(const Message &message)
Signal used to forward important message to Doomseeker.
void timeoutRefresh()
Times the refreshing process out.
virtual void timeoutRefreshEx()
Reimplement this for clean up purposes.
QString masterBanHelp() const
[Virtual] Help message displayed to the user when ban is detected.
static const unsigned CUSTOM_ERROR
Programmer-defined error message.
bool isAddressSame(const QHostAddress &address, unsigned short port) const
Returns true if the passed address:port is the same as this master server's.
QString contents() const
Customized displayable contents of this Message.
Abstract base for all MasterClients.
QString engineName() const
Extracts engine name from pluginInfo() if available.
virtual QByteArray createServerListRequest()=0
Produce contents of server list request packet that is sent to the master server. ...