Esempio n. 1
0
        public static bool ReadStreamData(ref NotifySrvInfo value, CMD_STREAM cmd)
        {
            try
            {
                int iPos = 0;

                UInt16 ver = BitConverter.ToUInt16(cmd.bData, iPos);
                iPos += sizeof(UInt16);
                UInt32 size = BitConverter.ToUInt32(cmd.bData, iPos);
                iPos += sizeof(UInt32);

                if (size > cmd.bData.Length - 2)
                {
                    return(false);
                }

                if (ReadData(ver, ref value.notifyID, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.time, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param1, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param2, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param3, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param4, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param5, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
                if (ReadData(ver, ref value.param6, cmd.bData, ref iPos) == false)
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        public static bool ReadStreamData(ref NotifySrvInfo value, CMD_STREAM cmd)
        {
            try
            {
                int iPos = 0;

                UInt16 ver = BitConverter.ToUInt16(cmd.bData, iPos);
                iPos += sizeof(UInt16);
                UInt32 size = BitConverter.ToUInt32(cmd.bData, iPos);
                iPos += sizeof(UInt32);

                if (size > cmd.bData.Length - 2)
                {
                    return false;
                }

                if (ReadData(ver, ref value.notifyID, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.time, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param1, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param2, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param3, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param4, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param5, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
                if (ReadData(ver, ref value.param6, cmd.bData, ref iPos) == false)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
Esempio n. 3
0
        public void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                if (StopFlag)
                {
                    return;
                }

                TcpClient client = listener.EndAcceptTcpClient(ar);

                NetworkStream stream = client.GetStream();

                CMD_STREAM stCmd = new CMD_STREAM();
                CMD_STREAM stRes = new CMD_STREAM();
                //コマンド受信
                if (cmdProc != null)
                {
                    byte[] bHead = new byte[8];

                    if (ReadAll(stream, bHead, 0, 8) == 8)
                    {
                        stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                        stCmd.uiSize  = BitConverter.ToUInt32(bHead, 4);
                        stCmd.bData   = new byte[stCmd.uiSize];
                        if (ReadAll(stream, stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length)
                        {
                            cmdProc.Invoke(stCmd, stRes);
                            BitConverter.GetBytes(stRes.uiParam).CopyTo(bHead, 0);
                            BitConverter.GetBytes(stRes.uiSize).CopyTo(bHead, 4);
                        }
                        stream.Write(bHead, 0, 8);
                        if (stRes.uiSize > 0)
                        {
                            stream.Write(stRes.bData, 0, (int)stRes.uiSize);
                        }
                    }
                }
                else
                {
                    stRes.uiSize  = 0;
                    stRes.uiParam = 1;
                }
                stream.Dispose();
                client.Client.Close();

                server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
            }
            catch
            {
                // server.Stop() すると例外が発生するのでここで catch して終了させる
            }
        }
Esempio n. 4
0
        public static bool CreateStreamData(int value, ref CMD_STREAM cmd)
        {
            cmd.uiSize = sizeof(uint);
            cmd.bData = new byte[cmd.uiSize];

            int iPos = 0;

            Array.Copy(BitConverter.GetBytes(value), 0, cmd.bData, iPos, sizeof(uint));
            iPos += sizeof(uint);

            return true;
        }
Esempio n. 5
0
        public static bool CreateStreamData(int value, ref CMD_STREAM cmd)
        {
            cmd.uiSize = sizeof(uint);
            cmd.bData  = new byte[cmd.uiSize];

            int iPos = 0;

            Array.Copy(BitConverter.GetBytes(value), 0, cmd.bData, iPos, sizeof(uint));
            iPos += sizeof(uint);

            return(true);
        }
Esempio n. 6
0
        public void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            TcpListener listener = (TcpListener)ar.AsyncState;

            TcpClient client = listener.EndAcceptTcpClient(ar);

            client.ReceiveBufferSize = 1024 * 1024;

            NetworkStream stream = client.GetStream();

            CMD_STREAM stCmd = new CMD_STREAM();
            CMD_STREAM stRes = new CMD_STREAM();

            //コマンド受信
            if (cmdProc != null)
            {
                byte[] bHead = new byte[8];

                if (stream.Read(bHead, 0, bHead.Length) == 8)
                {
                    stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                    stCmd.uiSize  = BitConverter.ToUInt32(bHead, 4);
                    if (stCmd.uiSize > 0)
                    {
                        stCmd.bData = new Byte[stCmd.uiSize];
                    }
                    int readSize = 0;
                    while (readSize < stCmd.uiSize)
                    {
                        readSize += stream.Read(stCmd.bData, readSize, (int)stCmd.uiSize);
                    }
                    cmdProc.Invoke(cmdParam, stCmd, ref stRes);

                    Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint));
                    Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint));
                    stream.Write(bHead, 0, 8);
                    if (stRes.uiSize > 0)
                    {
                        stream.Write(stRes.bData, 0, (int)stRes.uiSize);
                    }
                }
            }
            else
            {
                stRes.uiSize  = 0;
                stRes.uiParam = 1;
            }
            stream.Dispose();
            client.Client.Close();

            server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
        }
Esempio n. 7
0
        public static bool ReadStreamData(ref UInt16 value, CMD_STREAM cmd)
        {
            int iPos = 0;

            if (cmd.uiSize != cmd.bData.Length)
            {
                return(false);
            }

            value = BitConverter.ToUInt16(cmd.bData, iPos);
            iPos += sizeof(UInt16);

            return(true);
        }
Esempio n. 8
0
        public static bool ReadStreamData(ref UInt16 value, CMD_STREAM cmd)
        {
            int iPos = 0;

            if (cmd.uiSize != cmd.bData.Length)
            {
                return false;
            }

            value = BitConverter.ToUInt16(cmd.bData, iPos);
            iPos += sizeof(UInt16);

            return true;
        }
Esempio n. 9
0
        public static bool ReadStreamData(ref String value, CMD_STREAM cmd)
        {
            int      iPos     = 0;
            int      iStrSize = 0;
            Encoding uniEnc   = Encoding.GetEncoding("unicode");

            if (cmd.uiSize != cmd.bData.Length)
            {
                return(false);
            }

            iStrSize  = BitConverter.ToInt32(cmd.bData, iPos);
            iPos     += sizeof(int);
            iStrSize -= sizeof(int) + 2;

            value = uniEnc.GetString(cmd.bData, iPos, iStrSize);
            iPos += iStrSize;

            return(true);
        }
Esempio n. 10
0
        public static bool ReadStreamData(ref String value, CMD_STREAM cmd)
        {
            int iPos = 0;
            int iStrSize = 0;
            Encoding uniEnc = Encoding.GetEncoding("unicode");

            if (cmd.uiSize != cmd.bData.Length)
            {
                return false;
            }

            iStrSize = BitConverter.ToInt32(cmd.bData, iPos);
            iPos += sizeof(int);
            iStrSize -= sizeof(int) + 2;

            value = uniEnc.GetString(cmd.bData, iPos, iStrSize);
            iPos += iStrSize;

            return true;
        }
Esempio n. 11
0
        public void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                if (StopFlag)
                {
                    return;
                }

                TcpClient client = listener.EndAcceptTcpClient(ar);

                NetworkStream stream = client.GetStream();

                CMD_STREAM stCmd = new CMD_STREAM();
                CMD_STREAM stRes = new CMD_STREAM();
                //コマンド受信
                if (cmdProc != null)
                {
                    byte[] bHead = new byte[8];

                    if (ReadAll(stream, bHead, 0, 8) == 8)
                    {
                        stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                        stCmd.uiSize = BitConverter.ToUInt32(bHead, 4);
                        stCmd.bData = new byte[stCmd.uiSize];
                        if (ReadAll(stream, stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length)
                        {
                            cmdProc.Invoke(stCmd, stRes);
                            BitConverter.GetBytes(stRes.uiParam).CopyTo(bHead, 0);
                            BitConverter.GetBytes(stRes.uiSize).CopyTo(bHead, 4);
                        }
                        stream.Write(bHead, 0, 8);
                        if (stRes.uiSize > 0)
                        {
                            stream.Write(stRes.bData, 0, (int)stRes.uiSize);
                        }
                    }
                }
                else
                {
                    stRes.uiSize = 0;
                    stRes.uiParam = 1;
                }
                stream.Dispose();
                client.Client.Close();

                server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
            }
            catch
            {
                // server.Stop() すると例外が発生するのでここで catch して終了させる
            }
        }
Esempio n. 12
0
        public void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
            TcpListener listener = (TcpListener)ar.AsyncState;

            TcpClient client = listener.EndAcceptTcpClient(ar);
            client.ReceiveBufferSize = 1024 * 1024;

            NetworkStream stream = client.GetStream();

            CMD_STREAM stCmd = new CMD_STREAM();
            CMD_STREAM stRes = new CMD_STREAM();
            //コマンド受信
            if (cmdProc != null)
            {
                byte[] bHead = new byte[8];

                if (stream.Read(bHead, 0, bHead.Length) == 8)
                {
                    stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                    stCmd.uiSize = BitConverter.ToUInt32(bHead, 4);
                    if (stCmd.uiSize > 0)
                    {
                        stCmd.bData = new Byte[stCmd.uiSize];
                    }
                    int readSize = 0;
                    while (readSize < stCmd.uiSize)
                    {
                        readSize += stream.Read(stCmd.bData, readSize, (int)stCmd.uiSize);
                    }
                    cmdProc.Invoke(cmdParam, stCmd, ref stRes);

                    Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint));
                    Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint));
                    stream.Write(bHead, 0, 8);
                    if (stRes.uiSize > 0)
                    {
                        stream.Write(stRes.bData, 0, (int)stRes.uiSize);
                    }
                }
            }
            else
            {
                stRes.uiSize = 0;
                stRes.uiParam = 1;
            }
            stream.Dispose();
            client.Client.Close();

            server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
        }
