using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using Google.Protobuf.WellKnownTypes; namespace SMCenter { public class MsgHandler { public void HandlerMsg(ClientInfo client, RequestPacket packet) { SMProcAppMgr.Instance.m_currHandleClient = client;//先这样处理 switch (packet.MsgID) { case (int)SMMsgID.Ping: OnPing(client); break; case (int)SMMsgID.ClientRegReq: OnClientReg(client, packet); break; //命令处理 case (int)SMMsgID.ConsoleCommandReq: OnConsoleCommand(client, packet); break; case (int)SMMsgID.AgentDoCommandRes: OnAgentDoCommandRes(client, packet); break; // 文件传送在center和agent之间是双向, 可以center传给agent, 也可以agent传给center case (int)SMMsgID.TransFileNotify: OnTransFileNotify(client, packet); break; case (int)SMMsgID.FileContentReq: OnFileContentReq(client, packet); break; case (int)SMMsgID.FileContentRes: OnFileContentRes(client, packet); break; // 文件传送结果由recvNode主动通知transNode case (int)SMMsgID.FileRecvStateReq: OnFileRecvStateReq(client, packet); break; case (int)SMMsgID.FileRecvStateRes: OnFileRecvStateRes(client, packet); break; default: TraceLog.Error("MsgHandler.HandlerMsg unknow msgId {0}", packet.MsgID); break; } } private void OnPing(ClientInfo client) { TraceLog.Trace("MsgHandler.OnPing session {0} name {1}", client.SessionID, client.HostName); client.LastPingTime = SMCenterUtils.GetTimeSecond(); } private void OnClientReg(ClientInfo client, RequestPacket packet) { TraceLog.Trace("MsgHandler.OnClientReg session {0}", client.SessionID); SMClientRegReq req = (SMClientRegReq)packet.Message; client.HostName = req.MachineName; if (req.ClientType == 1) { client.ClientType = EClientType.Agent; } else if (req.ClientType == 2) { client.ClientType = EClientType.Console; } foreach (ClientInfo item in SMCenterNet.Instance.m_clientsDict.Values) { // 删除老的链接信息 if (item.HostName == client.HostName && item.ClientType == client.ClientType && item.SessionID != client.SessionID) { SMCenterNet.Instance.AddToNeedCloseClient(item.SessionID); } } SendClientRegRes(client); TraceLog.Trace("MsgHandler.OnClientReg session {0} type {1} name {2}" , client.SessionID, client.ClientType, client.HostName); } private void SendClientRegRes(ClientInfo client) { if (client.ClientType == EClientType.Agent) { SMClientRegRes regRes = new SMClientRegRes(); var appDict = SMProcAppMgr.Instance.GetAllProc(); foreach (var app in appDict) { if (app.Value.HostName == client.HostName) { regRes.ServerId.Add(app.Key); if (string.IsNullOrEmpty(regRes.Workpath)) { regRes.Workpath = app.Value.WorkPath; } } } client.SendMsg(regRes, SMMsgID.ClientRegRes); } } private void OnConsoleCommand(ClientInfo client, RequestPacket packet) { SMConsoleCommandReq req = (SMConsoleCommandReq)packet.Message; TraceLog.Trace("MsgHandler.OnConsoleCommand session {0} command {1}", client.SessionID, req.Command); ConsoleCmd.Instance.ProcessCmd(client.SessionID, req.Command, req.Httpid); } private void OnAgentDoCommandRes(ClientInfo client, RequestPacket packet) { SMAgentDoCommandRes res = (SMAgentDoCommandRes)packet.Message; TraceLog.Trace("MsgHandler.OnAgentDoCommandRes session {0} command {1}", client.SessionID, res.Command); ConsoleCmd.Instance.OnAgentDoCommandRes(client, res); } private void OnTransFileNotify(ClientInfo client, RequestPacket packet) { var notify = (SMTransFileNotify)packet.Message; TraceLog.Trace("MsgHandler.OnTransFileNotify session {0}", client.SessionID); CenterFilePullMng.Instance.OnTransFileNotify(notify); } private void OnFileContentRes(ClientInfo client,RequestPacket packet) { var res = (SMFileContentRes)packet.Message; TraceLog.Trace("MsgHandler.OnFileContentRes session {0}", client.SessionID); CenterFilePullMng.Instance.OnFileContentRes(res); } private void OnFileContentReq(ClientInfo client, RequestPacket packet) { var req = (SMFileContentReq)packet.Message; TraceLog.Trace("MsgHandler.OnFileContentReq session {0} file {1}", client.SessionID, req.FileName); CenterFileTransMng.Instance.OnFileContentReq(client, req); } private void OnFileRecvStateReq(ClientInfo client, RequestPacket packet) { var req = (SMFileRecvStateReq)packet.Message; TraceLog.Trace("MsgHandler.OnFileRecvStateReq session {0} host {1}", client.SessionID, req.HostName); CenterFileTransMng.Instance.OnFileRecvStateReq(client, req); } private void OnFileRecvStateRes(ClientInfo client, RequestPacket packet) { var res = (SMFileRecvStateRes)packet.Message; TraceLog.Trace("MsgHandler.OnFileRecvStateRes session {0} host {1}", client.SessionID, res.HostName); CenterFilePullMng.Instance.OnFileRecvStateRes(client, res); } } }