Example #1
0
THREAD_FUN CNet::NetLoop(LPVOID p) {
    CNet * pThis = (CNet *)p;
    if (NULL == pThis) {
        ASSERT(false);
        return -1;
    }
    s32 code = 0;
    s32 err = 0;
    struct iocp_event * pEvent;
    while (true) {
        if (pThis->m_stop) {
            CAutoLock(&(pThis->m_freelock));
            pThis->m_nThreadCount--;
            return 0;
        }

        pEvent = NULL;
        code = get_event(&pEvent, &err);
        if (ERROR_NO_ERROR == code && pEvent != NULL) {
            NET_TRACE("event %d, code %d, last error %d, io bytes %d", pEvent->event, code, err, pEvent->ioBytes);
            pThis->m_queue.add(pEvent);
        } else {
            if (pEvent != NULL) {
                NET_ERROR("event %d, code %d, error %d", pEvent->event, code, err);
            }
            CSleep(10);
        }
    }
}
Example #2
0
 void CSelectModel::Stop() {
     if (!m_bShutdown) {
         m_bShutdown = true;
         while (m_nThreadCount != 0) {
             CSleep(1);
         }
     }
 }
Example #3
0
 void CHttpEngine::Stop() {
     if (!m_shutdown) {
         m_shutdown = true;
         while (m_nThreadCount !=  0){
             CSleep(10);
         }
         HttpBackCall(9999999);
     }
 }
Example #4
0
void CNet::CStop() {
    ASSERT(!m_stop);
    LogModuleStop();
    m_stop = true;
    iocp_stop();
    while (m_nThreadCount != 0) {
        CSleep(10);
    }
    NET_TRACE("网络停止工作");
}
Example #5
0
    THREAD_FUN CHttpEngine::HttpThread(void * p) {
        CHttpEngine * pThis = ((ThreadArgs  * ) p)->pHttpEngine;
        s32 nQueueID = ((ThreadArgs  * ) p)->nQueueID;
        TQueue<HTTPStream  * , false, HTTP_QUEUE_SIZE> * pQueue = &(pThis->m_pHttpQueue[nQueueID]);
        HTTPStream * pStream = NULL;

        CURL * pCurlHandle = curl_easy_init();

        while (true) {
            if (pQueue->read(pStream)) {
                curl_easy_reset(pCurlHandle);
                curl_easy_setopt(pCurlHandle, CURLOPT_TIMEOUT, 10);
                curl_easy_setopt(pCurlHandle, CURLOPT_VERBOSE, 0L);  
                curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, write_callback); 

                ASSERT(pStream->size() == sizeof(HTTPContext));
                HTTPContext * pHttp = (HTTPContext *)pStream->buff();
                //这个变量可作为接收或传递数据的作用
                curl_easy_setopt(pCurlHandle, CURLOPT_URL, pHttp->pUrl->buff());
                HTTPStream * pBack = pThis->m_HTTPStreamPool.Create();
                curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, pBack);
                
                pHttp->code = curl_easy_perform(pCurlHandle);
                char end = '\0';
                pBack->in(&end, sizeof(end));
                pHttp->pUrl->clear();
                pThis->m_HTTPStreamPool.Recover(pHttp->pUrl);
                pHttp->pBack = pBack;
                
                pThis->m_backQueue.add(pStream);
            } else if (pQueue->IsEmpty() && pThis->m_shutdown){
                curl_easy_cleanup(pCurlHandle);
                delete p;
                CAutoLock(&(pThis->m_freelock));
                pThis->m_nThreadCount--;
                return 0;
            } else {
                CSleep(10);
            }
        }
    }
Example #6
0
    THREAD_FUN CSelectModel::WriteThread(void * p) {
        CSelectModel * pThis = (CSelectModel *) p;
        ASSERT(pThis);
        {
            CAutoLock autoLock(&pThis->m_lock);
            pThis->m_nThreadCount++;
        }

        while (true) {
            if (pThis->m_bShutdown) {
                CAutoLock autoLock(&pThis->m_lock);
                pThis->ClearHandle();
                pThis->m_nThreadCount--;
                return 0;
            }

            pThis->CheckWrite();
            CSleep(0);
        }

        return 0;
    }
