1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| public Session ()
{
InitializeTimer();
//Lancement thread de test de connexion Internet et Serveur
testConnectionThread = new Thread(new ThreadStart(testConnections));
// Lancement du thread
testConnectionThread.Start();
}
private void testConnections()
{
HttpWebRequest request;
HttpWebResponse response;
//Envoi evenement aucune connexion internet
NoInternetConnection();
Thread.Sleep(2000);
//Test connexion serveur au démarrage
while (true)
{
try
{
// Create an HTTP Web request
request = (HttpWebRequest)HttpWebRequest.Create("http://www.monserveur.com");
// Perform GET
response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
//HTTP = 200, close the request and return true
response.Close();
//Log connexion Serveur
this.serveurConnection = true;
break;
}
else
{
this.serveurConnection = false;
}
}
catch (WebException)
{
// Deffinitely offline
this.serveurConnection = false;
}
//Si aucune connexion, attente 20s avant autre essai (ping)
Thread.Sleep(20000);
}
//Première connexion Ok. Test en continue de l'état de connexion Internet Windows
while (true)
{
try
{
//Test connexion internet
long lngFlags = 0;
int temp_int = (int)lngFlags;
if (InternetGetConnectedState(ref temp_int, 0))
{
if (this.internetConnection == false)
{
//Envoi evenement connexion internet Ok
InternetConnectionOk();
this.startMonnayeur();
}
}
else
{
if (this.internetConnection == true)
{
this.internetConnection = false;
this.serveurConnection = false;
//Envoi evenement aucune connexion internet
NoInternetConnection();
this.StopComMonnayeur();
}
}
}
catch (WebException)
{
// Deffinitely offline
}
Thread.Sleep(2000);
}
}
//Demarrage monnayeur
private void startMonnayeur()
{
//Initialisation communication Monnayeur
try
{
//Initialisation communication monnayeur (COM via un thread)
monnayeur = new Monnayeur(this);
this.monnayeurConnected = monnayeur.initComMonnayeur ();
//Lancement de la méthode de reception
comMonnayeurThread = new Thread(new ThreadStart(StartComMonnayeur));
// Lancement du thread
comMonnayeurThread.Start();
this.isMonnayeurStarted = true;
}
catch (Exception err)
{
//Log
}
}
public void creditInserer(double valeurCredit)
{
//Declenchement evenement debut de session IHM
StartSession();
//Demarrage session
this.startTimerSession(false);
//Envoi evenement credit inséré
CreditInserer();
} |
Partager