ServerQuery.cpp 6.1 KB

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