Skip to content

Commit 561facb

Browse files
committed
info
1 parent 6e4459e commit 561facb

File tree

2 files changed

+148
-21
lines changed

2 files changed

+148
-21
lines changed

NetDebug/MainWindow.xaml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:MeowType.NetDebug"
77
mc:Ignorable="d"
8-
Title="Net Debug" Height="450" Width="800" MinHeight="450" MinWidth="800">
8+
Title="Net Debug" Height="450" Width="800" MinHeight="450" MinWidth="800" FontFamily="Consolas, Fira Code">
99
<Grid Margin="5">
1010
<Grid.ColumnDefinitions>
1111
<ColumnDefinition MinWidth="200" Width="0.5*" MaxWidth="300"></ColumnDefinition>
@@ -56,13 +56,13 @@
5656
</Grid.RowDefinitions>
5757
<RadioButton Name="UDP_type_defalut" GroupName="UDP_type" IsChecked="True">Default</RadioButton>
5858
<RadioButton Name="UDP_type_broadcast" GroupName="UDP_type" Grid.Row="1" Checked="UDP_type_broadcast_Checked" Unchecked="UDP_type_broadcast_UnChecked">Broadcast</RadioButton>
59-
<RadioButton Name="UDP_type_multicast" GroupName="UDP_type" Grid.Row="2">Multicast</RadioButton>
60-
<ComboBox Grid.Row="3" IsEditable="True" Name="UDP_multicast_ip" SelectedIndex="0" Margin="20,0,0,0">
59+
<RadioButton Name="UDP_type_multicast" GroupName="UDP_type" Grid.Row="2" Checked="UDP_type_multicast_Checked" Unchecked="UDP_type_multicast_Unchecked">Multicast</RadioButton>
60+
<ComboBox Grid.Row="3" IsEditable="True" Name="UDP_multicast_ip" SelectedIndex="0" Loaded="UDP_Target_ip_Loaded" Margin="20,0,0,0">
6161
<Label>224.0.0.0</Label>
6262
<Label>224.0.0.1</Label>
6363
</ComboBox>
6464
</Grid>
65-
<Button Name="UDP_button" Grid.Row="3" Margin="20,5" Click="Button_Click" FontSize="16">Open</Button>
65+
<Button Name="UDP_button" Grid.Row="3" Margin="20,5" Click="Open_Button_Click" FontSize="16">Open</Button>
6666
<Grid Grid.Row="4">
6767
<Grid.RowDefinitions>
6868
<RowDefinition Height="1*"></RowDefinition>
@@ -94,14 +94,23 @@
9494
<RowDefinition Height="1*"></RowDefinition>
9595
<RowDefinition Height="60"></RowDefinition>
9696
</Grid.RowDefinitions>
97-
<RichTextBox Grid.Row="0" Name="MsgBox" Margin="10,0,0,0" FontFamily="Consolas, Fira Code Light"/>
97+
<RichTextBox Grid.Row="0" Name="MsgBox" Margin="10,0,0,0" VerticalScrollBarVisibility="Auto" FontFamily="Consolas, Fira Code Light" IsReadOnly="True" Loaded="MsgBox_Loaded"/>
9898
<Grid Grid.Row="1" Margin="10,5,0,0">
9999
<Grid.ColumnDefinitions>
100+
<ColumnDefinition Width="50"></ColumnDefinition>
100101
<ColumnDefinition Width="1*"></ColumnDefinition>
101102
<ColumnDefinition Width="0.25*" MinWidth="50" MaxWidth="120"></ColumnDefinition>
102103
</Grid.ColumnDefinitions>
103-
<RichTextBox Name="Send_Msg" Grid.Column="0" Margin="0,0,5,0" Keyboard.KeyUp="Send_Msg_KeyUp"/>
104-
<Button Name="Send_Button" Grid.Column="1" Click="Send_Button_Click" IsEnabled="False">Send</Button>
104+
<Grid Grid.Column="0">
105+
<Grid.RowDefinitions>
106+
<RowDefinition Height="1*"></RowDefinition>
107+
<RowDefinition Height="1*"></RowDefinition>
108+
</Grid.RowDefinitions>
109+
<Button Grid.Row="0" Click="Clear_Button_Click" Margin="0,0,0,2.5">Clear↑</Button>
110+
<Button Grid.Row="1" Click="Clear_Send_Button_Click" Margin="0,2.5,0,0">Clear→</Button>
111+
</Grid>
112+
<RichTextBox Name="Send_Msg" Grid.Column="1" Margin="5,0" Keyboard.KeyUp="Send_Msg_KeyUp"/>
113+
<Button Name="Send_Button" Grid.Column="2" Click="Send_Button_Click" IsEnabled="False" FontSize="20">Send</Button>
105114
</Grid>
106115
</Grid>
107116

