示例#1
0
        public void SendShutdownOK(MsgRequestShutdownPlayer request_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS status_)
        {
            ShutdownRequestResponse response = new ShutdownRequestResponse(0, status_)
            {
                RequestID = request_.Head.ID,
            };

            var observer = new IPEndPoint(IPAddress.Parse(request_.ControllerIP), request_.ControllerPort);
            int n = response.Serialize(_outData);
            //Debug.Log(string.Format("SendLaunchOK: {0} to {1}", response, observer));
            SendTo(_outSocket, _outData, n, SocketFlags.None, observer);
        }
示例#2
0
        public void Update()
        {
            int n = 0;
            //LogUtil.WriteLog("CmdReceiver Update Enter");
            while ((n = _inSocket.Available) > 0)
            {
                LogUtil.WriteLog("CmdReceiver Update GetData");
                MessageHead head = null;
                n = ReceiveFrom(_inSocket, _inData, ref _inRemoteEp);

                head = Message.ParseHead(_inData, _inData.Length);
                if (head == null)
                    break;

                var remoteIP = (_inRemoteEp as IPEndPoint).Address;
                LogUtil.WriteLog(string.Format("remoteIP:{0}",remoteIP.ToString()));
                if (FilterIP != "" && FilterIP != remoteIP.ToString())
                {
                    continue;
                }

                switch (head.Type)
                {
                    case MessageType.REQUEST_LaunchPlayerFromService:
                        {
                            MsgRequestLaunchPlayerFromService launch = new MsgRequestLaunchPlayerFromService(0, 0);
                            launch.Deserialize(_inData, n);
                            launch.ControllerIP = remoteIP.ToString();
                            LogUtil.WriteLog(string.Format("Receive msg {0}",launch));
                            //启动应用程序
                            string errorMsg = string.Empty;

                            _exePath = launch.EXEPath;
                            string appDir = new DirectoryInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase).FullName;
                            string exePath = appDir + "\\" + exePathConfigFile;
                            LogUtil.WriteLog(exePath);

                            if (File.Exists(exePath))
                            {
                                try
                                {
                                    XmlDocument xmlDoc = new XmlDocument();
                                    xmlDoc.Load(exePath);
                                    XmlNode root = xmlDoc.SelectSingleNode("ExeConfig");
                                    XmlNode exePathNode = root.SelectSingleNode("ExePath");
                                    _exePath = exePathNode.InnerText;
                                    LogUtil.WriteLog("_exePath:" + _exePath);
                                }
                                catch (Exception ex)
                                {
                                    LogUtil.WriteLog("exception:"+ex.Message);
                                }
                            }
                            else
                            {
                                LogUtil.WriteLog(exePathConfigFile+"not exist");
                            }

                            //_exePath = "D:\\VideoPlayer.exe";
                            if (ProcessUtil.StartProgram(_exePath, "", ref errorMsg))
                            {
                                LogUtil.WriteLog(string.Format("StartProgram:Success"));

                                //发送反馈
                                LogUtil.WriteLog(string.Format("Send OK Ack Ip:{0} , Port:{1}",remoteIP.ToString(),launch.ControllerPort));
                                SendLaunchOK(launch, LaunchRequestResponse.ENUM_LAUNCH_STATUS.OK);
                                
                            }
                            else
                            {
                                LogUtil.WriteLog(string.Format("StartProgram:{0} Error", errorMsg));
                                LogUtil.WriteLog(string.Format("Send NotFound Ack Ip:{0} , Port:{1}", remoteIP.ToString(), launch.ControllerPort));
                                SendLaunchOK(launch, LaunchRequestResponse.ENUM_LAUNCH_STATUS.NotFound);
                            }
                            break;
                        }
                    case MessageType.REQUEST_ShutdownPlayer:
                        {
                            MsgRequestShutdownPlayer shutdown = new MsgRequestShutdownPlayer(0, 0);
                            shutdown.Deserialize(_inData, n);
                            shutdown.ControllerIP = remoteIP.ToString();
                            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Receive msg REQUEST_ShutdownPlayer"));
                            LogUtil.WriteLog(string.Format("Receive msg {0}",shutdown));

                            _handleRequest.HandleShutdown(shutdown);

                            //string fileName = Path.GetFileName(_exePath);
                            //string processName = fileName.Substring(0, fileName.LastIndexOf('.'));
                            //LogUtil.WriteLog(string.Format("FileName : {0}, ProcessName : {1}",fileName,processName));
                            ////关闭应用程序
                            //string errorMsg = string.Empty;
                            //if (ProcessUtil.CloseProgram(_exePath,ref errorMsg))
                            //{
                            //    LogUtil.WriteLog(string.Format("CloseProgram:Success"));

                            //    LogUtil.WriteLog(string.Format("Send Shutdown Ack to Ip:{0} , Port:{1}",remoteIP.ToString(),shutdown.ControllerPort));
                            //    SendShutdownOK(shutdown, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);

                            //}
                            //else
                            //{
                            //    LogUtil.WriteLog(string.Format("CloseProgram:{0} Error", errorMsg));
                            //}
                           

                            break;
                        }
                    case MessageType.REQUEST_PingService:
                        {
                            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Receive msg REQUEST_PingService"));
                            LogUtil.WriteLog(string.Format("Receive msg REQUEST_PingService"));
                            MsgRequestPingService ping = new MsgRequestPingService(0,0);
                            ping.Deserialize(_inData, n);
                            //反馈
                            LogUtil.WriteLog(string.Format("Ping Ack Ip: {0}, Port:{1}",remoteIP.ToString(),ping.ControllerPort));
                            SendPingOK(ping,remoteIP);
                            break;
                        }
                    case MessageType.REQUEST_BY_NAME:
                        {
                            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Receive msg REQUEST_BY_NAME"));
                            //按照名称启动应用
                            //启动成功后向控制端发送alive
                            var msg = new RequestByName();
                            msg.Deserialize(_inData, _inData.Length);
                            switch (msg.Name)
                            {
                                case "RunBatchRequest":
                                    {
                                        //var batch = new RunBatchRequest();
                                        //batch.Deserialize(_inData, _inData.Length);
                                        //batch.ControllerIP = remoteIP.ToString();
                                        //OnMessage(batch);
                                    }
                                    break;
                                case "LaunchAppRequest":
                                    {
                                        var launchApp = new LaunchAppRequest();
                                        launchApp.Deserialize(_inData, _inData.Length);
                                        launchApp.ControllerIP = remoteIP.ToString();
                                        //OnMessage(launchApp);
                                        _handleRequest.HandleLaunch(launchApp);
                                        
                                    }
                                    break;
                                default:
                                    //Debug.LogWarning("Failed to handle Request: " + msg);
                                    LogUtil.WriteLog("Failed to handle Request: " + msg);
                                    break;
                            }
                            //break;
                        }
                        break;
                    //case MessageType.CMD_Register:
                    //    {
                            
                    //        MsgCmdRegister register = new MsgCmdRegister(((IPEndPoint)_inRemoteEp).Address.ToString(), 0);
                    //        register.Deserialize(_inData, _inData.Length);
                    //        LogUtil.WriteLog(string.Format("Receive msg{0}", register));
                    //        _nodeID = register.PlayerID;

                    //        MsgAckRegisterOK registerOK = new MsgAckRegisterOK(0, _nodeID);

                    //        _outPort = register.CmdPort;
                    //        _observer = new IPEndPoint(IPAddress.Parse(register.IP), _outPort);
                    //        _isRegister = true;
                    //        SendRegisterOK(registerOK);
                    //        break;
                    //    }
                    default:
                        {
                            LogUtil.WriteLog(string.Format("Error: msg<head:<id:{0}, type:{1}>>",head.ID,head.Type));
                            break;
                        }
                }
            }
        }
