int main(int argc, char** argv) { #ifdef DEBUG #if 0 while (!IsDebuggerPresent()) gcSleep( 500 ); #endif #endif if (GetFileAttributes("desura_service_debug.txt") != 0xFFFFFFFF) { gcTraceS("Waiting for debugger"); while (!IsDebuggerPresent()) gcSleep( 500 ); } if (argc >= 2 && strcmp(argv[1], "-exe")== 0) { if (g_ServiceApp.start(argc, argv)) { printf("Service has started. Press any key to exit....\n"); getchar(); } else { printf("Failed to start service.\n"); } g_ServiceApp.stop(); } else { SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = SERVICE_NAME; ServiceTable[0].lpServiceProc = &ServiceMain; ServiceTable[1].lpServiceName = nullptr; ServiceTable[1].lpServiceProc = nullptr; g_StopEvent = CreateEvent(nullptr, false, false, nullptr); StartServiceCtrlDispatcher(ServiceTable); WaitForSingleObject(g_StopEvent, INFINITE); CloseHandle(g_StopEvent); g_StopEvent = nullptr; } }
void User::initPipe() { if (m_pPipeClient) return; m_pPipeClient = new UserIPCPipeClient(getUserName(), getAppDataPath(), true); m_pPipeClient->onDisconnectEvent += delegate(&onPipeDisconnect); size_t x=0; do { try { m_pPipeClient->start(); break; } catch (gcException &) { if (x > 5) { logOut(); throw; } else { gcSleep(100); x++; } } } while (true); }
void ToolIPCPipeClient::start() { startHelper(); size_t x=0; do { try { tryStart(); break; } catch (gcException) { if (x > 5) { throw; } else { gcSleep(500); x++; } } } while (true); }
void ThreadPool::purgeTasks() { { m_TaskMutex.lock(); m_ThreadMutex.readLock(); for (size_t x=0; x<m_vThreadList.size(); x++) { m_vThreadList[x]->stopTask(); } m_ThreadMutex.readUnlock(); m_ForcedMutex.writeLock(); for (size_t x=0; x<m_vForcedList.size(); x++) { m_vForcedList[x]->stop(); m_vForcedList[x]->onCompleteEvent -= delegate(this, &ThreadPool::onThreadComplete); } safe_delete(m_vForcedList); m_ForcedMutex.writeUnlock(); safe_delete(m_vTaskList); m_TaskMutex.unlock(); } while (activeThreads() > 0) gcSleep(50); }
void SFTWorker::run() { assert(m_pCT); while (true) { uint32 status = m_pCT->getStatus(m_uiId); int32 res = BZ_OK; while (status == BaseMCFThread::SF_STATUS_PAUSE) { gcSleep(500); status = m_pCT->getStatus(m_uiId); } if (status == BaseMCFThread::SF_STATUS_STOP) { break; } else if (status == BaseMCFThread::SF_STATUS_NULL) { if (!newTask()) gcSleep(500); continue; } else if (status == BaseMCFThread::SF_STATUS_ENDFILE) { do { res = doWork(); if (bzErrorCheck(res)) break; } while (res != BZ_STREAM_END); } else if (status == BaseMCFThread::SF_STATUS_CONTINUE) { res = doWork(); bzErrorCheck(res); } if (res == BZ_STREAM_END || status == BaseMCFThread::SF_STATUS_SKIP) finishFile(); } }
void uninstallService(const char* szName) { SC_HANDLE service; SC_HANDLE scm; BOOL SUCCESS; SERVICE_STATUS status; gcWString wName(szName); //Open connection to SCM scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); if (!scm) throw gcException(ERR_NULLSCMANAGER, GetLastError(), gcString("Failed to open the Service Control Manager")); //Get service's handle service = OpenService(scm, wName.c_str(), SERVICE_ALL_ACCESS | DELETE); if (!service) { CloseServiceHandle(scm); throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName)); } //Get service status SUCCESS = QueryServiceStatus(service, &status); if (!SUCCESS) { CloseServiceHandle(service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName)); } //Stop service if necessary if (status.dwCurrentState != SERVICE_STOPPED) { SUCCESS = ControlService(service, SERVICE_CONTROL_STOP, &status); if (!SUCCESS) { CloseServiceHandle(service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to stop service: {0}", szName)); } gcSleep(500); } //Delete service SUCCESS = DeleteService(service); //Clean up CloseServiceHandle(service); CloseServiceHandle(scm); if (!SUCCESS) throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to delete service: {0}", szName)); }
void UserIPCPipeClient::startService() { #ifdef DEBUG #if 0 //servcie started via debugger return; #endif #endif uint32 res = UTIL::WIN::queryService(SERVICE_NAME); if (res != SERVICE_STATUS_STOPPED) { try { UTIL::WIN::stopService(SERVICE_NAME); gcSleep(500); } catch (gcException &) { } } std::vector<std::string> args; args.push_back("-wdir"); args.push_back(gcString(UTIL::OS::getCurrentDir())); UTIL::WIN::startService(SERVICE_NAME, args); uint32 count = 0; while (UTIL::WIN::queryService(SERVICE_NAME) != SERVICE_STATUS_RUNNING) { //wait five seconds if (count > 50) throw gcException(ERR_SERVICE, "Failed to start desura Service (PipeManager)."); gcSleep(100); count++; } }
void CreateMCFThread::waitForItemInfo() { m_pUserCore->getItemManager()->retrieveItemInfoAsync(getItemId()); size_t count = 0; while (!isStopped() && !getItemInfo() && count < 100) { count++; gcSleep(500); } }
void UpdateProgThread::run() { m_tStartTime = gcTime(); m_tLastUpdateTime = gcTime(); for (uint8 x=0; x<m_pCount; x++) m_vProgInfo.push_back(0); while (!isStopped()) { doPause(); gcSleep(500); calcResults(); } }
void AppUpdateInstall::startService() { gcTrace(""); uint32 res = UTIL::WIN::queryService(SERVICE_NAME); if (res != SERVICE_STATUS_STOPPED) { try { UTIL::WIN::stopService(SERVICE_NAME); gcSleep(500); } catch (gcException &) { } } std::vector<std::string> args; args.push_back("-wdir"); args.push_back(gcString(UTIL::OS::getCurrentDir())); UTIL::WIN::startService(SERVICE_NAME, args); uint32 count = 0; while (UTIL::WIN::queryService(SERVICE_NAME) != SERVICE_STATUS_RUNNING) { //wait 10 seconds if (count > 40) throw gcException(ERR_SERVICE, "Failed to start desura Service (PipeManager)."); gcSleep(250); count++; } }
void SMTWorker::run() { assert(m_pCT); while (!isStopped()) { MCFThreadStatus status = m_pCT->getStatus(m_uiId); while (status == MCFThreadStatus::SF_STATUS_PAUSE) { gcSleep(500); status = m_pCT->getStatus(m_uiId); } if (status == MCFThreadStatus::SF_STATUS_STOP) break; if (status == MCFThreadStatus::SF_STATUS_NULL) { if (!newTask()) continue; } //make sure we dont do compression if we are ment to be stopped if (!isStopped() && status == MCFThreadStatus::SF_STATUS_CONTINUE) { try { doWork(); } catch (gcException &e) { gcString name("Null File"); if (m_pCurFile) name = m_pCurFile->getName(); gcException e2((ERROR_ID)e.getErrId(), e.getSecErrId(), gcString("{0} [{1}]", e.getErrMsg(), name)); finishTask(); m_pCT->reportError(m_uiId, e2); return; } } } m_phFhSink->close(); }
void startService(const char* szName, std::vector<std::string> &args) { gcAssert(szName); gcWString wName(szName); SC_HANDLE scm, Service; SERVICE_STATUS ssStatus; DWORD dwWaitTime; //open connection to SCM scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT); if (!scm) throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager"); //open service Service = OpenService(scm, wName.c_str(), SERVICE_START|SERVICE_QUERY_STATUS); if (!Service) { CloseServiceHandle(scm); throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName)); } // Check the status until the service is no longer start pending. if (!QueryServiceStatus( Service, &ssStatus) ) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName)); } while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) { gcSleep(1000); } if (ssStatus.dwCurrentState != SERVICE_STOPPED) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: Service {0} is not stopped [{1}]", szName, ssStatus.dwCurrentState)); } BOOL res = doStartService(Service, szName, args); if (res == 0) { if (GetLastError() == 1058) throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service {0} as the service is disabled. Please use msconfig and renable service.", szName)); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName)); } // Check the status until the service is no longer start pending. if (!QueryServiceStatus( Service, &ssStatus) ) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName)); } DWORD totalTime = 0; while (ssStatus.dwCurrentState != SERVICE_RUNNING) { if (ssStatus.dwCurrentState == SERVICE_STOPPED) { //start service BOOL res = doStartService(Service, szName, args); if (res == 0) throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName)); } dwWaitTime = 1000; totalTime += dwWaitTime; gcSleep( dwWaitTime ); // Check the status again. if (!QueryServiceStatus( Service, &ssStatus) ) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName)); } if (totalTime > 30000) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, gcString("Service {0} Startup timed out after 30 seconds.", szName)); } } // Check the status again. if (!QueryServiceStatus( Service, &ssStatus) ) { CloseServiceHandle(Service); CloseServiceHandle(scm); throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName)); } CloseServiceHandle(scm); CloseServiceHandle(Service); if (ssStatus.dwCurrentState != SERVICE_RUNNING) throw gcException(ERR_SERVICE, gcString("Failed to start service: {0} [{1}]", szName, ssStatus.dwCurrentState)); }
void WGTWorker::run() { m_szUrl = m_pProvMng->getUrl(m_uiId); if (m_szUrl == "NULL") { Warning(gcString("Mcf Download Thread [{0}] failed to get valid url for download.\n", m_uiId)); return; } gcString name = m_pProvMng->getName(m_uiId); m_pMcfCon = new MCFCore::Misc::MCFServerCon(); m_pMcfCon->setDPInformation(name.c_str()); m_pMcfCon->onProgressEvent += delegate(this, &WGTWorker::onProgress); while (!isStopped()) { uint32 status = m_pCT->getStatus(m_uiId); uint32 pauseCount = 0; bool isPaused= (status == MCFCore::Thread::BaseMCFThread::SF_STATUS_PAUSE); if (isPaused) m_pMcfCon->onPause(); while (status == MCFCore::Thread::BaseMCFThread::SF_STATUS_PAUSE) { if (pauseCount > 30000 && m_pMcfCon->isConnected()) m_pMcfCon->disconnect(); gcSleep(500); pauseCount += 500; status = m_pCT->getStatus(m_uiId); } if (isPaused) { gcString name = m_pProvMng->getName(m_uiId); m_pMcfCon->setDPInformation(name.c_str()); } if (status == MCFCore::Thread::BaseMCFThread::SF_STATUS_STOP) break; m_ErrorMutex.lock(); if (m_bError) { requestNewUrl(m_Error); m_bError = false; } m_ErrorMutex.unlock(); if (status == MCFCore::Thread::BaseMCFThread::SF_STATUS_CONTINUE) doDownload(); } m_pProvMng->removeAgent(m_uiId); m_DeleteMutex.lock(); safe_delete(m_pMcfCon); m_DeleteMutex.unlock(); //need to do this incase we are the last thread and the controller is stuck on the wait mutex m_pCT->pokeThread(); }
void ToolManager::onSpecialCheck(WCSpecialInfo &info) { if (info.name == "msicheck") { info.handled = true; if (UTIL::WIN::isMsiInstalled(info.result.c_str())) { UTIL::FS::Path filePath(m_pUser->getAppDataPath(), "", false); filePath += "tools"; filePath += "installcheck"; UTIL::FS::recMakeFolder(filePath); filePath += UTIL::FS::File(info.result); try { UTIL::FS::FileHandle fh(filePath, UTIL::FS::FILE_WRITE); fh.write("installed", 9); fh.close(); info.result = filePath.getFullPath(); } catch (gcException) { Warning(gcString("Failed to open file {0} for Tool Msi Check.", filePath.getFullPath())); } } else { info.result = "!! Not Installed !!"; } } else if (info.name == "javacheck") { info.handled = true; std::string cur = UTIL::WIN::getRegValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\CurrentVersion"); std::string home = UTIL::WIN::getRegValue(gcString("HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\{0}\\JavaHome", cur)); char szJ[2] = {0}; szJ[0] = home.back(); uint32 cVer = atoi(info.result.c_str()); uint32 jVer = atoi(szJ); if (cVer != 0 && cVer >= jVer) info.result = home; else info.result = "!! Not Installed !!"; } else if (info.name == "dotnetcheck") { info.handled = true; std::vector<std::string> list; UTIL::STRING::tokenize(info.result, list, "."); uint32 major = 0; uint32 minor = 0; bool client = false; if (list.size() >= 1) major = atoi(list[0].c_str()); if (list.size() >= 2) minor = atoi(list[1].c_str()); if (list.size() >= 3) client = list[2] == "c"; if (UTIL::WIN::isDotNetVersionInstalled(major, minor, client)) { UTIL::FS::Path filePath(m_pUser->getAppDataPath(), "", false); filePath += "tools"; filePath += "installcheck"; UTIL::FS::recMakeFolder(filePath); filePath += UTIL::FS::File(gcString("dot_net_") + info.result); try { UTIL::FS::FileHandle fh(filePath, UTIL::FS::FILE_WRITE); fh.write("installed", 9); fh.close(); info.result = filePath.getFullPath(); } catch (gcException) { Warning(gcString("Failed to open file {0} for Tool .Net Check.", filePath.getFullPath())); } } else { info.result = "!! Not Installed !!"; } } else if (info.name == "temp") { UTIL::FS::Path path(m_pUser->getAppDataPath(), "", false); path += "temp"; time_t t = time(nullptr) + GetTickCount(); gcSleep(1000); path += gcString("{0}", t); UTIL::FS::recMakeFolder(path); info.result = path.getFullPath(); info.handled = true; } }