NetDebug/MainWindow.xaml.cs

Lines changed: 132 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void SetEnables()
4545
UDP_type_defalut.IsEnabled = false;
4646
UDP_type_broadcast.IsEnabled = false;
4747
UDP_type_multicast.IsEnabled = false;
48+
UDP_multicast_ip.IsEnabled = false;
4849

4950
Send_Button.IsEnabled = true;
5051
}
@@ -63,6 +64,7 @@ void SetEnables()
6364
UDP_type_defalut.IsEnabled = true;
6465
UDP_type_broadcast.IsEnabled = true;
6566
UDP_type_multicast.IsEnabled = true;
67+
UDP_multicast_ip.IsEnabled = true;
6668

6769
Send_Button.IsEnabled = false;
6870
}
@@ -74,6 +76,7 @@ IPAddress ParseIp(string str)
7476
{
7577
switch (str)
7678
{
79+
case "loopback":
7780
case "localhost": return IPAddress.Loopback;
7881
case "broadcast": return IPAddress.Broadcast;
7982
default: return IPAddress.Any;
@@ -82,6 +85,78 @@ IPAddress ParseIp(string str)
8285
return ip;
8386
}
8487

88+
void LogSystem(string msg)
89+
{
90+
Dispatcher.Invoke(() =>
91+
{
92+
MsgBox.Document.Blocks.Add(new Paragraph()
93+
{
94+
Inlines = {
95+
new Run($"[ {DateTime.Now.ToString("zzz yyyy.MM.dd tt hh:mm:ss:fff")} ] [Info]"){
96+
Foreground = new SolidColorBrush(Colors.DarkGray),
97+
Typography =
98+
{
99+
StylisticAlternates = 1,
100+
}
101+
},
102+
new LineBreak(),
103+
new Run(msg){
104+
Typography =
105+
{
106+
StylisticAlternates = 1,
107+
}
108+
},
109+
},
110+
Typography =
111+
{
112+
StandardLigatures = true,
113+
ContextualLigatures = true,
114+
DiscretionaryLigatures = true,
115+
HistoricalLigatures = true,
116+
ContextualAlternates = true,
117+
},
118+
});
119+
120+
MsgBox.ScrollToEnd();
121+
});
122+
}
123+
124+
void LogError(Exception ex)
125+
{
126+
Dispatcher.Invoke(() =>
127+
{
128+
MsgBox.Document.Blocks.Add(new Paragraph()
129+
{
130+
Inlines = {
131+
new Run($"[ {DateTime.Now.ToString("zzz yyyy.MM.dd tt hh:mm:ss:fff")} ] [Error]"){
132+
Foreground = new SolidColorBrush(Colors.DarkRed),
133+
Typography =
134+
{
135+
StylisticAlternates = 1,
136+
}
137+
},
138+
new LineBreak(),
139+
new Run(ex.ToString()){
140+
Foreground = new SolidColorBrush(Colors.Red),
141+
Typography =
142+
{
143+
StylisticAlternates = 1,
144+
}
145+
},
146+
},
147+
Typography =
148+
{
149+
StandardLigatures = true,
150+
ContextualLigatures = true,
151+
DiscretionaryLigatures = true,
152+
HistoricalLigatures = true,
153+
ContextualAlternates = true,
154+
},
155+
});
156+
157+
MsgBox.ScrollToEnd();
158+
});
159+
}
85160
void Log(EndPoint ep, string msg, bool from = false)
86161
{
87162
msg = msg.Replace("\0", string.Empty);
@@ -100,7 +175,7 @@ void Log(EndPoint ep, string msg, bool from = false)
100175
MsgBox.Document.Blocks.Add(new Paragraph()
101176
{
102177
Inlines = {
103-
new Run($"[{DateTime.Now.ToString("zzz yyyy.MM.dd tt hh:mm:ss:fff")} ]"){
178+
new Run($"[ {DateTime.Now.ToString("zzz yyyy.MM.dd tt hh:mm:ss:fff")} ]"){
104179
Foreground = new SolidColorBrush(Colors.DarkGray),
105180
Typography =
106181
{
@@ -138,7 +213,9 @@ void Log(EndPoint ep, string msg, bool from = false)
138213
HistoricalLigatures = true,
139214
ContextualAlternates = true,
140215
},
141-
}) ;
216+
});
217+
218+
MsgBox.ScrollToEnd();
142219
});
143220

144221
}
@@ -152,7 +229,7 @@ enum NowType
152229
Socket socket;
153230
CancellationTokenSource loop;
154231
List<Task> loops = new List<Task>();
155-
private void Button_Click(object sender, RoutedEventArgs e)
232+
private void Open_Button_Click(object sender, RoutedEventArgs e)
156233
{
157234
if (socket != null)
158235
{
@@ -194,11 +271,27 @@ private void Button_Click(object sender, RoutedEventArgs e)
194271
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
195272
socket.Bind(new IPEndPoint(ip, port));
196273

274+
LogSystem($"Open on {{ {ip} :{port} }}");
275+
197276
SetEnables();
198277
nowType = NowType.Udp;
199278

200279
socket.EnableBroadcast = UDP_type_broadcast.IsChecked ?? false;
201280

281+
if(UDP_type_broadcast.IsChecked ?? false)
282+
{
283+
LogSystem($"Enabled Broadcast on {{ {ParseIp(UDP_Target_ip.Text)} }}");
284+
}
285+
286+
if (UDP_type_multicast.IsChecked ?? false)
287+
{
288+
var mip = ParseIp(UDP_multicast_ip.Text);
289+
var opt = new MulticastOption(mip, ip);
290+
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, opt);
291+
292+
LogSystem($"Join Multicast Group {{ {mip} }}");
293+
}
294+
202295
loop = new CancellationTokenSource();
203296
loops.Add(Task.Run(() =>
204297
{
@@ -217,15 +310,22 @@ private void Button_Click(object sender, RoutedEventArgs e)
217310
}
218311
catch (Exception ex)
219312
{
220-
MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
313+
if(socket == null)
314+
{
315+
LogSystem("Closed");
316+
}
317+
else
318+
{
319+
LogError(ex);
320+
}
221321
return;
222322
}
223323
}
224324
}, loop.Token));
225325
}
226326
catch (Exception ex)
227327
{
228-
MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
328+
LogError(ex);
229329
return;
230330
}
231331
}
@@ -250,19 +350,12 @@ private void Send_Button_Click(object sender, RoutedEventArgs e)
250350
try
251351
{
252352
var @byte = Encoding.Default.GetBytes(new TextRange(Send_Msg.Document.ContentStart, Send_Msg.Document.ContentEnd).Text);
253-
if(UDP_type_broadcast.IsChecked ?? false)
254-
{
255-
socket.SendTo(@byte, @byte.Length, SocketFlags.Broadcast, new IPEndPoint(ip, port));
256-
}
257-
else
258-
{
259-
socket.SendTo(@byte, @byte.Length, SocketFlags.None, new IPEndPoint(ip, port));
260-
}
353+
socket.SendTo(@byte, new IPEndPoint(ip, port));
261354
Log(new IPEndPoint(ip, port), new TextRange(Send_Msg.Document.ContentStart, Send_Msg.Document.ContentEnd).Text);
262355
}
263356
catch (Exception ex)
264357
{
265-
MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
358+
LogError(ex);
266359
return;
267360
}
268361
}
@@ -287,5 +380,30 @@ private void Send_Msg_KeyUp(object sender, KeyEventArgs e)
287380
{
288381
if (e.Key == Key.Enter) Send_Button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
289382
}
383+
384+
private void UDP_type_multicast_Checked(object sender, RoutedEventArgs e)
385+
{
386+
UDP_Target_ip.IsEnabled = false;
387+
}
388+
389+
private void UDP_type_multicast_Unchecked(object sender, RoutedEventArgs e)
390+
{
391+
UDP_Target_ip.IsEnabled = true;
392+
}
393+
394+
private void Clear_Button_Click(object sender, RoutedEventArgs e)
395+
{
396+
MsgBox.Document.Blocks.Clear();
397+
}
398+
399+
private void MsgBox_Loaded(object sender, RoutedEventArgs e)
400+
{
401+
MsgBox.Document.Blocks.Clear();
402+
}
403+
404+
private void Clear_Send_Button_Click(object sender, RoutedEventArgs e)
405+
{
406+
Send_Msg.Document.Blocks.Clear();
407+
}
290408
}
291409
}

0 commit comments

Comments
 (0)