zoukankan      html  css  js  c++  java
  • c# udp通讯实现

       public class launcherJson
        {
            public string dispatcher_ip;        // dispatcher地址
            public int dispatcher_port;      // dispatcher端口 5999
            public string launcher_ip;          // launcher地址
            public int launcher_port;        // launcher端口 6001
            public string gtaplugin_ip;         // gtaplugin地址
            public int gtaplugin_port;       // gtaplugin端口 6000
    
            public string gta_game_depth_path;
            public string gta_game_truth_path;
            public string gta_game_config_path;
            public string renderdoc_exec_path;
            public string capture_data_dir;
            public string task_json_path;
            public int time_wait_before_capture;  // 2分钟 2*60*1000        
        }
    
     public class Comm
        {
            UdpClient udp_recv = null;
            System.Timers.Timer timer = null;
            private launcherJson launcher_json = null;
    
            public void init(ref launcherJson launcher, string file_path)
            {
                string json = File.ReadAllText(file_path, Encoding.UTF8);
                if (json.Length > 0)
                {
                    launcher = JsonConvert.DeserializeObject<launcherJson>(json);
                }
            }
    
            public Comm(launcherJson json)
            {
                launcher_json = json;
    
                udp_recv = new UdpClient(new IPEndPoint(IPAddress.Parse(json.launcher_ip), json.launcher_port));//端口要与发送端相同
                Thread thread = new Thread(recv_msg);//用线程接收,避免UI卡住
                thread.IsBackground = true;
                thread.Start();
    
                timer = new System.Timers.Timer();
                timer.Interval = json.time_wait_before_capture;
                timer.Elapsed += delegate
                {
                    string run_task_msg = string.Format("run_task,{0}", json.task_json_path);
                    send_msg(json.gtaplugin_ip, json.gtaplugin_port, run_task_msg);                
                };
                timer.Start();
            }
    
            public void require_task()
            {
                send_msg(launcher_json.dispatcher_ip, launcher_json.dispatcher_port, "请发采集任务");
            }
    
            public void send_msg(string ip, int port, string msg)
            {
                IPEndPoint ip_pt = new IPEndPoint(IPAddress.Parse(ip), port);
                byte[] data = Encoding.UTF8.GetBytes(msg);
                udp_recv.Send(data, data.Length, ip_pt);
            }
    
            public void recv_msg(object obj)
            {
                IPEndPoint recv_ip = new IPEndPoint(IPAddress.Any, 0);
                while (true)
                {
                    try
                    {
                        byte[] bytRecv = udp_recv.Receive(ref recv_ip);
                        string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);                    
                        Console.WriteLine("recv:" + message);
                        Thread.Sleep(100);
    
                        if(recv_ip.Address== IPAddress.Parse(launcher_json.dispatcher_ip))
                        {
                            // write json
                            File.WriteAllText(launcher_json.task_json_path, message);
                        }
                        else if (recv_ip.Address == IPAddress.Parse(launcher_json.gtaplugin_ip))
                        {
                            // handle 
                        }                    
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception:" + ex.Message);
                        //break;
                    }
                }
            }
    
            public void modify_screen_solution(string xml_file, taskJson task)
            {
    
            }
    
            public void modify_task_solution()
            {
    
            }
    
            public void start_process_renderdoc(string exe_file, string launcher_cfg)
            {
                if (File.Exists(exe_file) && File.Exists(launcher_cfg))
                {
                    FileInfo exe = new FileInfo(exe_file);
                    FileInfo cfg = new FileInfo(launcher_cfg);
    
                    //copy config files
                    string new_cfg = Path.Combine(exe.DirectoryName, cfg.Name);
                    File.Copy(launcher_cfg, new_cfg, true);
    
                    ProcessStartInfo startinfo = new ProcessStartInfo(exe_file);                
                    startinfo.WorkingDirectory = exe.DirectoryName;
    
                    Process process = new Process();
                    process.StartInfo = startinfo;
                    process.Start();
                }            
            }
    
        }
  • 相关阅读:
    2013ACM多校联合(1)
    AcDream 1083 完美数 数位DP
    AcDream 1079 郭氏数
    AcDream 1084 同心树 几何
    AcDream 1078 递推数 嵌套循环节+矩阵快速幂
    AcDream 1081 平衡树 Tire树
    ZOJ1455 Schedule Problem 差分约束
    在程序中加载log4net配置,防止其他人看到配置文件
    sqlite错误 The database disk image is malformed database disk image is malformed 可解决
    由于这台计算机没有终端服务器客户端访问许可证,远程会话被中断。请与服务器管理员联系 解决
  • 原文地址:https://www.cnblogs.com/jobgeo/p/10097136.html
Copyright © 2011-2022 走看看