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