You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ptsocket.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "ptsocket.h"
  2. PTSocket::PTSocket(QObject* p) : QTcpSocket(p)
  3. {
  4. #ifdef PT_DEBUG
  5. m_willPing = true;
  6. dbgSetWillHandshake(true);
  7. #endif
  8. setSocketOption(QAbstractSocket::LowDelayOption, 1);
  9. m_packetSize = 0;
  10. m_timeoutTimer.setSingleShot(true);
  11. setTimeout(5000);
  12. m_pingTimer.setSingleShot(true);
  13. #ifdef PT_DEBUG
  14. setPingInterval(1000 * 7);
  15. #else
  16. setPingInterval(1000 * 60);
  17. #endif
  18. m_state = Disconnected;
  19. m_isServerSide = false;
  20. connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(m_timedout()));
  21. connect(&m_pingTimer, SIGNAL(timeout()), this, SLOT(m_ping()));
  22. connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(m_stateChanged(QAbstractSocket::SocketState)));
  23. connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(m_socketError(QAbstractSocket::SocketError)));
  24. connect(this, SIGNAL(readyRead()), this, SLOT(m_readyRead()));
  25. }
  26. PTSocket::State PTSocket::getState() const
  27. {
  28. return m_state;
  29. }
  30. int PTSocket::getTimeout() const
  31. {
  32. return m_timeout;
  33. }
  34. int PTSocket::getPingInterval() const
  35. {
  36. return m_pingInterval;
  37. }
  38. bool PTSocket::isServerSide() const
  39. {
  40. return m_isServerSide;
  41. }
  42. QString PTSocket::getError() const
  43. {
  44. return (m_error.isEmpty()? errorString() : m_error);
  45. }
  46. QByteArray PTSocket::handshakeData()
  47. {
  48. return "LibPTSocketv1";
  49. }
  50. void PTSocket::setTimeout(int t)
  51. {
  52. m_timeout = t;
  53. if(m_timeoutTimer.isActive())
  54. m_timeoutTimer.start(m_timeout);
  55. else
  56. m_timeoutTimer.setInterval(m_timeout);
  57. }
  58. void PTSocket::setPingInterval(int p)
  59. {
  60. m_pingInterval = p;
  61. if(m_pingTimer.isActive())
  62. m_pingTimer.start(m_pingInterval);
  63. else
  64. m_pingTimer.setInterval(m_pingInterval);
  65. }
  66. void PTSocket::setIsServerSide(bool s)
  67. {
  68. m_isServerSide = s;
  69. }
  70. void PTSocket::send(int pktType, QByteArray data)
  71. {
  72. send(false, pktType, data);
  73. }
  74. void PTSocket::disconnectFromServer()
  75. {
  76. disconnectFromHost();
  77. }
  78. void PTSocket::dbgSetWillPing(bool w)
  79. {
  80. #ifdef PT_DEBUG
  81. m_willPing = w;
  82. #endif
  83. }
  84. void PTSocket::dbgSetWillHandshake(bool w)
  85. {
  86. #ifdef PT_DEBUG
  87. m_willHandshake = w;
  88. m_willHandshakeFake1 = false;
  89. m_willHandshakeFake2 = false;
  90. #endif
  91. }
  92. void PTSocket::dbgSetWillHandshakeFake1(bool w)
  93. {
  94. #ifdef PT_DEBUG
  95. m_willHandshakeFake1 = w;
  96. if(!w)
  97. m_willHandshake = true;
  98. else
  99. m_willHandshake = false;
  100. m_willHandshakeFake2 = false;
  101. #endif
  102. }
  103. void PTSocket::dbgSetWillHandshakeFake2(bool w)
  104. {
  105. #ifdef PT_DEBUG
  106. m_willHandshakeFake2 = w;
  107. if(!w)
  108. m_willHandshake = true;
  109. else
  110. m_willHandshake = false;
  111. m_willHandshakeFake1 = false;
  112. #endif
  113. }
  114. void PTSocket::send(bool sys, int pktType, QByteArray data)
  115. {
  116. if(data.isEmpty())
  117. data = "\0";
  118. data.prepend(intToByteArray(pktType));
  119. data.prepend((char)sys);
  120. data.prepend(intToByteArray(data.size()));
  121. write(data);
  122. }
  123. void PTSocket::m_ping()
  124. {
  125. m_pingTimer.stop();
  126. if(m_timeoutTimer.isActive())
  127. return;
  128. #ifdef PT_DEBUG
  129. if(m_willPing)
  130. send(true, PingRequest);
  131. #else
  132. send(true, PingRequest);
  133. #endif
  134. m_timeoutTimer.start();
  135. }
  136. void PTSocket::m_readyRead()
  137. {
  138. m_timeoutTimer.stop();
  139. m_pingTimer.start();
  140. if(m_state == Handshaked)
  141. {
  142. if(m_packetSize == 0)
  143. {
  144. if((unsigned)bytesAvailable() >= sizeof(int))
  145. m_packetSize = byteArrayToInt(read(sizeof(int)));
  146. else if(bytesAvailable() != 0)
  147. m_timeoutTimer.start();
  148. }
  149. if(m_packetSize != 0)
  150. {
  151. if((unsigned)bytesAvailable() >= m_packetSize)
  152. {
  153. bool sys = (bool)read(1).at(0);
  154. QByteArray p = read(sizeof(int));
  155. int pktType = byteArrayToInt(p);
  156. QByteArray data = read(m_packetSize - 1 - p.size());
  157. if(sys)
  158. {
  159. if(pktType == PingRequest)
  160. {
  161. #ifdef PT_DEBUG
  162. if(m_willPing)
  163. send(true, PingAnswer);
  164. #else
  165. send(true, PingAnswer);
  166. #endif
  167. }
  168. //else if(pktType == PingAnswer)
  169. }
  170. else
  171. emit packetReceived(pktType, data);
  172. m_packetSize = 0;
  173. m_readyRead();
  174. }
  175. else
  176. m_timeoutTimer.start();
  177. }
  178. }
  179. else
  180. {
  181. QByteArray handshake = handshakeData();
  182. if(bytesAvailable() >= handshake.size())
  183. {
  184. QByteArray data = read(handshake.size());
  185. if(data == handshake)
  186. {
  187. setState(Handshaked);
  188. m_readyRead();
  189. }
  190. else
  191. m_handshakeError();
  192. }
  193. else
  194. m_timeoutTimer.start();
  195. }
  196. }
  197. void PTSocket::m_timedout()
  198. {
  199. if(m_state == Disconnected)
  200. return;
  201. if(m_state == HostLookUp)
  202. setError("Unable to find the host " + peerName() + " in the time (" + QString::number(m_timeout) + " ms)");
  203. else if(m_state == Connecting)
  204. setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the connection request in the time (" + QString::number(m_timeout) + " ms)");
  205. else if(m_state == Connected)
  206. setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the handshake in the time (" + QString::number(m_timeout) + " ms)");
  207. else if(m_state == Handshaked)
  208. setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " timed out (" + QString::number(m_timeout) + " ms)");
  209. else if(m_state == Disconnecting)
  210. abort();
  211. }
  212. void PTSocket::m_handshakeError()
  213. {
  214. setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the handshake correclty");
  215. }
  216. void PTSocket::m_stateChanged(QAbstractSocket::SocketState s)
  217. {
  218. if(s == QAbstractSocket::HostLookupState)
  219. setState(HostLookUp);
  220. else if(s == QAbstractSocket::ConnectingState)
  221. setState(Connecting);
  222. else if(s == QAbstractSocket::ConnectedState)
  223. {
  224. m_packetSize = 0;
  225. setState(Connected);
  226. #ifdef PT_DEBUG
  227. if(m_willHandshakeFake1)
  228. write("LibPTSocketv");
  229. else if(m_willHandshakeFake2)
  230. write("42LibPTSocketv4242");
  231. else if(m_willHandshake)
  232. write(handshakeData());
  233. #else
  234. write(handshakeData());
  235. #endif
  236. }
  237. else if(s == QAbstractSocket::ClosingState)
  238. setState(Disconnecting);
  239. m_timeoutTimer.start();
  240. if(s == QAbstractSocket::UnconnectedState)
  241. {
  242. if(error() == QAbstractSocket::UnknownSocketError || error() == QAbstractSocket::RemoteHostClosedError)
  243. {
  244. m_timeoutTimer.stop();
  245. setState(Disconnected);
  246. }
  247. }
  248. }
  249. void PTSocket::m_socketError(QAbstractSocket::SocketError s)
  250. {
  251. if(s != QAbstractSocket::RemoteHostClosedError)
  252. setState(Error);
  253. }
  254. void PTSocket::setState(PTSocket::State s)
  255. {
  256. if(s == m_state)
  257. return;
  258. if(s == Error && (m_state == Disconnected || m_state == Disconnecting))
  259. return;
  260. if(m_state == Error && (s == Disconnected || s == Disconnecting))
  261. return;
  262. m_state = s;
  263. if(s == Error)
  264. abort();
  265. else
  266. m_error.clear();
  267. if(s == Handshaked)
  268. m_pingTimer.start();
  269. else
  270. m_pingTimer.stop();
  271. emit stateChanged(m_state);
  272. }
  273. void PTSocket::setError(QString e)
  274. {
  275. if(m_state == Error || m_state == Disconnected || m_state == Disconnecting)
  276. return;
  277. m_error = e;
  278. setState(Error);
  279. }
  280. QByteArray PTSocket::intToByteArray(int a)
  281. {
  282. QByteArray data;
  283. for(unsigned char i = 0; i < sizeof(a); ++i)
  284. data.append((char)(a >> 8 * i));
  285. return data;
  286. }
  287. int PTSocket::byteArrayToInt(QByteArray d)
  288. {
  289. int a = 0;
  290. for(unsigned char i = 0; i < d.size(); ++i)
  291. a |= (unsigned int)((unsigned char)d.at(i) << i * 8);
  292. return a;
  293. }
  294. QDebug operator <<(QDebug dbg, const PTSocket::State s)
  295. {
  296. const QMetaObject & mo = PTSocket::staticMetaObject;
  297. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("State"));
  298. return dbg<<me.valueToKey(s);
  299. }