Merge pull request #342 from PhenomRetroShare/Fix_AllowOnlyOneInst

Fix Allow Only One Instance
This commit is contained in:
Cyril Soler 2016-04-08 20:41:12 -04:00
commit 85c8274f47

View file

@ -128,54 +128,55 @@ Rshare::Rshare(QStringList args, int &argc, char **argv, const QString &dir)
{ {
QString serverName = QString(TARGET); QString serverName = QString(TARGET);
if (args.isEmpty()) args.append("Empty"); if (!args.isEmpty()) {
// load into shared memory // load into shared memory
QBuffer buffer; QBuffer buffer;
buffer.open(QBuffer::ReadWrite); buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer); QDataStream out(&buffer);
out << args; out << args;
int size = buffer.size(); int size = buffer.size();
QSharedMemory newArgs; QSharedMemory newArgs;
newArgs.setKey(serverName + "_newArgs"); newArgs.setKey(serverName + "_newArgs");
if (newArgs.isAttached()) newArgs.detach(); if (newArgs.isAttached()) newArgs.detach();
if (!newArgs.create(size)) { if (!newArgs.create(size)) {
std::cerr << "(EE) Rshare::Rshare Unable to create shared memory segment of size:" std::cerr << "(EE) Rshare::Rshare Unable to create shared memory segment of size:"
<< size << " error:" << newArgs.errorString().toStdString() << "." << std::endl; << size << " error:" << newArgs.errorString().toStdString() << "." << std::endl;
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
std::cerr << "Look with `ipcs -m` for nattch==0 segment. And remove it with `ipcrm -m 'shmid'`." << std::endl; std::cerr << "Look with `ipcs -m` for nattch==0 segment. And remove it with `ipcrm -m 'shmid'`." << std::endl;
//No need for windows, as it removes shared segment directly even when crash. //No need for windows, as it removes shared segment directly even when crash.
#endif #endif
newArgs.detach(); newArgs.detach();
::exit(EXIT_FAILURE); ::exit(EXIT_FAILURE);
} }
newArgs.lock(); newArgs.lock();
char *to = (char*)newArgs.data(); char *to = (char*)newArgs.data();
const char *from = buffer.data().data(); const char *from = buffer.data().data();
memcpy(to, from, qMin(newArgs.size(), size)); memcpy(to, from, qMin(newArgs.size(), size));
newArgs.unlock(); newArgs.unlock();
// Connect to the Local Server of the main process to notify it // Connect to the Local Server of the main process to notify it
// that a new process had been started // that a new process had been started
QLocalSocket localSocket; QLocalSocket localSocket;
localSocket.connectToServer(QString(TARGET)); localSocket.connectToServer(QString(TARGET));
std::cerr << "Rshare::Rshare waitForConnected to other instance." << std::endl; std::cerr << "Rshare::Rshare waitForConnected to other instance." << std::endl;
if( localSocket.waitForConnected(100) ) if( localSocket.waitForConnected(100) )
{ {
std::cerr << "Rshare::Rshare Connection etablished. Waiting for disconnection." << std::endl; std::cerr << "Rshare::Rshare Connection etablished. Waiting for disconnection." << std::endl;
localSocket.waitForDisconnected(1000); localSocket.waitForDisconnected(1000);
newArgs.detach();
::exit(EXIT_SUCCESS); // Terminate the program using STDLib's exit function
}
newArgs.detach(); newArgs.detach();
::exit(EXIT_SUCCESS); // Terminate the program using STDLib's exit function
} else {
newArgs.detach();
// No main process exists
// So we start a Local Server to listen for connections from new process
localServer= new QLocalServer();
QObject::connect(localServer, SIGNAL(newConnection()), this, SLOT(slotConnectionEstablished()));
updateLocalServer();
} }
// No main process exists
// Or started without arguments
// So we start a Local Server to listen for connections from new process
localServer= new QLocalServer();
QObject::connect(localServer, SIGNAL(newConnection()), this, SLOT(slotConnectionEstablished()));
updateLocalServer();
} }
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)