@@ -27,6 +27,8 @@ class Program
2727
2828 static BinaryWriter _binaryWriter ;
2929
30+ static Client _client ;
31+
3032 static FileStream _fileStream ;
3133
3234 static Thread _fileWriteThread ;
@@ -102,7 +104,7 @@ static void Main(string[] args)
102104 {
103105 ParseArgs ( args ) ;
104106
105- using ( var client = new Client ( _tibiaDirectory ) )
107+ using ( _client = new Client ( _tibiaDirectory ) )
106108 {
107109 var utcNow = DateTime . UtcNow ;
108110 var filename = $ "{ utcNow . Day } _{ utcNow . Month } _{ utcNow . Year } __{ utcNow . Hour } _{ utcNow . Minute } _{ utcNow . Second } .oxr";
@@ -112,30 +114,25 @@ static void Main(string[] args)
112114 Directory . CreateDirectory ( recordingDirectory ) ;
113115 }
114116
117+ Console . CancelKeyPress += Console_CancelKeyPress ;
118+
115119 _fileStream = new FileStream ( Path . Combine ( recordingDirectory , filename ) , FileMode . Append ) ;
116120 _binaryWriter = new BinaryWriter ( _fileStream ) ;
117121
118- _binaryWriter . Write ( client . Version ) ;
119-
120- client . Logger . Level = _logLevel ;
121- client . Logger . Output = _logOutput ;
122-
123- client . Connection . OnReceivedClientMessage += Proxy_OnReceivedClientMessage ;
124- client . Connection . OnReceivedServerMessage += Proxy_OnReceivedServerMessage ;
122+ _binaryWriter . Write ( _client . Version ) ;
125123
126- // Disable packet parsing as we only care about the raw, decrypted packets and speed.
127- client . Connection . IsClientPacketParsingEnabled = false ;
128- client . Connection . IsServerPacketParsingEnabled = false ;
129- client . StartConnection ( httpPort : _httpPort , loginWebService : _loginWebService ) ;
124+ _client . Logger . Level = _logLevel ;
125+ _client . Logger . Output = _logOutput ;
130126
131- while ( Console . ReadLine ( ) != "quit" )
132- {
133- }
127+ _client . Connection . OnReceivedClientMessage += Proxy_OnReceivedClientMessage ;
128+ _client . Connection . OnReceivedServerMessage += Proxy_OnReceivedServerMessage ;
134129
135- client . StopConnection ( ) ;
130+ // Disable packet parsing as we only care about the raw, decrypted packets, and speed.
131+ _client . Connection . IsClientPacketParsingEnabled = false ;
132+ _client . Connection . IsServerPacketParsingEnabled = false ;
133+ _client . StartConnection ( httpPort : _httpPort , loginWebService : _loginWebService ) ;
136134
137- client . Connection . OnReceivedClientMessage -= Proxy_OnReceivedClientMessage ;
138- client . Connection . OnReceivedServerMessage -= Proxy_OnReceivedServerMessage ;
135+ while ( Console . ReadLine ( ) != "quit" ) { }
139136 }
140137 }
141138 catch ( Exception ex )
@@ -144,28 +141,46 @@ static void Main(string[] args)
144141 }
145142 finally
146143 {
147- if ( _fileWriteThread != null )
148- {
149- // Block the application from shutting down until the file-write thread
150- // finishes writing all incoming packets to disk. This is safe to do as
151- // the proxy connection will have been stopped, no matter what, by now.
152- _fileWriteThread . Join ( ) ;
153- }
144+ Shutdown ( ) ;
145+ }
146+ }
154147
155- if ( _binaryWriter != null )
156- {
157- _binaryWriter . Close ( ) ;
158- }
148+ private static void Console_CancelKeyPress ( object sender , ConsoleCancelEventArgs e )
149+ {
150+ Shutdown ( ) ;
151+ }
159152
160- if ( _fileStream != null )
161- {
162- _fileStream . Close ( ) ;
163- }
153+ private static void Shutdown ( )
154+ {
155+ if ( _client != null )
156+ {
157+ _client . StopConnection ( ) ;
164158
165- if ( _stopWatch . IsRunning )
166- {
167- _stopWatch . Stop ( ) ;
168- }
159+ _client . Connection . OnReceivedClientMessage -= Proxy_OnReceivedClientMessage ;
160+ _client . Connection . OnReceivedServerMessage -= Proxy_OnReceivedServerMessage ;
161+ }
162+
163+ if ( _fileWriteThread != null )
164+ {
165+ // Block the application from shutting down until the file-write thread
166+ // finishes writing all incoming packets to disk. This is safe to do as
167+ // the proxy connection will have been stopped, no matter what, by now.
168+ _fileWriteThread . Join ( ) ;
169+ }
170+
171+ if ( _binaryWriter != null )
172+ {
173+ _binaryWriter . Close ( ) ;
174+ }
175+
176+ if ( _fileStream != null )
177+ {
178+ _fileStream . Close ( ) ;
179+ }
180+
181+ if ( _stopWatch . IsRunning )
182+ {
183+ _stopWatch . Stop ( ) ;
169184 }
170185 }
171186
0 commit comments