#ifndef PTSOCKET_H #define PTSOCKET_H #include "libptsocket_global.h" #include #include #include #include #include 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(); #ifdef PT_DEBUG void dbgSetWillPing(bool w); void dbgSetWillHandshake(bool w); void dbgSetWillHandshakeFake1(bool w); void dbgSetWillHandshakeFake2(bool w); #endif 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 }; QDebug operator <<(QDebug dbg, const PTSocket::State s); #endif // PTSOCKET_H