public void OnTCPConnect(IAsyncResult asyn) { if (closing) { exitontcpconnect = true; return; } Socket newConnection = null; // Accept incoming connection, create a worker object and add to arraylist of workers. try { waitingfortcp = false; newConnection = m_MainTCPSocket.EndAccept(asyn); newConnection.NoDelay = true; var newWorker = new WorkerTCPSocket(Configuration, newConnection, m_DBWriter, m_RawDataManager); m_WorkerTCPSocketList.Add(newWorker); newWorker.WaitForData(); } catch (ObjectDisposedException) { // Socket was unexpectedly closed, so just ignore } catch (Exception e) { try { Logger.GetInstance().Exception("Incoming TCP Connection error", e, string.Empty); if (newConnection != null) { newConnection.Close(); } } catch (Exception e2) { Logger.GetInstance().Exception("Incoming TCP Connection error, failed to close", e2, string.Empty); } } waitingfortcp = true; // Start listening for the next connection try { m_MainTCPSocket.BeginAccept(new AsyncCallback(OnTCPConnect), null); } catch (Exception e3) { Logger.GetInstance().Exception("Listen for new TCP connection error", e3, string.Empty); // this would be fatal as it would totally block the socket. // should we just force an exit here and let windows restart the service? // or should we destroy the mainSocket and start again? } }
protected override void OnStop() { //set class level closing flag used to refuse any new UDP/TCP connections closing = true; //wait for timers to run and to allow the workertcpsockets to process any outstanding requests var shorttimeout = 5000; Thread.Sleep(shorttimeout); //disable the timers Timer_Cleanup.Enabled = false; Timer_Parse.Enabled = false; //this.timer1.Enabled = false; //this.timer2.Enabled = false; //this.timer3.Enabled = false; //this.timer4.Enabled = false; //set worker tcp socket max age to 30 seconds TimeSpan maxAge = new TimeSpan(0, 0, 30); // 30 seconds long waiting = Environment.TickCount; try { //while loop to break when socket count reaches 0 or 30s timeout expires while (m_WorkerTCPSocketList.Count > 0 && Environment.TickCount - waiting <= 30000) { WorkerTCPSocket workerObj = null; foreach (WorkerTCPSocket worker in m_WorkerTCPSocketList) { if (worker.GetAge() > maxAge) { workerObj = worker; break; } } //check to see if socket is too old if (workerObj != null) { try { workerObj.CloseAndDestroy(); } catch (Exception excep) { Logger.GetInstance().Exception("Exception on TCP Worker disposal: ", excep, string.Empty); } Thread.Sleep(100); } else { Thread.Sleep(1000); } } } catch (Exception excep) { Logger.GetInstance().Exception("Exception on age check foreach: ", excep, string.Empty); } //close main Socket Handler if (m_MainTCPSocket != null) { //if waiting for tcp socket to finish or pending connection if (!exitontcpconnect && !waitingfortcp) { Thread.Sleep(shorttimeout); } m_MainTCPSocket.Close(); } //purge and close the raw data manager if (m_RawDataManager != null) { //force all pending parses to be parsed m_RawDataManager.moveRawData(); m_RawDataManager.parseAllReadyData(m_DBWriter); m_RawDataManager.Close(); } //close the db writer m_DBWriter?.Close(); }
/// <summary> /// Removes a worker object from the array list /// </summary> public static void RemoveWorkerTCPSocket(WorkerTCPSocket worker) { m_WorkerTCPSocketList.Remove(worker); }