private void ConsoleListenerThread() { try { CSL = new TcpListener(new System.Net.IPEndPoint(0, consolePort)); CSL.Start(); while (true) { CWC = new TCP.WorkerClient(CSL.AcceptTcpClient()); CWC.InitOps(); CWC.Disconnected += CWCDisconnected; CWC.MessageArrived += CWCMessageArrived; Logger.MajorInfo("Console client from " + CWC.ClientIP() + " is connected"); consoleCurCons = 1; while (consoleCurCons == 1) { System.Threading.Thread.Sleep(50); } } } catch (Exception ex) { Logger.MajorInfo("Exception on console listening thread (" + ex.Message + ")"); if (CSL != null) { CSL.Stop(); CSL = null; } } }
private void CWCDisconnected(TCP.WorkerClient sender) { Logger.MajorInfo("Console client disconnected."); sender.TermClient(); consoleCurCons -= 1; curMsg = null; }
private void ListenerThread() { WC = null; try { SL = new TcpListener(new System.Net.IPEndPoint(0, port)); SL.Start(); while (true) { TCP.WorkerClient wClient = new TCP.WorkerClient(SL.AcceptTcpClient()); wClient.InitOps(); wClient.Disconnected += WCDisconnected; wClient.MessageArrived += WCMessageArrived; Logger.MajorInfo("Client from " + wClient.ClientIP() + " is connected"); curCons += 1; bool slotedIt = false; for (int i = 0; i < WC.GetUpperBound(0); i++) { if ((WC[i] == null) || (WC[i].IsConnected == false)) { WC[i] = wClient; slotedIt = true; } } if (slotedIt == false) { Array.Resize(ref WC, WC.GetUpperBound(0)); WC[WC.GetUpperBound(0) - 1] = wClient; } while (curCons >= maxCons) { Thread.Sleep(50); } } } catch (Exception ex) { Logger.MajorInfo("Exception on listening thread (" + ex.Message + ")"); if (SL != null) { SL.Stop(); SL = null; } } }
private void WCMessageArrived(TCP.WorkerClient sender, byte[] b, int len) { TCPEventArgs e = new TCPEventArgs(); e.RemoteClient = sender.ClientIP(); e.Data = new byte[len]; DataArrived(this, e); Array.Copy(b, 0, e.Data, 0, len); Message.Message msg = new Message.Message(b); Logger.MajorVerbose("Client: " + sender.ClientIP() + System.Environment.NewLine + "Request: " + msg.MessageData); try { Logger.MajorDebug("Parsing header and code of message " + msg.MessageData + "..."); string messageHeader = msg.GetSubstring(HeaderLength); msg.AdvanceIndex(HeaderLength); string commandCode = msg.GetSubstring(2); msg.AdvanceIndex(2); CommandCalled(this, commandCode); CommandClass CC = CE.GetLoadedCommand(commandCode); if (CC == null) { Logger.MajorError("No implementor for " + commandCode + "." + System.Environment.NewLine + "Disconnecting client."); sender.TermClient(); } else { Logger.MajorDebug("Found implementor " + CC.DeclaringType.FullName + ", instantiating..."); AHostCommand o = (AHostCommand)Activator.CreateInstance(CC.DeclaringType); o = (AHostCommand)Activator.CreateInstance(CC.DeclaringType); Message.MessageResponse retMsg; Message.MessageResponse retMsgAfterIO; try { string trailingChars = ""; if (ExpectTrailers) { trailingChars = msg.GetTrailers(); } if ((CheckLMKParity == false) || (Cryptography.LMK.LMKStorage.CheckLMKStorage())) { //Logger.MinorInfo("=== [" + commandCode + "], starts " + Utility.getTimeMMHHSSmmmm + " ======="); Logger.MajorDebug("Calling AcceptMessage()..."); o.AcceptMessage(msg); Logger.MinorVerbose(o.DumpFields()); } } catch (Exception ex) { } } } catch (Exception ex) { } }
public void ShutDown() { if (LT == null) { try { SL.Stop(); SL = null; } catch (Exception ex) { } Logger.MajorVerbose("Stopping the listening thread..."); try { LT.Abort(); LT = null; } catch (Exception ex) { } Logger.MajorVerbose("Disconnecting connected clients..."); if (WC != null) { for (int i = 0; i < WC.GetUpperBound(0) + 1; i++) { try { if (!(WC[i] == null) && (WC[i].IsConnected == true)) { WC[i].TermClient(); } WC[i] = null; } catch (Exception ex) { } } } try { CSL.Stop(); CSL = null; } catch (Exception ex) { } Logger.MajorVerbose("Stopping the console listening thread..."); try { CLT.Abort(); CLT = null; } catch (Exception ex) { } try { if (!(CWC == null) && (CWC.IsConnected == true)) { CWC.TermClient(); } CWC = null; } catch (Exception ex) { } } Logger.MajorInfo("Shutdown complete"); }