diff --git a/src/app/qtlocalpeer/qtlocalpeer.cpp b/src/app/qtlocalpeer/qtlocalpeer.cpp index d8a1bce31..701e9fa79 100644 --- a/src/app/qtlocalpeer/qtlocalpeer.cpp +++ b/src/app/qtlocalpeer/qtlocalpeer.cpp @@ -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 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ - +**************************************************************************** +*/ #include "qtlocalpeer.h" -#include -#include -#include -#if defined(Q_OS_WIN) -#include -#include -typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*); -static PProcessIdToSessionId pProcessIdToSessionId = 0; -#endif #if defined(Q_OS_UNIX) #include #include #include #endif -namespace QtLP_Private { +#include +#include +#include +#include +#include + +#if defined(Q_OS_WIN) +#include + +typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*); +static PProcessIdToSessionId pProcessIdToSessionId = 0; +#endif + +namespace QtLP_Private +{ #include "qtlockedfile.cpp" + #if defined(Q_OS_WIN) #include "qtlockedfile_win.cpp" #else @@ -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) lockFile.open(QIODevice::ReadWrite); } - - bool QtLocalPeer::isClient() { if (lockFile.isLocked()) @@ -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() 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) return res; } +QString QtLocalPeer::applicationId() const +{ + return id; +} void QtLocalPeer::receiveConnection() { diff --git a/src/app/qtlocalpeer/qtlocalpeer.h b/src/app/qtlocalpeer/qtlocalpeer.h index 1b533b1ab..ac8b1ff27 100644 --- a/src/app/qtlocalpeer/qtlocalpeer.h +++ b/src/app/qtlocalpeer/qtlocalpeer.h @@ -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 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ +**************************************************************************** +*/ #ifndef QTLOCALPEER_H #define QTLOCALPEER_H -#include -#include -#include - #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: diff --git a/src/app/qtlocalpeer/qtlockedfile.cpp b/src/app/qtlocalpeer/qtlockedfile.cpp index c142a863a..9b6b2acb1 100644 --- a/src/app/qtlocalpeer/qtlockedfile.cpp +++ b/src/app/qtlocalpeer/qtlockedfile.cpp @@ -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 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ +**************************************************************************** +*/ #include "qtlockedfile.h" @@ -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."); diff --git a/src/app/qtlocalpeer/qtlockedfile.h b/src/app/qtlocalpeer/qtlockedfile.h index ad3c5ab39..11f0b3b95 100644 --- a/src/app/qtlocalpeer/qtlockedfile.h +++ b/src/app/qtlocalpeer/qtlockedfile.h @@ -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 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ +**************************************************************************** +*/ #ifndef QTLOCKEDFILE_H #define QTLOCKEDFILE_H #include + #ifdef Q_OS_WIN #include #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 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 rmutexes; + QString mutexname; #endif - LockMode m_lock_mode; -}; + + LockMode m_lock_mode; + }; } + #endif diff --git a/src/app/qtlocalpeer/qtlockedfile_unix.cpp b/src/app/qtlocalpeer/qtlockedfile_unix.cpp index 976c1b9ee..c1145ab73 100644 --- a/src/app/qtlocalpeer/qtlockedfile_unix.cpp +++ b/src/app/qtlocalpeer/qtlockedfile_unix.cpp @@ -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,25 +63,26 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ - -#include -#include -#include -#include +**************************************************************************** +*/ #include "qtlockedfile.h" +#include +#include +#include +#include + bool QtLockedFile::lock(LockMode mode, bool block) { if (!isOpen()) { 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) 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() 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() if (isOpen()) unlock(); } - diff --git a/src/app/qtlocalpeer/qtlockedfile_win.cpp b/src/app/qtlocalpeer/qtlockedfile_win.cpp index 5e2126204..004194aaf 100644 --- a/src/app/qtlocalpeer/qtlockedfile_win.cpp +++ b/src/app/qtlocalpeer/qtlockedfile_win.cpp @@ -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 @@ ** ** $QT_END_LICENSE$ ** -****************************************************************************/ +**************************************************************************** +*/ #include "qtlockedfile.h" -#include + #include #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) return false; } - - bool QtLockedFile::lock(LockMode mode, bool block) { if (!isOpen()) {