SyncClient.cpp   SyncClient.cpp 
skipping to change at line 30 skipping to change at line 30
#include "SyncClient.hpp" #include "SyncClient.hpp"
#include "SyncClientHandlers.hpp" #include "SyncClientHandlers.hpp"
#include "SyncMessages.hpp" #include "SyncMessages.hpp"
#include "StelTranslator.hpp" #include "StelTranslator.hpp"
#include <QDateTime> #include <QDateTime>
#include <QTcpSocket> #include <QTcpSocket>
#include <QTimerEvent> #include <QTimerEvent>
Q_LOGGING_CATEGORY(syncClient,"stel.plugin.remoteSync.client")
using namespace SyncProtocol; using namespace SyncProtocol;
SyncClient::SyncClient(QObject *parent) SyncClient::SyncClient(SyncOptions options, const QStringList &excludePrope
: QObject(parent), isConnecting(false), server(NULL), timeoutTimerId rties, QObject *parent)
(-1) : QObject(parent),
options(options),
stelPropFilter(excludeProperties),
isConnecting(false),
server(Q_NULLPTR),
timeoutTimerId(-1)
{ {
handlerList.resize(MSGTYPE_SIZE);
handlerList[ERROR] = new ClientErrorHandler(this);
handlerList[SERVER_CHALLENGE] = new ClientAuthHandler(this);
handlerList[SERVER_CHALLENGERESPONSEVALID] = new ClientAuthHandler(t
his);
handlerList[ALIVE] = new ClientAliveHandler();
//these are the actual sync handlers
if(options.testFlag(SyncTime))
handlerList[TIME] = new ClientTimeHandler();
if(options.testFlag(SyncLocation))
handlerList[LOCATION] = new ClientLocationHandler();
if(options.testFlag(SyncSelection))
handlerList[SELECTION] = new ClientSelectionHandler();
if(options.testFlag(SyncStelProperty))
handlerList[STELPROPERTY] = new ClientStelPropertyUpdateHand
ler(options.testFlag(SkipGUIProps), stelPropFilter);
if(options.testFlag(SyncView))
handlerList[VIEW] = new ClientViewHandler();
if(options.testFlag(SyncFov))
handlerList[FOV] = new ClientFovHandler();
//fill unused handlers with dummies
for(int t = TIME;t<MSGTYPE_SIZE;++t)
{
if(!handlerList[t]) handlerList[t] = new DummyMessageHandler
();
}
} }
SyncClient::~SyncClient() SyncClient::~SyncClient()
{ {
disconnectFromServer(); disconnectFromServer();
delete server;
//delete handlers
foreach(SyncMessageHandler* h, handlerList)
{
if(h)
delete h;
}
handlerList.clear();
qCDebug(syncClient)<<"Destroyed";
} }
void SyncClient::connectToServer(const QString &host, const int port) void SyncClient::connectToServer(const QString &host, const int port)
{ {
if(server) if(server)
{ {
disconnectFromServer(); disconnectFromServer();
} }
handlerList.resize(MSGTYPE_SIZE); QTcpSocket* sock = new QTcpSocket();
handlerList[ERROR] = new ClientErrorHandler(this); connect(sock, SIGNAL(connected()), this, SLOT(socketConnected()));
handlerList[SERVER_CHALLENGE] = new ClientAuthHandler(this); server = new SyncRemotePeer(sock, true, handlerList );
handlerList[SERVER_CHALLENGERESPONSEVALID] = new ClientAuthHandler(t connect(server, SIGNAL(disconnected(bool)), this, SLOT(serverDisconn
his); ected(bool)));
handlerList[ALIVE] = new ClientAliveHandler();
//these are the actual sync handlers
handlerList[TIME] = new ClientTimeHandler();
handlerList[LOCATION] = new ClientLocationHandler();
handlerList[SELECTION] = new ClientSelectionHandler();
server = new SyncRemotePeer(new QTcpSocket(this), true, handlerList
);
connect(server->sock, SIGNAL(connected()), this, SLOT(socketConnecte
d()));
connect(server->sock, SIGNAL(disconnected()),this, SLOT(socketDiscon
nected()));
connect(server->sock, SIGNAL(error(QAbstractSocket::SocketError)), t
his, SLOT(socketError(QAbstractSocket::SocketError)));
connect(server->sock, SIGNAL(readyRead()), this, SLOT(dataReceived()
));
isConnecting = true; isConnecting = true;
qDebug()<<"[SyncClient] Connecting to"<<(host + ":" + QString::numbe qCDebug(syncClient)<<"Connecting to"<<(host + ":" + QString::number(
r(port)); port))<<", with options"<<options;
server->sock->connectToHost(host,port);
timeoutTimerId = startTimer(2000,Qt::VeryCoarseTimer); //the connect ion is checked all 5 seconds timeoutTimerId = startTimer(2000,Qt::VeryCoarseTimer); //the connect ion is checked all 5 seconds
sock->connectToHost(host,port);
} }
void SyncClient::disconnectFromServer() void SyncClient::disconnectFromServer()
{ {
if(server) if(server)
{ {
if(server->sock->state()!= QAbstractSocket::UnconnectedState server->disconnectPeer();
)
{
server->sock->disconnectFromHost();
if(!server)
return;
if(server->sock->state() != QAbstractSocket::Unconne
ctedState && !server->sock->waitForDisconnected(500))
{
qDebug()<<"[SyncClient] Error disconnecting,
aborting socket:"<<server->sock->error();
server->sock->abort();
}
}
//this must be tested AGAIN because waitForDisconnected migh
t re-enter here trough the disconnected signal
if(server)
{
killTimer(timeoutTimerId);
isConnecting = false;
server->sock->deleteLater();
delete server;
server = NULL;
//delete handlers
foreach(SyncMessageHandler* h, handlerList)
{
if(h)
delete h;
}
handlerList.clear();
}
} }
} }
void SyncClient::timerEvent(QTimerEvent *evt) void SyncClient::timerEvent(QTimerEvent *evt)
{ {
if(evt->timerId() == timeoutTimerId) if(evt->timerId() == timeoutTimerId)
{ {
checkTimeout(); checkTimeout();
evt->accept(); evt->accept();
} }
} }
void SyncClient::checkTimeout() void SyncClient::checkTimeout()
{ {
if(!server) if(!server)
return; return;
qint64 curTime = QDateTime::currentMSecsSinceEpoch(); server->checkTimeout();
qint64 diff = curTime - server->lastReceiveTime;
qint64 writeDiff = curTime - server->lastSendTime;
if(writeDiff>5000)
{
//send an ALIVE message
Alive msg;
server->writeMessage(msg);
}
if(diff > 15000)
{
qDebug()<<"[SyncClient] No data received for"<<diff<<"ms, ti
ming out";
emitError(q_("Connection timed out"));
emit connectionError();
}
} }
bool SyncClient::isConnected() const void SyncClient::serverDisconnected(bool clean)
{ {
return server->sock->state() == QAbstractSocket::ConnectedState; qCDebug(syncClient)<<"Disconnected from server";
if(!clean)
errorStr = server->getError();
server->deleteLater();
server = Q_NULLPTR;
emit disconnected(errorStr.isEmpty());
} }
void SyncClient::socketConnected() void SyncClient::socketConnected()
{ {
isConnecting = false; qCDebug(syncClient)<<"Socket connected";
qDebug()<<"[SyncClient] Socket connection established, waiting for c
hallenge";
//set low delay option
server->sock->setSocketOption(QAbstractSocket::LowDelayOption,1);
}
void SyncClient::socketDisconnected()
{
qDebug()<<"[SyncClient] Socket disconnected";
disconnectFromServer();
emit disconnected();
}
void SyncClient::socketError(QAbstractSocket::SocketError err)
{
Q_UNUSED(err);
emitError(server->sock->errorString());
}
void SyncClient::emitError(const QString &msg)
{
errorStr = msg;
qDebug()<<"[SyncClient] Connection error:"<<msg<<", connection state
:"<<server->sock->state();
disconnectFromServer();
emit connectionError();
} }
void SyncClient::dataReceived() void SyncClient::emitServerError(const QString &errorStr)
{ {
qDebug()<<"[SyncClient] server data received"; this->errorStr = errorStr;
//a chunk of data is avaliable for reading
server->receiveMessage();
} }
 End of changes. 14 change blocks. 
114 lines changed or deleted 69 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/