Merge pull request #195 from pljones/feature/130-signal-handling
Handle server application closure more cleanly
This commit is contained in:
commit
5806093878
10 changed files with 446 additions and 39 deletions
|
@ -337,7 +337,8 @@ HEADERS += src/audiomixerboard.h \
|
||||||
src/recorder/jamrecorder.h \
|
src/recorder/jamrecorder.h \
|
||||||
src/recorder/creaperproject.h \
|
src/recorder/creaperproject.h \
|
||||||
src/recorder/cwavestream.h \
|
src/recorder/cwavestream.h \
|
||||||
src/historygraph.h
|
src/historygraph.h \
|
||||||
|
src/signalhandler.h
|
||||||
|
|
||||||
HEADERS_OPUS = libs/opus/celt/arch.h \
|
HEADERS_OPUS = libs/opus/celt/arch.h \
|
||||||
libs/opus/celt/bands.h \
|
libs/opus/celt/bands.h \
|
||||||
|
@ -451,6 +452,7 @@ SOURCES += src/audiomixerboard.cpp \
|
||||||
src/serverlist.cpp \
|
src/serverlist.cpp \
|
||||||
src/serverlogging.cpp \
|
src/serverlogging.cpp \
|
||||||
src/settings.cpp \
|
src/settings.cpp \
|
||||||
|
src/signalhandler.cpp \
|
||||||
src/socket.cpp \
|
src/socket.cpp \
|
||||||
src/soundbase.cpp \
|
src/soundbase.cpp \
|
||||||
src/util.cpp \
|
src/util.cpp \
|
||||||
|
|
|
@ -1207,7 +1207,7 @@ fflush(pFileDelay);
|
||||||
// update socket buffer size
|
// update socket buffer size
|
||||||
Channel.UpdateSocketBufferSize();
|
Channel.UpdateSocketBufferSize();
|
||||||
|
|
||||||
Q_UNUSED ( iUnused );
|
Q_UNUSED ( iUnused )
|
||||||
}
|
}
|
||||||
|
|
||||||
int CClient::EstimatedOverallDelay ( const int iPingTimeMs )
|
int CClient::EstimatedOverallDelay ( const int iPingTimeMs )
|
||||||
|
|
|
@ -327,9 +327,18 @@ void CJamRecorder::Init( const CServer* server,
|
||||||
this, SLOT( OnFrame (const int, const QString, const CHostAddress, const int, const CVector<int16_t> ) ),
|
this, SLOT( OnFrame (const int, const QString, const CHostAddress, const int, const CVector<int16_t> ) ),
|
||||||
Qt::ConnectionType::QueuedConnection );
|
Qt::ConnectionType::QueuedConnection );
|
||||||
|
|
||||||
|
QObject::connect( QCoreApplication::instance(),
|
||||||
|
SIGNAL ( aboutToQuit() ),
|
||||||
|
this, SLOT( OnAboutToQuit() ) );
|
||||||
|
|
||||||
iServerFrameSizeSamples = _iServerFrameSizeSamples;
|
iServerFrameSizeSamples = _iServerFrameSizeSamples;
|
||||||
|
|
||||||
|
thisThread = new QThread();
|
||||||
|
moveToThread ( thisThread );
|
||||||
|
thisThread->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief CJamRecorder::OnStart Start up tasks when the first client connects
|
* @brief CJamRecorder::OnStart Start up tasks when the first client connects
|
||||||
*/
|
*/
|
||||||
|
@ -372,6 +381,13 @@ void CJamRecorder::OnEnd()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CJamRecorder::OnAboutToQuit()
|
||||||
|
{
|
||||||
|
OnEnd();
|
||||||
|
|
||||||
|
thisThread->exit();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief CJamRecorder::SessionDirToReaper Replica of CJamRecorder::OnEnd() but using the directory contents to construct the CReaperProject object
|
* @brief CJamRecorder::SessionDirToReaper Replica of CJamRecorder::OnEnd() but using the directory contents to construct the CReaperProject object
|
||||||
* @param strSessionDirName
|
* @param strSessionDirName
|
||||||
|
|
|
@ -132,13 +132,16 @@ private:
|
||||||
QList<CJamClientConnection*> jamClientConnections;
|
QList<CJamClientConnection*> jamClientConnections;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CJamRecorder : public QThread
|
class CJamRecorder : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CJamRecorder ( const QString recordingDirName ) :
|
CJamRecorder ( const QString recordingDirName ) :
|
||||||
recordBaseDir (recordingDirName), isRecording (false) {}
|
recordBaseDir ( recordingDirName ),
|
||||||
|
isRecording ( false )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void Init( const CServer* server, const int _iServerFrameSizeSamples );
|
void Init( const CServer* server, const int _iServerFrameSizeSamples );
|
||||||
|
|
||||||
|
@ -155,6 +158,11 @@ public slots:
|
||||||
*/
|
*/
|
||||||
void OnEnd();
|
void OnEnd();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Raised when application is stopping
|
||||||
|
*/
|
||||||
|
void OnAboutToQuit();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Raised when an existing client leaves the server.
|
* @brief Raised when an existing client leaves the server.
|
||||||
* @param iChID channel number of client
|
* @param iChID channel number of client
|
||||||
|
@ -162,7 +170,7 @@ public slots:
|
||||||
void OnDisconnected ( int iChID );
|
void OnDisconnected ( int iChID );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Raised when a frame of data fis available to process
|
* @brief Raised when a frame of data is available to process
|
||||||
*/
|
*/
|
||||||
void OnFrame ( const int iChID, const QString name, const CHostAddress address, const int numAudioChannels, const CVector<int16_t> data );
|
void OnFrame ( const int iChID, const QString name, const CHostAddress address, const int numAudioChannels, const CVector<int16_t> data );
|
||||||
|
|
||||||
|
@ -172,6 +180,8 @@ private:
|
||||||
bool isRecording;
|
bool isRecording;
|
||||||
CJamSession* currentSession;
|
CJamSession* currentSession;
|
||||||
int iServerFrameSizeSamples;
|
int iServerFrameSizeSamples;
|
||||||
|
|
||||||
|
QThread* thisThread;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -251,7 +251,8 @@ CServer::CServer ( const int iNewMaxNumChan,
|
||||||
bAutoRunMinimized ( false ),
|
bAutoRunMinimized ( false ),
|
||||||
strWelcomeMessage ( strNewWelcomeMessage ),
|
strWelcomeMessage ( strNewWelcomeMessage ),
|
||||||
eLicenceType ( eNLicenceType ),
|
eLicenceType ( eNLicenceType ),
|
||||||
bDisconnectAllClients ( bNDisconnectAllClients )
|
bDisconnectAllClients ( bNDisconnectAllClients ),
|
||||||
|
pSignalHandler ( CSignalHandler::getSingletonP() )
|
||||||
{
|
{
|
||||||
int iOpusError;
|
int iOpusError;
|
||||||
int i;
|
int i;
|
||||||
|
@ -398,11 +399,10 @@ CServer::CServer ( const int iNewMaxNumChan,
|
||||||
QString().number( static_cast<int> ( iPortNumber ) ) );
|
QString().number( static_cast<int> ( iPortNumber ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable jam recording (if requested)
|
// Enable jam recording (if requested) - kicks off the thread
|
||||||
if ( bEnableRecording )
|
if ( bEnableRecording )
|
||||||
{
|
{
|
||||||
JamRecorder.Init ( this, iServerFrameSizeSamples );
|
JamRecorder.Init ( this, iServerFrameSizeSamples );
|
||||||
JamRecorder.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// enable all channels (for the server all channel must be enabled the
|
// enable all channels (for the server all channel must be enabled the
|
||||||
|
@ -466,6 +466,14 @@ CServer::CServer ( const int iNewMaxNumChan,
|
||||||
SIGNAL ( SvrRegStatusChanged() ),
|
SIGNAL ( SvrRegStatusChanged() ),
|
||||||
this, SLOT ( OnSvrRegStatusChanged() ) );
|
this, SLOT ( OnSvrRegStatusChanged() ) );
|
||||||
|
|
||||||
|
QObject::connect ( QCoreApplication::instance(),
|
||||||
|
SIGNAL ( aboutToQuit() ),
|
||||||
|
this, SLOT ( OnAboutToQuit() ) );
|
||||||
|
|
||||||
|
QObject::connect ( pSignalHandler,
|
||||||
|
SIGNAL ( ShutdownSignal ( int ) ),
|
||||||
|
this, SLOT ( OnShutdown ( int ) ) );
|
||||||
|
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
||||||
connectChannelSignalsToServerSlots<MAX_NUM_CHANNELS>();
|
connectChannelSignalsToServerSlots<MAX_NUM_CHANNELS>();
|
||||||
|
|
||||||
|
@ -908,6 +916,23 @@ void CServer::OnCLDisconnection ( CHostAddress InetAddr )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CServer::OnAboutToQuit()
|
||||||
|
{
|
||||||
|
Stop();
|
||||||
|
|
||||||
|
// if server was registered at the central server, unregister on shutdown
|
||||||
|
if ( GetServerListEnabled() )
|
||||||
|
{
|
||||||
|
UnregisterSlaveServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CServer::OnShutdown ( int )
|
||||||
|
{
|
||||||
|
// This should trigger OnAboutToQuit
|
||||||
|
QCoreApplication::instance()->exit();
|
||||||
|
}
|
||||||
|
|
||||||
void CServer::Start()
|
void CServer::Start()
|
||||||
{
|
{
|
||||||
// only start if not already running
|
// only start if not already running
|
||||||
|
@ -1273,7 +1298,7 @@ opus_custom_encoder_ctl ( CurOpusEncoder,
|
||||||
Stop();
|
Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_UNUSED ( iUnused );
|
Q_UNUSED ( iUnused )
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Mix all audio data from all clients together.
|
/// @brief Mix all audio data from all clients together.
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#endif
|
#endif
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
|
#include "signalhandler.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "channel.h"
|
#include "channel.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -370,6 +371,8 @@ protected:
|
||||||
ELicenceType eLicenceType;
|
ELicenceType eLicenceType;
|
||||||
bool bDisconnectAllClients;
|
bool bDisconnectAllClients;
|
||||||
|
|
||||||
|
CSignalHandler* pSignalHandler;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void Started();
|
void Started();
|
||||||
void Stopped();
|
void Stopped();
|
||||||
|
@ -455,6 +458,10 @@ public slots:
|
||||||
|
|
||||||
void OnCLDisconnection ( CHostAddress InetAddr );
|
void OnCLDisconnection ( CHostAddress InetAddr );
|
||||||
|
|
||||||
|
void OnAboutToQuit();
|
||||||
|
|
||||||
|
void OnShutdown ( int );
|
||||||
|
|
||||||
#if QT_VERSION < 0x50000 // MOC does not expand macros in Qt 4, so we cannot use QT_VERSION_CHECK(5, 0, 0)
|
#if QT_VERSION < 0x50000 // MOC does not expand macros in Qt 4, so we cannot use QT_VERSION_CHECK(5, 0, 0)
|
||||||
// CODE TAG: MAX_NUM_CHANNELS_TAG
|
// CODE TAG: MAX_NUM_CHANNELS_TAG
|
||||||
// make sure we have MAX_NUM_CHANNELS connections!!!
|
// make sure we have MAX_NUM_CHANNELS connections!!!
|
||||||
|
|
|
@ -342,18 +342,6 @@ lvwClients->setMinimumHeight ( 140 );
|
||||||
Timer.start ( GUI_CONTRL_UPDATE_TIME );
|
Timer.start ( GUI_CONTRL_UPDATE_TIME );
|
||||||
}
|
}
|
||||||
|
|
||||||
void CServerDlg::closeEvent ( QCloseEvent* Event )
|
|
||||||
{
|
|
||||||
// if server was registered at the central server, unregister on shutdown
|
|
||||||
if ( pServer->GetServerListEnabled() )
|
|
||||||
{
|
|
||||||
pServer->UnregisterSlaveServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// default implementation of this event handler routine
|
|
||||||
Event->accept();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CServerDlg::OnStartOnOSStartStateChanged ( int value )
|
void CServerDlg::OnStartOnOSStartStateChanged ( int value )
|
||||||
{
|
{
|
||||||
const bool bCurAutoStartMinState = ( value == Qt::Checked );
|
const bool bCurAutoStartMinState = ( value == Qt::Checked );
|
||||||
|
|
|
@ -58,7 +58,6 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void changeEvent ( QEvent* pEvent );
|
virtual void changeEvent ( QEvent* pEvent );
|
||||||
virtual void closeEvent ( QCloseEvent* Event );
|
|
||||||
|
|
||||||
void UpdateGUIDependencies();
|
void UpdateGUIDependencies();
|
||||||
void UpdateSystemTrayIcon ( const bool bIsActive );
|
void UpdateSystemTrayIcon ( const bool bIsActive );
|
||||||
|
|
183
src/signalhandler.cpp
Executable file
183
src/signalhandler.cpp
Executable file
|
@ -0,0 +1,183 @@
|
||||||
|
/******************************************************************************\
|
||||||
|
* Copyright (c) 2020
|
||||||
|
*
|
||||||
|
* Author(s):
|
||||||
|
* Peter L Jones
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*
|
||||||
|
* This code contains some ideas derived from QCtrlSignals
|
||||||
|
* https://github.com/Skycoder42/QCtrlSignals.git
|
||||||
|
* - mostly the singleton and emitSignal code, plus some of the structure
|
||||||
|
* - virtually everything else is common knowledge across SourceForge answers
|
||||||
|
*
|
||||||
|
* BSD 3-Clause License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016, Felix Barz
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of the copyright holder nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
\******************************************************************************/
|
||||||
|
|
||||||
|
#include "signalhandler.h"
|
||||||
|
|
||||||
|
class CSignalHandlerSingleton : public CSignalHandler {
|
||||||
|
public:
|
||||||
|
inline CSignalHandlerSingleton() : CSignalHandler() {}
|
||||||
|
};
|
||||||
|
Q_GLOBAL_STATIC ( CSignalHandlerSingleton, singleton )
|
||||||
|
|
||||||
|
CSignalHandler::CSignalHandler() : pSignalBase ( CSignalBase::withSignalHandler ( this ) ) {}
|
||||||
|
|
||||||
|
CSignalHandler::~CSignalHandler() = default;
|
||||||
|
|
||||||
|
CSignalHandler* CSignalHandler::getSingletonP() { return singleton; }
|
||||||
|
|
||||||
|
bool CSignalHandler::emitSignal ( int sigNum )
|
||||||
|
{
|
||||||
|
return QMetaObject::invokeMethod( singleton, "ShutdownSignal", Qt::QueuedConnection, Q_ARG( int, sigNum ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
void CSignalHandler::OnSocketNotify( int socket )
|
||||||
|
{
|
||||||
|
int sigNum;
|
||||||
|
if ( ::read ( socket, &sigNum, sizeof ( int ) ) == sizeof ( int ) )
|
||||||
|
{
|
||||||
|
emitSignal ( sigNum );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
|
||||||
|
CSignalBase::CSignalBase ( CSignalHandler* pSignalHandler ) :
|
||||||
|
pSignalHandler ( pSignalHandler )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CSignalBase::~CSignalBase() = default;
|
||||||
|
|
||||||
|
CSignalBase* CSignalBase::withSignalHandler ( CSignalHandler* pSignalHandler )
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return new CSignalWin ( pSignalHandler );
|
||||||
|
#else
|
||||||
|
return new CSignalUnix ( pSignalHandler );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
CSignalWin::CSignalWin ( CSignalHandler* nPSignalHandler ) :
|
||||||
|
CSignalBase ( nPSignalHandler ),
|
||||||
|
lock ( QReadWriteLock::Recursive )
|
||||||
|
{
|
||||||
|
SetConsoleCtrlHandler ( signalHandler, true );
|
||||||
|
}
|
||||||
|
|
||||||
|
CSignalWin::~CSignalWin() {
|
||||||
|
SetConsoleCtrlHandler ( signalHandler, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
QReadWriteLock* CSignalWin::getLock() const
|
||||||
|
{
|
||||||
|
return &lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL WINAPI CSignalWin::signalHandler ( _In_ DWORD sigNum )
|
||||||
|
{
|
||||||
|
auto self = getSelf<CSignalWin>();
|
||||||
|
QReadLocker lock ( self->getLock() );
|
||||||
|
return self->pSignalHandler->emitSignal ( static_cast<int>( sigNum ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
int CSignalUnix::socketPair[2];
|
||||||
|
|
||||||
|
CSignalUnix::CSignalUnix ( CSignalHandler* nPSignalHandler ) :
|
||||||
|
CSignalBase ( nPSignalHandler )
|
||||||
|
{
|
||||||
|
if ( ::socketpair ( AF_UNIX, SOCK_STREAM, 0, socketPair ) == 0 )
|
||||||
|
{
|
||||||
|
socketNotifier = new QSocketNotifier ( socketPair[ 1 ], QSocketNotifier::Read );
|
||||||
|
|
||||||
|
QObject::connect ( socketNotifier, &QSocketNotifier::activated, nPSignalHandler, &CSignalHandler::OnSocketNotify );
|
||||||
|
|
||||||
|
socketNotifier->setEnabled ( true );
|
||||||
|
|
||||||
|
setSignalHandled ( SIGINT, true );
|
||||||
|
setSignalHandled ( SIGTERM, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CSignalUnix::~CSignalUnix() {
|
||||||
|
setSignalHandled ( SIGINT, false );
|
||||||
|
setSignalHandled ( SIGTERM, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
QReadWriteLock* CSignalUnix::getLock() const { return nullptr; }
|
||||||
|
|
||||||
|
bool CSignalUnix::setSignalHandled ( int sigNum, bool state )
|
||||||
|
{
|
||||||
|
struct sigaction sa;
|
||||||
|
sigemptyset ( &sa.sa_mask );
|
||||||
|
|
||||||
|
if ( state )
|
||||||
|
{
|
||||||
|
sa.sa_handler = CSignalUnix::signalHandler;
|
||||||
|
sa.sa_flags |= SA_RESTART;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sa.sa_handler = SIG_DFL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ::sigaction ( sigNum, &sa, nullptr ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CSignalUnix::signalHandler ( int sigNum )
|
||||||
|
{
|
||||||
|
const auto res = ::write ( socketPair[ 0 ], &sigNum, sizeof ( int ) );
|
||||||
|
Q_UNUSED ( res );
|
||||||
|
}
|
||||||
|
#endif
|
177
src/signalhandler.h
Executable file
177
src/signalhandler.h
Executable file
|
@ -0,0 +1,177 @@
|
||||||
|
/******************************************************************************\
|
||||||
|
* Copyright (c) 2020
|
||||||
|
*
|
||||||
|
* Author(s):
|
||||||
|
* Peter L Jones
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*
|
||||||
|
* This code contains some ideas derived from QCtrlSignals
|
||||||
|
* https://github.com/Skycoder42/QCtrlSignals.git
|
||||||
|
* - mostly the singleton and emitSignal code, plus some of the structure
|
||||||
|
* - virtually everything else is common knowledge across SourceForge answers
|
||||||
|
*
|
||||||
|
* BSD 3-Clause License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016, Felix Barz
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of the copyright holder nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
\******************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtCore/QObject>
|
||||||
|
#include <QtCore/QScopedPointer>
|
||||||
|
#include <QtCore/QHash>
|
||||||
|
#include <QtCore/QReadWriteLock>
|
||||||
|
#include <QtCore/QSet>
|
||||||
|
#include <QGlobalStatic>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <qt_windows.h>
|
||||||
|
#include <QtCore/QCoreApplication>
|
||||||
|
#include <QtCore/QSemaphore>
|
||||||
|
#include <QtCore/QThread>
|
||||||
|
#include <QtCore/QDebug>
|
||||||
|
#else
|
||||||
|
#include <QSocketNotifier>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <csignal>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class CSignalBase;
|
||||||
|
|
||||||
|
class CSignalHandler : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
friend class CSignalBase;
|
||||||
|
friend class CSignalHandlerSingleton;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static CSignalHandler* getSingletonP();
|
||||||
|
|
||||||
|
bool emitSignal ( int );
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
public slots:
|
||||||
|
void OnSocketNotify ( int socket );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void ShutdownSignal ( int sigNum );
|
||||||
|
|
||||||
|
private:
|
||||||
|
QScopedPointer<CSignalBase> pSignalBase;
|
||||||
|
|
||||||
|
explicit CSignalHandler();
|
||||||
|
~CSignalHandler() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
|
||||||
|
class CSignalBase
|
||||||
|
{
|
||||||
|
Q_DISABLE_COPY ( CSignalBase )
|
||||||
|
|
||||||
|
public:
|
||||||
|
static CSignalBase* withSignalHandler ( CSignalHandler* );
|
||||||
|
virtual ~CSignalBase();
|
||||||
|
|
||||||
|
virtual QReadWriteLock* getLock() const = 0;
|
||||||
|
|
||||||
|
QSet<int> sHandledSigNums;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
CSignalBase ( CSignalHandler* );
|
||||||
|
|
||||||
|
CSignalHandler* pSignalHandler;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T *getSelf()
|
||||||
|
{
|
||||||
|
return static_cast<T*>( CSignalHandler::getSingletonP()->pSignalBase.data() );
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
|
||||||
|
class CSignalWin : public CSignalBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CSignalWin ( CSignalHandler* );
|
||||||
|
~CSignalWin() override;
|
||||||
|
|
||||||
|
virtual QReadWriteLock* getLock() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable QReadWriteLock lock;
|
||||||
|
|
||||||
|
static BOOL WINAPI signalHandler ( _In_ DWORD sigNum );
|
||||||
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
class CSignalUnix : public CSignalBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CSignalUnix ( CSignalHandler* );
|
||||||
|
~CSignalUnix() override;
|
||||||
|
|
||||||
|
virtual QReadWriteLock* getLock() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSocketNotifier* socketNotifier = nullptr;
|
||||||
|
bool setSignalHandled ( int sigNum, bool state );
|
||||||
|
|
||||||
|
static int socketPair[2];
|
||||||
|
static void signalHandler ( int sigNum );
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue