ServerQuery.cpp 6.8 KB

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