Browse Source

Forward declare types as much as possible

adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
5c015d573b
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 69
      src/app/qtlocalpeer/qtlocalpeer.cpp
  2. 50
      src/app/qtlocalpeer/qtlocalpeer.h
  3. 34
      src/app/qtlocalpeer/qtlockedfile.cpp
  4. 85
      src/app/qtlocalpeer/qtlockedfile.h
  5. 53
      src/app/qtlocalpeer/qtlockedfile_unix.cpp
  6. 38
      src/app/qtlocalpeer/qtlockedfile_win.cpp

69
src/app/qtlocalpeer/qtlocalpeer.cpp

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,28 +63,34 @@ @@ -36,28 +63,34 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#include "qtlocalpeer.h"
#if defined(Q_OS_UNIX)
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#endif
#include <QCoreApplication>
#include <QDataStream>
#include <QTime>
#include <QDir>
#include <QLocalServer>
#include <QLocalSocket>
#if defined(Q_OS_WIN)
#include <QLibrary>
#include <qt_windows.h>
typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*);
static PProcessIdToSessionId pProcessIdToSessionId = 0;
#endif
#if defined(Q_OS_UNIX)
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#endif
namespace QtLP_Private {
namespace QtLP_Private
{
#include "qtlockedfile.cpp"
#if defined(Q_OS_WIN)
#include "qtlockedfile_win.cpp"
#else
@ -68,7 +101,8 @@ namespace QtLP_Private { @@ -68,7 +101,8 @@ namespace QtLP_Private {
const char* QtLocalPeer::ack = "ack";
QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
: QObject(parent), id(appId)
: QObject(parent)
, id(appId)
{
QString prefix = id;
if (id.isEmpty()) {
@ -109,8 +143,6 @@ QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId) @@ -109,8 +143,6 @@ QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
lockFile.open(QIODevice::ReadWrite);
}
bool QtLocalPeer::isClient()
{
if (lockFile.isLocked())
@ -120,7 +152,7 @@ bool QtLocalPeer::isClient() @@ -120,7 +152,7 @@ bool QtLocalPeer::isClient()
return true;
bool res = server->listen(socketName);
#if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
#if defined(Q_OS_UNIX)
// ### Workaround
if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
@ -133,8 +165,7 @@ bool QtLocalPeer::isClient() @@ -133,8 +165,7 @@ bool QtLocalPeer::isClient()
return false;
}
bool QtLocalPeer::sendMessage(const QString &message, int timeout)
bool QtLocalPeer::sendMessage(const QString &message, const int timeout)
{
if (!isClient())
return false;
@ -170,6 +201,10 @@ bool QtLocalPeer::sendMessage(const QString &message, int timeout) @@ -170,6 +201,10 @@ bool QtLocalPeer::sendMessage(const QString &message, int timeout)
return res;
}
QString QtLocalPeer::applicationId() const
{
return id;
}
void QtLocalPeer::receiveConnection()
{

50
src/app/qtlocalpeer/qtlocalpeer.h

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,38 +63,37 @@ @@ -36,38 +63,37 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#ifndef QTLOCALPEER_H
#define QTLOCALPEER_H
#include <QLocalServer>
#include <QLocalSocket>
#include <QDir>
#include "qtlockedfile.h"
class QLocalServer;
class QtLocalPeer : public QObject
{
Q_OBJECT
public:
QtLocalPeer(QObject *parent = 0, const QString &appId = QString());
QtLocalPeer(QObject *parent = nullptr, const QString &appId = QString());
bool isClient();
bool sendMessage(const QString &message, int timeout);
QString applicationId() const
{ return id; }
QString applicationId() const;
Q_SIGNALS:
signals:
void messageReceived(const QString &message);
protected Q_SLOTS:
protected slots:
void receiveConnection();
protected:
QString id;
QString socketName;
QLocalServer* server;
QLocalServer *server = nullptr;
QtLP_Private::QtLockedFile lockFile;
private:

34
src/app/qtlocalpeer/qtlockedfile.cpp

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,7 +63,8 @@ @@ -36,7 +63,8 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#include "qtlockedfile.h"
@ -120,7 +148,7 @@ QtLockedFile::QtLockedFile(const QString &name) @@ -120,7 +148,7 @@ QtLockedFile::QtLockedFile(const QString &name)
\sa QFile::open(), QFile::resize()
*/
bool QtLockedFile::open(OpenMode mode)
bool QtLockedFile::open(const OpenMode mode)
{
if (mode & QIODevice::Truncate) {
qWarning("QtLockedFile::open(): Truncate mode not allowed.");

85
src/app/qtlocalpeer/qtlockedfile.h

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,46 +63,54 @@ @@ -36,46 +63,54 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#ifndef QTLOCKEDFILE_H
#define QTLOCKEDFILE_H
#include <QFile>
#ifdef Q_OS_WIN
#include <QVector>
#endif
namespace QtLP_Private {
class QtLockedFile : public QFile
namespace QtLP_Private
{
public:
enum LockMode { NoLock = 0, ReadLock, WriteLock };
class QtLockedFile : public QFile
{
public:
enum LockMode
{
NoLock = 0,
ReadLock,
WriteLock
};
QtLockedFile();
QtLockedFile(const QString &name);
~QtLockedFile();
QtLockedFile();
QtLockedFile(const QString &name);
~QtLockedFile();
bool open(OpenMode mode) override;
bool open(OpenMode mode) override;
bool lock(LockMode mode, bool block = true);
bool unlock();
bool isLocked() const;
LockMode lockMode() const;
bool lock(LockMode mode, bool block = true);
bool unlock();
bool isLocked() const;
LockMode lockMode() const;
private:
private:
#ifdef Q_OS_WIN
Qt::HANDLE wmutex;
Qt::HANDLE rmutex;
QVector<Qt::HANDLE> rmutexes;
QString mutexname;
Qt::HANDLE getMutexHandle(int idx, bool doCreate);
bool waitMutex(Qt::HANDLE mutex, bool doBlock);
Qt::HANDLE getMutexHandle(int idx, bool doCreate);
bool waitMutex(Qt::HANDLE mutex, bool doBlock);
Qt::HANDLE wmutex;
Qt::HANDLE rmutex;
QVector<Qt::HANDLE> rmutexes;
QString mutexname;
#endif
LockMode m_lock_mode;
};
LockMode m_lock_mode;
};
}
#endif

53
src/app/qtlocalpeer/qtlockedfile_unix.cpp

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,14 +63,15 @@ @@ -36,14 +63,15 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#include "qtlockedfile.h"
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "qtlockedfile.h"
#include <string.h>
#include <unistd.h>
bool QtLockedFile::lock(LockMode mode, bool block)
{
@ -51,10 +79,10 @@ bool QtLockedFile::lock(LockMode mode, bool block) @@ -51,10 +79,10 @@ bool QtLockedFile::lock(LockMode mode, bool block)
qWarning("QtLockedFile::lock(): file is not opened");
return false;
}
if (mode == NoLock)
return unlock();
if (mode == m_lock_mode)
return true;
@ -68,19 +96,17 @@ bool QtLockedFile::lock(LockMode mode, bool block) @@ -68,19 +96,17 @@ bool QtLockedFile::lock(LockMode mode, bool block)
fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
int cmd = block ? F_SETLKW : F_SETLK;
int ret = fcntl(handle(), cmd, &fl);
if (ret == -1) {
if (errno != EINTR && errno != EAGAIN)
qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
return false;
}
m_lock_mode = mode;
return true;
}
bool QtLockedFile::unlock()
{
if (!isOpen()) {
@ -97,12 +123,12 @@ bool QtLockedFile::unlock() @@ -97,12 +123,12 @@ bool QtLockedFile::unlock()
fl.l_len = 0;
fl.l_type = F_UNLCK;
int ret = fcntl(handle(), F_SETLKW, &fl);
if (ret == -1) {
qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
return false;
}
m_lock_mode = NoLock;
return true;
}
@ -112,4 +138,3 @@ QtLockedFile::~QtLockedFile() @@ -112,4 +138,3 @@ QtLockedFile::~QtLockedFile()
if (isOpen())
unlock();
}

38
src/app/qtlocalpeer/qtlockedfile_win.cpp

@ -1,4 +1,31 @@ @@ -1,4 +1,31 @@
/****************************************************************************
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2019 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
@ -36,19 +63,18 @@ @@ -36,19 +63,18 @@
**
** $QT_END_LICENSE$
**
****************************************************************************/
****************************************************************************
*/
#include "qtlockedfile.h"
#include <qt_windows.h>
#include <QFileInfo>
#define MUTEX_PREFIX "QtLockedFile mutex "
// Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
#define MAX_READERS MAXIMUM_WAIT_OBJECTS
#if QT_VERSION >= 0x050000
#define QT_WA(unicode, ansi) unicode
#endif
Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
{
@ -99,8 +125,6 @@ bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock) @@ -99,8 +125,6 @@ bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
return false;
}
bool QtLockedFile::lock(LockMode mode, bool block)
{
if (!isOpen()) {

Loading…
Cancel
Save