Skip to content

Commit

Permalink
增加自定义端口功能
Browse files Browse the repository at this point in the history
  • Loading branch information
王宁 committed Jul 22, 2022
1 parent 6932d2e commit b311a4e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
34 changes: 23 additions & 11 deletions WpfApp1/DataHandle/DataReciver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,64 @@ 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();
}

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
}
}

public static GameVersion Version => _version;

public static void Dispose()
{
MicroTimer.Dispose();
MicroTimer.Stop();
UDP.Close();
UDP.Dispose();
}
Expand Down
5 changes: 4 additions & 1 deletion WpfApp1/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<local:F1Instrument x:Name="f1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:F1Instrument>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="sp_ip" Width="500" Height="200" Opacity="0.95" Orientation="Vertical" Background="White">
<Label x:Name="lb_ip" FontFamily="SourceHanSansCN-Normal" Content="本机IP:0.0.0.0" Foreground="Black" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label x:Name="lb_port" FontFamily="SourceHanSansCN-Normal" Content="端口:20777" Foreground="Black" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Stretch" Opacity="0.95" Orientation="Horizontal" Background="White">
<Label x:Name="lb_port" FontFamily="SourceHanSansCN-Normal" Content="端口:" Foreground="Black" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<TextBox x:Name="port" FontFamily="SourceHanSansCN-Normal" Text="20777" Width="150" Foreground="Black" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center" TextChanged="port_TextChanged"></TextBox>
</StackPanel>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="82" Orientation="Vertical" Background="Transparent" Margin="100,3,100,0">
<Label FontFamily="SourceHanSansCN-Normal" Content="透明度调节:鼠标滚轮" Foreground="Black" FontSize="25" HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
<Label FontFamily="SourceHanSansCN-Normal" Content="大小调节:Ctrl+鼠标滚轮" Foreground="Black" FontSize="25" HorizontalAlignment="Left" VerticalAlignment="Center"></Label>
Expand Down
19 changes: 14 additions & 5 deletions WpfApp1/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -41,10 +41,10 @@ public MainWindow()
{
//Task.Run(() =>
//{
if (Helper.CheckUpdate())
{
Dispatcher.Invoke(new WindowDelegate(TipShow));
}
if (Helper.CheckUpdate())
{
Dispatcher.Invoke(new WindowDelegate(TipShow));
}
//});
});
}
Expand Down Expand Up @@ -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;
}

}
}
}
2 changes: 1 addition & 1 deletion WpfApp1/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
[assembly: AssemblyVersion("2.5.1.1")]
[assembly: AssemblyVersion("2.5.1.2")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit b311a4e

Please sign in to comment.