ServerQuery.cpp 8.3 KB

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