diff --git a/WpfApp1/DataHandle/DataReciver.cs b/WpfApp1/DataHandle/DataReciver.cs index 877ac3b..38fdc08 100644 --- a/WpfApp1/DataHandle/DataReciver.cs +++ b/WpfApp1/DataHandle/DataReciver.cs @@ -8,14 +8,16 @@ namespace F1Tools public static class DataReciver { private static UdpClient UDP; - private static IPEndPoint IP; + private static IPEndPoint FromIP; + private static IPEndPoint ListenEndPoint; public static MicroTimer MicroTimer; - private static int _port = 20777; + //private static int _port = 20777; private static GameVersion _version = GameVersion.Unkonwn; static DataReciver() { - UDP = new UdpClient(_port); + ListenEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20777); + UDP = new UdpClient(ListenEndPoint); MicroTimer = new MicroTimer(1, 1); MicroTimer.OnRunningCallback += MicroTimer_OnRunningCallback; MicroTimer.Start(); @@ -23,29 +25,39 @@ static DataReciver() private static void MicroTimer_OnRunningCallback(int id, int msg, int user, int param1, int param2) { - if (UDP.Available <= 0) + if (UDP == null || UDP.Available <= 0) return; - var bytes = UDP.Receive(ref IP); + var bytes = UDP.Receive(ref FromIP); if (bytes.Length > 0) { var data = GetData(bytes, _version, out _version); if (_version == GameVersion.Unkonwn || data == null) return; ReciveEvent?.Invoke(data); +#if DEBUG //Console.WriteLine($"{data.Throttle} {data.Brake}"); +#endif } } public static int Port { - get => _port; + get => ListenEndPoint.Port; set { - MicroTimer.Stop(); - _port = Port; - UDP = new UdpClient(Port); - MicroTimer.Start(); + if (value < 1 || value > 65536) + return; + + if (ListenEndPoint.Port == value) + return; + + ListenEndPoint.Port = value; + UDP.Dispose(); + UDP = new UdpClient(ListenEndPoint); +#if DEBUG + Console.WriteLine($"端口已修改为{value}"); +#endif } } @@ -53,7 +65,7 @@ public static int Port public static void Dispose() { - MicroTimer.Dispose(); + MicroTimer.Stop(); UDP.Close(); UDP.Dispose(); } diff --git a/WpfApp1/MainWindow.xaml b/WpfApp1/MainWindow.xaml index c6d6c8f..d02047d 100644 --- a/WpfApp1/MainWindow.xaml +++ b/WpfApp1/MainWindow.xaml @@ -21,7 +21,10 @@ - + + + + diff --git a/WpfApp1/MainWindow.xaml.cs b/WpfApp1/MainWindow.xaml.cs index fbace21..16ffa5f 100644 --- a/WpfApp1/MainWindow.xaml.cs +++ b/WpfApp1/MainWindow.xaml.cs @@ -29,7 +29,7 @@ public MainWindow() DataReciver.ReciveEvent += DataReciver_ReciveEvent; var ip = Helper.GetLocalIP(); lb_ip.Content = $"本机IP:{ip}"; - lb_port.Content = $"端口:{DataReciver.Port}"; + //port.Text = DataReciver.Port.ToString(); Timer = new Timer(3000) { @@ -41,10 +41,10 @@ public MainWindow() { //Task.Run(() => //{ - if (Helper.CheckUpdate()) - { - Dispatcher.Invoke(new WindowDelegate(TipShow)); - } + if (Helper.CheckUpdate()) + { + Dispatcher.Invoke(new WindowDelegate(TipShow)); + } //}); }); } @@ -151,5 +151,14 @@ private void Hyperlink_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("explorer.exe", "https://gitee.com/n-i-n-g/F1-2020-Telemetering-Tools/releases"); } + + private void port_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) + { + if (int.TryParse(port.Text, out int udpPort)) + { + DataReciver.Port = udpPort; + } + + } } } diff --git a/WpfApp1/Properties/AssemblyInfo.cs b/WpfApp1/Properties/AssemblyInfo.cs index 8a588f4..9b6062b 100644 --- a/WpfApp1/Properties/AssemblyInfo.cs +++ b/WpfApp1/Properties/AssemblyInfo.cs @@ -50,5 +50,5 @@ // //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: -[assembly: AssemblyVersion("2.5.1.1")] +[assembly: AssemblyVersion("2.5.1.2")] [assembly: AssemblyFileVersion("1.0.0.0")]