Esempio n. 13
0
        private int OutsideCmdCallback(object pParam, CMD_STREAM pCmdParam, ref CMD_STREAM pResParam)
        {
            System.Diagnostics.Trace.WriteLine((CtrlCmd)pCmdParam.uiParam);
            switch ((CtrlCmd)pCmdParam.uiParam)
            {
                case CtrlCmd.CMD_TIMER_GUI_SHOW_DLG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        this.Visibility = System.Windows.Visibility.Visible;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_RESERVE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        if (Dispatcher.CheckAccess() == true)
                        {
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.ReserveInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.RecInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddEpgInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddManualInfo);
                            reserveView.UpdateReserveData();
                            epgView.UpdateReserveData();
                            tunerReserveView.UpdateReserveData();
                            autoAddView.UpdateAutoAddInfo();
                            recInfoView.UpdateInfo();

                            CommonManager.Instance.DB.ReloadReserveInfo();
                            ReserveData item = new ReserveData();
                            if (CommonManager.Instance.DB.GetNextReserve(ref item) == true)
                            {
                                String timeView = item.StartTime.ToString("yyyy/MM/dd(ddd) HH:mm:ss ~ ");
                                DateTime endTime = item.StartTime + TimeSpan.FromSeconds(item.DurationSecond);
                                timeView += endTime.ToString("HH:mm:ss");
                                taskTray.Text = "次の予約:" + item.StationName + " " + timeView + " " + item.Title;
                            }
                            else
                            {
                                taskTray.Text = "次の予約なし";
                            }
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.ReserveInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.RecInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddEpgInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddManualInfo);
                                reserveView.UpdateReserveData();
                                epgView.UpdateReserveData();
                                tunerReserveView.UpdateReserveData();
                                autoAddView.UpdateAutoAddInfo();
                                recInfoView.UpdateInfo();

                                CommonManager.Instance.DB.ReloadReserveInfo();
                                ReserveData item = new ReserveData();
                                if (CommonManager.Instance.DB.GetNextReserve(ref item) == true)
                                {
                                    String timeView = item.StartTime.ToString("yyyy/MM/dd(ddd) HH:mm:ss ~ ");
                                    DateTime endTime = item.StartTime + TimeSpan.FromSeconds(item.DurationSecond);
                                    timeView += endTime.ToString("HH:mm:ss");
                                    taskTray.Text = "次の予約:" + item.StationName + " " + timeView + " " + item.Title;
                                }
                                else
                                {
                                    taskTray.Text = "次の予約なし";
                                }

                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_EPGDATA:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        if (Dispatcher.CheckAccess() == true)
                        {
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.EpgData);
                            if (CommonManager.Instance.NWMode == false)
                            {
                                CommonManager.Instance.DB.ReloadEpgData();
                            }
                            epgView.UpdateEpgData();
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.EpgData);
                                if (CommonManager.Instance.NWMode == false)
                                {
                                    CommonManager.Instance.DB.ReloadEpgData();
                                }
                                epgView.UpdateEpgData();
                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_VIEW_EXECUTE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        String exeCmd = "";
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref exeCmd);
                        try
                        {
                            string[] cmd = exeCmd.Split('\"');
                            System.Diagnostics.Process process;
                            if (cmd.Length >= 3)
                            {
                                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(cmd[1], cmd[2]);
                                if (cmd[1].IndexOf(".bat") >= 0)
                                {
                                    startInfo.CreateNoWindow = true;
                                    if (Settings.Instance.ExecBat == 0)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
                                    }
                                    else if (Settings.Instance.ExecBat == 1)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                                    }

                                }
                                process = System.Diagnostics.Process.Start(startInfo);
                            }
                            else if (cmd.Length >= 2)
                            {
                                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(cmd[1]);
                                if (cmd[1].IndexOf(".bat") >= 0)
                                {
                                    startInfo.CreateNoWindow = true;
                                    if (Settings.Instance.ExecBat == 0)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
                                    }
                                    else if (Settings.Instance.ExecBat == 1)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                                    }

                                }
                                process = System.Diagnostics.Process.Start(startInfo);
                            }
                            else
                            {
                                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(cmd[0]);
                                if (cmd[1].IndexOf(".bat") >= 0)
                                {
                                    startInfo.CreateNoWindow = true;
                                    if (Settings.Instance.ExecBat == 0)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
                                    }
                                    else if (Settings.Instance.ExecBat == 1)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                                    }

                                }
                                process = System.Diagnostics.Process.Start(startInfo);
                            }
                            var w = new CtrlCmdWriter(new System.IO.MemoryStream());
                            w.Write(process.Id);
                            w.Stream.Close();
                            pResParam.bData = w.Stream.ToArray();
                            pResParam.uiSize = (uint)pResParam.bData.Length;
                        }
                        catch
                        {
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_SUSPEND:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref param);

                        Dispatcher.BeginInvoke(new Action(() => ShowSleepDialog(param)));
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_REBOOT:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref param);

                        Byte reboot = (Byte)((param & 0xFF00) >> 8);
                        Byte suspendMode = (Byte)(param & 0x00FF);

                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            SuspendCheckWindow dlg = new SuspendCheckWindow();
                            dlg.SetMode(reboot, suspendMode);
                            if (dlg.ShowDialog() != true)
                            {
                                cmd.SendReboot();
                            }
                        }));
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_CHG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        UInt16 status = 0;
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref status);

                        if (Dispatcher.CheckAccess() == true)
                        {
                            if (status == 1)
                            {
                                taskTray.Icon = Properties.Resources.TaskIconRed;
                            }
                            else if (status == 2)
                            {
                                taskTray.Icon = Properties.Resources.TaskIconGreen;
                            }
                            else
                            {
                                taskTray.Icon = Properties.Resources.TaskIconBlue;
                            }
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                if (status == 1)
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconRed;
                                }
                                else if (status == 2)
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconGreen;
                                }
                                else
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconBlue;
                                }
                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        NotifySrvInfo status = new NotifySrvInfo();
                        var r = new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false));
                        ushort version = 0;
                        r.Read(ref version);
                        r.Version = version;
                        r.Read(ref status);
                        if (Dispatcher.CheckAccess() == true)
                        {
                            NotifyStatus(status);
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                NotifyStatus(status);
                            }));
                        }
                    }
                    break;
                default:
                    pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    break;
            }
            return 0;
        }
