ServerQuery.cpp 8.3 KB

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