Example #1
0
        /// <summary>
        /// 네트워크 작업을 시작합니다.
        /// </summary>
        /// <param name="generator">Session 객체를 생성하는 Delegator를 설정합니다. SessionManager에서는 내부적으로 Session Pool을 관리하는데, Pool에 객체가 부족할 때 이 Delegator가 호출됩니다. 그러므로 이 Delegator에서는 ObjectPool 대신 new를 사용해 인스턴스를 생성하는 것이 좋습니다.</param>
        /// <param name="initPoolCount">처음 생성할 Session의 개수를 지정합니다.</param>
        /// <param name="maxPoolCount">생성할 수 있는 Session의 최대개수를 지정합니다. 0이 입력되면 최대개수를 무시합니다.</param>
        /// <returns>현재 NetworkChannel 객체를 반환합니다.</returns>
        public NetworkChannel StartNetwork(SessionGenerateDelegator generator, Int32 initPoolCount, Int32 maxPoolCount)
        {
            if (generator == null)
            {
                throw new AegisException(AegisResult.InvalidArgument, $"Argument '{nameof(generator)}' could not be null.");
            }

            _sessionGenerator = generator;
            MaxSessionCount   = maxPoolCount;


            lock (this)
            {
                while (initPoolCount <= maxPoolCount && initPoolCount-- > 0)
                {
                    Session session = _sessionGenerator();
                    session.Activated   += OnSessionActivated;
                    session.Inactivated += OnSessionInactivated;

                    InactiveSessions.Add(session);
                }
            }


            return(this);
        }
Example #2
0
 private void OnSessionInactivated(Session session)
 {
     lock (this)
     {
         ActiveSessions.Remove(session);
         InactiveSessions.Add(session);
     }
 }
Example #3
0
 private void SessionInactivated(Session session)
 {
     lock (Channels)
     {
         ActiveSessions.Remove(session);
         InactiveSessions.Add(session);
     }
 }
Example #4
0
        internal Session GenerateSession()
        {
            lock (this)
            {
                if (InactiveSessions.Count == 0)
                {
                    if (MaxSessionCount == 0)
                    {
                        Session session = _sessionGenerator();
                        session.Activated   += OnSessionActivated;
                        session.Inactivated += OnSessionInactivated;

                        InactiveSessions.Add(session);
                    }
                    else
                    {
                        return(null);
                    }
                }

                return(InactiveSessions[0]);
            }
        }
Example #5
0
        internal Session PopInactiveSession()
        {
            lock (Channels)
            {
                if (MaxSessionCount > 0 &&
                    ActiveSessions.Count + InactiveSessions.Count >= MaxSessionCount)
                {
                    return(null);
                }


                if (InactiveSessions.Count == 0)
                {
                    Session session = SessionGenerator();
                    session.Activated   += SessionActivated;
                    session.Inactivated += SessionInactivated;

                    InactiveSessions.Add(session);
                }

                return(InactiveSessions[0]);
            }
        }