Esempio n. 14
0
        private void OutsideCmdCallback(CMD_STREAM pCmdParam, CMD_STREAM pResParam, bool networkFlag)
        {
            System.Diagnostics.Trace.WriteLine((CtrlCmd)pCmdParam.uiParam);

            switch ((CtrlCmd)pCmdParam.uiParam)
            {
                case CtrlCmd.CMD_TIMER_GUI_SHOW_DLG:
                    if (networkFlag)
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    }
                    else
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        this.Visibility = System.Windows.Visibility.Visible;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_VIEW_EXECUTE:
                    if (networkFlag)
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    }
                    else
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        String exeCmd = "";
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref exeCmd);
                        try
                        {
                            string[] cmd = exeCmd.Split('\"');
                            System.Diagnostics.Process process;
                            System.Diagnostics.ProcessStartInfo startInfo;
                            if (cmd.Length >= 3)
                            {
                                startInfo = new System.Diagnostics.ProcessStartInfo(cmd[1], cmd[2]);
                            }
                            else if (cmd.Length >= 2)
                            {
                                startInfo = new System.Diagnostics.ProcessStartInfo(cmd[1]);
                            }
                            else
                            {
                                startInfo = new System.Diagnostics.ProcessStartInfo(cmd[0]);
                            }
                            if (cmd.Length >= 2)
                            {
                                if (cmd[1].IndexOf(".bat") >= 0)
                                {
                                    startInfo.CreateNoWindow = true;
                                    if (Settings.Instance.ExecBat == 0)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
                                    }
                                    else if (Settings.Instance.ExecBat == 1)
                                    {
                                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                                    }
                                }
                            }
                            process = System.Diagnostics.Process.Start(startInfo);
                            var w = new CtrlCmdWriter(new System.IO.MemoryStream());
                            w.Write(process.Id);
                            w.Stream.Close();
                            pResParam.bData = w.Stream.ToArray();
                            pResParam.uiSize = (uint)pResParam.bData.Length;
                        }
                        catch { }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_SUSPEND:
                    if (networkFlag)
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    }
                    else
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref param);

                        Dispatcher.BeginInvoke(new Action(() => { if (closeFlag == false) ShowSleepDialog(param); }));
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_REBOOT:
                    if (networkFlag)
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    }
                    else
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        (new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false))).Read(ref param);

                        Byte reboot = (Byte)((param & 0xFF00) >> 8);
                        Byte suspendMode = (Byte)(param & 0x00FF);

                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            if (closeFlag == true) return;
                            SuspendCheckWindow dlg = new SuspendCheckWindow();
                            dlg.SetMode(reboot, suspendMode);
                            if (dlg.ShowDialog() != true)
                            {
                                SaveData();
                                cmd.SendReboot();
                            }
                        }));
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        NotifySrvInfo status = new NotifySrvInfo();
                        var r = new CtrlCmdReader(new System.IO.MemoryStream(pCmdParam.bData, false));
                        ushort version = 0;
                        r.Read(ref version);
                        r.Version = version;
                        r.Read(ref status);
                        //通知の巡回カウンタをuiSizeを利用して返す(やや汚い)
                        pCmdParam.uiSize = status.param3;
                        Dispatcher.BeginInvoke(new Action(() => { if (closeFlag == false) NotifyStatus(status); }));
                    }
                    break;
                default:
                    pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    break;
            }
        }
