1- using Codeplex . Data ;
2- using System ;
1+ using System ;
32using System . Collections . Generic ;
43using System . Configuration ;
54using System . Data ;
65using System . IO ;
76using System . Linq ;
87using System . Net ;
98using System . Net . Sockets ;
9+ using System . Runtime . Serialization ;
10+ using System . Runtime . Serialization . Json ;
1011using System . Text ;
1112using System . Threading . Tasks ;
1213using System . Windows ;
@@ -29,15 +30,18 @@ protected override void OnStartup(StartupEventArgs e)
2930
3031 Task . Run ( ( ) => {
3132 var data = new StreamReader ( "./settings.json" ) . ReadToEnd ( ) ;
32- var json = DynamicJson . Parse ( data ) ;
33- int port = ( int ) json . port ;
34- IPEndPoint ep = new IPEndPoint ( IPAddress . Any , port ) ;
35- var client = new UdpClient ( port ) ;
36- while ( true )
33+ DataContractJsonSerializer serializer = new DataContractJsonSerializer ( typeof ( SettingData ) ) ;
34+ using ( MemoryStream ms = new MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) )
3735 {
38- var buff = client . Receive ( ref ep ) ;
39- var text = Encoding . UTF8 . GetString ( buff ) ;
40- component . notify ( text ) ;
36+ SettingData setting = ( SettingData ) serializer . ReadObject ( ms ) ;
37+ IPEndPoint ep = new IPEndPoint ( IPAddress . Any , setting . port ) ;
38+ var client = new UdpClient ( setting . port ) ;
39+ while ( true )
40+ {
41+ var buff = client . Receive ( ref ep ) ;
42+ var text = Encoding . UTF8 . GetString ( buff ) ;
43+ component . notify ( text ) ;
44+ }
4145 }
4246 } ) ;
4347 }
@@ -48,4 +52,11 @@ protected override void OnExit(ExitEventArgs e)
4852 component . Dispose ( ) ;
4953 }
5054 }
55+
56+ [ DataContract ]
57+ class SettingData
58+ {
59+ [ DataMember ]
60+ public int port ;
61+ }
5162}
0 commit comments