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

ptsocket.h 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //#define PT_DEBUG
  10. class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
  11. {
  12. Q_OBJECT
  13. public:
  14. enum State
  15. {
  16. Disconnected,
  17. HostLookUp,
  18. Connecting,
  19. Connected,
  20. Handshaked,
  21. Disconnecting,
  22. Error
  23. };
  24. Q_ENUMS(State)
  25. PTSocket(QObject* p);
  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. void dbgSetWillPing(bool w);
  43. void dbgSetWillHandshake(bool w);
  44. void dbgSetWillHandshakeFake1(bool w);
  45. void dbgSetWillHandshakeFake2(bool w);
  46. private slots:
  47. void m_ping();
  48. void m_readyRead();
  49. void m_timedout();
  50. void m_handshakeError();
  51. void m_stateChanged(QAbstractSocket::SocketState s);
  52. void m_socketError(QAbstractSocket::SocketError s);
  53. void setState(State s);
  54. void setError(QString e);
  55. void send(bool sys, int pktType, QByteArray data = "");
  56. private:
  57. QTimer m_timeoutTimer;
  58. QTimer m_pingTimer;
  59. int m_timeout;
  60. int m_pingInterval;
  61. State m_state;
  62. bool m_isServerSide;
  63. unsigned int m_packetSize;
  64. QString m_error;
  65. enum PacketType
  66. {
  67. PingRequest = 1,
  68. PingAnswer = 2
  69. };
  70. #ifdef PT_DEBUG
  71. bool m_willPing;
  72. bool m_willHandshake;
  73. bool m_willHandshakeFake1;
  74. bool m_willHandshakeFake2;
  75. #endif
  76. #ifdef PT_DEBUG
  77. #endif
  78. };
  79. QDebug operator <<(QDebug dbg, const PTSocket::State s);
  80. #endif // PTSOCKET_H