您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ptsocket.cpp 848B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "ptsocket.h"
  2. PTSocket::PTSocket(QObject* p) : QTcpSocket(p)
  3. {
  4. m_hasHandshaked = false;
  5. m_timeout = 5000;
  6. connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(m_handshakeError()));
  7. connect(this, SIGNAL(readyRead()), this, SLOT(m_readyRead()));
  8. }
  9. int PTSocket::getTimeout() const
  10. {
  11. return m_timeout;
  12. }
  13. QByteArray PTSocket::handshakeData() const
  14. {
  15. return "LibPTSocket";
  16. }
  17. void PTSocket::handshake()
  18. {
  19. write(handshakeData());
  20. }
  21. void PTSocket::setTimeout(int t)
  22. {
  23. m_timeout = t;
  24. }
  25. void PTSocket::m_readyRead()
  26. {
  27. if(m_hasHandshaked)
  28. {
  29. }
  30. else
  31. {
  32. QByteArray handshake = handshakeData();
  33. if(bytesAvailable() >= handshake.size())
  34. {
  35. QByteArray data = read(handshake.size());
  36. if(data == handshake)
  37. {
  38. m_hasHandshaked = true;
  39. }
  40. else
  41. m_handshakeError();
  42. }
  43. }
  44. }
  45. void PTSocket::m_handshakeError()
  46. {
  47. }