示例#3
0
        public void HandleShutdown(MsgRequestShutdownPlayer shutdown_)
        {
            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Shutdown Last Launched App..."));
            var info = "HandleShutdown:\n";

            if (shutdown_ != null)
            {
                LogUtil.WriteLog("shutdown_!=null");
                if (_lastProcess != null)
                {
                    LogUtil.WriteLog("_lastProcess != null");
                    _lastProcess.Refresh();
                    if (_lastProcess.Responding)
                    {
                        LogUtil.WriteLog("_lastProcess.Responding true");
                        try
                        {
                            LogUtil.WriteLog("pos1");
                            if (_lastProcess.MainWindowHandle != IntPtr.Zero)
                            {
                                LogUtil.WriteLog("_lastProcess.MainWindowHandle != IntPtr.Zero");
                                info += string.Format("CloseMainWindow: {0}\n", _lastProcess.MainWindowTitle);
                                _lastProcess.CloseMainWindow();
                            }
                            LogUtil.WriteLog("pos2");
                            info += string.Format("Close _lastProcess: {0}\n", _lastProcess.ProcessName);
                            //info += string.Format("Closed {0} {1} {2}\n",
                            //_lastProcess.ProcessName, _lastProcess.ExitCode, _lastProcess.ExitTime);
                            //_lastProcess.Close();
                            //新的关闭进程的方法
                            LogUtil.WriteLog("before close process");
                            _lastProcess.Kill();
                            _lastProcess.WaitForExit();
                            _lastProcess.Close();
                            LogUtil.WriteLog("after close process");

                            _lastProcess = null;
                            //Debug.Log(info);
                            LogUtil.WriteLog(info);
                            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Shutdown App Success"));
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                        catch (InvalidOperationException ex)
                        {
                            //Debug.Log(ex); // HasExited
                            LogUtil.WriteLog("InvalidOperationException1:" + ex.Message);
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                        catch (Exception ex)
                        {
                            //Debug.Log(ex);
                            LogUtil.WriteLog("Exception1:" + ex.Message);
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                    }
                    else
                    {
                        try
                        {
                            _lastProcess.Kill();
                            //_lastProcess.WaitForExit(5000);
                            _lastProcess.WaitForExit();
                            info += string.Format("Kill {0} {1} {2}\n", _lastProcess.ProcessName, _lastProcess.ExitCode, _lastProcess.ExitTime);
                            _lastProcess.Close();
                            _lastProcess = null;
                            //Debug.Log(info);
                            LogUtil.WriteLog(info);
                            ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Shutdown App Success"));
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                        catch (InvalidOperationException ex)
                        {
                            //Debug.Log(ex);
                            LogUtil.WriteLog("InvalidOperationException2:" + ex.Message);
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                        catch (Exception ex)
                        {
                            //Debug.Log(ex);
                            LogUtil.WriteLog("Exception2:" + ex.Message);
                            CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                        }
                    }
                }
                else
                {
                    //Debug.Log("There is not process running, _lastProcess is null");
                    LogUtil.WriteLog("There is not process running, _lastProcess is null");
                    //ThreadStarter._instance.OnMessageSend(this, new MessageEventArgs("Shutdown App Success"));
                    CmdChannel.SendShutdownOK(shutdown_, ShutdownRequestResponse.ENUM_SHUTDOWN_STATUS.OK);
                }
            }
        }