Example #7
0
void MusicDriver_ExtMidi::DoStop()
{
	if (this->pid <= 0) return;

	/* First try to gracefully stop for about five seconds;
	 * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
	for (int i = 0; i < 500; i++) {
		kill(this->pid, SIGTERM);
		if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
			/* It has shut down, so we are done */
			this->pid = -1;
			return;
		}
		/* Wait 10 milliseconds. */
		CSleep(10);
	}

	DEBUG(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
	/* Gracefully stopping failed. Do it the hard way
	 * and wait till the process finally died. */
	kill(this->pid, SIGKILL);
	waitpid(this->pid, NULL, 0);
	this->pid = -1;
}
Example #8
0
void VideoDriver_SDL::MainLoop()
{
	uint32 cur_ticks = SDL_CALL SDL_GetTicks();
	uint32 last_cur_ticks = cur_ticks;
	uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
	uint32 mod;
	int numkeys;
	Uint8 *keys;

	CheckPaletteAnim();

	if (_draw_threaded) {
		/* Initialise the mutex first, because that's the thing we *need*
		 * directly in the newly created thread. */
		_draw_mutex = ThreadMutex::New();
		if (_draw_mutex == NULL) {
			_draw_threaded = false;
		} else {
			_draw_mutex->BeginCritical();
			_draw_continue = true;

			_draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);

			/* Free the mutex if we won't be able to use it. */
			if (!_draw_threaded) {
				_draw_mutex->EndCritical();
				delete _draw_mutex;
				_draw_mutex = NULL;
			} else {
				/* Wait till the draw mutex has started itself. */
				_draw_mutex->WaitForSignal();
			}
		}
	}

	DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");

	for (;;) {
		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
		InteractiveRandom(); // randomness

		while (PollEvent() == -1) {}
		if (_exit_game) break;

		mod = SDL_CALL SDL_GetModState();
#if SDL_VERSION_ATLEAST(1, 3, 0)
		keys = SDL_CALL SDL_GetKeyboardState(&numkeys);
#else
		keys = SDL_CALL SDL_GetKeyState(&numkeys);
#endif
#if defined(_DEBUG)
		if (_shift_pressed)
#else
		/* Speedup when pressing tab, except when using ALT+TAB
		 * to switch to another application */
#if SDL_VERSION_ATLEAST(1, 3, 0)
		if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
#else
		if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
#endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
#endif /* defined(_DEBUG) */
		{
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
		} else if (_fast_forward & 2) {
			_fast_forward = 0;
		}

		cur_ticks = SDL_CALL SDL_GetTicks();
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
			_realtime_tick += cur_ticks - last_cur_ticks;
			last_cur_ticks = cur_ticks;
			next_tick = cur_ticks + MILLISECONDS_PER_TICK;

			bool old_ctrl_pressed = _ctrl_pressed;

			_ctrl_pressed  = !!(mod & KMOD_CTRL);
			_shift_pressed = !!(mod & KMOD_SHIFT);

			/* determine which directional keys are down */
			_dirkeys =
#if SDL_VERSION_ATLEAST(1, 3, 0)
				(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
				(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
				(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
				(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
#else
				(keys[SDLK_LEFT]  ? 1 : 0) |
				(keys[SDLK_UP]    ? 2 : 0) |
				(keys[SDLK_RIGHT] ? 4 : 0) |
				(keys[SDLK_DOWN]  ? 8 : 0);
#endif
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();

			/* The gameloop is the part that can run asynchronously. The rest
			 * except sleeping can't. */
			if (_draw_mutex != NULL) _draw_mutex->EndCritical();

			GameLoop();

			if (_draw_mutex != NULL) _draw_mutex->BeginCritical();

			UpdateWindows();
			_local_palette = _cur_palette;
		} else {
			/* Release the thread while sleeping */
			if (_draw_mutex != NULL) _draw_mutex->EndCritical();
			CSleep(1);
			if (_draw_mutex != NULL) _draw_mutex->BeginCritical();

			NetworkDrawChatMessage();
			DrawMouseCursor();
		}

		/* End of the critical part. */
		if (_draw_mutex != NULL && !HasModalProgress()) {
			_draw_mutex->SendSignal();
		} else {
			/* Oh, we didn't have threads, then just draw unthreaded */
			CheckPaletteAnim();
			DrawSurfaceToScreen();
		}
	}

	if (_draw_mutex != NULL) {
		_draw_continue = false;
		/* Sending signal if there is no thread blocked
		 * is very valid and results in noop */
		_draw_mutex->SendSignal();
		_draw_mutex->EndCritical();
		_draw_thread->Join();

		delete _draw_mutex;
		delete _draw_thread;

		_draw_mutex = NULL;
		_draw_thread = NULL;
	}
}
Example #9
0
void VideoDriver_Allegro::MainLoop()
{
	uint32 cur_ticks = GetTime();
	uint32 last_cur_ticks = cur_ticks;
	uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;

	CheckPaletteAnim();

	for (;;) {
		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
		InteractiveRandom(); // randomness

		PollEvent();
		if (_exit_game) return;

#if defined(_DEBUG)
		if (_shift_pressed)
#else
		/* Speedup when pressing tab, except when using ALT+TAB
		 * to switch to another application */
		if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
#endif
		{
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
		} else if (_fast_forward & 2) {
			_fast_forward = 0;
		}

		cur_ticks = GetTime();
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
			_realtime_tick += cur_ticks - last_cur_ticks;
			last_cur_ticks = cur_ticks;
			next_tick = cur_ticks + MILLISECONDS_PER_TICK;

			bool old_ctrl_pressed = _ctrl_pressed;

			_ctrl_pressed  = !!(key_shifts & KB_CTRL_FLAG);
			_shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);

			/* determine which directional keys are down */
			_dirkeys =
				(key[KEY_LEFT]  ? 1 : 0) |
				(key[KEY_UP]    ? 2 : 0) |
				(key[KEY_RIGHT] ? 4 : 0) |
				(key[KEY_DOWN]  ? 8 : 0);

			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();

			GameLoop();

			UpdateWindows();
			CheckPaletteAnim();
			DrawSurfaceToScreen();
		} else {
			CSleep(1);
			NetworkDrawChatMessage();
			DrawMouseCursor();
			DrawSurfaceToScreen();
		}
	}
}
Example #10
0
void VideoDriver_Dedicated::MainLoop()
{
	uint32 cur_ticks = GetTime();
	uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;

	/* Signal handlers */
#if defined(UNIX) || defined(PSP)
	signal(SIGTERM, DedicatedSignalHandler);
	signal(SIGINT, DedicatedSignalHandler);
	signal(SIGQUIT, DedicatedSignalHandler);
#endif

	/* Load the dedicated server stuff */
	_is_network_server = true;
	_network_dedicated = true;
	_current_company = _local_company = COMPANY_SPECTATOR;

	/* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
	if (_switch_mode != SM_LOAD_GAME) {
		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
		SwitchToMode(_switch_mode);
		_switch_mode = SM_NONE;
	} else {
		_switch_mode = SM_NONE;
		/* First we need to test if the savegame can be loaded, else we will end up playing the
		 *  intro game... */
		if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
			/* Loading failed, pop out.. */
			DEBUG(net, 0, "Loading requested map failed, aborting");
			_networking = false;
		} else {
			/* We can load this game, so go ahead */
			SwitchToMode(SM_LOAD_GAME);
		}
	}

	/* Done loading, start game! */

	if (!_networking) {
		DEBUG(net, 0, "Dedicated server could not be started, aborting");
		return;
	}

	while (!_exit_game) {
		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
		InteractiveRandom(); // randomness

		if (!_dedicated_forks) DedicatedHandleKeyInput();

		cur_ticks = GetTime();
		_realtime_tick += cur_ticks - prev_cur_ticks;
		if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
			next_tick = cur_ticks + MILLISECONDS_PER_TICK;

			GameLoop();
			UpdateWindows();
		}

		/* Don't sleep when fast forwarding (for desync debugging) */
		if (!_ddc_fastforward) CSleep(1);
	}
}