Esempio n. 15
0
        private int OutsideCmdCallback(object pParam, CMD_STREAM pCmdParam, ref CMD_STREAM pResParam)
        {
            System.Diagnostics.Trace.WriteLine((CtrlCmd)pCmdParam.uiParam);
            switch ((CtrlCmd)pCmdParam.uiParam)
            {
                case CtrlCmd.CMD_TIMER_GUI_SHOW_DLG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        this.Visibility = System.Windows.Visibility.Visible;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_RESERVE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        if (Dispatcher.CheckAccess() == true)
                        {
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.ReserveInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.RecInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddEpgInfo);
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddManualInfo);
                            reserveView.UpdateReserveData();
                            epgView.UpdateReserveData();
                            tunerReserveView.UpdateReserveData();
                            autoAddView.UpdateAutoAddInfo();
                            recInfoView.UpdateInfo();

                            CommonManager.Instance.DB.ReloadReserveInfo();
                            ReserveData item = new ReserveData();
                            if (CommonManager.Instance.DB.GetNextReserve(ref item) == true)
                            {
                                String timeView = item.StartTime.ToString("yyyy/MM/dd(ddd) HH:mm:ss ~ ");
                                DateTime endTime = item.StartTime + TimeSpan.FromSeconds(item.DurationSecond);
                                timeView += endTime.ToString("HH:mm:ss");
                                taskTray.Text = "次の予約:" + item.StationName + " " + timeView + " " + item.Title;
                            }
                            else
                            {
                                taskTray.Text = "次の予約なし";
                            }
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.ReserveInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.RecInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddEpgInfo);
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddManualInfo);
                                reserveView.UpdateReserveData();
                                epgView.UpdateReserveData();
                                tunerReserveView.UpdateReserveData();
                                autoAddView.UpdateAutoAddInfo();
                                recInfoView.UpdateInfo();

                                CommonManager.Instance.DB.ReloadReserveInfo();
                                ReserveData item = new ReserveData();
                                if (CommonManager.Instance.DB.GetNextReserve(ref item) == true)
                                {
                                    String timeView = item.StartTime.ToString("yyyy/MM/dd(ddd) HH:mm:ss ~ ");
                                    DateTime endTime = item.StartTime + TimeSpan.FromSeconds(item.DurationSecond);
                                    timeView += endTime.ToString("HH:mm:ss");
                                    taskTray.Text = "次の予約:" + item.StationName + " " + timeView + " " + item.Title;
                                }
                                else
                                {
                                    taskTray.Text = "次の予約なし";
                                }

                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_EPGDATA:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        if (Dispatcher.CheckAccess() == true)
                        {
                            CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.EpgData);
                            epgView.UpdateEpgData();
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.EpgData);
                                epgView.UpdateEpgData();
                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_VIEW_EXECUTE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        String exeCmd = "";
                        CmdStreamUtil.ReadStreamData(ref exeCmd, pCmdParam);
                        try
                        {
                            string[] cmd = exeCmd.Split('\"');
                            System.Diagnostics.Process process;
                            if (cmd.Length >= 3)
                            {
                                process = System.Diagnostics.Process.Start(cmd[1], cmd[2]);
                            }
                            else if (cmd.Length >= 2)
                            {
                                process = System.Diagnostics.Process.Start(cmd[1]);
                            }
                            else
                            {
                                process = System.Diagnostics.Process.Start(cmd[0]);
                            }
                            CmdStreamUtil.CreateStreamData(process.Id, ref pResParam);
                        }
                        catch
                        {
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_SUSPEND:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        CmdStreamUtil.ReadStreamData(ref param, pCmdParam);

                        ///////////////////////////////////////////紅////////////////////////////////////
                        ParameterizedThreadStart ts = new ParameterizedThreadStart(SuspendThread);
                        Thread thread = new Thread(ts);
                        thread.Start(param);
                        /////////////////////////////////////////////////////////////////////////////////////

                        /*Byte reboot = (Byte)((param & 0xFF00) >> 8);
                        Byte suspendMode = (Byte)(param & 0x00FF);

                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            SuspendCheckWindow dlg = new SuspendCheckWindow();
                            dlg.SetMode(0, suspendMode);
                            if (dlg.ShowDialog() != true)
                            {
                                cmd.SendSuspend(param);
                            }
                        }));*/
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_REBOOT:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        UInt16 param = 0;
                        CmdStreamUtil.ReadStreamData(ref param, pCmdParam);

                        Byte reboot = (Byte)((param & 0xFF00) >> 8);
                        Byte suspendMode = (Byte)(param & 0x00FF);

                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            SuspendCheckWindow dlg = new SuspendCheckWindow();
                            dlg.SetMode(reboot, suspendMode);
                            if (dlg.ShowDialog() != true)
                            {
                                cmd.SendReboot();
                            }
                        }));
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_CHG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        UInt16 status = 0;
                        CmdStreamUtil.ReadStreamData(ref status, pCmdParam);

                        if (Dispatcher.CheckAccess() == true)
                        {
                            if (status == 1)
                            {
                                taskTray.Icon = Properties.Resources.TaskIconRed;
                            }
                            else if (status == 2)
                            {
                                taskTray.Icon = Properties.Resources.TaskIconGreen;
                            }
                            else
                            {
                                taskTray.Icon = Properties.Resources.TaskIconBlue;
                            }
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                if (status == 1)
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconRed;
                                }
                                else if (status == 2)
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconGreen;
                                }
                                else
                                {
                                    taskTray.Icon = Properties.Resources.TaskIconBlue;
                                }
                            }));
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;

                        NotifySrvInfo status = new NotifySrvInfo();
                        CmdStreamUtil.ReadStreamData(ref status, pCmdParam);
                        if (Dispatcher.CheckAccess() == true)
                        {
                            NotifyStatus(status);
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(new Action(() =>
                            {
                                NotifyStatus(status);
                            }));
                        }
                    }
                    break;
                default:
                    pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    break;
            }
            return 0;
        }
