1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/** \file LocalListener.cpp
\brief The have local server, to have unique instance, and send arguments to the current running instance
\author alpha_one_x86
\licence GPL3, see the file COPYING */
#include "LocalListener.h"
#include "PluginsManager.h"
#include <QLocalSocket>
#include <QDir>
LocalListener::LocalListener(QObject *parent) :
QObject(parent)
{
//for detect the timeout on QLocalSocket
TimeOutQLocalSocket.setInterval(500);
TimeOutQLocalSocket.setSingleShot(true);
connect(&TimeOutQLocalSocket, &QTimer::timeout, this, &LocalListener::timeoutDectected);
connect(PluginsManager::pluginsManager,&PluginsManager::pluginListingIsfinish,this, &LocalListener::allPluginIsloaded,Qt::QueuedConnection);
}
LocalListener::~LocalListener()
{
if(localServer.isListening())
{
localServer.close();
if(!QLocalServer::removeServer(QString::fromStdString(ExtraSocket::pathSocket(ULTRACOPIER_SOCKETNAME))))
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to remove the listening server");
}
}
bool LocalListener::tryConnect()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
QStringList ultracopierArguments=QCoreApplication::arguments();
//remove excutable path because is useless (unsafe to use)
ultracopierArguments.removeFirst();
//add the current path to file full path resolution if needed
ultracopierArguments.insert(0,QDir::currentPath());
std::vector<std::string> ultracopierArgumentsStd;
{
int index=0;
while(index<ultracopierArguments.size())
{
ultracopierArgumentsStd.push_back(ultracopierArguments.at(index).toStdString());
index++;
}
}
QLocalSocket localSocket;
localSocket.connectToServer(QString::fromStdString(ExtraSocket::pathSocket(ULTRACOPIER_SOCKETNAME)),QIODevice::WriteOnly);
if(localSocket.waitForConnected(1000))
{
if(!localSocket.isValid())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"localSocket is not valid!");
return false;
}
emit cli(ultracopierArgumentsStd,false,true);
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"connection succes, number arguments given: "+std::to_string(ultracopierArgumentsStd.size()));
#ifdef ULTRACOPIER_DEBUG
for (int i = 0; i < ultracopierArguments.size(); ++i) {
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"argument["+std::to_string(i)+"]: "+ultracopierArgumentsStd.at(i));
}
#endif // ULTRACOPIER_DEBUG
//cut string list and send it as block of 32KB
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
//for total size
out << int(0);
//send the arguments
out << ultracopierArguments;
//write the size content
out.device()->seek(0);
out << block.size();
do
{
QByteArray blockToSend;
blockToSend=block.left(32*1024);//32KB
block.remove(0,blockToSend.size());
#ifdef ULTRACOPIER_DEBUG
int byteWriten =
#endif
localSocket.write(blockToSend);
localSocket.readAll();
#ifdef ULTRACOPIER_DEBUG
if(!localSocket.isValid())
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"localSocket is not valid!");
if(localSocket.errorString()!="Unknown error" && localSocket.errorString()!="")
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"localSocket->errorString(): "+localSocket.errorString().toStdString());
if(blockToSend.size()!=byteWriten)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"blockToSend("+std::to_string(blockToSend.size())+
")!=byteWriten("+std::to_string(byteWriten)+")");
#endif // ULTRACOPIER_DEBUG
if(localSocket.waitForBytesWritten())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Block send correctly");
}
else
{
QMessageBox::critical(NULL,"Alert","No arguments send because timeout detected!");
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Block not send correctly");
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"blockToSend: "+blockToSend.toHex().toStdString());
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"byteWriten: "+std::to_string(byteWriten)+
", size sending: "+std::to_string(blockToSend.size()));
}
while(block.size());
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"disconnect the socket");
localSocket.readAll();
localSocket.waitForBytesWritten();
QThread::msleep(50);
localSocket.disconnectFromServer();
QThread::msleep(50);
localSocket.waitForDisconnected();
return true;
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"connection failed, continue...");
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"ultracopierArguments: "+ultracopierArguments.join(";").toStdString());
return false;
}
}
/// the listen server
void LocalListener::listenServer()
{
if(!QLocalServer::removeServer(QString::fromStdString(ExtraSocket::pathSocket(ULTRACOPIER_SOCKETNAME))))
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to remove the listening server");
#ifndef Q_OS_MAC
localServer.setSocketOptions(QLocalServer::UserAccessOption);
#endif
if(!localServer.listen(QString::fromStdString(ExtraSocket::pathSocket(ULTRACOPIER_SOCKETNAME))))
{
#ifndef Q_OS_MAC
//QMessageBox::critical(NULL,"Alert",QStringLiteral("Ultracopier have not able to lock unique instance: %1").arg(localServer.errorString()));
#endif
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Ultracopier have not able to lock unique instance: "+localServer.errorString().toStdString()+
", error code: "+std::to_string((int32_t)localServer.serverError()));
}
else
connect(&localServer, &QLocalServer::newConnection, this, &LocalListener::newConnexion);
}
//the time is done
void LocalListener::timeoutDectected()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"start");
if(clientList.size()>0)
{
unsigned int index=0;
bool haveData=false;
while(index<clientList.size())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"clientList.first().size: "+std::to_string(clientList.front().size));
if(!clientList.at(index).data.isEmpty() || clientList.at(index).haveData)
{
haveData=true;
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Timeout while recomposing data from connected clients: "+clientList.at(index).data.toHex().toStdString());
clientList.erase(clientList.cbegin());
}
else
index++;
}
if(haveData)
QMessageBox::warning(NULL,tr("Warning"),tr("Timeout while recomposing data from connected clients"));
}
}
/// \brief Data is incomming
void LocalListener::dataIncomming()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"start");
// 1 : we get packets from client
//Which client send the message (Search of the QLocalSocket of client)
QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender());
if (socket == 0) // If not found
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"bad socket");
return;
}
int index=-1;
for (unsigned int i=0;i<clientList.size(); ++i) {
if(clientList.at(i).socket==socket)
index=i;
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"socket->bytesAvailable() "+std::to_string(socket->bytesAvailable()));
if(index!=-1)
{
if(!clientList.at(index).haveData)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"tempComposed index found, but no have data, create new entry");
// If all is ok we get the message
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_4);
if (socket->bytesAvailable() < (int)sizeof(quint32)*2) // We have not receveive all the message, ignore because first int is cuted!
{
/*socket->readAll();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"wrong size for set the message size");*/
return;
}
in >> clientList[index].size; // Store the size of the message
clientList[index].size-=sizeof(int);
// Check if all the message size is the same as the size given
if(socket->bytesAvailable() < clientList.at(index).size) // If not all get then stop it
{
clientList[index].haveData=true;
clientList[index].data.append(socket->readAll());
TimeOutQLocalSocket.start();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Need wait to recomposite: "+std::to_string(clientList.at(index).data.size())+
", targeted: "+std::to_string(clientList.at(index).size));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"tempComposed.data: "+clientList.at(index).data.toHex().toStdString());
}
else if(socket->bytesAvailable() == clientList.at(index).size) //if the size match
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"socket->bytesAvailable(): "+std::to_string(socket->bytesAvailable())+
", for total of: "+std::to_string(socket->bytesAvailable()+sizeof(uint32_t)));
QStringList ultracopierArguments;
in >> ultracopierArguments;
std::vector<std::string> ultracopierArgumentsStd;
{
int index=0;
while(index<ultracopierArguments.size())
{
ultracopierArgumentsStd.push_back(ultracopierArguments.at(index).toStdString());
index++;
}
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"ultracopierArguments: "+ultracopierArguments.join(";").toStdString());
emit cli(ultracopierArgumentsStd,true,false);
clientList[index].data.clear();
clientList[index].haveData=false;
TimeOutQLocalSocket.stop();
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"socket->bytesAvailable(): "+std::to_string(socket->bytesAvailable())+" > clientList.at(index).size!: "+std::to_string(clientList.at(index).size));
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Query recomposed with this size: "+std::to_string(clientList.at(index).data.size()));
clientList[index].data.append(socket->readAll());
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Query recomposed with this size: "+std::to_string(clientList.at(index).data.size()));
if(clientList.at(index).data.size()==clientList.at(index).size)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"QByteArray reconstruction finished");
QDataStream in(clientList.at(index).data);
QStringList ultracopierArguments;
in >> ultracopierArguments;
std::vector<std::string> ultracopierArgumentsStd;
{
int index=0;
while(index<ultracopierArguments.size())
{
ultracopierArgumentsStd.push_back(ultracopierArguments.at(index).toStdString());
index++;
}
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"ultracopierArguments: "+ultracopierArguments.join(";").toStdString());
emit cli(ultracopierArgumentsStd,true,false);
clientList[index].data.clear();
clientList[index].haveData=false;
TimeOutQLocalSocket.stop();
}
else
{
TimeOutQLocalSocket.start();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Need wait to recomposite: "+std::to_string(clientList.at(index).data.size())+", targeted: "+std::to_string(clientList.at(index).size));
return;
}
}
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Socket not found???");
}
/// \brief Deconnexion client
/// \todo Remove the data in wait linker with this socket
void LocalListener::deconnectClient()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
// Wich client leave
QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender());
if (socket == 0) // If not found
return;
for (unsigned int i = 0; i < clientList.size(); ++i) {
if(clientList.at(i).socket==socket)
clientList.erase(clientList.cbegin()+i);
}
socket->deleteLater();
}
/// LocalListener New connexion
void LocalListener::newConnexion()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"start");
ComposedData newClient;
newClient.socket = localServer.nextPendingConnection();
#ifdef ULTRACOPIER_DEBUG
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
connect(newClient.socket, static_cast<void(QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::error), this, &LocalListener::error);
#else
connect(newClient.socket, static_cast<void(QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::errorOccurred), this, &LocalListener::error);
#endif
//connect(newClient.socket, &QLocalSocket::error, this, &LocalListener::error);
//connect(newClient.socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(error(QLocalSocket::LocalSocketError)));
#endif
connect(newClient.socket, &QLocalSocket::readyRead, this, &LocalListener::dataIncomming);
connect(newClient.socket, &QLocalSocket::disconnected, this, &LocalListener::deconnectClient);
newClient.size=-1;
newClient.haveData=false;
clientList.push_back(newClient);
}
#ifdef ULTRACOPIER_DEBUG
/** \brief If error occured at socket
\param theErrorDefine The error define */
void LocalListener::error(const QLocalSocket::LocalSocketError &theErrorDefine)
{
if(theErrorDefine!=QLocalSocket::PeerClosedError)
{
QLocalSocket *client=qobject_cast<QLocalSocket *>(QObject::sender());
if(client!=NULL)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Value:"+std::to_string(theErrorDefine)+", Error message: "+client->errorString().toStdString());
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Value:"+std::to_string(theErrorDefine));
}
}
#endif
/// \can now parse the cli
void LocalListener::allPluginIsloaded()
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
QStringList ultracopierArguments=QCoreApplication::arguments();
//remove excutable path because is useless (unsafe to use)
ultracopierArguments.removeFirst();
//add the current path to file full path resolution if needed
ultracopierArguments.insert(0,QDir::currentPath());
std::vector<std::string> ultracopierArgumentsStd;
{
int index=0;
while(index<ultracopierArguments.size())
{
ultracopierArgumentsStd.push_back(ultracopierArguments.at(index).toStdString());
index++;
}
}
emit cli(ultracopierArgumentsStd,false,false);
}
|