ServerQuery.cpp 8.2 KB

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