Esempio n. 16
0
        private int OutsideCmdCallback(object pParam, CMD_STREAM pCmdParam, ref CMD_STREAM pResParam)
        {
            Trace.WriteLine((CtrlCmd)pCmdParam.uiParam);
            switch ((CtrlCmd)pCmdParam.uiParam)
            {
                case CtrlCmd.CMD_TIMER_GUI_SHOW_DLG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_RESERVE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.ReserveInfo);
                        CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.RecInfo);
                        CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddEpgInfo);
                        CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.AutoAddManualInfo);

                        CommonManager.Instance.DB.ReloadReserveInfo();
                        SocketAction.SendAllMessage("UPDATED RESERVE");
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_UPDATE_EPGDATA:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        CommonManager.Instance.DB.SetUpdateNotify((UInt32)UpdateNotifyItem.EpgData);
                        CommonManager.Instance.DB.ReloadEpgData();
                        SocketAction.SendAllMessage("UPDATED EPG");
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_VIEW_EXECUTE:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        String exeCmd = "";
                        CmdStreamUtil.ReadStreamData(ref exeCmd, pCmdParam);
                        try
                        {
                            string[] cmd = exeCmd.Split('\"');
                            Process process;
                            if (cmd.Length >= 3)
                            {
                                process = Process.Start(cmd[1], cmd[2]);
                            }
                            else if (cmd.Length >= 2)
                            {
                                process = Process.Start(cmd[1]);
                            }
                            else
                            {
                                process = Process.Start(cmd[0]);
                            }
                            CmdStreamUtil.CreateStreamData(process.Id, ref pResParam);
                        }
                        catch
                        {
                        }
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_SUSPEND:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_QUERY_REBOOT:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_CHG:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        UInt16 status = 0;
                        CmdStreamUtil.ReadStreamData(ref status, pCmdParam);

                        if (status == 1) //Rec
                        {
                        }
                        else if (status == 2) //EPG
                        {
                        }
                        else
                        {

                        }

                    }
                    break;
                case CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2:
                    {
                        pResParam.uiParam = (uint)ErrCode.CMD_SUCCESS;
                        var Status = new NotifySrvInfo();
                        CmdStreamUtil.ReadStreamData(ref Status, pCmdParam);
                        var Notify = new NotifySrvInfoItem();
                        Notify.NotifyInfo = Status;

                        if (Notify.Title != "")
                        {
                            SocketAction.SendAllMessage("EVENT " + JsonUtil.Serialize(Notify, false));
                            //Console.WriteLine("\n" + (Notify.Title + Notify.LogText).Replace("\n", ""));
                            EventStore.Instance.AddMessage(Notify);
                        }
                        NotifyStatus(Status);
                    }
                    break;
                default:
                    pResParam.uiParam = (uint)ErrCode.CMD_NON_SUPPORT;
                    break;
            }
            return 0;
        }
Esempio n. 17
0
        private void StartPolling(TcpClient client, IPAddress srvIP, uint srvPort, uint targetCount)
        {
            //巡回カウンタがtargetCountよりも大きくなる新しい通知を待ち受ける
            var w = new CtrlCmdWriter(new MemoryStream());

            w.Write((ushort)0);
            w.Write(targetCount);
            byte[] bHead = new byte[8 + w.Stream.Length];
            BitConverter.GetBytes((uint)CtrlCmd.CMD_EPG_SRV_GET_STATUS_NOTIFY2).CopyTo(bHead, 0);
            BitConverter.GetBytes((uint)w.Stream.Length).CopyTo(bHead, 4);
            w.Stream.Close();
            w.Stream.ToArray().CopyTo(bHead, 8);

            try
            {
                client.Connect(srvIP, (int)srvPort);
            }
            catch (SocketException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                Interlocked.CompareExchange(ref pollingClient, null, client);
                return;
            }
            NetworkStream stream = client.GetStream();

            if (CommonManager.Instance.CtrlCmd.Authenticate(stream, ref bHead) != ErrCode.CMD_SUCCESS)
            {
                // 認証エラー
                Interlocked.CompareExchange(ref pollingClient, null, client);
                return;
            }

            // 送信: 認証応答パケットとコマンドパケットをまとめて送る
            stream.Write(bHead, 0, bHead.Length);

            stream.BeginRead(bHead, 0, 8, (IAsyncResult ar) =>
            {
                using (client)
                {
                    int readSize = 0;
                    try
                    {
                        readSize = stream.EndRead(ar);
                    }
                    catch (IOException ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex);
                    }
                    if (readSize > 0 && ReadAll(stream, bHead, readSize, 8 - readSize) == 8 - readSize)
                    {
                        CMD_STREAM stCmd = new CMD_STREAM();
                        stCmd.uiParam    = BitConverter.ToUInt32(bHead, 0);
                        stCmd.uiSize     = BitConverter.ToUInt32(bHead, 4);
                        stCmd.bData      = new byte[stCmd.uiSize];
                        if (ReadAll(stream, stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length && stCmd.uiParam == (uint)ErrCode.CMD_SUCCESS)
                        {
                            //通常の通知コマンドに変換
                            stCmd.uiParam = (uint)CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2;
                            cmdProc.Invoke(stCmd, new CMD_STREAM());
                            targetCount = stCmd.uiSize;
                        }
                    }
                }
                //pollingClientが置きかわっていなければ引き続き待ち受ける
                var nextClient = new TcpClient();
                if (Interlocked.CompareExchange(ref pollingClient, nextClient, client) == client)
                {
                    StartPolling(nextClient, srvIP, srvPort, targetCount);
                }
            }, null);
        }
