BOOL P2PManager::RunP2P(char* szIP, unsigned short Port) { GetPuCfgInstance().Init(); GetPuLogInstance().Init(); SetIdleTime(1000); SetSessionTimeout(8);//5 GetPuCfgInstance().SetSerial( 0 ); // if (THREADSTATUS_ZOMBIE == GetThreadStatus()) { SetReceivePktQueue(FALSE); SetBindInfo("0.0.0.0", 0, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); SetRelayInfo( szIP, Port ); if (TRUE == CPuPeers::Start()) { GetSelfLocalInfo(m_sinLocal); SendCheckExternalIp(); SendCheckExternalIp(); } } return TRUE; }
DWORD CThread::DestroyThread(BOOL bQueue) { MSG sMsg; DWORD dwExitCode; if (IsThreadActive()) { for (SetEvent(m_hObject), PostThreadMessage(WM_QUIT, 0, 0); GetThreadID() != GetCurrentThreadId() && ((!bQueue && WaitForSingleObject(m_hThread, INFINITE)) || (bQueue && MsgWaitForMultipleObjects(1, &m_hThread, FALSE, INFINITE, QS_PAINT | QS_TIMER | QS_POSTMESSAGE | QS_SENDMESSAGE) == WAIT_OBJECT_0 + 1)); ) { if (PeekMessage(&sMsg, (HWND)NULL, 0, 0, PM_QS_PAINT | PM_QS_POSTMESSAGE | PM_QS_SENDMESSAGE | PM_REMOVE)) { if (!AfxPreTranslateMessage(&sMsg)) { TranslateMessage(&sMsg); DispatchMessage(&sMsg); } } RestoreWaitCursor(); } dwExitCode = GetThreadStatus(); CloseHandle(m_hThread); CommonConstruct(); m_bAutoDelete = 0; return dwExitCode; } return 0; }
bool CEsperEngine::IsIdle (void) // IsIdle // // Returns TRUE if the engine is idle { int i; try { for (i = 0; i < m_Processors.GetCount(); i++) { SThreadStatus Status; GetThreadStatus(i, &Status); if (Status.iState != processingWaiting) return false; } } catch (...) { return false; } return true; }
BOOL CWin32Thread::Start(LPVOID pData) { try { if (GetThreadStatus()==tsStopped) { //Recreate the thread //Our thread ID DWORD dwThreadID; //Create the thread in suspend mode m_hThread=CreateThread(NULL, 0, Win32Thread, this, CREATE_SUSPENDED, &dwThreadID); //Check if created if (m_hThread) { //Set the thread ID SetThreadID(dwThreadID); //Set the status SetThreadStatus(tsSuspended); } else //Can't run return FALSE; } else if (GetThreadStatus()!=tsSuspended) return FALSE; //Start the thread CGenericThread::Start(pData); //Resume the thread if (m_hThread) if (ResumeThread(m_hThread)!=-1) //We are running return TRUE; return FALSE; } ERROR_HANDLER_RETURN("Start",FALSE) }
BOOL CWin32Thread::Stop() { try { //Only if suspened or running //Do we have the thread ? if (m_hThread) { //What status are we if (GetThreadStatus()==tsRunning && GetBruteTermination()) //First try to close it if (!TerminateThread(m_hThread,THREAD_DO_NOTHING_EXIT_VALUE)) return FALSE; if (GetThreadStatus()==tsSuspended || GetThreadStatus()==tsRunning) if (CloseHandle(m_hThread)) { //Close the handle m_hThread=NULL; //Stopped SetThreadStatus(tsStopped); //Exit return TRUE; } else return FALSE; else { //Just close the handle if (!CloseHandle(m_hThread)) m_hThread=NULL; //Exit return FALSE; } } else return FALSE; } ERROR_HANDLER_RETURN("Stop",FALSE) }
BOOL CWin32Thread::SetPriority(CGenericThread::ThreadPriority aPriority) { try { static const int iThreadPriority[]={THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL}; //Do we have a thread if (GetThreadStatus()==tsStopped) { //Recreate the thread //Our thread ID DWORD dwThreadID; //Create the thread in suspend mode m_hThread=CreateThread(NULL, 0, Win32Thread, this, CREATE_SUSPENDED, &dwThreadID); //Check if created if (m_hThread) { //Set the thread ID SetThreadID(dwThreadID); //Set the status SetThreadStatus(tsSuspended); } else //Can't run return FALSE; } //Now we can set the priority return SetThreadPriority(m_hThread, iThreadPriority[aPriority]); } ERROR_HANDLER_RETURN("SetPriority",FALSE) }
CXPlatThread::THREAD_STATUS CXPlatThread::StartThread(void *pParam, BOOL bStartSuspended) { int iStartFlag; THREAD_STATUS eStatus = GetThreadStatus(); // Check to see if thread is active if (eStatus != NOT_STARTED && eStatus != THREAD_TERMINATED) return (eStatus); // If handle exists, thread already ran at least once if (hThreadHandle) { if (IsThreadActive(hThreadHandle)) return (ALREADY_RUNNING); CloseThreadHandle(); }; pUserParm = pParam; // Start thread iStartFlag = bStartSuspended ? 0 : 1; #if defined(_WIN32) unsigned uThread; // If you are using Visual C++, and you get an error here, // make sure under Project|Settings|C++|Code Generation // that you are linking with the Multithreaded libraries. hThreadHandle = (HANDLE) ::_beginthreadex(NULL, 0, ThreadFunc, (void *) this, iStartFlag, &uThread); // Save whether thread is suspended if (hThreadHandle) bSuspended = bStartSuspended; #else iStartFlag = ::pthread_create(&hThreadHandle, pthread_attr_default, (START_ROUTINE) ThreadFunc, (void *) this); #endif // Return start status return (!hThreadHandle ? CANT_START : THREAD_SUCCESS); };
CXPlatThread::THREAD_STATUS CXPlatThread::ResumeThread() { #if !defined(_WIN32) return (NOT_SUPPORTED_ON_PLATFORM); #endif #if defined(_WIN32) THREAD_STATUS eStatus = GetThreadStatus(); // Resume? if (eStatus != SUSPENDED) return (RESUMED); if (::ResumeThread(hThreadHandle) != -1) bSuspended = FALSE; return (RESUMED); #endif };
CXPlatThread::THREAD_STATUS CXPlatThread::SuspendThread() { THREAD_STATUS eStatus = GetThreadStatus(); #if !defined(_WIN32) return (NOT_SUPPORTED_ON_PLATFORM); #endif // Suspend? if (eStatus != RUNNING) return (eStatus == SUSPENDED ? ALREADY_SUSPENDED : eStatus); // Suspend #if defined(_WIN32) if (::SuspendThread(hThreadHandle) != -1) bSuspended = TRUE; return (SUSPENDED); #else return (NOT_SUPPORTED_ON_PLATFORM); #endif };