Initial
This commit is contained in:
114
watchdog/service.cpp
Normal file
114
watchdog/service.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <watchdog/service.hpp>
|
||||
|
||||
Service::Service()
|
||||
: mhServiceStatus(0), mServiceStatus(Stopped)
|
||||
{
|
||||
}
|
||||
|
||||
Service::~Service()
|
||||
{
|
||||
}
|
||||
|
||||
bool Service::startServiceControlDispatcher(const String &serviceName)
|
||||
{
|
||||
SERVICE_TABLE_ENTRY serviceTableEntry[2];
|
||||
bool retCode;
|
||||
|
||||
::memset(&serviceTableEntry,0,sizeof(serviceTableEntry));
|
||||
mServiceName=serviceName;
|
||||
serviceTableEntry[0].lpServiceName=(char*)serviceName.str();
|
||||
serviceTableEntry[0].lpServiceProc=(CBThunkSvcMain::LPFNSERVICEMAIN)CBThunkSvcMain::getAddress();
|
||||
retCode=::StartServiceCtrlDispatcher((SERVICE_TABLE_ENTRY*)&serviceTableEntry);
|
||||
return retCode;
|
||||
}
|
||||
|
||||
void Service::setStatus(ServiceStatus status,DWORD exitCode,DWORD checkPoint,DWORD waitHint)
|
||||
{
|
||||
SERVICE_STATUS serviceStatus;
|
||||
|
||||
mServiceStatus=status;
|
||||
::memset(&serviceStatus,0,sizeof(serviceStatus));
|
||||
serviceStatus.dwServiceType=SERVICE_WIN32;
|
||||
serviceStatus.dwCurrentState=status;
|
||||
serviceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN;
|
||||
serviceStatus.dwWin32ExitCode=0;
|
||||
serviceStatus.dwCheckPoint=0;
|
||||
serviceStatus.dwWaitHint=0;
|
||||
::SetServiceStatus(mhServiceStatus,&serviceStatus);
|
||||
if(Stopped==status)mServiceEvent.setEvent();
|
||||
}
|
||||
|
||||
// **********************************************************************************************
|
||||
// *************************************** VIRTUALS *********************************************
|
||||
// **********************************************************************************************
|
||||
|
||||
void Service::serviceMain(DWORD dwArgc,LPTSTR *pszArg)
|
||||
{
|
||||
mhServiceStatus=::RegisterServiceCtrlHandler(mServiceName,(CBThunkCtrlHandler::LPFNCONTROLHANDLER)CBThunkCtrlHandler::getAddress());
|
||||
setStatus(StartPending);
|
||||
if(!initialize())
|
||||
{
|
||||
setStatus(Stopped);
|
||||
return;
|
||||
}
|
||||
setStatus(Running);
|
||||
mServiceEvent.waitEvent();
|
||||
}
|
||||
|
||||
void Service::controlHandler(DWORD control)
|
||||
{
|
||||
switch(control)
|
||||
{
|
||||
case SERVICE_CONTROL_STOP :
|
||||
controlStop();
|
||||
setStatus(Stopped,NO_ERROR,1,1000);
|
||||
break;
|
||||
case SERVICE_CONTROL_PAUSE :
|
||||
controlPause();
|
||||
setStatus(Paused,NO_ERROR,1,1000);
|
||||
break;
|
||||
case SERVICE_CONTROL_CONTINUE :
|
||||
controlContinue();
|
||||
setStatus(Running);
|
||||
break;
|
||||
case SERVICE_CONTROL_INTERROGATE :
|
||||
controlInterrogate();
|
||||
setStatus(mServiceStatus);
|
||||
break;
|
||||
case SERVICE_CONTROL_SHUTDOWN :
|
||||
controlShutdown();
|
||||
setStatus(Stopped,NO_ERROR,1,1000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Service::initialize(void)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void Service::controlStop(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void Service::controlPause(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void Service::controlContinue(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void Service::controlInterrogate(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void Service::controlShutdown(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user