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

ptsocket.h 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef PTSOCKET_H
  2. #define PTSOCKET_H
  3. #include "libptsocket_global.h"
  4. #include <QObject>
  5. #include <QTcpSocket>
  6. #include <QHostAddress>
  7. #include <QTimer>
  8. #include <QMetaEnum>
  9. class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
  10. {
  11. Q_OBJECT
  12. public:
  13. enum State
  14. {
  15. Disconnected,
  16. HostLookUp,
  17. Connecting,
  18. Connected,
  19. Handshaked,
  20. Disconnecting,
  21. Error
  22. };
  23. Q_ENUMS(State)
  24. PTSocket(QObject* p);
  25. State getState() const;
  26. int getTimeout() const;
  27. int getPingInterval() const;
  28. bool isServerSide() const;
  29. QString getError() const;
  30. static QByteArray handshakeData();
  31. static QByteArray intToByteArray(int a);
  32. static int byteArrayToInt(QByteArray d);
  33. signals:
  34. void stateChanged(PTSocket::State);
  35. void packetReceived(int, QByteArray);
  36. public slots:
  37. void setTimeout(int t);
  38. void setPingInterval(int p);
  39. void setIsServerSide(bool s);
  40. void send(int pktType, QByteArray data);
  41. void disconnectFromServer();
  42. #ifdef PT_DEBUG
  43. void dbgSetWillPing(bool w);
  44. void dbgSetWillHandshake(bool w);
  45. void dbgSetWillHandshakeFake1(bool w);
  46. void dbgSetWillHandshakeFake2(bool w);
  47. #endif
  48. private slots:
  49. void m_ping();
  50. void m_readyRead();
  51. void m_timedout();
  52. void m_handshakeError();
  53. void m_stateChanged(QAbstractSocket::SocketState s);
  54. void m_socketError(QAbstractSocket::SocketError s);
  55. void setState(State s);
  56. void setError(QString e);
  57. void send(bool sys, int pktType, QByteArray data = "");
  58. private:
  59. QTimer m_timeoutTimer;
  60. QTimer m_pingTimer;
  61. int m_timeout;
  62. int m_pingInterval;
  63. State m_state;
  64. bool m_isServerSide;
  65. unsigned int m_packetSize;
  66. QString m_error;
  67. enum PacketType
  68. {
  69. PingRequest = 1,
  70. PingAnswer = 2
  71. };
  72. #ifdef PT_DEBUG
  73. bool m_willPing;
  74. bool m_willHandshake;
  75. bool m_willHandshakeFake1;
  76. bool m_willHandshakeFake2;
  77. #endif
  78. };
  79. QDebug operator <<(QDebug dbg, const PTSocket::State s);
  80. #endif // PTSOCKET_H