Exemplo n.º 1
0
        /// <summary>
        /// 状態通知(送信)用のサーバー(生成部)
        /// </summary>
        /// <param name="port">ポート番号(コマンドは10011を使用)</param>
        public void CreateStatusServer(int port)
        {
            _statusSock = new SockCtrl();

            _statusSock.Init(_serverIP, port);

            //送信データありを示すイベント
            _statSendEvent = new AutoResetEvent(false);

            //クライアント接続待ち
            _statusThread = new Thread(new ThreadStart(StatusThread));
            _statusThread.Start();
        }
Exemplo n.º 2
0
        /// <summary>
        ///  コマンド受信スレッド
        /// </summary>
        unsafe private void CommandThread(Object argObj)
        {
            //引数の展開
            object[] argsTmp = (object[])argObj;

            byte[] buffer = new byte[1024];

            //ソケットの生成
            _sock = new SockCtrl();
            _sock.Init(_serverIP, (int)argsTmp[0]);

            while (_sock.listener == null)
            {
                Thread.Sleep(10);
            }
            try {
                _sock.listener.Pending();
            }
            catch (Exception err) {
                _sock.listener.Start();
            }

            while (true)
            {
                if (_sock.listener.Pending() == true)
                {
                    _sock.Accept();

                    ConnectionMsg = "接続中";
                    Trace.TraceFormat("Socket Connected");

                    int           len    = 0;
                    NetworkStream stream = _sock.stream;
                    MemoryStream  ms     = new MemoryStream();

                    int  cmdMode = 0;
                    bool loop    = true;
                    while (loop)
                    {
                        //コマンド文字列受信
                        do
                        {
                            try {
                                len = stream.Read(buffer, 0, 1);

                                if (len == 0)
                                {
                                    //クライアント側から切断された
                                    loop        = false;
                                    ms.Position = 0;
                                    ms.Dispose();
                                    break;
                                }
                                ms.Write(buffer, 0, len);
                            }
                            catch (Exception err) {
                                //Read中に切断された
                                //MessageBox.Show("CMD::" + err.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                                WriteConsole(err.Message);
                                loop = false;
                                ms.Flush();
                                ms.Position = 0;
                                ms.Dispose();
                                break;
                            }
                            //コマンドの最終文字は';' または'@'とする
                            if (buffer[0] == ';')
                            {
                                cmdMode = 1;
                                break;
                            }
                            if (buffer[0] == '@')
                            {
                                cmdMode = 2;
                                break;
                            }
                            if (buffer[0] == '?')
                            {
                                cmdMode = 3;
                                break;
                            }
                        }while (stream.DataAvailable);

                        //コマンドを受信した
                        if (loop == true && (int)ms.Position != 0)
                        {
                            Encoding enc = Encoding.UTF8;

                            string cmd = enc.GetString(ms.GetBuffer(), 0, (int)ms.Length - 1);

                            ms.Flush();
                            ms.Position = 0;
                            ms.Dispose();

                            //コマンドスクリプトを実行する
                            if (cmdMode == 1)
                            {
                                MessageData msg;
                                try {
                                    ScriptSource src = _pythonConsole.Pad.Console.ScriptScope.Engine.CreateScriptSourceFromString(cmd);
                                    Object       obj = src.Execute(_pythonConsole.Pad.Console.ScriptScope);

                                    msg = CreateMsg(MessageCode.SYS_CMD_RETOK, obj);
                                }
                                catch (Exception err) {
                                    //スクリプトの実行で例外発生
                                    string errMsg = _pythonConsole.Pad.Console.ScriptScope.Engine.GetService <ExceptionOperations>().FormatException(err);
                                    Trace.TraceFormat("[ERR]Script:{0}", errMsg);

                                    //MessageBox.Show(errMsg, "err", MessageBoxButton.OK, MessageBoxImage.Error);
                                    _pythonConsole.Pad.Console.WriteLine(errMsg, Microsoft.Scripting.Hosting.Shell.Style.Out);

                                    msg = CreateMsg(MessageCode.SYS_CMD_RETNG, (int)ErrCode.ERR_EXEC);
                                }
                                try {
                                    //結果を送信する
                                    stream.Write(msg.Binary(), 0, Marshal.SizeOf(msg));
                                }
                                catch (Exception err) {
                                    //MessageBox.Show("CMD_Write::" + err.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                                    WriteConsole(err.Message);
                                    loop = false;
                                }
                            }
                            else if (cmdMode == 3)
                            {
                                //スクリプトファイルで実行
                                MessageData msg;
                                try {
                                    ScriptSource src = _pythonConsole.Pad.Console.ScriptScope.Engine.CreateScriptSourceFromFile(cmd);
                                    Object       obj = src.Execute(_pythonConsole.Pad.Console.ScriptScope);

                                    msg = CreateMsg(MessageCode.SYS_CMD_RETOK, obj);
                                }
                                catch (Exception err) {
                                    //スクリプトの実行で例外発生
                                    string errMsg = _pythonConsole.Pad.Console.ScriptScope.Engine.GetService <ExceptionOperations>().FormatException(err);
                                    Trace.TraceFormat("[ERR]Script:{0}", errMsg);

//									MessageBox.Show(errMsg, "err", MessageBoxButton.OK, MessageBoxImage.Error);

                                    msg = CreateMsg(MessageCode.SYS_CMD_RETNG, (int)ErrCode.ERR_EXEC);
                                }
                                try {
                                    //結果を送信する
                                    stream.Write(msg.Binary(), 0, Marshal.SizeOf(msg));
                                }
                                catch (Exception err) {
                                    Trace.TraceFormat("[ERR]Socket Write:{0}", err.Message);
                                    //MessageBox.Show("CMD_Write::" + err.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                                    WriteConsole(err.Message);
                                    loop = false;
                                }
                                _ExecuteScript(this, new ScriptEventArgs("\n"));
                            }
                            else
                            {
                                // 別スレッドでのコマンド実行
                                bool threadRun = true;
                                if (_scriptThread != null)
                                {
                                    if (_scriptThread.ThreadState != ThreadState.Stopped)
                                    {
                                        //前回のスクリプトスレッドが動作中
                                        Trace.TraceFormat("Script already Run");
                                        MessageData msg = CreateMsg(MessageCode.SYS_CMD_RETNG, (int)ErrCode.ERR_EXEC);
                                        try {
                                            //結果を送信する
                                            stream.Write(msg.Binary(), 0, Marshal.SizeOf(msg));
                                        }
                                        catch (Exception err) {
                                            Trace.TraceFormat("[ERR]Socket Write:{0}", err.Message);
                                            //MessageBox.Show("CMD_Write::" + err.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                                            WriteConsole(err.Message);
                                            loop      = false;
                                            threadRun = false;
                                        }
                                        threadRun = false;
                                    }
                                }
                                if (threadRun == true)
                                {
                                    //スクリプト実行のイベント
                                    PEventManager.PEvent evObj = GetEvent();

                                    ThreadArgs args = new ThreadArgs();
                                    args.vm     = this;
                                    args.cmd    = cmd;
                                    args._event = evObj;

                                    _scriptThread = new Thread(new ParameterizedThreadStart(ScriptThread));
                                    _scriptThread.Start(args);

                                    Trace.TraceFormat("Script thread started");

                                    //execute_program() のリターン用データ
                                    MessageData msg = CreateMsg(MessageCode.SYS_CMD_RETOK, evObj._number);
                                    try {
                                        stream.Write(msg.Binary(), 0, Marshal.SizeOf(msg));
                                    }
                                    catch (Exception err) {
                                        Trace.TraceFormat("[ERR]Socket:{0}", err.Message);
                                        //MessageBox.Show("SCR_Write::" + err.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        WriteConsole(err.Message);
                                        loop = false;
                                    }
                                }
                            }
                            ms = new MemoryStream();
                        }
                    }
                    _sock.Close();
                    ConnectionMsg  = "未接続";
                    _threadRestart = 1;
                    Trace.TraceFormat("Socket DisConnected");
                }
                else
                {
                    Thread.Sleep(500);
                    if (_threadStop != 0)
                    {
                        break;
                    }
                }
            }
            _sock.Close();
            ConnectionMsg  = "未接続";
            _threadRestart = 1;
            Trace.TraceFormat("Socket DisConnected");
        }