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.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. 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