SoundBand/sync/ETcpSocket.cpp

282 lines
7.2 KiB
C++
Raw Normal View History

2017-11-18 01:29:14 +03:00
#include "ETcpSocket.h"
#include "exaptions.h"
#include <QTimer>
2017-11-18 01:29:14 +03:00
ETcpSocket::ETcpSocket()
{
source=new QTcpSocket();
init();
}
ETcpSocket::ETcpSocket(QTcpSocket*ptr)
{
source=ptr;
init();
2018-01-09 22:14:54 +03:00
pingTimer->start(0);
2017-11-18 01:29:14 +03:00
}
ETcpSocket::ETcpSocket(const QString& address, int port){
source = new QTcpSocket();
2017-11-27 22:59:59 +03:00
source->connectToHost(address, port);
if(!source->waitForConnected(DEEP_SCANER_INTERVAL) || !source->open(QIODevice::ReadWrite)){
2017-11-24 21:17:41 +03:00
throw AddNodeExaption();
2017-11-18 01:29:14 +03:00
}
init();
}
void ETcpSocket::init(){
2017-12-17 01:23:38 +03:00
array = new QByteArray;
fSynced = false;
differenceTime = 0;
pingTimer = new QTimer();
checInterval = CHECK_PING_INTERVAL;
2017-12-17 01:23:38 +03:00
srand(time(0));
connect(pingTimer, SIGNAL(timeout()), SLOT(calcPing(int)));
2017-11-18 01:29:14 +03:00
connect(source,SIGNAL(connected()),this,SLOT(connected_()));
connect(source,SIGNAL(disconnected()),this,SLOT(disconnected_()));
connect(source,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error_(QAbstractSocket::SocketError)));
connect(source,SIGNAL(hostFound()),this,SLOT(hostFound_()));
connect(source,SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),this,SLOT(proxyAuthenticationRequired_(const QNetworkProxy &, QAuthenticator *)));
connect(source,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged_(QAbstractSocket::SocketState)));
connect(source,SIGNAL(readyRead()),this,SLOT(readReady_()));
}
int ETcpSocket::getPing()const{
return ping;
}
void ETcpSocket::calcPing(int flag){
pingTimer->setInterval(rand() % checInterval);
QByteArray cArray;
QDataStream stream(&cArray,QIODevice::ReadWrite);
stream << flag;
lastTime = ChronoTime::now();
source->write(cArray);
}
void ETcpSocket::setCheckInterval(int newInterval){
checInterval = newInterval;
}
int ETcpSocket::getCheckInterval()const{
return checInterval;
}
2017-11-18 01:29:14 +03:00
void ETcpSocket::error_(QAbstractSocket::SocketError i){
emit Error(this,i);
}
void ETcpSocket::connected_(){
pingTimer->start(0);
2017-11-18 01:29:14 +03:00
emit Connected(this);
}
void ETcpSocket::disconnected_(){
emit Disconnected(this);
}
void ETcpSocket::hostFound_(){
emit HostFound(this);
}
void ETcpSocket::proxyAuthenticationRequired_(const QNetworkProxy &proxy, QAuthenticator *authenticator){
emit ProxyAuthenticationRequired(this,proxy,authenticator);
}
void ETcpSocket::stateChanged_(QAbstractSocket::SocketState socketState){
emit StateChanged(this,socketState);
}
void ETcpSocket::readReady_(){
bool sizewrite=array->isEmpty();
array->append(source->readAll());
QDataStream stream(array,QIODevice::ReadOnly);
2017-12-17 01:23:38 +03:00
if(sizewrite){
stream >> size;
switch (size) {
case CALIBRATION_SENDER:{
milliseconds ms;
stream >> ms;
milliseconds range = ChronoTime::now() - ms;
QByteArray cArray;
QDataStream stream(&cArray,QIODevice::ReadWrite);
if(range < MIN_DIFFERENCE){
stream << CALIBRATION_RECEIVER_DONE;
fSynced = true;
}else{
stream << CALIBRATION_RECEIVER;
stream << range;
}
source->write(cArray);
2017-12-17 02:23:01 +03:00
array->clear();
2017-12-17 01:23:38 +03:00
return;
}
case CALIBRATION_RECEIVER:{
ping = ChronoTime::now() - lastTime;
milliseconds ms;
stream >> ms;
differenceTime += ms + ping / 2;
QByteArray cArray;
QDataStream stream(&cArray,QIODevice::ReadWrite);
stream << CALIBRATION_SENDER;
stream << milliseconds(ChronoTime::now() + differenceTime);
lastTime = ChronoTime::now();
source->write(cArray);
2017-12-17 02:23:01 +03:00
array->clear();
return;
2017-12-17 01:23:38 +03:00
}
case CALIBRATION_RECEIVER_DONE:{
QByteArray cArray;
QDataStream stream(&cArray,QIODevice::ReadWrite);
stream << CALIBRATION_SENDER_DONE;
stream << milliseconds(-differenceTime);
fSynced = true;
source->write(cArray);
2017-12-17 02:23:01 +03:00
array->clear();
return;
2017-12-17 01:23:38 +03:00
}
case CALIBRATION_SENDER_DONE:{
stream >> differenceTime;
2017-12-17 02:23:01 +03:00
array->clear();
2017-12-17 01:23:38 +03:00
return;
}
case CALIBRATION_PING_DONE:{
ping = ChronoTime::now() - lastTime;
}
2017-12-17 01:23:38 +03:00
case CALIBRATION_PING:{
calcPing(CALIBRATION_PING_DONE);
2017-12-17 01:23:38 +03:00
}
}
2017-12-17 01:23:38 +03:00
}
2017-11-18 01:29:14 +03:00
#ifdef QT_DEBUG
qDebug()<<"messae size:"<<size;
qDebug()<<"message package size:"<<array->size();
#endif
if(size==array->size())
{
array->remove(0,sizeof(qint32));
ReadyStack.push_back(array);
2017-11-20 00:37:12 +03:00
array=new QByteArray();
2017-11-18 01:29:14 +03:00
emit Message(this);
}else{
emit donwload(array->size(),size);
}
}
QString ETcpSocket::name() const{
return source->peerAddress().toString();
}
QByteArray* ETcpSocket::topStack(){
if(ReadyStack.size())
return ReadyStack.front();
return NULL;
}
QTcpSocket* ETcpSocket::getSource()const{
return source;
}
2017-11-26 19:19:43 +03:00
void ETcpSocket::nextItem(bool free){
2017-11-20 00:37:12 +03:00
if( ReadyStack.size()){
2017-11-26 19:19:43 +03:00
if(free){
ReadyStack.front()->clear();
delete ReadyStack.front();
}
2017-11-18 01:29:14 +03:00
ReadyStack.pop_front();
2017-11-20 00:37:12 +03:00
}
2017-11-18 01:29:14 +03:00
}
int ETcpSocket::sizeDescriptPackege(){
return sizeof(qint32);
}
QString ETcpSocket::toStringTcp(){
return source->peerAddress().toString();
}
2017-12-17 01:23:38 +03:00
milliseconds ETcpSocket::getDifferenceTime() const{
return differenceTime;
}
bool ETcpSocket::isSynced()const{
return fSynced;
}
2017-11-18 01:29:14 +03:00
bool ETcpSocket::Write(const QByteArray&data){
if(source->state()==QTcpSocket::ConnectedState){
QByteArray array;
QDataStream stream(&array,QIODevice::ReadWrite);
2017-12-17 01:23:38 +03:00
stream << qint32(0);
2017-11-18 01:29:14 +03:00
array.append(data);
stream.device()->seek(0);
stream<<qint32(array.size());
#ifdef QT_DEBUG
qDebug()<<"size :"<<array.size();
qint64 temp= source->write(array);
qDebug()<<"size write:"<<temp<<" size packege:"<<array.size();
return temp==(array.size());
#else
return source->write(array)==(array.size());
#endif
}
return false;
}
2017-12-17 01:23:38 +03:00
void ETcpSocket::calibration(){
if(!fSynced){
QByteArray cArray;
QDataStream stream(&cArray,QIODevice::ReadWrite);
stream << CALIBRATION_SENDER;
stream << milliseconds(ChronoTime::now());
lastTime = ChronoTime::now();
source->write(cArray);
}
}
2017-11-18 01:29:14 +03:00
ETcpSocket::~ETcpSocket()
{
for(QByteArray*i:ReadyStack){
i->clear();
delete i;
}
disconnect(source,SIGNAL(connected()),this,SLOT(connected_()));
disconnect(source,SIGNAL(disconnected()),this,SLOT(disconnected_()));
disconnect(source,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error_(QAbstractSocket::SocketError)));
disconnect(source,SIGNAL(hostFound()),this,SLOT(hostFound_()));
disconnect(source,SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),this,SLOT(proxyAuthenticationRequired_(const QNetworkProxy &, QAuthenticator *)));
disconnect(source,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged_(QAbstractSocket::SocketState)));
disconnect(source,SIGNAL(readyRead()),this,SLOT(readReady_()));
source->deleteLater();
}