ServerQuery.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(!mySocket->waitForBytesWritten(300))
  64. {
  65. emit error(SendError);
  66. myActiveFlag = false;
  67. return false;
  68. }
  69. if(pingServer)
  70. myPing = ping(10); //avg ping
  71. else
  72. myPing = 0;
  73. return true;
  74. }
  75. const QHostAddress &ServerQuery::address() const
  76. {
  77. return myAddress;
  78. }
  79. quint16 ServerQuery::port() const
  80. {
  81. return myPort;
  82. }
  83. bool ServerQuery::setAddress(const QHostAddress &address, quint16 port)
  84. {
  85. myAddress = address;
  86. myPort = port;
  87. return true;
  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. void ServerQuery::parseServerInfo()
  102. {
  103. QByteArray serverData = mySocket->readAll();
  104. if(serverData.isEmpty())
  105. {
  106. emit error(EmptyResponseError);
  107. myActiveFlag = false;
  108. return;
  109. }
  110. if(!serverData.startsWith("\xff\xff\xff\xffn"))
  111. {
  112. emit error(InvalidResponseError);
  113. myActiveFlag = false;
  114. return;
  115. }
  116. /* Parse server rules */
  117. myRules.clear();
  118. QString infoString(serverData.data()+5);
  119. QStringList splitInfoString = infoString.split("\n", QString::SkipEmptyParts);
  120. int i;
  121. QStringList rules = splitInfoString.at(0).split("\\", QString::SkipEmptyParts);
  122. if((rules.size() % 2) != 0)
  123. {
  124. emit error(InvalidInfoStringError);
  125. myActiveFlag = false;
  126. return;
  127. }
  128. ServerRule rule;
  129. for(i = 0; i < rules.size(); i += 2)
  130. {
  131. rule.rule = rules.at(i);
  132. rule.value = rules.at(i+1);
  133. /* Proxy detection */
  134. if(rule.rule == "*QTV")
  135. {
  136. myIsProxyFlag = true;
  137. }
  138. else if(rule.rule == "*version")
  139. {
  140. QString value = rule.value;
  141. if(value.startsWith("qwfwd", Qt::CaseInsensitive) || value.startsWith("QTV", Qt::CaseInsensitive) || value.startsWith("2.91"))
  142. myIsProxyFlag = true;
  143. }
  144. /* Adjust FPS */
  145. if(rule.rule == "maxfps" && myPing > 0)
  146. myPing += rule.value.toUInt() * 12 / 77;
  147. myRules.append(rule);
  148. }
  149. /* Parse player info */
  150. myPlayers.clear();
  151. Player player;
  152. QRegExp regex("^([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+\"([^\"]*)\"\\s+\"([^\"]*)\"\\s+([0-9]+)\\s+([0-9]+)(?:\\s+\"([^\"]*)\")?$");
  153. QStringList playerInfo;
  154. for(i = 1; i < splitInfoString.size(); i++)
  155. {
  156. if(regex.indexIn(splitInfoString.at(i)) == -1)
  157. {
  158. emit error(InvalidPlayerInfoError);
  159. continue;
  160. }
  161. playerInfo = regex.capturedTexts();
  162. player.id = playerInfo.at(1).toInt();
  163. player.frags = playerInfo.at(2).toInt();
  164. player.time = playerInfo.at(3).toInt();
  165. player.ping = playerInfo.at(4).toInt();
  166. player.name = convertNameFun(playerInfo.at(5));
  167. player.skin = playerInfo.at(6);
  168. player.topColor = playerInfo.at(7).toInt();
  169. player.bottomColor = playerInfo.at(8).toInt();
  170. player.team = convertNameFun(playerInfo.at(9));
  171. if(player.name.startsWith("\\s\\"))
  172. {
  173. player.spectator = true;
  174. player.team = "(spec)";
  175. player.name = player.name.replace(QRegExp("^\\\\s\\\\"), "");
  176. if(player.ping < 0)
  177. player.ping = -player.ping;
  178. }
  179. else
  180. {
  181. player.spectator = false;
  182. }
  183. myPlayers.append(player);
  184. }
  185. myActiveFlag = false;
  186. emit finished();
  187. return;
  188. }
  189. bool ServerQuery::isActive() const
  190. {
  191. return myActiveFlag;
  192. }
  193. void ServerQuery::socketError(QAbstractSocket::SocketError)
  194. {
  195. emit error(UnknownError);
  196. myActiveFlag = false;
  197. }
  198. int ServerQuery::ping(int count, int timeout)
  199. {
  200. int pong = 0;
  201. QTime timer;
  202. timer.start();
  203. for(int i = 0; i < count; i++)
  204. {
  205. mySocket->write("\xff\xff\xff\xffk\n");
  206. timer.restart();
  207. if(!mySocket->waitForReadyRead(timeout))
  208. {
  209. pong += 999;
  210. }
  211. else
  212. {
  213. if(mySocket->readAll() == "l")
  214. pong += timer.elapsed();
  215. else
  216. pong += 999;
  217. }
  218. }
  219. return pong/count;
  220. }
  221. PlayerList ServerQuery::playerList() const
  222. {
  223. return myPlayers;
  224. }
  225. ServerRules ServerQuery::serverRules() const
  226. {
  227. return myRules;
  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. /* Name fun */
  241. char ServerQuery::ourReadableCharsTable[256] = { '.', '_' , '_' , '_' , '_' , '.' , '_' , '_' , '_' , '_' , '\n' , '_' , '\n' , '>' , '.' , '.',
  242. '[', ']', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', '_', '_'
  243. };
  244. bool ServerQuery::ourReadableCharsTableInitialized = false;
  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. QString ServerQuery::convertNameFun(const QString &name)
  259. {
  260. if(!ourReadableCharsTableInitialized)
  261. fillReadableCharsTable();
  262. QString stripped;
  263. for(int i = 0; i < name.length(); i++)
  264. stripped.append(QChar(ourReadableCharsTable[(unsigned char)name.at(i).toAscii()] & 127));
  265. return stripped;
  266. }