public static CommManager instance() { lock (m_singltonlock) { if (m_instance == null) { m_instance = new CommManager(); } } return(m_instance); }
void _VerifyTerminalsStatus(ICommunicationMessage message) { C5.HashDictionary <long, AdapterInfo> ads = new C5.HashDictionary <long, AdapterInfo>(); AlarmManager.instance().GetAdaptersInfo(ads); foreach (C5.KeyValuePair <long, AdapterInfo> info in ads) { try { System.Net.IPEndPoint end = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(info.Value.Address), info.Value.ControllerPort); if (end.Port == 0) { continue; } else { ICommClient comm = CommManager.instance().CreateCommClient <CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>("控制器", end.Address.ToString(), end.Port, 30, false, false); if (!comm.Start()) { // remove adapter AlarmManager.instance().RemoveAdapterInfo(info.Key); } else { comm.Close(); } } } catch (Exception ex) { DefLib.Util.Logger.Instance().SendLog(ex.ToString()); } } CommandMsgV2 msg = new CommandMsgV2(); msg.TK_CommandType = Constants.TK_CommandType.MON_GETTERMINALSINFO; msg.SeqID = message.SeqID; msg.SetValue("ClientID", message.GetValue("ClientID")); CommandProcessor.instance().DispatchCommand(msg); }
public bool Start() { lock (this) { if (Interlocked.Exchange(ref m_Started, 1) == 1) { return(true); } if (!connectServer()) { Interlocked.Exchange(ref m_Started, 0); return(false); } if (m_Comm == null) { m_ClientID = CommManager.AllocateClientID(); m_Comm = new AsyncCommunicator(this, m_ClientID, m_Client, null, m_Extractor, m_Encoder, m_bCompress); m_Comm.onLog += new LogHandler(m_Comm_onLog); m_Comm.onConnectionBroken += new ConnectionBrokenHandler(m_Comm_onConnectionBroken); } m_Comm.startWork(); if (m_bKeepAlive) { m_NotAliveCounter = 0; this.m_TimerKeepAlive.Start(); } //CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.RESPONSE, this, new Dictionary<Constants.TK_CommandType, byte>()); } return(true); }
protected void Init() { try { //获取应用程序运行路径 string path = AppDomain.CurrentDomain.BaseDirectory; DataSet DBds = new DataSet(); //读入数据库连接参数 DBds = MD5Encrypt.DES.instance().DecryptXML2DS(path + "conf.xml", 1); m_SysPara.Clear(); foreach (DataRow r in DBds.Tables["Parameters"].Rows) { m_SysPara.Add(r["name"].ToString(), r["value"].ToString()); } m_CommClient = CommManager.instance().CreateCommClient <CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>( m_SysPara["Adapter Name"].ToString(), m_SysPara["Server IP"].ToString(), Convert.ToInt32(m_SysPara["Server Port"]), Convert.ToInt32(m_SysPara["Comm Timeout"]), true, false); m_CommClient.onLog += new TK_AlarmManagement.LogHandler(LogReceiver); #region Declear Adapter m_Adapter = new T(); m_Adapter.ControllerPort = Convert.ToInt32(m_SysPara["Controller Port"]); m_Adapter.Name = m_SysPara["Adapter Name"].ToString(); m_Adapter.Interval = Convert.ToInt32(m_SysPara["Retrieve Interval"]); m_Adapter.SvrID = Convert.ToInt32(m_SysPara["SvrID"]); m_Adapter.EncodingStr = m_SysPara["Encoding"]; m_Adapter.Init(m_CommClient); #endregion DefLib.Util.Logger.Instance().SubscibeLog("", new DefLib.Util.Logger.LogFunction(LogReceiver)); //m_Adapter.LogReceived += new TK_AlarmManagement.LogHandler(LogReceiver); m_Adapter.StateChanged += new StateChangeHandler(m_Adapter_StateChanged); m_Name = m_Adapter.Name; // 配置监控终端服务器 List <Constants.TK_CommandType> acceptedCommands = new List <Constants.TK_CommandType>(); acceptedCommands.Add(Constants.TK_CommandType.RESPONSE); List <Constants.TK_CommandType> superCommands = new List <Constants.TK_CommandType>(); superCommands.Add(Constants.TK_CommandType.RESPONSE); m_ControllerServer = CommManager.instance().CreateCommServer <DefaultInterpreter, CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>("监控服务器", acceptedCommands, superCommands, Convert.ToInt32(m_SysPara["Controller Port"]), Convert.ToInt32(m_SysPara["MaxController"]), 30, true, false); m_ControllerServer.onLog += new LogHandler(LogReceiver); acceptedCommands.Clear(); superCommands.Clear(); Dictionary <Constants.TK_CommandType, byte> empty = new Dictionary <Constants.TK_CommandType, byte>(); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_START, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_STOP, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETRUNTIMEINFO, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETOMCLIST, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETCURLOG, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETLOGFILES, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_SHUTDOWN, this, empty); } catch (Exception ex) { Logger.Instance().SendLog("AdapterController", ex.ToString()); throw ex; } }
private void _accepter() { try { m_AccepterStopEvent.Reset(); while (Interlocked.Read(ref m_Run) == 1) { Socket newclient = m_Listener.Accept();//.AcceptTcpClient(); lock (m_ClientCommunicators) { if (m_ClientCommunicators.Count == m_MaxClients) { byte[] buf = System.Text.Encoding.Default.GetBytes("Client connection exceeds. New request is closing."); try { newclient.Send(buf); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } catch (Exception ex) { SendLog(ex.ToString()); } continue; } else { try { //IPEndPoint ep = newclient.Client.RemoteEndPoint as IPEndPoint; //byte[] addr = ep.Address.GetAddressBytes(); //long id = addr[3] * 0x10000000000L + // addr[2] * 0x100000000L + // addr[1] * 0x1000000L + // addr[0] * 0x10000L + ep.Port; long id = CommManager.AllocateClientID(); ICommunicator comm = new AsyncCommunicator(this, id, newclient, new INTERPRETER(), new EXTRACTOR(), new ENCODER(), m_bCompress); lock (m_ClientCommunicators) m_ClientCommunicators[id] = comm; lock (m_ClientActiveCounter) m_ClientActiveCounter[id] = 0; //newclient.NoDelay = true; //newclient.LingerState = new LingerOption(false, 0); //newclient.SendBufferSize = 2048; //newclient.SendTimeout = 1000; comm.onLog += new LogHandler(comm_onLog); comm.onConnectionBroken += new ConnectionBrokenHandler(_HandleConnectionBroken); comm.startWork(); // 发送报文注册客户端 MSGTYPE msg = new MSGTYPE(); msg.SeqID = CommandProcessor.AllocateID(); msg.TK_CommandType = Constants.TK_CommandType.REGISTERCLIENT; msg.SetValue("ClientID", id); msg.SetValue("SERVERNAME", Name); msg.SetValue(Constants.MSG_PARANAME_AUTHORIZED, false); CommandProcessor.instance().DispatchCommand(msg); SendLog("客户端 " + newclient.RemoteEndPoint.ToString() + " 连接到:" + Name); SendLog(Name + "当前客户端的数量:" + m_ClientCommunicators.Count); //int wt, iot; //ThreadPool.GetAvailableThreads(out wt, out iot); //SendLog("可用工作线程数: " + wt + " I/O线程数: " + iot); } catch (Exception ex) { try { //NetworkStream ns = newclient.GetStream(); //if (ns != null) // ns.Close(); newclient.Close(); SendLog(ex.ToString()); } catch { } } } } // endlock } } catch (Exception ex) { SendLog(ex.ToString()); } finally { m_AccepterStopEvent.Set(); } }
private void _accepterCallback(IAsyncResult ar) { Socket newclient = m_Listener.EndAccept(ar); lock (m_ClientCommunicators) { if (m_ClientCommunicators.Count == m_MaxClients) { byte[] buf = System.Text.Encoding.Default.GetBytes("Client connection exceeds. New request is closing."); try { newclient.Send(buf); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } catch (Exception ex) { SendLog(ex.ToString()); } } else { try { long id = CommManager.AllocateClientID(); ICommunicator comm = new AsyncCommunicator(this, id, newclient, new INTERPRETER(), new EXTRACTOR(), new ENCODER(), m_bCompress); lock (m_ClientCommunicators) m_ClientCommunicators[id] = comm; lock (m_ClientActiveCounter) m_ClientActiveCounter[id] = 0; comm.onLog += new LogHandler(comm_onLog); comm.onConnectionBroken += new ConnectionBrokenHandler(_HandleConnectionBroken); comm.startWork(); // 发送报文注册客户端 MSGTYPE msg = new MSGTYPE(); msg.SeqID = CommandProcessor.AllocateID(); msg.TK_CommandType = Constants.TK_CommandType.REGISTERCLIENT; msg.SetValue("ClientID", id); msg.SetValue("SERVERNAME", Name); msg.SetValue(Constants.MSG_PARANAME_AUTHORIZED, false); CommandProcessor.instance().DispatchCommand(msg); SendLog("客户端 " + newclient.RemoteEndPoint.ToString() + " 连接到:" + Name); SendLog(Name + "当前客户端的数量:" + m_ClientCommunicators.Count); //int wt, iot; //ThreadPool.GetAvailableThreads(out wt, out iot); //SendLog("可用工作线程数: " + wt + " I/O线程数: " + iot); } catch (Exception ex) { try { newclient.Close(); SendLog(ex.ToString()); } catch { } } } } // endlock if (Interlocked.Read(ref m_Run) == 1) { _asyncAccepter(); } }
CommandMsgV2 _StartAdapter(ICommunicationMessage message) { try { CommandMsgV2 resp = new CommandMsgV2(); resp.TK_CommandType = Constants.TK_CommandType.RESPONSE; resp.SeqID = CommandProcessor.AllocateID(); resp.SetValue("ClientID", message.GetValue("ClientID")); resp.SetValue(Constants.MSG_PARANAME_RESPONSE_TO, message.SeqID); long adapterid = Convert.ToInt64(message.GetValue(Constants.MSG_PARANAME_ADAPTER_ID)); C5.HashDictionary <long, AdapterInfo> ads = new C5.HashDictionary <long, AdapterInfo>(); AlarmManager.instance().GetAdaptersInfo(ads); if (!ads.Contains(adapterid)) { resp.SetValue(Constants.MSG_PARANAME_RESULT, "NOK"); resp.SetValue(Constants.MSG_PARANAME_REASON, "采集器不存在."); return(resp); } try { CommandMsgV2 cmd = new CommandMsgV2(); cmd.SeqID = CommandProcessor.AllocateID(); cmd.TK_CommandType = Constants.TK_CommandType.ADAPTER_START; cmd.SetValue("ClientID", adapterid); cmd.SetValue(Constants.MSG_PARANAME_ADAPTER_NAME, ads[adapterid].Name); System.Net.IPEndPoint end = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ads[adapterid].Address), ads[adapterid].ControllerPort); if (end.Port == 0) { resp.SetValue(Constants.MSG_PARANAME_RESULT, "NOK"); resp.SetValue(Constants.MSG_PARANAME_REASON, "不可远程控制的采集器"); } else { ICommClient comm = CommManager.instance().CreateCommClient <CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>("控制器", end.Address.ToString(), end.Port, 30, false, false); comm.Start(); ICommunicationMessage r2 = comm.SendCommand(cmd); resp.SetValue(Constants.MSG_PARANAME_RESULT, r2.GetValue(Constants.MSG_PARANAME_RESULT)); comm.Close(); } } catch (Exception ex) { resp.SetValue(Constants.MSG_PARANAME_RESULT, "NOK"); resp.SetValue(Constants.MSG_PARANAME_REASON, ex.Message); } return(resp); } catch (Exception ex) { Logger.Instance().SendLog(ex.ToString()); return(null); } }