コード例 #1
0
LVPNConnector::~LVPNConnector()
{
    SyncDisconnect();
}
コード例 #2
0
ファイル: main.c プロジェクト: mastertheknife/FSTimeSync
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {
	/* Mutex to prevent multiple instances of this application */
  	if (CreateMutex(NULL,FALSE,"_FSTimeSyncMutex_") && GetLastError() == ERROR_ALREADY_EXISTS) {
		MessageBox(NULL,"An instance of FS Time Sync is already running.","FS Time Sync Error",MB_OK | MB_ICONERROR);
		return 100;
	}

	/* Initialize the critical sections and perform indepenedant modules startup */
	InitializeCriticalSection(&ProgramDataCS);
	InitializeCriticalSection(&ProgramControlCS);
	DebugStartup();
	RegistryStartup();
	SyncStartup();
 	GUIStartup();

	/* Load the settings from the registry */
	RegistryReadSettings(&Settings);
	
	/* Set the operating mode (manual or auto) based on the setting and next sync */
 	SetRTVal(FST_AUTOMODE,Settings.AutoOnStart);
	Stats.SyncNext = time(NULL)+Settings.AutoSyncInterval;
	Stats.SyncInterval = Settings.AutoSyncInterval;
	
	/* Start the GUI Thread, based on the Start Minimized setting */
	if(Settings.StartMinimized == 1)
		GUIStartThread(SW_MINIMIZE);
	else	
		GUIStartThread(nCmdShow);	
			 		
	
	/* Main program loop */
	while(!GetRTVal(FST_QUIT)) {
		time_t CurrentTime;
		struct tm* systm;
		int nResult;
		time_t SimUTCDest;
		
		/* Reset CancelSync */
		if(CancelSync)
			CancelSync = 0;

		EnterCriticalSection(&ProgramDataCS);

		Stats.SimStatus = SyncGetConStatus(); /* Get updated state of the connection */
		CurrentTime = time(NULL); /* Saves multiple expensive calls to time() */
			
		/* System UTC time */
		systm = gmtime(&CurrentTime);
		if((Stats.SysUTCTime = mktime(systm)) == (time_t)-1) {
			debuglog(DEBUG_ERROR,"mktime failed making system UTC time!\n");
			Stats.SysUTCTime = CurrentTime; /* If we can't get UTC, then lets have local */
		}
		if(Settings.SystemUTCCorrectionState)
			Stats.SysUTCTime += Settings.SystemUTCCorrection*60;

		/* Only proceed if sim is connected */
		if(Stats.SimStatus) {	
			/* Get the simulator's UTC time */
			SyncGetFSTimestamp(&Stats.SimUTCTime);			
			/* Get some information what the simulator is doing */
			SyncGetState(&Stats.SimState,Settings.FSMenuDetection);
			
			/* Check to see if FSMenuDetection is not set to 0 (skip) */
			if(Settings.FSMenuDetection) {
				/* Cancel any possible sync if simulator is in a menu or dialog*/
				if(!Stats.SimState.SimInFlight)
					CancelSync = 1;	
				/* Sim in flight state might have changed, need to update the GUI */
				if(Stats.SimState.SimInFlight != SimStatePrev.SimInFlight)
					ReUpdateGUI = 1;
			}
			/* Check for the setting to sync if paused */
			if(Settings.NoSyncPaused) {
				/* Cancel any possible sync if simulator is paused */
				if(Stats.SimState.SimPaused)
					CancelSync = 1;	
				/* Sim pause state might have changed, need to update the GUI */
				if(Stats.SimState.SimPaused != SimStatePrev.SimPaused)
					ReUpdateGUI = 1;
			}
			/* Check the no sync when sim rate other than 1x setting */
			if(Settings.NoSyncSimRate) {
				/* Cancel any possible sync if simulator is at any other rate than 1x */
				if(Stats.SimState.SimRate != 256)
					CancelSync = 1;
				/* Sim rate might have changed, need to update the GUI */	
				if(Stats.SimState.SimRate != SimStatePrev.SimRate)
					ReUpdateGUI = 1;
			}
						
			if(GetRTVal(FST_AUTOMODE)) {
				/* Check for no sync, e.g. paused or sim rate */
				if(CancelSync) {
					Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
					Stats.SyncInterval = Settings.AutoSyncInterval;
				}
				/* Check if switched to faster synch interval and there's too long to wait */
				if((Stats.SyncNext-CurrentTime) > Settings.AutoSyncInterval) {
					Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
					Stats.SyncInterval = Settings.AutoSyncInterval; /* Progress bar is drawn based on current synch, not next one */
				}
				/* Check if switched to slower interval */
				if(Stats.SyncInterval < Settings.AutoSyncInterval) {
					Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
					Stats.SyncInterval = Settings.AutoSyncInterval; /* Progress bar is drawn based on current synch, not next one */
				}
				/* Check if it's time to sync and if time, sync*/
				if(Stats.SyncNext <= CurrentTime) {
					/* Use system UTC time */
					SimUTCDest = Stats.SysUTCTime;
					if(nResult = SyncGo(SimUTCDest,Settings.FSSyncMethod)) {
						Stats.SyncNext = time(&CurrentTime)+Settings.AutoSyncInterval; /* Sync takes time, update the time on the way */
						Stats.SyncLast = CurrentTime;
						Stats.SyncInterval = Settings.AutoSyncInterval;
						Stats.SyncLastModified = 1;
					} else {
						Stats.SyncNext = CurrentTime+3;
						Stats.SyncInterval = 3;
					}
				}
			} else {
				/* Manual mode */
				Stats.SyncInterval = 0;
				if(GetRTVal(FST_SYNCNOW)) {
					SetRTVal(FST_SYNCNOW, 0); /* Clear the event, regardless of success or failure */
					if(!CancelSync) {
						/* Use system UTC time */
						SimUTCDest = Stats.SysUTCTime;														
						if(SyncGo(SimUTCDest,Settings.FSSyncMethod)) {
							Stats.SyncLast = time(&CurrentTime);
							Stats.SyncLastModified = 1;
						}
					}
				} /* Sync now */
			} /* Mode */
		} else { /* Sim not connected */
		  
			/* Connect to the sim */
			Stats.SimStatus = SyncConnect(SIM_ANY);

			if(Stats.SimStatus != SimStatusPrev)
				ReUpdateGUI = 1; /* Force updating of the GUI */

			/* Set up the affinity fix and\or the priority fix depending on settings */
			if(Stats.SimStatus)
				AffinityPriorityFix(Settings.EnableAffinityFix,Settings.EnablePriorityFix);
				
		} /* SimStatus */

		/* Copy the variables over the previous ones
		so in next loop iteration we can know if something changed */
		memcpy(&SimStatePrev,&Stats.SimState,sizeof(FSState_t));
		SimStatusPrev = Stats.SimStatus;
		
		/* Call GUIUpdate() (indirectly) if set to 1 */
		/* This updates everything in the main window and calls update elements and update tray */
		if(ReUpdateGUI) {
			ReUpdateGUI = 0;
			/* Signal GUI thread to call GUIUpdate */
			PostMessage(hDummyWindow,FSTSGUI_WM_MAINMSG,FSTSGUI_GUIFULLUPDATE,0);
		} else {		
			/* Updating the elements (such as progress bar) should happen at every loop iteration
			If GUIUpdate() was called, it will also update the elements so no need to call it again.
			But if it wasn't, we do it ourselves by telling GUI thread to update the elements */
			/* Signal GUI thread to call GUIElementsUpdate */
			PostMessage(hDummyWindow,FSTSGUI_WM_MAINMSG,FSTSGUI_GUIELEMUPDATE,0);
		}
			
		LeaveCriticalSection(&ProgramDataCS);		
		Sleep(SLEEP_DURATION);
	} /* Loop end */ 

	/* Stop the GUI thread, or at least wait for it to close */
	GUIStopThread();
	
	/* Disconnect from the simulator if it's still connected */
	if(SyncGetConStatus())
		SyncDisconnect();
	
	/* Write settings to the registry */
	RegistryWriteSettings(&Settings);

	/* Delete the critical sections and perform complete shutdown */	
	DeleteCriticalSection(&ProgramDataCS);
	DeleteCriticalSection(&ProgramControlCS);
	GUIShutdown();
	SyncShutdown();
	RegistryShutdown();
	DebugShutdown();

	return 0;
}