Example #1
0
void CP2PServiceModule::OnCheckTimer( UINT uID, UINT uTime )
{
	if ( !g_socketClientMgr.IsConnected())
	{
		CKLog::WriteLog( LOG_TYPE_DEBUG, "reconnect server.%d", GetTickCount());
		g_server.stop();
		g_socketClientMgr.Release();
		if ( g_socketClientMgr.Connect())
			g_server.start();
	}
}
BOOL CtrlHandler(DWORD fdwCtrlType)
{
	HttpServer* server;
	switch (fdwCtrlType)
	{
		// Handle the CTRL-C signal. 
	case CTRL_C_EVENT:
		LOG(INFO) << "The user wants to close server.";
		// shutdown server
		server = (HttpServer*)MainObjectFactoryImpl::getInstance()->getObject("HttpServer");
		if (server != NULL){
			server->stop();
		}
		else{
			LOG(WARNING) << "Failed to get server instance from factory.";
		}
		return(TRUE);

		// CTRL-CLOSE: confirm that the user wants to exit. 
		//case CTRL_CLOSE_EVENT:
		//	// shutdown server
		//	if (server != NULL){
		//		server->stop();
		//	}
		//	return(TRUE);

		//	// Pass other signals to the next handler. 
		//case CTRL_BREAK_EVENT:
		//	Beep(900, 200);
		//	printf("Ctrl-Break event\n\n");
		//	return FALSE;

		//case CTRL_LOGOFF_EVENT:
		//	Beep(1000, 200);
		//	printf("Ctrl-Logoff event\n\n");
		//	return FALSE;

		//case CTRL_SHUTDOWN_EVENT:
		//	Beep(750, 500);
		//	printf("Ctrl-Shutdown event\n\n");
		//	return FALSE;

	default:
		return FALSE;
	}
}
Example #3
0
void CP2PServiceModule::RunMessageLoop()   
{    
#if defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	CKLog::SetDebugType( LOG_TYPE_DEBUG);
#else
	CKLog::SetDebugType( LOG_TYPE_WARNING);
#endif

	char szServiceName[256];
	::LoadString( NULL, IDS_SERVICENAME, szServiceName, sizeof(szServiceName));
	ChangeServiceStartType( szServiceName, SERVICE_AUTO_START);

	TCHAR szClient_Mutex[] = _T("PPVOD_MUTEX_K_1.0");
	if ( ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, szClient_Mutex) ) return;
	::CreateMutex(0, FALSE, szClient_Mutex);

	g_socketClientMgr.Connect();

	bool bListen = false;
	UINT uPort = 81;

	for ( int i = 1; i < 20; i ++ )
	{
		g_server.setPort( uPort);
		if ( g_server.create( string("P2PLocalService"), string("C:\\") ) )
		{
			bListen = true;
			break;
		}
		uPort += i;
	}

	HttpResponse::setServer( g_server );

	if ( bListen )
	{
		// 设置共享内存监听端口为 uPort
		CRegKey reg;
		LPCTSTR lpszKey = "SOFTWARE\\P2PService_K";

		if ( reg.Open( HKEY_LOCAL_MACHINE, lpszKey ) == ERROR_SUCCESS )
		{
			TCHAR szAppPath[MAX_PATH];
			ULONG ulLen = sizeof( szAppPath );
			if ( reg.SetDWORDValue( "ListenPort", uPort ) == ERROR_SUCCESS )
			{
			}
			reg.Close();
		}

		SetTimer( NULL, CHECK_SERVER_TIMER, 1000, NULL);

		MSG msg;
		while( GetMessage(&msg, NULL, 0, 0 )) 
		{
			switch( msg.message) {
			case WM_TIMER:
				OnCheckTimer( 0, 0);
				break;
			default:
				break;
			}
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}

		KillTimer( NULL, CHECK_SERVER_TIMER);
	}

	g_server.stop();

	SetServiceStatus(SERVICE_STOPPED);
}