/// <summary> /// Adds session. /// </summary> /// <param name="sessionID">Session ID.</param> /// <param name="session">Session object.</param> internal void AddSession(string sessionID, Session session) { lock (m_SessionTable) { m_SessionTable.Add(sessionID, session); } /* if(m_LogCmds) { logWriter.AddEntry("//----- Sys: 'Session:'" + sessionID + " added " + DateTime.Now); } */ }
private void Run() { eventClose.Reset(); try { string strError = ""; // Log.WriteEntry("dp2ZServer service start step 1"); // 装载配置文件 int nRet = LoadCfgDom(out strError); if (nRet == -1) { Log.WriteEntry("dp2ZServer error : " + strError, EventLogEntryType.Error); return; } // return: // -2 出错。但后面可以重试 // -1 出错,后面不再重试 // 0 成功 nRet = GetSlowCfgInfo(out strError); if (nRet == -1) { Log.WriteEntry("ERR001 首次初始化信息失败(系统不再重试): " + strError, EventLogEntryType.Error); return; } if (nRet == -2) { Log.WriteEntry("ERR002 首次初始化信息失败(系统将定期重试): " + strError, EventLogEntryType.Error); } // check which ip's to listen (all or assigned) if (m_IPAddress.ToLower().IndexOf("all") > -1) { Listener = new TcpListener(IPAddress.Any, m_port); } else { Listener = new TcpListener(IPAddress.Parse(m_IPAddress), m_port); } // Log.WriteEntry("dp2ZServer service start step 3"); // Start listening Listener.Start(); //-------- Main Server message loop --------------------------------// while (true) { // Check if maximum allowed thread count isn't exceeded if (this.m_nMaxThreads == -1 || m_SessionTable.Count <= this.m_nMaxThreads) { // Thread is sleeping, until a client connects TcpClient client = Listener.AcceptTcpClient(); string sessionID = client.GetHashCode().ToString(); //**** // _LogWriter logWriter = new _LogWriter(this.SessionLog); Session session = new Session(client, this, sessionID); Thread clientThread = new Thread(new ThreadStart(session.Processing)); // Add session to session list AddSession(sessionID, session); // Start proccessing clientThread.Start(); } else { Thread.Sleep(100); } } } catch (ThreadInterruptedException) { // string dummy = e.Message; // Neede for to remove compile warning Thread.CurrentThread.Abort(); } catch (Exception x) { // 一个封锁操作被对 WSACancelBlockingCall 的调用中断 if (x.Message != "A blocking operation was interrupted by a call to WSACancelBlockingCall") { Log.WriteEntry("dp2ZServer Exception Name: " + x.GetType().Name + ", Listener::Run() error : " + x.Message, EventLogEntryType.Error); } } finally { eventClose.Set(); } }