//private object m_LockCommunication = new int(); private void _listener(object state) { int buflen = 0; byte[] buf = new byte[8192]; //C5.LinkedList<byte> totalBuf = new C5.LinkedList<byte>(); DefLib.Util.ArrayBuilder <byte> totalBuf = new DefLib.Util.ArrayBuilder <byte>(); byte[] rest = null; try { //NetworkStream stream = m_Client.GetStream(); m_ListenerClearEvent.Reset(); while (Interlocked.Read(ref m_Run) == 1) { if (Interlocked.Read(ref m_Run) == 0) { break; } try { totalBuf.Clear(); /// 读取Socket缓冲区 /// while (true) { lock (m_Client) { if (!m_Client.Poll(100, SelectMode.SelectRead)) { break; } } if (Interlocked.Read(ref m_Run) == 0) { totalBuf.Clear(); break; } buflen = 0; lock (m_Client) { if (m_Client.Connected) { buflen = m_Client.Receive(buf, buf.Length, SocketFlags.None); } } if (buflen > 0) { totalBuf.Append(buf, buflen); //for (int i = 0; i < buflen; ++i) // totalBuf.Add(buf[i]); } else { throw new CommException("读到零字节数据."); } if (totalBuf.Length > 102400) { break; } } if (totalBuf.Length > 0) { if (rest != null) { totalBuf.Insert(0, rest, rest.Length); //totalBuf.InsertAll(0, rest); rest = null; } if (m_Extractor == null) { totalBuf.Clear(); continue; } //SendLog(totalBuf.Count + " Bytes recved: " + DumpByte2Str(totalBuf.ToArray())); // 解析收到的数据 List <ICommunicationMessage> messages = m_Extractor.extractMessages(totalBuf.ToArray(), ref rest); // 派发给侦听者 if (messages.Count > 0) { List <ICommunicationMessage> queue = new List <ICommunicationMessage>(); foreach (ICommunicationMessage command in messages) { try { command.SetValue("ClientID", m_ClientID); if (command.TK_CommandType == Constants.TK_CommandType.KEEPALIVE) { command.TK_CommandType = Constants.TK_CommandType.RESPONSE; command.SetValue("RESPONSE_TO", command.SeqID); command.SetValue("RESULT", "OK"); command.SeqID = CommandProcessor.AllocateID(); enqueueMessage(command); continue; } queue.Add(command); } // end try catch { } } // end for if (queue.Count > 0) { m_ParentCommer.FilterResponse(queue); } if (queue.Count > 0) { CommandProcessor.instance().DispatchCommands(queue); } } // end if messages.Count } } catch (CommException commex) { throw new CommException(commex.ToString()); } catch (Exception ex) { throw new Exception("通讯异常:\n" + ex.ToString()); } System.Threading.Thread.Sleep(100); } } catch (Exception ex) { try { //SendLog(m_ClientInfo.ToString() + "侦听线程异常退出:\n" + ex.ToString()); m_ListenerClearEvent.Set(); } catch { } try { InvokeConnectionBroken(this, ex.Message); return; } catch { } } finally { try { m_ListenerClearEvent.Set(); } catch { } } }
private void _recvCallback(IAsyncResult ar) { SocketError err_code; try { int buflen = m_Client.EndReceive(ar, out err_code); m_RecvDone.Set(); if (err_code != SocketError.Success) { InvokeConnectionBroken(this, new SocketException((int)err_code).Message); return; } if (buflen == 0) { InvokeConnectionBroken(this, "收到零字节数据."); return; } totalBuf.Clear(); totalBuf.Append(buf, buflen); if (rest != null) { totalBuf.Insert(0, rest, rest.Length); rest = null; } if (m_Extractor == null) { totalBuf.Clear(); return; } //SendLog(totalBuf.Count + " Bytes recved: " + DumpByte2Str(totalBuf.ToArray())); // 解析收到的数据 List <ICommunicationMessage> messages = m_Extractor.extractMessages(totalBuf.ToArray(), ref rest); // 派发给侦听者 if (messages.Count > 0) { ++m_RecvCount; //DefLib.Util.Logger.Instance().SendLog("AsyncComm", "Received[" + ClientID + "]: " + m_RecvCount); List <ICommunicationMessage> queue = new List <ICommunicationMessage>(); foreach (ICommunicationMessage command in messages) { try { command.SetValue("ClientID", m_ClientID); if (command.TK_CommandType == Constants.TK_CommandType.KEEPALIVE) { command.TK_CommandType = Constants.TK_CommandType.RESPONSE; command.SetValue("RESPONSE_TO", command.SeqID); command.SetValue("RESULT", "OK"); command.SeqID = CommandProcessor.AllocateID(); enqueueMessage(command); continue; } queue.Add(command); } // end try catch { } } // end for if (queue.Count > 0) { m_ParentCommer.FilterResponse(queue); } if (queue.Count > 0) { CommandProcessor.instance().DispatchCommands(queue); } } // end if messages.Count if (Interlocked.Read(ref m_Run) != 0) { _recvAsync(); } } catch (Exception ex) { m_RecvDone.Set(); InvokeConnectionBroken(this, ex.ToString()); } finally { } }
private CommandMsgV2 UserLogin(ICommunicationMessage message) { CommandMsgV2 responseMsg = new CommandMsgV2(); try { if (message.Contains("ClientID")) { if (message.Contains("用户名") && message.Contains("密码")) { string sUserName = message.GetValue("用户名").ToString().Trim(); string sPassword = message.GetValue("密码").ToString().Trim(); string sQuery = "select manage,company from Operator where valid=1 and login_name = '" + sUserName + "' and password = '******'"; DataSet ds; ds = SqlHelper.ExecuteDataset(m_ConnStr, CommandType.Text, sQuery); if (ds.Tables[0].Rows.Count == 1) { //用户名、密码正确 object[] objs = ds.Tables[0].Rows[0].ItemArray; string sRight = objs[0].ToString(); string sCompany = ds.Tables[0].Rows[0]["company"].ToString(); //查询用户可管理的业务类型 sQuery = "select businesstype from Operator_BusinessType where login_name = '" + sUserName + "'"; ds.Tables.Clear(); ds = SqlHelper.ExecuteDataset(m_ConnStr, CommandType.Text, sQuery); string sFilter = ""; foreach (DataRow dr in ds.Tables[0].Rows) { object[] temps = dr.ItemArray; sFilter += temps[0].ToString().Trim() + ","; } #region 先发命令给CM,通知客户端登陆成功 CommandMsgV2 MsgLogOK = new CommandMsgV2(); MsgLogOK.TK_CommandType = Constants.TK_CommandType.RESPONSE; MsgLogOK.SeqID = CommandProcessor.AllocateID(); MsgLogOK.SetValue("ClientID", message.GetValue("ClientID")); MsgLogOK.SetValue("RESPONSE_TO", message.SeqID); MsgLogOK.SetValue("RESULT", "OK"); MsgLogOK.SetValue("BUSINESSTYPE", sFilter.Trim()); MsgLogOK.SetValue("RIGHT", sRight.Trim()); MsgLogOK.SetValue("COMPANY", sCompany.Trim()); CommandProcessor.instance().DispatchCommand(MsgLogOK); #endregion //发命令给AM,注册客户端 responseMsg.SeqID = message.SeqID; responseMsg.TK_CommandType = Constants.TK_CommandType.REGISTERCLIENT; responseMsg.SetValue("ClientID", message.GetValue("ClientID")); responseMsg.SetValue(Constants.MSG_PARANAME_AUTHORIZED, true); responseMsg.SetValue("SERVERNAME", Constants.ALARM_SERVERNAME); responseMsg.SetValue("Filter", sFilter.Trim()); responseMsg.SetValue("RIGHT", sRight.Trim()); responseMsg.SetValue("COMPANY", sCompany.Trim()); sQuery = "update Operator set lastlogintime = '" + DateTime.Now.ToString() + "' where login_name = '" + sUserName + "'"; SqlHelper.ExecuteNonQuery(m_ConnStr, CommandType.Text, sQuery); Logger.Instance().SendLog("UM", "用户:" + sUserName + " 已登录到系统."); } else { //登录失败 responseMsg.TK_CommandType = Constants.TK_CommandType.RESPONSE; responseMsg.SetValue("ClientID", message.GetValue("ClientID")); responseMsg.SetValue("RESPONSE_TO", message.SeqID); responseMsg.SetValue("RESULT", "NOK"); Logger.Instance().SendLog("UM", "用户:" + sUserName + " 登录失败."); } } else { //登录失败 responseMsg.TK_CommandType = Constants.TK_CommandType.RESPONSE; responseMsg.SetValue("ClientID", message.GetValue("ClientID")); responseMsg.SetValue("RESPONSE_TO", message.SeqID); responseMsg.SetValue("RESULT", "NOK"); Logger.Instance().SendLog("UM", "无效登录包"); } } else { return(null); } } catch (Exception ex) { try { //登录失败 responseMsg.TK_CommandType = Constants.TK_CommandType.RESPONSE; responseMsg.SetValue("ClientID", message.GetValue("ClientID")); responseMsg.SetValue("RESPONSE_TO", message.SeqID); responseMsg.SetValue("RESULT", "NOK"); Logger.Instance().SendLog("UM", "登录时出现异常:" + ex.ToString()); } catch { } } finally { } return(responseMsg); }
public void handleCommand(ICommunicationMessage message) { CommandMsgV2 resp = new CommandMsgV2(); resp.TK_CommandType = Constants.TK_CommandType.RESPONSE; resp.SeqID = CommandProcessor.AllocateID(); resp.SetValue(Constants.MSG_PARANAME_RESPONSE_TO, message.SeqID); try { if (message.Contains("ClientID")) { resp.SetValue("ClientID", message.GetValue("ClientID")); } else { throw new Exception("No ClientID in incoming package."); } switch (message.TK_CommandType) { case Constants.TK_CommandType.SERVER_GETRUNTIMEINFO: { Process p = Process.GetCurrentProcess(); resp.SetValue("PROCESSID", p.Id.ToString()); resp.SetValue("THREADCOUNT", p.Threads.Count.ToString()); resp.SetValue("MAX_THREADCOUNT", 30 + (AlarmManager.instance().MaxAdapters + AlarmManager.instance().MaxClients + AlarmManager.instance().MaxControllers) * 2); resp.SetValue("PHYMEMORY", p.WorkingSet64.ToString()); /// SystemInfo使用了性能计数器,有可能构造不出来 /// resp.SetValue("AVAIL_PHYMEMORY", 0); resp.SetValue("MAX_PHYMEMORY", 0); resp.SetValue("CPUUSAGE", 0); try { resp.SetValue("AVAIL_PHYMEMORY", SystemInfo.Instance.MemoryAvailable); resp.SetValue("MAX_PHYMEMORY", SystemInfo.Instance.PhysicalMemory); resp.SetValue("CPUUSAGE", (int)SystemInfo.Instance.CpuLoad); } catch { } resp.SetValue("STATUS", AlarmManager.instance().GetStatus().ToString()); resp.SetValue("STARTTIME", p.StartTime.ToString()); resp.SetValue("CPUTIME", ((long)p.TotalProcessorTime.TotalMinutes).ToString()); resp.SetValue("ALARMCLIENTS", AlarmManager.instance().GetAlarmClientsNum().ToString()); resp.SetValue("MAX_ALARMCLIENTS", AlarmManager.instance().MaxClients); resp.SetValue("ADAPTERCLIENTS", AlarmManager.instance().GetAdapterClientsNum().ToString()); resp.SetValue("MAX_ADAPTERCLIENTS", AlarmManager.instance().MaxAdapters); resp.SetValue("ACTIVEALARMNUM", AlarmManager.instance().GetActiveAlarmsNum().ToString()); resp.SetValue(Constants.MSG_PARANAME_RESULT, "OK"); } break; case Constants.TK_CommandType.SERVER_GETCURLOG: { StringBuilder sb = new StringBuilder(); foreach (string s in GetCurrentLog()) { sb.Append(s); } resp.SetValue("CURLOG", sb.ToString()); resp.SetValue(Constants.MSG_PARANAME_RESULT, "OK"); } break; case Constants.TK_CommandType.SERVER_GETLOGFILES: { StringBuilder sb = new StringBuilder(); foreach (string s in GetLogFiles()) { sb.Append(s); sb.Append(","); } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); } resp.SetValue("LOGFILES", sb.ToString()); resp.SetValue(Constants.MSG_PARANAME_RESULT, "OK"); } break; case Constants.TK_CommandType.MON_GETTERMINALSINFO: { C5.HashDictionary <long, AdapterInfo> ads = new C5.HashDictionary <long, AdapterInfo>(); AlarmManager.instance().GetAdaptersInfo(ads); resp.SetValue(Constants.MSG_PARANAME_TERMINALS_INFO, ads); } break; default: break; } CommandProcessor.instance().DispatchCommand(resp); } catch (Exception ex) { Main_LogReceived("", ex.ToString()); } }