Пример #1
0
        /// <summary>
        /// Создать сессию и добавить в список сессий
        /// </summary>
        public Session CreateSession()
        {
            lock (sessions)
            {
                long sessionID = 0;
                bool sessionOK = false;

                if (sessions.Count < MaxSessionCnt)
                {
                    sessionID = ScadaUtils.GetRandomLong();
                    int  attemptNum = 0;
                    bool duplicated;

                    while (duplicated = sessionID == 0 || sessions.ContainsKey(sessionID) &&
                                        ++attemptNum <= MaxGetSessionIDAttempts)
                    {
                        sessionID = ScadaUtils.GetRandomLong();
                    }

                    sessionOK = !duplicated;
                }

                if (sessionOK)
                {
                    Session session = new Session(sessionID);
                    sessions.Add(sessionID, session);
                    log.WriteAction(string.Format(Localization.UseRussian ?
                                                  "Создана сессия с ид. {0}" :
                                                  "Session with ID {0} created", sessionID));
                    return(session);
                }
                else
                {
                    log.WriteError(Localization.UseRussian ?
                                   "Не удалось создать сессию" :
                                   "Unable to create session");
                    return(null);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Creates a new session.
        /// </summary>
        protected bool CreateSession(out ConnectedClient client)
        {
            long sessionID = 0;
            bool sessionOK = false;

            client = null;

            if (clients.Count < MaxSessionCount)
            {
                client    = new ConnectedClient();
                sessionID = ScadaUtils.GetRandomLong();
                int  attemptNum = 0;
                bool duplicated;

                while (duplicated = sessionID == 0 ||
                                    ++attemptNum <= MaxGetSessionIDAttempts && !clients.TryAdd(sessionID, client))
                {
                    sessionID = ScadaUtils.GetRandomLong();
                }

                sessionOK = !duplicated;
            }

            if (sessionOK)
            {
                log.WriteAction(Locale.IsRussian ?
                                "Создана сессия с ид. {0}" :
                                "Session with ID {0} created", sessionID);
                client.SessionID = sessionID;
                return(true);
            }
            else
            {
                log.WriteError(Locale.IsRussian ?
                               "Не удалось создать сессию" :
                               "Unable to create session");
                client = null;
                return(false);
            }
        }