Esempio n. 18
0
        public void Run()
        {
            IntPtr hPipe = IntPtr.Zero;
            IntPtr hEventCmdWait = IntPtr.Zero;
            IntPtr hEventConnect = IntPtr.Zero;
            IntPtr[] hEventArray = new IntPtr[2];
            NativeOverlapped stOver = new NativeOverlapped();

             	        if( m_iCtrlCmdEventID != -1 ){
                string strCmdEvent;
                strCmdEvent = CMD_CTRL_EVENT_WAIT + m_iCtrlCmdEventID.ToString();
                hEventCmdWait = CommonUtil._CreateEvent(false, true, strCmdEvent);
            }
            hEventConnect = CommonUtil._CreateEvent(false, false, m_strEventName);
            hEventArray[0] = m_hStopEvent;
            hEventArray[1] = CommonUtil._CreateEvent(false, false, null);

            while (true)
            {
                if (hPipe == IntPtr.Zero)
                {
                    while (true)
                    {
                        hPipe = CommonUtil._CreateNamedPipe(m_strPipeName,
                            CommonUtil.PIPE_ACCESS_DUPLEX | CommonUtil.FILE_FLAG_WRITE_THROUGH | CommonUtil.FILE_FLAG_OVERLAPPED,
                        CommonUtil.PIPE_TYPE_BYTE, 1,
                        SEND_BUFF_SIZE, RES_BUFF_SIZE, PIPE_TIMEOUT);
                        if (Marshal.GetLastWin32Error() == CommonUtil.ERROR_PIPE_BUSY)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            if (hPipe == IntPtr.Zero)
                            {
                                hPipe = IntPtr.Zero;
                            }
                            break;
                        }
                    }
                    stOver.EventHandle = hEventArray[1];
                    CommonUtil._ConnectNamedPipe(hPipe, ref stOver);
                    CommonUtil._SetEvent(hEventConnect);
                }

                uint uiRes = CommonUtil._WaitForMultipleObjects(2, hEventArray, false, CommonUtil.INFINITE);
                if (uiRes == CommonUtil.WAIT_OBJECT_0)
                {
                    //STOP
                    break;
                }
                else if (uiRes == CommonUtil.WAIT_OBJECT_0 + 1)
                {
                    //ほかのサーバーで処理中?
                    if (hEventCmdWait != IntPtr.Zero)
                    {
                        CommonUtil._WaitForSingleObject(hEventCmdWait, CommonUtil.INFINITE);
                    }
                    //コマンド受信
                    if (m_pCmdProc != null)
                    {
                        CMD_STREAM stCmd = new CMD_STREAM();
                        CMD_STREAM stRes = new CMD_STREAM();
                        uint dwRead = 0;
                        uint dwWrite = 0;
                        do
                        {
                            byte[] bHead = new byte[8];

                            Array.Copy(BitConverter.GetBytes(stCmd.uiParam), 0, bHead, 0, sizeof(uint));
                            Array.Copy(BitConverter.GetBytes(stCmd.uiSize), 0, bHead, 4, sizeof(uint));
                            if (CommonUtil._ReadFile(hPipe, ref bHead, sizeof(uint) * 2, out dwRead, IntPtr.Zero) == false)
                            {
                                break;
                            }
                            stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                            stCmd.uiSize = BitConverter.ToUInt32(bHead, 4);
                            if (stCmd.uiSize > 0)
                            {
                                stCmd.bData = new byte[stCmd.uiSize];
                                uint dwReadNum = 0;
                                while (dwReadNum < stCmd.uiSize)
                                {
                                    uint dwReadSize = 0;
                                    if (stCmd.uiSize - dwReadNum < SEND_BUFF_SIZE)
                                    {
                                        dwReadSize = stCmd.uiSize - dwReadNum;
                                    }
                                    else
                                    {
                                        dwReadSize = SEND_BUFF_SIZE;
                                    }
                                    byte[] bRead = new byte[dwReadSize];
                                    if (CommonUtil._ReadFile(hPipe, ref bRead, dwReadSize, out dwRead, IntPtr.Zero) == false)
                                    {
                                        break;
                                    }
                                    Array.Copy(bRead, dwReadNum, stCmd.bData, 0, dwRead);
                                    dwReadNum += dwRead;
                                }
                                if (dwReadNum < stCmd.uiSize)
                                {
                                    break;
                                }
                            }

                            m_pCmdProc.Invoke(m_pParam, stCmd, ref stRes);
                            if (stRes.uiParam == (uint)ErrCode.CMD_NO_RES)
                            {
                                break;
                            }

                            Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint));
                            Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint));

                            if (CommonUtil._WriteFile(hPipe, ref bHead, sizeof(uint) * 2, out dwWrite, IntPtr.Zero) == false)
                            {
                                break;
                            }
                            if (stRes.uiSize > 0)
                            {
                                if (stRes.bData == null)
                                {
                                    break;
                                }
                                if (CommonUtil._WriteFile(hPipe, ref stRes.bData, stRes.uiSize, out dwWrite, IntPtr.Zero) == false)
                                {
                                    break;
                                }
                            }
                        } while (stRes.uiParam == (uint)ErrCode.CMD_NEXT); //Emun用の繰り返し
                    }
                    CommonUtil._FlushFileBuffers(hPipe);
                    CommonUtil._DisconnectNamedPipe(hPipe);
                    CommonUtil._CloseHandle(hPipe);
                    hPipe = IntPtr.Zero;
                    if (hEventCmdWait != IntPtr.Zero)
                    {
                        CommonUtil._SetEvent(hEventCmdWait);
                    }
                }
            }

            if (hPipe != IntPtr.Zero)
            {
                CommonUtil._FlushFileBuffers(hPipe);
                CommonUtil._DisconnectNamedPipe(hPipe);
                CommonUtil._CloseHandle(hPipe);
            }

            CommonUtil._CloseHandle(hEventArray[1]);
            CommonUtil._CloseHandle(hEventConnect);
            if (hEventCmdWait != IntPtr.Zero)
            {
                CommonUtil._CloseHandle(hEventCmdWait);
            }
        }
