private bool AddPreparedGroupList(MatchWaitingGroup group, bool flagLock)
 {
     if (flagLock)
     {
         lock (pglock)
         {
             if (pgIndexPool.Count > 0)
             {
                 int index = pgIndexPool.Pop();
                 pgList[index]       = group;
                 pgList[index].Index = index;
                 return(true);
             }
         }
     }
     else
     {
         if (pgIndexPool.Count > 0)
         {
             int index = pgIndexPool.Pop();
             pgList[index]       = group;
             pgList[index].Index = index;
             return(true);
         }
     }
     return(false);
 }
 private void RemovePreparedGroupList(MatchWaitingGroup group, bool flagLock)
 {
     if (flagLock)
     {
         lock (pglock)
         {
             pgList[group.Index] = null;
             pgIndexPool.Push(group.Index);
             group.Index = -1;
         }
     }
     else
     {
         pgList[group.Index] = null;
         pgIndexPool.Push(group.Index);
         group.Index = -1;
     }
 }
        private void Procedure_Matchmaking()
        {
            /// 현재 n명의 유저를 스택에 집어넣고 5명 이상의 그룹이 완성되면 그것을 넘겨준다.

            Stopwatch watch;

            while (true)
            {
                watch = Stopwatch.StartNew();

                MatchWaitingGroup group = new MatchWaitingGroup();

                lock (lockMatchingList)
                {
                    for (int i = 0; i < Config.MAX_CONNECTIONS; i++)
                    {
                        if (matchingUserList[i] != null)
                        {
                            group.Users.Push(matchingUserList[i]);
                            if (group.Users.Count == Config.USER_COUNT_PER_ONE_GAME)
                            {                                               // 그룹에 포함된 유저수가 한 게임당 유저수를 넘었다면
                                if (AddPreparedGroupList(group, true))      // 그룹을 '준비된그룹리스트' 에 넣고
                                {
                                    var iter = group.Users.GetEnumerator(); //모든 유저에게 매칭완료 패킷을 보내야함
                                    while (iter.MoveNext())
                                    {
                                        iter.Current.BeGameEnter = true;
                                        RemoveMatchingList(iter.Current, false);
                                        iter.Current.Send(complete_matchmake);
                                    }
                                }
                                group = new MatchWaitingGroup();
                            }
                        }
                    }
                }

                watch.Stop();

                Thread.Sleep((int)(MATCHING_PERIOD - watch.ElapsedMilliseconds));
            }
        }