using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; using System.Runtime.InteropServices; namespace WindowsFormsApp2 { public partial class UserControl1 : UserControl { [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); const int NumberOfSupportedCameras = 8; public Thread recvThread = null; public Thread filerecvThread = null; bool m_Done = false; public static byte[] clientIP = new byte[] { 192, 168, 10, 127 }; public static byte[] localIP = new byte[] { 192, 168, 10, 127 }; public static int ordPort = 3333; public static int feedPort = 2999; public static int filePort = 2999; UdpClient u = null; UdpClient fileu = null; public static int[] status = new int[5]; DialogSetup setup = new DialogSetup(); protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) { m_Done = true; } public static void SendMsg(string obj) { try { UdpClient udpcSend = new UdpClient(0); byte[] sendbytes = Encoding.UTF8.GetBytes(obj); // IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("192.168.31.243"), 3333); IPEndPoint remoteIpep = new IPEndPoint(new IPAddress(clientIP), ordPort); udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep); udpcSend.Close(); } catch { } } public void RecMsg() { try { u = new UdpClient(new IPEndPoint(new IPAddress(localIP), feedPort)); recvThread = new Thread(new ThreadStart(Received)); recvThread.Priority = ThreadPriority.Normal; recvThread.Start(); fileu = new UdpClient(new IPEndPoint(new IPAddress(localIP), filePort)); filerecvThread = new Thread(new ThreadStart(FileReceived)); filerecvThread.Priority = ThreadPriority.Normal; filerecvThread.Start(); } catch { //SOCKETEventArrive("receive:Nullerror"); } } private void FileSend(String FileName ,int port) { try { UdpClient udpcSend = new UdpClient(0); FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); StreamReader sw = new StreamReader(fs, Encoding.ASCII); String filename = Path.GetFileName(FileName); String sync = "SYNC"; String FileStartFrame = sync; FileStartFrame = FileStartFrame + "str"; FileStartFrame = FileStartFrame + filename; byte[] sendbytes = Encoding.ASCII.GetBytes(FileStartFrame); udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port)); //尚未读取的⽂件内容长度 long left = fs.Length; //存储读取结果 char[] bytes = new char[2000]; //每次读取长度 int maxLength = bytes.Length; //读取位置 int start = 0; //实际返回结果长度 int num = 0; //当⽂件未读取长度⼤于0时,不断进⾏读取 while (left > 0) { fs.Position = start; num = 0; if (left < maxLength) num = sw.Read(bytes, 0, Convert.ToInt32(left)); else num = sw.Read(bytes, 0, maxLength); if (num == 0) break; start += num; left -= num; String FileFrame = sync; FileFrame = FileFrame + "con"; FileFrame = FileFrame + new string(bytes,0, num + 7); sendbytes = Encoding.ASCII.GetBytes(FileFrame); udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port)); Thread.Sleep(10); //防止系统资源耗尽 } String FileEndFrame = sync; FileEndFrame = FileEndFrame + "end"; FileEndFrame = FileEndFrame + fs.Length.ToString(); sendbytes = Encoding.ASCII.GetBytes(FileEndFrame); udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port)); sw.Close(); fs.Close(); udpcSend.Close(); } catch { throw; } } private void FileReceived() { FileStream fs = null; StreamWriter sw = null; String filename = ""; while (!m_Done) { IPEndPoint endpoint = null; if (fileu != null && null != filerecvThread && filerecvThread.IsAlive) { //接收数据 try { Byte[] data = fileu.Receive(ref endpoint); //得到数据的ACSII的字符串形式 String receiveString = Encoding.Default.GetString(data); if (receiveString != null) { String str = receiveString.Substring(0, 4); if(str == "SYNC") { str = receiveString.Substring(4, 3); if(str == "str") { filename = receiveString.Substring(7, receiveString.Length - 7); if (fs == null) fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); } else if(str == "con") { str = receiveString.Substring(7, receiveString.Length - 7); if(fs != null) { if(sw == null) sw = new StreamWriter(fs, Encoding.Default); sw.Write(str); } } else if (str == "end") { str = receiveString.Substring(7, receiveString.Length - 7); if (sw != null) { sw.Close(); sw = null; } if (fs != null) { fs.Close(); fs = null; } if(filename == "conf.txt") { string startuppath = Application.StartupPath; var p = System.Diagnostics.Process.Start(startuppath + "/" + filename); if (p != null) SetForegroundWindow(p.MainWindowHandle); } } } } } catch { throw; } } Thread.Sleep(10); //防止系统资源耗尽 } } private void Received() { //ASCII 编码 Encoding ASCII = Encoding.ASCII; while (!m_Done) { IPEndPoint endpoint = null; if (u != null && null != recvThread && recvThread.IsAlive) { //接收数据 try { Byte[] data = u.Receive(ref endpoint); //得到数据的ACSII的字符串形式 String receiveString = ASCII.GetString(data); if (receiveString != null) { if (receiveString.CompareTo("OpNoLogin") == 0) { // 需要登录 DialogLogin dialogLogin = new DialogLogin(); dialogLogin.IsAdmin = false; dialogLogin.StartPosition = FormStartPosition.CenterScreen; // dialogLogin.Location = new Point((SystemInformation.PrimaryMonitorSize.Width - dialogLogin.Width) / 2,(SystemInformation.PrimaryMonitorSize.Height - dialogLogin.Height) / 2); dialogLogin.TopMost = true; dialogLogin.ShowDialog(); } else if (receiveString.CompareTo("AdminNoLogin") == 0) { // 需要登录 DialogLogin dialogLogin = new DialogLogin(); dialogLogin.IsAdmin = true; dialogLogin.StartPosition = FormStartPosition.CenterScreen; // dialogLogin.Location = new Point((SystemInformation.PrimaryMonitorSize.Width - dialogLogin.Width) / 2,(SystemInformation.PrimaryMonitorSize.Height - dialogLogin.Height) / 2); dialogLogin.TopMost = true; dialogLogin.ShowDialog(); } else if (receiveString.CompareTo("UnKickFail") == 0) { CheckBox2Progress(false); } else if (receiveString.CompareTo("UnKickOk") == 0) { CheckBox2Progress(true); } else if (receiveString.CompareTo("DebugEnStillWork") == 0) { CheckBox1Progress(false); DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,才能进入调试模式", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("DebugDisStillWork") == 0) { CheckBox1Progress(false); DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,才能进入调试模式", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("DebugEnFail") == 0) { CheckBox1Progress(false); } else if (receiveString.CompareTo("DebugDisFail") == 0) { CheckBox1Progress(true); } else if (receiveString.CompareTo("DebugEn") == 0) { CheckBox1Progress(true); } else if (receiveString.CompareTo("DebugDis") == 0) { CheckBox1Progress(false); } else if (receiveString.CompareTo("KickFail") == 0) { CheckBox2Progress(true); } else if (receiveString.CompareTo("LoginIn") == 0) { // CheckBox1Progress(true); } else if (receiveString.CompareTo("PswError") == 0) { DialogResult dr = MessageBox.Show("密码错误,请重新输入", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("Need_Login") == 0) { DialogResult dr = MessageBox.Show("请先点击解锁按钮,进入管理员模式", "权限检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("CheckWorkState") == 0) { DialogResult dr = MessageBox.Show("请先点击“暂停工作”按钮,进入暂停模式,才可以进行换班操作", "状态检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("UnlockOk") == 0) { button3.BackgroundImage = WindowsFormsApp2.Properties.Resources._lock; } else if (receiveString.CompareTo("LockOk") == 0) { button3.BackgroundImage = WindowsFormsApp2.Properties.Resources.unlock; } else if (receiveString.CompareTo("StillDebug") == 0) { DialogResult dr = MessageBox.Show("请先点击“调试模式”复选框,取消调试模式,才可以开始工作", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("KickOk") == 0) { CheckBox2Progress(false); } else if (receiveString.CompareTo("CanGetSetting") == 0) { SendMsg("GETSETTINGPRO:" + NumberOfSupportedCameras.ToString()); var task = Task.Run(async delegate { await Task.Delay(500); setup.UpdateDialog(); setup.StartPosition = FormStartPosition.CenterScreen;//中心 setup.TopMost = true; setup.ShowDialog(); }); } else if (receiveString.CompareTo("CanSetConf") == 0) { try { FileSend("conf.txt", filePort); DialogResult dr = MessageBox.Show("配置已发送", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } catch { DialogResult dr = MessageBox.Show("配置发送失败", "状态", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } } else if (receiveString.CompareTo("RestartFailInWorking") == 0) // still in working when restart application { DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,再重启程序", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("Work") == 0) { label6.Text = " 运行中 "; label8.BackColor = Color.Lime; label6.BackColor = Color.Lime; } else if (receiveString.CompareTo("NoWork") == 0) //no work { label6.Text = " 未运行 "; label8.BackColor = Color.Yellow; label6.BackColor = Color.Yellow; } else if (receiveString.CompareTo("UpSettingOk") == 0) { DialogResult dr = MessageBox.Show("保存参数成功", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else if (receiveString.CompareTo("ReStartOk") == 0) { ///重启恢复状态 label6.Text = " 运行中 "; label8.BackColor = Color.Lime; label6.BackColor = Color.Lime; DialogResult dr = MessageBox.Show("启动成功", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } else { if(receiveString.IndexOf('_') != -1) { string[] str = receiveString.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries); if(str[0] == "total") { textBox2.Text=str[1]; } else if(str[0] == "totallast") { textBox1.Text=str[1]; } else if(str[0] == "getsetting") { int index =0; DialogSetup.cam_setting.ok = true; DialogSetup.cam_setting.IsAutoOpenCam = Convert.ToBoolean(int.Parse(str[1])); DialogSetup.cam_setting.IsAutoWork = Convert.ToBoolean(int.Parse(str[2])); DialogSetup.cam_setting.saveimage = int.Parse(str[3]); DialogSetup.cam_setting.shift_time = int.Parse(str[1]); DialogSetup.cam_setting.shift_byhand = int.Parse(str[1]); for (;index