33 VOID start_service(LPSTR *argv);
34 SERVICE_STATUS m_ServiceStatus;
35 SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
37 void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
38 void WINAPI ServiceCtrlHandler(DWORD Opcode);
39 BOOL InstallService(
char *,
char *);
40 BOOL DeleteService(
char *);
43 int main(
int argc,
char* argv[])
47 if(strcmp(argv[1],
"-i")==0)
51 cout <<
"Usage: generic_server -i <GENERIC_SERVER_NAME> <GENERIC_SERVER_CONF_FILE>" << endl;
54 if(InstallService(argv[2],argv[3]))
55 cout <<
"Service Installed Successfully: " << argv[2] << endl;
57 cout <<
"Error Installing Service: " << argv[2] << endl;
61 if(strcmp(argv[1],
"-d")==0)
65 cout <<
"Usage: generic_server -d <GENERIC_SERVER_NAME>" << endl;
68 if(DeleteService(argv[2]))
69 cout <<
"Service UnInstalled Successfully: " << argv[2] << endl;
71 cout <<
"Error UnInstalling Service: " << argv[2] << endl;
75 SERVICE_TABLE_ENTRY DispatchTable[] = {{argv[1],ServiceMain},{NULL,NULL}};
76 if((StartServiceCtrlDispatcher(DispatchTable)) == 0)
77 cout <<
"Error starting service: " << GetLastError() << endl;
82 void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
84 m_ServiceStatus.dwServiceType = SERVICE_WIN32;
85 m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
86 m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE;
87 m_ServiceStatus.dwWin32ExitCode = 0;
88 m_ServiceStatus.dwServiceSpecificExitCode = 0;
89 m_ServiceStatus.dwCheckPoint = 0;
90 m_ServiceStatus.dwWaitHint = 0;
92 m_ServiceStatusHandle = RegisterServiceCtrlHandler(argv[0],
94 if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
98 m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
99 SetServiceStatus(m_ServiceStatusHandle, &m_ServiceStatus);
102 m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
103 m_ServiceStatus.dwCheckPoint = 0;
104 m_ServiceStatus.dwWaitHint = 0;
105 SetServiceStatus(m_ServiceStatusHandle, &m_ServiceStatus);
113 void WINAPI ServiceCtrlHandler(DWORD Opcode)
120 case SERVICE_CONTROL_PAUSE:
121 m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
122 SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
125 case SERVICE_CONTROL_CONTINUE:
126 m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
128 SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
131 case SERVICE_CONTROL_STOP:
132 os << fwork->get_cur_time() <<
" Thr.ID: SERVICE STOP signal received. Shutting down.. ";
133 fwork->
log(LOG_LOW,LOG_LOW,(
char *) os.str().c_str());
134 m_ServiceStatus.dwWin32ExitCode = 0;
135 m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
136 m_ServiceStatus.dwCheckPoint = 0;
137 m_ServiceStatus.dwWaitHint = 0;
139 SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
143 case SERVICE_CONTROL_INTERROGATE:
144 GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT,0);
157 struct sockaddr_in sockaddr;
160 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
161 sockaddr.sin_family = AF_INET;
162 sockaddr.sin_addr.s_addr = inet_addr((
const char *)
"127.0.0.1");
163 sockaddr.sin_port = (u_short)(u_short)htons(fwork->get_command_port());
164 if ( (status = connect(sock, (
struct sockaddr *)&sockaddr,
sizeof(sockaddr))) < 0)
166 os << fwork->get_cur_time() <<
" Thr.ID: SERVICE 07 cannot connect to COMMAND_PORT: " << fwork->get_command_port() <<
" " << status;
167 fwork->log(LOG_LOW,LOG_LOW,(
char *) os.str().c_str());
180 BOOL InstallService(
char *service_name,
char *conf_file)
182 char strDir[1024],strExe[1024];
183 SC_HANDLE schSCManager,schService;
185 memset(strExe,0,1024);
186 GetCurrentDirectory(1024,strDir);
187 strcat(strExe,strDir);
188 strcat(strExe,
"\\GENERIC_SERVER.exe ");
189 strcat(strExe,strDir);
191 strcat(strExe,conf_file);
192 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
194 if (schSCManager == NULL)
196 LPCTSTR lpszBinaryPathName=strExe;
198 schService = CreateService(schSCManager,service_name,
201 SERVICE_WIN32_OWN_PROCESS,
203 SERVICE_ERROR_NORMAL,
210 if (schService == NULL)
213 CloseServiceHandle(schService);
218 BOOL DeleteService(
char *service_name)
220 SC_HANDLE schSCManager;
221 SC_HANDLE schService;
222 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
224 if (schSCManager == NULL)
226 schService=OpenService(schSCManager,service_name,SERVICE_ALL_ACCESS);
227 if (schService == NULL)
229 if(DeleteService(schService)==0)
231 if(CloseServiceHandle(schService)==0)
int log(unsigned int, unsigned int, char *)
enables logging to framework managed log file.
Singleton class to manage framework state and provide utility functions. This class stores master Vec...
This is a singleton class and provides framework functionality.
This is the base class for all plugins. All plug-ins should derive from this class. This class has a bunch of virtual functions that all plug-in could/should implement. In addition to virtual functions, this class also provides a lot of utility functions for all plug-ins.
static generic_server * instance(void)
Instantiate tis singleton class only once and return pointer to instantiated object later on...