ServerQuery.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "ServerQuery.h"
  16. #include <QUdpSocket>
  17. #include <QHostInfo>
  18. #include <QStringList>
  19. #include <QRegExp>
  20. #include <QTime>
  21. ServerQuery::ServerQuery(QObject *parent) :
  22. QObject(parent),
  23. mySocket(new QUdpSocket(this)),
  24. myPort(27500),
  25. myActiveFlag(false)
  26. {
  27. connect(mySocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
  28. }
  29. ServerQuery::~ServerQuery()
  30. {
  31. delete mySocket;
  32. }
  33. void ServerQuery::run()
  34. {
  35. if(mySocket->hasPendingDatagrams())
  36. parseServerInfo();
  37. }
  38. bool ServerQuery::query(bool pingServer)
  39. {
  40. myActiveFlag = true;
  41. if(myAddress.isNull() || !myPort)
  42. {
  43. emit error(HostLookupError);
  44. myActiveFlag = false;
  45. return false;
  46. }
  47. if(mySocket->isOpen())
  48. mySocket->close();
  49. mySocket->connectToHost(myAddress, myPort);
  50. if(!mySocket->waitForConnected(100))
  51. {
  52. emit error(ConnectionError);
  53. myActiveFlag = false;
  54. return false;
  55. }
  56. if(mySocket->write("\xff\xff\xff\xffstatus 23\n", 14) == -1)
  57. {
  58. emit error(SendError);
  59. myActiveFlag = false;
  60. return false;
  61. }
  62. if(!mySocket->waitForBytesWritten(300))
  63. {
  64. emit error(SendError);
  65. myActiveFlag = false;
  66. return false;
  67. }
  68. if(pingServer)
  69. myPing = ping(10); //avg ping
  70. else
  71. myPing = 0;
  72. return true;
  73. }
  74. const QHostAddress &ServerQuery::address() const
  75. {
  76. return myAddress;
  77. }
  78. quint16 ServerQuery::port() const
  79. {
  80. return myPort;
  81. }
  82. bool ServerQuery::setAddress(const QHostAddress &address, quint16 port)
  83. {
  84. myAddress = address;
  85. myPort = port;
  86. return true;
  87. }
  88. bool ServerQuery::setAddress(const QString &address, quint16 port)
  89. {
  90. QHostInfo hi = QHostInfo::fromName(address);
  91. if(hi.error() != QHostInfo::NoError)
  92. {
  93. emit error(HostLookupError);
  94. return false;
  95. }
  96. myAddress = hi.addresses().at(0);
  97. myPort = port;
  98. return true;
  99. }
  100. void ServerQuery::parseServerInfo()
  101. {
  102. QByteArray serverData = mySocket->readAll();
  103. if(serverData.isEmpty())
  104. {
  105. emit error(EmptyResponseError);
  106. myActiveFlag = false;
  107. return;
  108. }
  109. if(!serverData.startsWith("\xff\xff\xff\xffn"))
  110. {
  111. emit error(InvalidResponseError);
  112. myActiveFlag = false;
  113. return;
  114. }
  115. /* Parse server rules */
  116. myRules.clear();
  117. QString infoString(serverData.data()+5);
  118. QStringList splitInfoString = infoString.split("\n", QString::SkipEmptyParts);
  119. int i;
  120. QStringList rules = splitInfoString.at(0).split("\\", QString::SkipEmptyParts);
  121. if((rules.size() % 2) != 0)
  122. {
  123. emit error(InvalidInfoStringError);
  124. myActiveFlag = false;
  125. return;
  126. }
  127. ServerRule rule;
  128. for(i = 0; i < rules.size(); i += 2)
  129. {
  130. rule.rule = rules.at(i);
  131. rule.value = rules.at(i+1);
  132. /* Proxy detection */
  133. if(rule.rule == "*QTV")
  134. {
  135. myIsProxyFlag = true;
  136. }
  137. else if(rule.rule == "*version")
  138. {
  139. QString value = rule.value;
  140. if(value.startsWith("qwfwd", Qt::CaseInsensitive) || value.startsWith("QTV", Qt::CaseInsensitive) || value.startsWith("2.91"))
  141. myIsProxyFlag = true;
  142. }
  143. /* Adjust FPS */
  144. if(rule.rule == "maxfps" && myPing > 0)
  145. myPing += rule.value.toUInt() * 12 / 77;
  146. myRules.append(rule);
  147. }
  148. /* Parse player info */
  149. myPlayers.clear();
  150. Player player;
  151. QRegExp regex("^([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+\"([^\"]*)\"\\s+\"([^\"]*)\"\\s+([0-9]+)\\s+([0-9]+)(?:\\s+\"([^\"]*)\")?$");
  152. QStringList playerInfo;
  153. for(i = 1; i < splitInfoString.size(); i++)
  154. {
  155. if(regex.indexIn(splitInfoString.at(i)) == -1)
  156. {
  157. emit error(InvalidPlayerInfoError);
  158. continue;
  159. }
  160. playerInfo = regex.capturedTexts();
  161. player.id = playerInfo.at(1).toInt();
  162. player.frags = playerInfo.at(2).toInt();
  163. player.time = playerInfo.at(3).toInt();
  164. player.ping = playerInfo.at(4).toInt();
  165. player.name = convertNameFun(playerInfo.at(5));
  166. player.skin = playerInfo.at(6);
  167. player.topColor = playerInfo.at(7).toInt();
  168. player.bottomColor = playerInfo.at(8).toInt();
  169. player.team = convertNameFun(playerInfo.at(9));
  170. if(player.name.startsWith("\\s\\"))
  171. {
  172. player.spectator = true;
  173. player.team = "(spec)";
  174. player.name = player.name.replace(QRegExp("^\\\\s\\\\"), "");
  175. if(player.ping < 0)
  176. player.ping = -player.ping;
  177. }
  178. else
  179. {
  180. player.spectator = false;
  181. }
  182. myPlayers.append(player);
  183. }
  184. myActiveFlag = false;
  185. emit finished();
  186. return;
  187. }
  188. bool ServerQuery::isActive() const
  189. {
  190. return myActiveFlag;
  191. }
  192. void ServerQuery::socketError(QAbstractSocket::SocketError)
  193. {
  194. emit error(UnknownError);
  195. myActiveFlag = false;
  196. }
  197. int ServerQuery::ping(int count, int timeout)
  198. {
  199. int pong = 0;
  200. QTime timer;
  201. timer.start();
  202. for(int i = 0; i < count; i++)
  203. {
  204. mySocket->write("\xff\xff\xff\xffk\n");
  205. timer.restart();
  206. if(!mySocket->waitForReadyRead(timeout))
  207. {
  208. pong += 999;
  209. }
  210. else
  211. {
  212. if(mySocket->readAll() == "l")
  213. pong += timer.elapsed();
  214. else
  215. pong += 999;
  216. }
  217. }
  218. return pong/count;
  219. }
  220. PlayerList ServerQuery::playerList() const
  221. {
  222. return myPlayers;
  223. }
  224. ServerRules ServerQuery::serverRules() const
  225. {
  226. return myRules;
  227. }
  228. QString ServerQuery::serverRuleValue(const QString &ruleName) const
  229. {
  230. ServerRule r;
  231. foreach(r, myRules)
  232. {
  233. if(r.rule == ruleName)
  234. return r.value;
  235. }
  236. return QString();
  237. }
  238. //========================================================================
  239. /* Name fun */
  240. char ServerQuery::ourReadableCharsTable[256] = { '.', '_' , '_' , '_' , '_' , '.' , '_' , '_' , '_' , '_' , '\n' , '_' , '\n' , '>' , '.' , '.',
  241. '[', ']', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', '_', '_'
  242. };
  243. bool ServerQuery::ourReadableCharsTableInitialized = false;
  244. void ServerQuery::fillReadableCharsTable()
  245. {
  246. int i;
  247. for(i = 32; i < 127; i++)
  248. ourReadableCharsTable[i] = ourReadableCharsTable[128 + i] = i;
  249. ourReadableCharsTable[127] = ourReadableCharsTable[128 + 127] = '_';
  250. for(i = 0; i < 32; i++)
  251. ourReadableCharsTable[128 + i] = ourReadableCharsTable[i];
  252. ourReadableCharsTable[128] = '_';
  253. ourReadableCharsTable[10 + 128] = '_';
  254. ourReadableCharsTable[12 + 128] = '_';
  255. ourReadableCharsTableInitialized = true;
  256. }
  257. QString ServerQuery::convertNameFun(const QString &name)
  258. {
  259. if(!ourReadableCharsTableInitialized)
  260. fillReadableCharsTable();
  261. QString stripped;
  262. for(int i = 0; i < name.length(); i++)
  263. stripped.append(QChar(ourReadableCharsTable[(unsigned char)name.at(i).toAscii()] & 127));
  264. return stripped;
  265. }