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 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
  9. {
  10. Q_OBJECT
  11. public:
  12. enum State
  13. {
  14. Disconnected,
  15. HostLookUp,
  16. Connecting,
  17. Connected,
  18. Handshaked,
  19. Disconnecting,
  20. Error
  21. };
  22. PTSocket(QObject* p);
  23. int getTimeout() const;
  24. int getPingInterval() const;
  25. bool isServerSide() const;
  26. static QByteArray handshakeData();
  27. static QByteArray intToByteArray(int a);
  28. static int byteArrayToInt(QByteArray d);
  29. signals:
  30. void stateChanged(PTSocket::State);
  31. void packetReceived(int, QByteArray);
  32. public slots:
  33. void setTimeout(int t);
  34. void setPingInterval(int p);
  35. void setIsServerSide(bool s);
  36. void send(int pktType, QByteArray data);
  37. private slots:
  38. void m_ping();
  39. void m_readyRead();
  40. void m_timedout();
  41. void m_handshakeError();
  42. void m_stateChanged(QAbstractSocket::SocketState s);
  43. void m_socketError(QAbstractSocket::SocketError);
  44. void setState(State s);
  45. void setError(QString e);
  46. void send(bool sys, int pktType, QByteArray data = "");
  47. private:
  48. QTimer m_timeoutTimer;
  49. QTimer m_pingTimer;
  50. int m_timeout;
  51. int m_pingInterval;
  52. State m_state;
  53. bool m_isServerSide;
  54. unsigned int m_packetSize;
  55. enum PacketType
  56. {
  57. PingRequest = 1,
  58. PingAnswer = 2
  59. };
  60. };
  61. #endif // PTSOCKET_H