Esempio n. 19
0
        public void Run()
        {
            IntPtr hPipe         = IntPtr.Zero;
            IntPtr hEventCmdWait = IntPtr.Zero;
            IntPtr hEventConnect = IntPtr.Zero;

            IntPtr[]         hEventArray = new IntPtr[2];
            NativeOverlapped stOver      = new NativeOverlapped();

            if (m_iCtrlCmdEventID != -1)
            {
                string strCmdEvent;
                strCmdEvent   = CMD_CTRL_EVENT_WAIT + m_iCtrlCmdEventID.ToString();
                hEventCmdWait = CommonUtil._CreateEvent(false, true, strCmdEvent);
            }
            hEventConnect  = CommonUtil._CreateEvent(false, false, m_strEventName);
            hEventArray[0] = m_hStopEvent;
            hEventArray[1] = CommonUtil._CreateEvent(false, false, null);

            while (true)
            {
                if (hPipe == IntPtr.Zero)
                {
                    while (true)
                    {
                        hPipe = CommonUtil._CreateNamedPipe(m_strPipeName,
                                                            CommonUtil.PIPE_ACCESS_DUPLEX | CommonUtil.FILE_FLAG_WRITE_THROUGH | CommonUtil.FILE_FLAG_OVERLAPPED,
                                                            CommonUtil.PIPE_TYPE_BYTE, 1,
                                                            SEND_BUFF_SIZE, RES_BUFF_SIZE, PIPE_TIMEOUT);
                        if (Marshal.GetLastWin32Error() == CommonUtil.ERROR_PIPE_BUSY)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            if (hPipe == IntPtr.Zero)
                            {
                                hPipe = IntPtr.Zero;
                            }
                            break;
                        }
                    }
                    stOver.EventHandle = hEventArray[1];
                    CommonUtil._ConnectNamedPipe(hPipe, ref stOver);
                    CommonUtil._SetEvent(hEventConnect);
                }

                uint uiRes = CommonUtil._WaitForMultipleObjects(2, hEventArray, false, CommonUtil.INFINITE);
                if (uiRes == CommonUtil.WAIT_OBJECT_0)
                {
                    //STOP
                    break;
                }
                else if (uiRes == CommonUtil.WAIT_OBJECT_0 + 1)
                {
                    //ほかのサーバーで処理中?
                    if (hEventCmdWait != IntPtr.Zero)
                    {
                        CommonUtil._WaitForSingleObject(hEventCmdWait, CommonUtil.INFINITE);
                    }
                    //コマンド受信
                    if (m_pCmdProc != null)
                    {
                        CMD_STREAM stCmd   = new CMD_STREAM();
                        CMD_STREAM stRes   = new CMD_STREAM();
                        uint       dwRead  = 0;
                        uint       dwWrite = 0;
                        do
                        {
                            byte[] bHead = new byte[8];

                            Array.Copy(BitConverter.GetBytes(stCmd.uiParam), 0, bHead, 0, sizeof(uint));
                            Array.Copy(BitConverter.GetBytes(stCmd.uiSize), 0, bHead, 4, sizeof(uint));
                            if (CommonUtil._ReadFile(hPipe, ref bHead, sizeof(uint) * 2, out dwRead, IntPtr.Zero) == false)
                            {
                                break;
                            }
                            stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                            stCmd.uiSize  = BitConverter.ToUInt32(bHead, 4);
                            if (stCmd.uiSize > 0)
                            {
                                stCmd.bData = new byte[stCmd.uiSize];
                                uint dwReadNum = 0;
                                while (dwReadNum < stCmd.uiSize)
                                {
                                    uint dwReadSize = 0;
                                    if (stCmd.uiSize - dwReadNum < SEND_BUFF_SIZE)
                                    {
                                        dwReadSize = stCmd.uiSize - dwReadNum;
                                    }
                                    else
                                    {
                                        dwReadSize = SEND_BUFF_SIZE;
                                    }
                                    byte[] bRead = new byte[dwReadSize];
                                    if (CommonUtil._ReadFile(hPipe, ref bRead, dwReadSize, out dwRead, IntPtr.Zero) == false)
                                    {
                                        break;
                                    }
                                    Array.Copy(bRead, dwReadNum, stCmd.bData, 0, dwRead);
                                    dwReadNum += dwRead;
                                }
                                if (dwReadNum < stCmd.uiSize)
                                {
                                    break;
                                }
                            }

                            m_pCmdProc.Invoke(m_pParam, stCmd, ref stRes);
                            if (stRes.uiParam == (uint)ErrCode.CMD_NO_RES)
                            {
                                break;
                            }

                            Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint));
                            Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint));

                            if (CommonUtil._WriteFile(hPipe, ref bHead, sizeof(uint) * 2, out dwWrite, IntPtr.Zero) == false)
                            {
                                break;
                            }
                            if (stRes.uiSize > 0)
                            {
                                if (stRes.bData == null)
                                {
                                    break;
                                }
                                if (CommonUtil._WriteFile(hPipe, ref stRes.bData, stRes.uiSize, out dwWrite, IntPtr.Zero) == false)
                                {
                                    break;
                                }
                            }
                        } while (stRes.uiParam == (uint)ErrCode.CMD_NEXT); //Emun用の繰り返し
                    }
                    CommonUtil._FlushFileBuffers(hPipe);
                    CommonUtil._DisconnectNamedPipe(hPipe);
                    CommonUtil._CloseHandle(hPipe);
                    hPipe = IntPtr.Zero;
                    if (hEventCmdWait != IntPtr.Zero)
                    {
                        CommonUtil._SetEvent(hEventCmdWait);
                    }
                }
            }

            if (hPipe != IntPtr.Zero)
            {
                CommonUtil._FlushFileBuffers(hPipe);
                CommonUtil._DisconnectNamedPipe(hPipe);
                CommonUtil._CloseHandle(hPipe);
            }

            CommonUtil._CloseHandle(hEventArray[1]);
            CommonUtil._CloseHandle(hEventConnect);
            if (hEventCmdWait != IntPtr.Zero)
            {
                CommonUtil._CloseHandle(hEventCmdWait);
            }
        }
