diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2010-09-18 10:02:13 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2010-09-18 10:02:13 +0000 |
| commit | 758badf800f49c7db3ca2ee81afd1e885ae97da5 (patch) | |
| tree | 5c68e69d374bc222fee974cb47990d3313efe9e1 /src | |
| parent | c6e0029ee9ead0cc4c638831048f605b8160d1de (diff) | |
| download | qmmp-758badf800f49c7db3ca2ee81afd1e885ae97da5.tar.gz qmmp-758badf800f49c7db3ca2ee81afd1e885ae97da5.tar.bz2 qmmp-758badf800f49c7db3ca2ee81afd1e885ae97da5.zip | |
switching to QLocalServer and QLocalSocket
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@1890 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/ui/mainwindow.cpp | 2 | ||||
| -rw-r--r-- | src/ui/qmmpstarter.cpp | 79 | ||||
| -rw-r--r-- | src/ui/qmmpstarter.h | 6 | ||||
| -rw-r--r-- | src/ui/ui.pro | 4 | ||||
| -rw-r--r-- | src/ui/unixdomainsocket.cpp | 88 | ||||
| -rw-r--r-- | src/ui/unixdomainsocket.h | 69 |
7 files changed, 49 insertions, 201 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 98fa8e197..2a1eb1915 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -76,7 +76,6 @@ SET(ui_SRCS titlebarcontrol.cpp titlebar.cpp togglebutton.cpp - unixdomainsocket.cpp visualmenu.cpp volumebar.cpp cursorimage.cpp @@ -132,7 +131,6 @@ SET(ui_MOC_HDRS titlebarcontrol.h titlebar.h togglebutton.h - unixdomainsocket.h visualmenu.h volumebar.h cursorimage.h diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 2b23fe17b..1af3184e0 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -428,7 +428,7 @@ void MainWindow::createActions() m_mainMenu->addSeparator(); QAction *repeateAllAction = m_mainMenu->addAction(tr("&Repeat Playlist")); QAction *shuffleAction = m_mainMenu->addAction(tr("&Shuffle")); - QAction *autoStopAction = m_mainMenu->addAction(tr("&Auto Stop")); + QAction *autoStopAction = m_mainMenu->addAction(tr("&Stop After Track")); QAction *repeateTrackAction = m_mainMenu->addAction(tr("&Repeat Track")); repeateAllAction->setCheckable (true); repeateTrackAction->setCheckable (true); diff --git a/src/ui/qmmpstarter.cpp b/src/ui/qmmpstarter.cpp index 8735a66f2..bccd6e250 100644 --- a/src/ui/qmmpstarter.cpp +++ b/src/ui/qmmpstarter.cpp @@ -20,22 +20,23 @@ #include <QApplication> #include <QDir> - +#include <QLocalServer> +#include <QLocalSocket> #include <cstdlib> #include <iostream> #include <unistd.h> #include <sys/types.h> #include <string.h> #include <qmmpui/commandlinemanager.h> - -#ifndef Q_OS_WIN32 -#include "unixdomainsocket.h" -#endif #include "mainwindow.h" #include "qmmpstarter.h" #include "builtincommandlineoption.h" +#ifdef Q_OS_WIN32 +#define UDS_PATH QString("qmmp") +#else #define UDS_PATH QString("/tmp/qmmp.sock.%1").arg(getuid()).toAscii().constData() +#endif using namespace std; @@ -75,31 +76,36 @@ QMMPStarter::QMMPStarter(int argc,char **argv, QObject* parent) : QObject(parent } } -#ifndef Q_OS_WIN32 - m_sock = new UnixDomainSocket(this); - if (m_sock->bind(UDS_PATH)) + m_server = new QLocalServer(this); + m_socket = new QLocalSocket(this); + if(m_server->listen (UDS_PATH)) //trying to create server { startMainWindow(); } - else if (!m_sock->alive(UDS_PATH)) + else { - // Socket is present but not connectable - application was terminated previously??? - unlink(UDS_PATH); - if (m_sock->bind(UDS_PATH)) + m_socket->connectToServer(UDS_PATH); //connecting + m_socket->waitForConnected(); + if(!m_socket->isValid()) //invalid connection { - startMainWindow(); + qWarning("QMMPStarter: trying to remove invalid socket file"); + if(!QLocalServer::removeServer(UDS_PATH)) + { + qWarning("QMMPStarter: unable to remove invalid socket file"); + exit(1); + return; + } + if(m_server->listen (UDS_PATH)) + startMainWindow(); + else + { + qWarning("QMMPStarter: server error: %s", qPrintable(m_server->errorString())); + exit(1); + } } else - { - qWarning("QMMPStarter: fatal socket error, exiting"); - exit(1); - } + writeCommand(); } - else // socket is alive, qmmp application is already running. passing command to it! - writeCommand(); -#else - startMainWindow(); -#endif } QMMPStarter::~ QMMPStarter() @@ -110,16 +116,13 @@ QMMPStarter::~ QMMPStarter() void QMMPStarter::startMainWindow() { -#ifndef Q_OS_WIN32 - connect(m_sock, SIGNAL(readyRead()),this, SLOT(readCommand())); -#endif + connect(m_server, SIGNAL(newConnection()), SLOT(readCommand())); QStringList arg_l = argString.split("\n", QString::SkipEmptyParts); mw = new MainWindow(arg_l,m_option_manager,0); } void QMMPStarter::writeCommand() { -#ifndef Q_OS_WIN32 if (!argString.isEmpty()) { QString workingDir = QDir::currentPath() + "\n"; @@ -127,7 +130,12 @@ void QMMPStarter::writeCommand() QByteArray barray; barray.append(workingDir.toUtf8 ()); barray.append(argString.toUtf8 ()); - m_sock->writeDatagram ( barray.data(),UDS_PATH); + while(!barray.isEmpty()) + { + qint64 size = m_socket->write(barray); + barray.remove(0, size); + } + m_socket->flush(); } else { @@ -135,25 +143,22 @@ void QMMPStarter::writeCommand() } exit(0); -#endif } void QMMPStarter::readCommand() -{ -#ifndef Q_OS_WIN32 - QByteArray inputArray; - inputArray.resize(m_sock->pendingDatagramSize ()); - bzero(inputArray.data(),inputArray.size()); - m_sock->readDatagram(inputArray.data(), inputArray.size()); +{ + QLocalSocket *socket = m_server->nextPendingConnection(); + socket->waitForDisconnected(); + QByteArray inputArray = socket->readAll(); + socket->deleteLater(); + if(inputArray.isEmpty()) + return; QStringList slist = QString::fromUtf8(inputArray.data()).split("\n",QString::SkipEmptyParts); QString cwd = slist.takeAt(0); if (mw) { mw->processCommandArgs(slist,cwd); } - if(m_sock->pendingDatagramSize () > 0) - readCommand(); -#endif } void QMMPStarter::printUsage() diff --git a/src/ui/qmmpstarter.h b/src/ui/qmmpstarter.h index e886980da..de01d08d7 100644 --- a/src/ui/qmmpstarter.h +++ b/src/ui/qmmpstarter.h @@ -25,7 +25,8 @@ #include <QAbstractSocket> #include <QStringList> -class UnixDomainSocket; +class QLocalServer; +class QLocalSocket; class MainWindow; class BuiltinCommandLineOption; @@ -64,9 +65,10 @@ private: private: MainWindow* mw; - UnixDomainSocket* m_sock; QString argString; BuiltinCommandLineOption* m_option_manager; + QLocalServer *m_server; + QLocalSocket *m_socket; }; #endif diff --git a/src/ui/ui.pro b/src/ui/ui.pro index 993ee3ea5..2d570c4d6 100644 --- a/src/ui/ui.pro +++ b/src/ui/ui.pro @@ -112,8 +112,8 @@ SOURCES += mainwindow.cpp \ viewmenu.cpp win32:HEADERS += ../qmmp/visual.h unix { - HEADERS += unixdomainsocket.h - SOURCES += unixdomainsocket.cpp + HEADERS += + SOURCES += } # Some conf to redirect intermediate stuff in separate dirs diff --git a/src/ui/unixdomainsocket.cpp b/src/ui/unixdomainsocket.cpp deleted file mode 100644 index acb42c772..000000000 --- a/src/ui/unixdomainsocket.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include <unistd.h> -#include <strings.h> -#include <string.h> -#include <errno.h> - -#include "unixdomainsocket.h" - -#define LISTEN_PORT_BASE 33000 - -UnixDomainSocket::UnixDomainSocket(QObject * parent ) : QUdpSocket(parent){ - _bound = false; - _s = socket(AF_UNIX, SOCK_DGRAM, 0); - this->setSocketDescriptor(_s); -} - -UnixDomainSocket::~UnixDomainSocket(){ - - if(_bound){ - ::unlink(_local.sun_path); - } -} - -bool UnixDomainSocket::bind(const QString& path){ - - int len; - bzero(&_local,sizeof(_local)); - _local.sun_family = AF_UNIX; - strcpy(_local.sun_path,path.toLocal8Bit().data()); - len = strlen(_local.sun_path) + sizeof(_local.sun_family) + 1; - bool res = !(::bind(_s, (struct sockaddr *)&_local, len)); - if(res) - _bound = true; - return res; -} - - -bool UnixDomainSocket::alive(const QString& path) -{ - socklen_t len; - struct sockaddr_un server; - bzero(&server,sizeof(server)); - server.sun_family = AF_UNIX; - strcpy(server.sun_path,path.toLocal8Bit().data()); - len = strlen(server.sun_path) + sizeof(server.sun_family) + 1; - - if (::connect(_s, (struct sockaddr *)&server, len) == -1) - { - perror("connect"); - return false; - } - return true; -} -// -void UnixDomainSocket::writeDatagram(const char* command,const QString& path) -{ - socklen_t len; - struct sockaddr_un server; - bzero(&server,sizeof(server)); - server.sun_family = AF_UNIX; - strcpy(server.sun_path,path.toLocal8Bit().data()); - - len = strlen(server.sun_path) + sizeof(server.sun_family) + 1; - - sendto(_s,command,strlen(command),0,(struct sockaddr*)&server,len); -} - - - diff --git a/src/ui/unixdomainsocket.h b/src/ui/unixdomainsocket.h deleted file mode 100644 index f9bfa73e8..000000000 --- a/src/ui/unixdomainsocket.h +++ /dev/null @@ -1,69 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#ifndef UNIXDOMAINSOCKET_H -#define UNIXDOMAINSOCKET_H - -#include <QUdpSocket> -#include <sys/socket.h> -#include <netinet/in.h> -#include <sys/types.h> -#ifdef Q_OS_LINUX -#include <linux/un.h> -#else -#include <sys/un.h> -#endif - -/*! - * UnixDomainSocket class is a wrapper around the unix domain sockets implementation. - * Used for QMMP interprocess communications. - * @author Vladimir Kuznetsov <vovanec@gmail.com> - */ -class UnixDomainSocket : public QUdpSocket -{ -Q_OBJECT -public: - UnixDomainSocket(QObject * parent = 0 ); - - /*! - * Try to bind socket to \b path. Returns \b true on success, otherwise \b false - */ - bool bind(const QString& file); - - /*! - * Checks if socket at \b path path is alive( i.e. connectable). - */ - bool alive(const QString& path); - ~UnixDomainSocket(); - -public slots: - /*! - * Sends the datagram \b command to the socket at \b path path. - */ - void writeDatagram(const char* command,const QString& path); -private: - unsigned int _s; - struct sockaddr_un _local; - bool _bound; -}; - - -#endif - |
