SoundBand/sync/ETcpSocket.h

192 lines
5.1 KiB
C
Raw Normal View History

2017-11-18 01:29:14 +03:00
#ifndef CLIENT_H
#define CLIENT_H
#include <QTcpSocket>
#include <QTcpServer>
#include <QList>
#include <QDataStream>
2017-12-17 01:23:38 +03:00
#include "chronotime.h"
#define CALIBRATION_SENDER qint32(-1)
#define CALIBRATION_RECEIVER qint32(-2)
#define CALIBRATION_SENDER_DONE qint32(-3)
#define CALIBRATION_RECEIVER_DONE qint32(-4)
#define CALIBRATION_PING qint32(-5)
#define CALIBRATION_PING_DONE qint32(-6)
2017-12-17 01:23:38 +03:00
2017-11-20 00:37:12 +03:00
/**
* @brief The ETcpSocket class
* example :
* ETcpSocket *tcp;
* try{
* tcp = new ETcpSocket(addres,port);
* }catch(addNodeExaption e){
* e.what();
* }
* QByteArray *array;
* while(array = tcp.getSource()){
* package pkg(*array);
* package ans = ansver(pkg);
* tcp.Write(ans);
2017-11-26 19:19:43 +03:00
* tcp->nextItem();
2017-11-20 00:37:12 +03:00
*
* }
*
*/
2017-12-17 01:23:38 +03:00
2017-11-18 01:29:14 +03:00
class ETcpSocket:public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
private:
QTcpSocket *source;
QByteArray *array;
qint32 size;
QList<QByteArray*> ReadyStack;
void init();
2017-12-17 01:23:38 +03:00
/**
* @brief differenceTime - local time difference in milliseconds
* on milliseconds
*/
milliseconds differenceTime;
/**
* @brief fSynced - whether a time difference was found.
*/
bool fSynced;
milliseconds lastTime;
milliseconds ping;
2017-11-18 01:29:14 +03:00
/**
* @brief calcPing flag of calibration (CALIBRATION_PING or CALIBRATION_PING_DONE)
*/
void calcPing(int flag);
2017-11-18 01:29:14 +03:00
private slots:
void connected_();
void disconnected_();
void error_(QAbstractSocket::SocketError socketError);
void hostFound_();
void readReady_();
void proxyAuthenticationRequired_(const QNetworkProxy &proxy, QAuthenticator *authenticator);
void stateChanged_(QAbstractSocket::SocketState socketState);
public:
explicit ETcpSocket();
explicit ETcpSocket(QTcpSocket*);
explicit ETcpSocket(const QString& addres,int port);
/**
* @brief getPing
* @return ping of soccket;
*/
int getPing()const;
2017-11-26 19:19:43 +03:00
/**
* @brief getSource
* @return Qt TCP socket
*/
2017-11-18 01:29:14 +03:00
QTcpSocket* getSource()const;
2017-11-26 19:19:43 +03:00
/**
* @brief topStack
* @return top of a message stack.
*/
2017-11-18 01:29:14 +03:00
QByteArray* topStack();
2017-11-26 19:19:43 +03:00
/**
* @brief nextItem - Toggle header to next message.
* You need to call this function after the top Stack.
* @param free - clear memory of top stack (true by default)
*/
void nextItem(bool free = true);
/**
* @brief sizeDescriptPackege
* @return size of Descript of Packege
*/
2017-11-18 01:29:14 +03:00
int sizeDescriptPackege();
2017-11-26 19:19:43 +03:00
/**
* @brief Write - sends a message to the network.
* @return true if all done else false.
*/
2017-11-18 01:29:14 +03:00
bool Write(const QByteArray&);
2017-12-17 01:23:38 +03:00
/**
* @brief getDifferenceTime
* @return Difference Time of remoute node.
*/
milliseconds getDifferenceTime()const;
/**
* @brief isSynced
* @return
*/
bool isSynced()const;
/**
* @brief calibration - will calculate the difference in time between the connected node
* and the current node.
*/
void calibration();
2017-11-18 01:29:14 +03:00
~ETcpSocket();
public slots:
2017-11-26 19:19:43 +03:00
/**
* @brief name
* @return hot name
*/
2017-11-18 01:29:14 +03:00
QString name()const;
2017-11-26 19:19:43 +03:00
/**
* @brief toStringTcp
* @return string of tcp address
*/
2017-11-18 01:29:14 +03:00
QString toStringTcp();
signals:
2017-11-26 19:19:43 +03:00
/**
* @brief donwload - signal when received a package.
* @param val - size of package
* @param max - real received package size
*/
2017-11-18 01:29:14 +03:00
void donwload(int val,int max);
2017-11-26 19:19:43 +03:00
/**
* @brief Connected - signal when connected with other socket.
*/
2017-11-18 01:29:14 +03:00
void Connected(ETcpSocket*);
2017-11-26 19:19:43 +03:00
/**
* @brief Message - signal when received a Message.
*/
2017-11-18 01:29:14 +03:00
void Message(ETcpSocket*);
2017-11-26 19:19:43 +03:00
/**
* @brief Disconnected - signal when disconnected with other socket.
*/
2017-11-18 01:29:14 +03:00
void Disconnected(ETcpSocket*);
2017-11-26 19:19:43 +03:00
/**
* @brief Error - signal if there are problems with the connection.
* @param socketError - information about error.
*/
2017-11-18 01:29:14 +03:00
void Error(ETcpSocket*,QAbstractSocket::SocketError socketError);
2017-11-26 19:19:43 +03:00
/**
* @brief HostFound This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
* Note: Since Qt 4.6.3 QAbstractSocket may emit hostFound()
* directly from the connectToHost() call since a DNS result could have been cached.
*/
2017-11-18 01:29:14 +03:00
void HostFound(ETcpSocket*);
2017-11-26 19:19:43 +03:00
/**
* @brief ProxyAuthenticationRequired
* This signal can be emitted when a proxy that requires authentication is used.
* The authenticator object can then be filled in with the required details to allow authentication and continue the connection.
* Note: It is not possible to use a QueuedConnection to connect to this signal,
* as the connection will fail if the authenticator has not been filled in with new information when the signal returns.
*/
2017-11-18 01:29:14 +03:00
void ProxyAuthenticationRequired(ETcpSocket*,const QNetworkProxy &proxy, QAuthenticator *authenticator);
2017-11-26 19:19:43 +03:00
/**
* @brief StateChanged This signal is emitted whenever QAbstractSocket's state changes.
* The socketState parameter is the new state.
*/
2017-11-18 01:29:14 +03:00
void StateChanged(ETcpSocket*,QAbstractSocket::SocketState socketState);
2017-11-26 19:19:43 +03:00
2017-12-17 01:23:38 +03:00
2017-11-18 01:29:14 +03:00
};
#endif // CLIENT_H