Пример #1
0
        void ProcessTimer(object unused)
        {
            m_timer.Change(Timeout.Infinite, Timeout.Infinite);

            var    netEngin = new NetEngin(Program.NetworkSettings);
            string tmpFile  = Path.GetTempFileName();

            netEngin.Download(tmpFile, Urls.ConnectionRespURL, true);

            var seq = from msg in DialogEngin.ReadConnectionsResp(tmpFile)
                      where msg.ReqID >= m_lastMsgID
                      select msg;

            if (!seq.Any())
            {
                m_timer.Change(Timeout.Infinite, Timeout.Infinite);
            }
            else
            {
                Message resp = (from msg in seq
                                where msg.ReqID == m_lastMsgID
                                select msg).SingleOrDefault();

                if (resp == null)
                {
                    PostReqAsync();
                }
                else
                {
                    switch (resp.MessageCode)
                    {
                    case Message_t.Ok:
                        //reset dlg file
                        try
                        {
                            string dlgFile = SettingsManager.GetClientDialogFilePath(m_clientID);
                            DialogEngin.WriteHubDialog(dlgFile, m_clientID, Enumerable.Empty <Message>());
                            netEngin.Upload(SettingsManager.GetClientDialogURL(m_clientID), dlgFile, true);
                            m_callback(Result_t.Ok);
                        }
                        catch (Exception ex)
                        {
                            Dbg.Log(ex.Message);
                            PostReqAsync();
                        }
                        break;

                    case Message_t.InvalidID:
                    case Message_t.Rejected:
                        m_callback(Result_t.Rejected);
                        break;

                    default:
                        Dbg.Assert(false);
                        break;
                    }
                }
            }
        }
Пример #2
0
        void ProcessResp()
        {
            StopTimer();

            string tmpFile = Path.GetTempFileName();

            Dbg.Log($"Processing Response, attempts = {m_attemptsCount + 1}.");

            m_exHandler = RespExceptionHandler;

            var netEngin = new NetEngin(Program.NetworkSettings);

            using (new AutoReleaser(() => File.Delete(tmpFile)))
            {
                SetProgressMessage("Réception des données à partir du serveur...");

                try
                {
                    netEngin.Download(tmpFile, Urls.ConnectionRespURL, true);
                }
                catch (Exception ex)
                {
                    Dbg.Log(ex.Message);
                    DialogEngin.WriteConnectionsResp(tmpFile, Enumerable.Empty <HubCore.DLG.Message>());
                }

                IEnumerable <HubCore.DLG.Message> messages = DialogEngin.ReadConnectionsResp(tmpFile);
                HubCore.DLG.Message[]             msgs     = (from resp in messages
                                                              where resp.ReqID >= m_msgID
                                                              select resp).ToArray();

                HubCore.DLG.Message msg = msgs.Where(m => m.ReqID == m_msgID).SingleOrDefault();

                uint clID = msg == null ? 0 : BitConverter.ToUInt32(msg.Data, 0);

                if (msg != null && clID == m_clInfo.ClientID)
                {
                    switch (msg.MessageCode)
                    {
                    case Message_t.InvalidID:
                        Dbg.Log($"Got invalid ID! (ClientID = {m_clInfo.ClientID}).");

                        ClientInfo clInfo = ClientInfo.CreateClient(m_clInfo.ProfileID);
                        clInfo.ContaclEMail = m_clInfo.ContaclEMail;
                        clInfo.ContactName  = m_clInfo.ContactName;
                        clInfo.ContactPhone = m_clInfo.ContactPhone;
                        m_clInfo            = clInfo;

                        if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                        {
                            if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) != DialogResult.Yes)
                            {
                                CloseDialog();
                                return;
                            }
                            else
                            {
                                m_attemptsCount = 0;
                            }
                        }

                        PostReq();
                        break;

                    case Message_t.Ok:
                        Dbg.Log("Client registered :-)!");


                        Program.Settings.ClientInfo = m_clInfo;
                        SetProgressMessage("Enregistrement terminé.");

                        //creation des fichier dlg
                        string dlgFile = SettingsManager.GetClientDialogFilePath(clID);
                        DialogEngin.WriteHubDialog(dlgFile, clID, Enumerable.Empty <HubCore.DLG.Message>());

                        try
                        {
                            netEngin.Upload(SettingsManager.GetClientDialogURL(clID), dlgFile, true);
                        }
                        catch (Exception ex)
                        {
                            Dbg.Log(ex.Message);
                        }

                        ShowMessage("Votre enregistrement est maintenant terminé. " +
                                    "Vous pouvez commencer à utiliser l’application.");

                        IsRegistered = true;
                        CloseDialog();
                        break;

                    case Message_t.InvalidProfile:
                        Dbg.Log($"Got invalid Profile! (ProfileID: = {m_clInfo.ProfileID}).");

                        ShowMessage(SRV_ERROR);
                        CloseDialog();
                        return;

                    case Message_t.Rejected:
                        Dbg.Log("Got reject connection!");
                        ShowMessage(REJECT_CONNCTION_ERROR);
                        CloseDialog();
                        return;

                    default:
                        Dbg.Log("Got invalid response!!!!");
                        Dbg.Assert(false);
                        break;
                    }
                }
                else if (msgs.Length > 0)
                {
                    Dbg.Log("Request message lost.");

                    if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                    {
                        if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) != DialogResult.Yes)
                        {
                            CloseDialog();
                            return;
                        }
                        else
                        {
                            m_attemptsCount = 0;
                        }
                    }

                    PostReq();
                }
                else if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                {
                    Dbg.Log("Timeout.");

                    if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        StartTimer();
                        m_attemptsCount = 0;
                        PostReq();
                    }
                    else
                    {
                        CloseDialog();
                    }
                }
                else
                {
                    StartTimer();
                    SetProgressMessage("Attente de la réponse du serveur...");
                }
            }
        }
Пример #3
0
        void ProcessResp()
        {
            m_timer.Stop();
            Dbg.Log("Processing start notification resp...");

            string tmpFile  = Path.GetTempFileName();
            var    netEngin = new NetEngin(Program.NetworkSettings);

            try
            {
                netEngin.Download(tmpFile, Urls.ConnectionRespURL, true);

                IEnumerable <Message> resps = from msg in DialogEngin.ReadConnectionsResp(tmpFile)
                                              where msg.ReqID >= m_reqID
                                              select msg;

                if (resps.Any())
                {
                    Message resp = resps.SingleOrDefault(m => m.ReqID == m_reqID);

                    if (resp != null)
                    {
                        var  ms     = new MemoryStream(resp.Data);
                        var  reader = new RawDataReader(ms, Encoding.UTF8);
                        uint clID   = reader.ReadUInt();

                        if (clID == m_clID)
                        {
                            switch (resp.MessageCode)
                            {
                            case Message_t.Ok:
                                m_callBack.Invoke(true);
                                Dbg.Log("Starting notification done. :-)");
                                return;

                            case Message_t.Rejected:
                                m_callBack.Invoke(false);
                                Dbg.Log("Starting rejected. :-(");
                                return;
                            }
                        }
                    }

                    Dbg.Log("Starting msg lost. Reposting...");
                    PostReq();
                }
                else if (++m_cnxAttempts >= MAX_ATTEMPTS)
                {
                    Dbg.Log("Starting msg lost. Reposting...");
                    PostReq();
                }
                else
                {
                    m_timer.Start();
                }
            }
            catch (Exception ex)
            {
                Dbg.Log(ex.Message);
                m_timer.Start();
            }
            finally
            {
                File.Delete(tmpFile);
            }
        }