예제 #1
0
        public TcpSessionThread AssignSessionThread_()
        {
            TcpSessionThread thread = null;

            lock (this.m_sessionThreads)
            {
                foreach (TcpSessionThread th in this.m_sessionThreads)
                {
                    if (!th.Running)
                    {
                        th.Running = true;
                        return(th);
                    }
                }
                if (this.m_sessionThreads.Count >= Config.SESSION_CNT) // add bok - 20130528
                {
                    return(null);
                }

                thread         = new TcpSessionThread();
                thread.Running = true;
                this.m_sessionThreads.Add(thread);
            }

            return(thread);
        }
예제 #2
0
        public void ListenLoop_()
        {
            bool bResult = ThreadPool.SetMaxThreads(Config.SESSION_CNT, Config.SESSION_CNT);

            Socket sock_listen = null;

            try
            {
                IPEndPoint ep = new IPEndPoint(IPAddress.Any, Config.LISTEN_PORT);

                sock_listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock_listen.Bind(ep);
                sock_listen.Listen(Config.LISTEN_PORT);
                AppLog.Write(LOG_LEVEL.MSG, string.Format("### 리슨 시작 : 포트[{0}] ###", Config.LISTEN_PORT));

                while (!BtfaxThread.Stop)
                {
                    if (!sock_listen.Poll(ONE_SECOND, SelectMode.SelectRead))
                    {
                        continue;
                    }

                    Socket sock_session = sock_listen.Accept();

                    int nThreadCnt, nNotUse;
                    ThreadPool.GetAvailableThreads(out nThreadCnt, out nNotUse);
                    if (nThreadCnt <= 0)
                    {
                        AppLog.Write(LOG_LEVEL.WRN, "사용가능한 세션이 존재하지 않습니다. : GetAvailableThreads()");
                        SendErrorPacket(sock_session, RESULT.F_SESSION_FULL);
                        continue;
                    }

                    TcpSessionThread sessionThread = AssignSessionThread_();
                    if (sessionThread == null) // add bok - 20130528
                    {
                        AppLog.Write(LOG_LEVEL.WRN, "사용가능한 세션이 존재하지 않습니다. : AssignSessionThread_()");
                        SendErrorPacket(sock_session, RESULT.F_SESSION_FULL);
                        continue;
                    }

                    sessionThread.Socket = sock_session;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(sessionThread.ThreadEntry));
                }
            }
            catch (Exception ex)
            {
                AppLog.ExceptionLog(ex, "리슨 소켓 쓰레드에서 다음과 같은 오류가 발생하였습니다.");
            }

            if (sock_listen != null)
            {
                sock_listen.Close();
            }

            AppLog.Write(LOG_LEVEL.MSG, string.Format("### 리슨 종료 : 포트[{0}] ###", Config.LISTEN_PORT));
        }