"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/qtsingleapplication/src/qtlocalpeer.cpp" between
cb2bib-1.9.9.tar.gz and cb2bib-2.0.0.tar.gz

About: cb2Bib is a multiplatform application for rapidly extracting unformatted, or unstandardized bibliographic references from email alerts, journal Web pages, and PDF files.

qtlocalpeer.cpp  (cb2bib-1.9.9):qtlocalpeer.cpp  (cb2bib-2.0.0)
skipping to change at line 58 skipping to change at line 58
#include <qt_windows.h> #include <qt_windows.h>
typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*); typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*);
static PProcessIdToSessionId pProcessIdToSessionId = 0; static PProcessIdToSessionId pProcessIdToSessionId = 0;
#endif #endif
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
namespace QtLP_Private { namespace QtLP_Private
{
#include "qtlockedfile.cpp" #include "qtlockedfile.cpp"
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
#include "qtlockedfile_win.cpp" #include "qtlockedfile_win.cpp"
#else #else
#include "qtlockedfile_unix.cpp" #include "qtlockedfile_unix.cpp"
#endif #endif
} }
const char* QtLocalPeer::ack = "ack"; const char* QtLocalPeer::ack = "ack";
QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId) QtLocalPeer::QtLocalPeer(QObject* parento, const QString& appId)
: QObject(parent), id(appId) : QObject(parento), id(appId)
{ {
QString prefix = id; QString prefix = id;
if (id.isEmpty()) { if (id.isEmpty())
{
id = QCoreApplication::applicationFilePath(); id = QCoreApplication::applicationFilePath();
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
id = id.toLower(); id = id.toLower();
#endif #endif
prefix = id.section(QLatin1Char('/'), -1); prefix = id.section(QLatin1Char('/'), -1);
} }
prefix.remove(QRegExp("[^a-zA-Z]")); prefix.remove(QRegExp("[^a-zA-Z]"));
prefix.truncate(6); prefix.truncate(6);
QByteArray idc = id.toUtf8(); QByteArray idc = id.toUtf8();
quint16 idNum = qChecksum(idc.constData(), idc.size()); quint16 idNum = qChecksum(idc.constData(), idc.size());
socketName = QLatin1String("qtsingleapp-") + prefix socketName = QLatin1String("qtsingleapp-") + prefix
+ QLatin1Char('-') + QString::number(idNum, 16); + QLatin1Char('-') + QString::number(idNum, 16);
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
if (!pProcessIdToSessionId) { if (!pProcessIdToSessionId)
{
QLibrary lib("kernel32"); QLibrary lib("kernel32");
pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToS essionId"); pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToS essionId");
} }
if (pProcessIdToSessionId) { if (pProcessIdToSessionId)
{
DWORD sessionId = 0; DWORD sessionId = 0;
pProcessIdToSessionId(GetCurrentProcessId(), &sessionId); pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
socketName += QLatin1Char('-') + QString::number(sessionId, 16); socketName += QLatin1Char('-') + QString::number(sessionId, 16);
} }
#else #else
socketName += QLatin1Char('-') + QString::number(::getuid(), 16); socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
#endif #endif
server = new QLocalServer(this); server = new QLocalServer(this);
QString lockName = QDir(QDir::tempPath()).absolutePath() QString lockName = QDir(QDir::tempPath()).absolutePath()
skipping to change at line 121 skipping to change at line 125
{ {
if (lockFile.isLocked()) if (lockFile.isLocked())
return false; return false;
if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false)) if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false))
return true; return true;
bool res = server->listen(socketName); bool res = server->listen(socketName);
#if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0)) #if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
// ### Workaround // ### Workaround
if (!res && server->serverError() == QAbstractSocket::AddressInUseError) { if (!res && server->serverError() == QAbstractSocket::AddressInUseError)
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketN {
ame); QFile::remove(QDir::cleanPath(QDir::tempPath()) + QLatin1Char('/') + soc
ketName);
res = server->listen(socketName); res = server->listen(socketName);
} }
#endif #endif
if (!res) if (!res)
qWarning("QtSingleCoreApplication: listen on local socket failed, %s", q Printable(server->errorString())); qWarning("QtSingleCoreApplication: listen on local socket failed, %s", q Printable(server->errorString()));
QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection())) ; QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection())) ;
return false; return false;
} }
bool QtLocalPeer::sendMessage(const QString &message, int timeout) bool QtLocalPeer::sendMessage(const QString& message, int timeout)
{ {
if (!isClient()) if (!isClient())
return false; return false;
QLocalSocket socket; QLocalSocket socket;
bool connOk = false; bool connOk = false;
for(int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++)
{
// Try twice, in case the other instance is just starting up // Try twice, in case the other instance is just starting up
socket.connectToServer(socketName); socket.connectToServer(socketName);
connOk = socket.waitForConnected(timeout/2); connOk = socket.waitForConnected(timeout / 2);
if (connOk || i) if (connOk || i)
break; break;
int ms = 250; int ms = 250;
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
Sleep(DWORD(ms)); Sleep(DWORD(ms));
#else #else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
#endif #endif
} }
if (!connOk) if (!connOk)
return false; return false;
QByteArray uMsg(message.toUtf8()); QByteArray uMsg(message.toUtf8());
QDataStream ds(&socket); QDataStream ds(&socket);
ds.writeBytes(uMsg.constData(), uMsg.size()); ds.writeBytes(uMsg.constData(), uMsg.size());
bool res = socket.waitForBytesWritten(timeout); bool res = socket.waitForBytesWritten(timeout);
if (res) { if (res)
{
res &= socket.waitForReadyRead(timeout); // wait for ack res &= socket.waitForReadyRead(timeout); // wait for ack
if (res) if (res)
res &= (socket.read(qstrlen(ack)) == ack); res &= (socket.read(qstrlen(ack)) == ack);
} }
return res; return res;
} }
void QtLocalPeer::receiveConnection() void QtLocalPeer::receiveConnection()
{ {
QLocalSocket* socket = server->nextPendingConnection(); QLocalSocket* socket = server->nextPendingConnection();
skipping to change at line 183 skipping to change at line 190
while (socket->bytesAvailable() < (int)sizeof(quint32)) while (socket->bytesAvailable() < (int)sizeof(quint32))
socket->waitForReadyRead(); socket->waitForReadyRead();
QDataStream ds(socket); QDataStream ds(socket);
QByteArray uMsg; QByteArray uMsg;
quint32 remaining; quint32 remaining;
ds >> remaining; ds >> remaining;
uMsg.resize(remaining); uMsg.resize(remaining);
int got = 0; int got = 0;
char* uMsgBuf = uMsg.data(); char* uMsgBuf = uMsg.data();
do { do
{
got = ds.readRawData(uMsgBuf, remaining); got = ds.readRawData(uMsgBuf, remaining);
remaining -= got; remaining -= got;
uMsgBuf += got; uMsgBuf += got;
} while (remaining && got >= 0 && socket->waitForReadyRead(2000)); }
if (got < 0) { while (remaining && got >= 0 && socket->waitForReadyRead(2000));
if (got < 0)
{
qWarning("QtLocalPeer: Message reception failed %s", socket->errorString ().toLatin1().constData()); qWarning("QtLocalPeer: Message reception failed %s", socket->errorString ().toLatin1().constData());
delete socket; delete socket;
return; return;
} }
QString message(QString::fromUtf8(uMsg)); QString message(QString::fromUtf8(uMsg));
socket->write(ack, qstrlen(ack)); socket->write(ack, qstrlen(ack));
socket->waitForBytesWritten(1000); socket->waitForBytesWritten(1000);
socket->waitForDisconnected(1000); // make sure client reads ack socket->waitForDisconnected(1000); // make sure client reads ack
delete socket; delete socket;
emit messageReceived(message); //### (might take a long time to return) emit messageReceived(message); //### (might take a long time to return)
 End of changes. 12 change blocks. 
16 lines changed or deleted 26 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)