Esempio n. 20
0
        public bool StartServer(string strEventName, string strPipeName, Action<CMD_STREAM, CMD_STREAM> pfnCmdProc)
        {
            if (pfnCmdProc == null || strEventName.Length == 0 || strPipeName.Length == 0)
            {
                return false;
            }
            if (m_ServerThread != null)
            {
                return false;
            }

            m_StopFlag = false;
            m_PulseEvent = new AutoResetEvent(false);
            m_ServerThread = new Thread(new ThreadStart(() =>
            {
                using (EventWaitHandle eventConnect = new EventWaitHandle(false, EventResetMode.AutoReset, strEventName))
                using (NamedPipeServerStream pipe = new NamedPipeServerStream(
                           strPipeName.Substring(strPipeName.StartsWith("\\\\.\\pipe\\", StringComparison.OrdinalIgnoreCase) ? 9 : 0),
                           PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
                {
                    while (m_StopFlag == false)
                    {
                        pipe.BeginWaitForConnection(asyncResult =>
                        {
                            try
                            {
                                if (m_StopFlag == false)
                                {
                                    pipe.EndWaitForConnection(asyncResult);
                                    m_PulseEvent.Set();
                                }
                            }
                            catch (ObjectDisposedException)
                            {
                            }
                        }, null);
                        eventConnect.Set();
                        m_PulseEvent.WaitOne();
                        if (pipe.IsConnected)
                        {
                            try
                            {
                                byte[] bHead = new byte[8];
                                if (pipe.Read(bHead, 0, 8) == 8)
                                {
                                    CMD_STREAM stCmd = new CMD_STREAM();
                                    stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                                    stCmd.uiSize = BitConverter.ToUInt32(bHead, 4);
                                    stCmd.bData = stCmd.uiSize == 0 ? null : new byte[stCmd.uiSize];
                                    if (stCmd.uiSize == 0 || pipe.Read(stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length)
                                    {
                                        CMD_STREAM stRes = new CMD_STREAM();
                                        pfnCmdProc.Invoke(stCmd, stRes);
                                        if (stRes.uiParam == (uint)ErrCode.CMD_NEXT)
                                        {
                                            // Emun用の繰り返しは対応しない
                                            throw new InvalidOperationException();
                                        }
                                        else if (stRes.uiParam != (uint)ErrCode.CMD_NO_RES)
                                        {
                                            BitConverter.GetBytes(stRes.uiParam).CopyTo(bHead, 0);
                                            BitConverter.GetBytes(stRes.uiSize).CopyTo(bHead, 4);
                                            pipe.Write(bHead, 0, 8);
                                            if (stRes.uiSize != 0 && stRes.bData != null && stRes.bData.Length >= stRes.uiSize)
                                            {
                                                pipe.Write(stRes.bData, 0, (int)stRes.uiSize);
                                            }
                                        }
                                    }
                                }
                                pipe.WaitForPipeDrain();
                                pipe.Disconnect();
                            }
                            catch
                            {
                                // Read & Write 中に切断されると例外が起きるはずなので一応 catch しておく
                            }
                        }
                    }
                }
            }));
            m_ServerThread.Start();

            return true;
        }
Esempio n. 21
0
        private void StartPolling(TcpClient client, IPAddress srvIP, uint srvPort, uint targetCount)
        {
            //巡回カウンタがtargetCountよりも大きくなる新しい通知を待ち受ける
            var w = new CtrlCmdWriter(new MemoryStream());
            w.Write((ushort)0);
            w.Write(targetCount);
            byte[] bHead = new byte[8 + w.Stream.Length];
            BitConverter.GetBytes((uint)CtrlCmd.CMD_EPG_SRV_GET_STATUS_NOTIFY2).CopyTo(bHead, 0);
            BitConverter.GetBytes((uint)w.Stream.Length).CopyTo(bHead, 4);
            w.Stream.Close();
            w.Stream.ToArray().CopyTo(bHead, 8);

            try
            {
                client.Connect(srvIP, (int)srvPort);
            }
            catch (SocketException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                Interlocked.CompareExchange(ref pollingClient, null, client);
                return;
            }
            NetworkStream stream = client.GetStream();

            if (CommonManager.Instance.CtrlCmd.Authenticate(stream, ref bHead) != ErrCode.CMD_SUCCESS)
            {
                // 認証エラー
                Interlocked.CompareExchange(ref pollingClient, null, client);
                return;
            }

            // 送信: 認証応答パケットとコマンドパケットをまとめて送る
            stream.Write(bHead, 0, bHead.Length);

            stream.BeginRead(bHead, 0, 8, (IAsyncResult ar) =>
            {
                using (client)
                {
                    int readSize = 0;
                    try
                    {
                        readSize = stream.EndRead(ar);
                    }
                    catch (IOException ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex);
                    }
                    if (readSize > 0 && ReadAll(stream, bHead, readSize, 8 - readSize) == 8 - readSize)
                    {
                        CMD_STREAM stCmd = new CMD_STREAM();
                        stCmd.uiParam = BitConverter.ToUInt32(bHead, 0);
                        stCmd.uiSize = BitConverter.ToUInt32(bHead, 4);
                        stCmd.bData = new byte[stCmd.uiSize];
                        if (ReadAll(stream, stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length && stCmd.uiParam == (uint)ErrCode.CMD_SUCCESS)
                        {
                            //通常の通知コマンドに変換
                            stCmd.uiParam = (uint)CtrlCmd.CMD_TIMER_GUI_SRV_STATUS_NOTIFY2;
                            cmdProc.Invoke(stCmd, new CMD_STREAM());
                            targetCount = stCmd.uiSize;
                        }
                    }
                }
                //pollingClientが置きかわっていなければ引き続き待ち受ける
                var nextClient = new TcpClient();
                if (Interlocked.CompareExchange(ref pollingClient, nextClient, client) == client)
                {
                    StartPolling(nextClient, srvIP, srvPort, targetCount);
                }
            }, null);
        }
Esempio n. 22
0
        public bool StartServer(string strEventName, string strPipeName, Action <CMD_STREAM, CMD_STREAM> pfnCmdProc)
        {
            if (pfnCmdProc == null || strEventName.Length == 0 || strPipeName.Length == 0)
            {
                return(false);
            }
            if (m_ServerThread != null)
            {
                return(false);
            }

            m_StopFlag     = false;
            m_PulseEvent   = new AutoResetEvent(false);
            m_ServerThread = new Thread(new ThreadStart(() =>
            {
                using (EventWaitHandle eventConnect = new EventWaitHandle(false, EventResetMode.AutoReset, strEventName))
                    using (NamedPipeServerStream pipe = new NamedPipeServerStream(
                               strPipeName.Substring(strPipeName.StartsWith("\\\\.\\pipe\\", StringComparison.OrdinalIgnoreCase) ? 9 : 0),
                               PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
                    {
                        while (m_StopFlag == false)
                        {
                            pipe.BeginWaitForConnection(asyncResult =>
                            {
                                try
                                {
                                    if (m_StopFlag == false)
                                    {
                                        pipe.EndWaitForConnection(asyncResult);
                                        m_PulseEvent.Set();
                                    }
                                }
                                catch (ObjectDisposedException)
                                {
                                }
                            }, null);
                            eventConnect.Set();
                            m_PulseEvent.WaitOne();
                            if (pipe.IsConnected)
                            {
                                try
                                {
                                    byte[] bHead = new byte[8];
                                    if (pipe.Read(bHead, 0, 8) == 8)
                                    {
                                        CMD_STREAM stCmd = new CMD_STREAM();
                                        stCmd.uiParam    = BitConverter.ToUInt32(bHead, 0);
                                        stCmd.uiSize     = BitConverter.ToUInt32(bHead, 4);
                                        stCmd.bData      = stCmd.uiSize == 0 ? null : new byte[stCmd.uiSize];
                                        if (stCmd.uiSize == 0 || pipe.Read(stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length)
                                        {
                                            CMD_STREAM stRes = new CMD_STREAM();
                                            pfnCmdProc.Invoke(stCmd, stRes);
                                            if (stRes.uiParam == (uint)ErrCode.CMD_NEXT)
                                            {
                                                // Emun用の繰り返しは対応しない
                                                throw new InvalidOperationException();
                                            }
                                            else if (stRes.uiParam != (uint)ErrCode.CMD_NO_RES)
                                            {
                                                BitConverter.GetBytes(stRes.uiParam).CopyTo(bHead, 0);
                                                BitConverter.GetBytes(stRes.uiSize).CopyTo(bHead, 4);
                                                pipe.Write(bHead, 0, 8);
                                                if (stRes.uiSize != 0 && stRes.bData != null && stRes.bData.Length >= stRes.uiSize)
                                                {
                                                    pipe.Write(stRes.bData, 0, (int)stRes.uiSize);
                                                }
                                            }
                                        }
                                    }
                                    pipe.WaitForPipeDrain();
                                    pipe.Disconnect();
                                }
                                catch
                                {
                                    // Read & Write 中に切断されると例外が起きるはずなので一応 catch しておく
                                }
                            }
                        }
                    }
            }));
            m_ServerThread.Start();

            return(true);
        }