12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #ifndef PTSOCKET_H
- #define PTSOCKET_H
-
- #include "libptsocket_global.h"
- #include <QObject>
- #include <QTcpSocket>
- #include <QHostAddress>
- #include <QTimer>
- #include <QMetaEnum>
-
- class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
- {
- Q_OBJECT
- public:
- enum State
- {
- Disconnected,
- HostLookUp,
- Connecting,
- Connected,
- Handshaked,
- Disconnecting,
- Error
- };
- Q_ENUMS(State)
- PTSocket(QObject* p);
- State getState() const;
- int getTimeout() const;
- int getPingInterval() const;
- bool isServerSide() const;
- QString getError() const;
-
- static QByteArray handshakeData();
-
- static QByteArray intToByteArray(int a);
- static int byteArrayToInt(QByteArray d);
-
- signals:
- void stateChanged(PTSocket::State);
- void packetReceived(int, QByteArray);
-
- public slots:
- void setTimeout(int t);
- void setPingInterval(int p);
- void setIsServerSide(bool s);
- void send(int pktType, QByteArray data);
- void disconnectFromServer();
-
- void dbgSetWillPing(bool w);
- void dbgSetWillHandshake(bool w);
- void dbgSetWillHandshakeFake1(bool w);
- void dbgSetWillHandshakeFake2(bool w);
-
- private slots:
- void m_ping();
- void m_readyRead();
- void m_timedout();
- void m_handshakeError();
- void m_stateChanged(QAbstractSocket::SocketState s);
- void m_socketError(QAbstractSocket::SocketError s);
- void setState(State s);
- void setError(QString e);
- void send(bool sys, int pktType, QByteArray data = "");
-
- private:
- QTimer m_timeoutTimer;
- QTimer m_pingTimer;
- int m_timeout;
- int m_pingInterval;
- State m_state;
- bool m_isServerSide;
- unsigned int m_packetSize;
- QString m_error;
- enum PacketType
- {
- PingRequest = 1,
- PingAnswer = 2
- };
- #ifdef PT_DEBUG
- bool m_willPing;
- bool m_willHandshake;
- bool m_willHandshakeFake1;
- bool m_willHandshakeFake2;
- #endif
- #ifdef PT_DEBUG
- #endif
- };
-
- QDebug operator <<(QDebug dbg, const PTSocket::State s);
-
- #endif // PTSOCKET_H
|