9
9
using System . Diagnostics ;
10
10
using System . IO ;
11
11
using System . Linq ;
12
- using System . Net . Sockets ;
13
12
using System . Runtime . InteropServices ;
14
13
using System . Text ;
15
14
using System . Threading ;
16
15
using System . Threading . Tasks ;
17
16
using WatsonTcp ;
18
- using static System . Runtime . InteropServices . JavaScript . JSType ;
19
17
20
18
namespace RemotePythonExecution . Services
21
19
{
@@ -33,7 +31,6 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
33
31
34
32
private WatsonTcpServer mTcpServer ;
35
33
private Process mProcess ;
36
- //private Guid CurrentConnectionGuid;
37
34
private UdpEndpoint mUdpEndpoint ;
38
35
private ( Guid Guid , string ip ) mCurrentClientParam ;
39
36
@@ -43,7 +40,6 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
43
40
private string mWorkingDirrectoryPath = string . Empty ;
44
41
private string mSourceCodeSavePath = string . Empty ;
45
42
46
- private bool mIsProcessEnded = false ;
47
43
private bool mIsOutputEnded = false ;
48
44
49
45
@@ -120,21 +116,23 @@ private void ClientConnected(object sender, ConnectionEventArgs e)
120
116
private void ClientDisconnected ( object sender , DisconnectionEventArgs e )
121
117
{
122
118
mLogger . LogInformation ( "Client disconnected. Connection id: {Guid}" , e . Client . Guid ) ;
123
-
124
119
KillProcess ( ) ;
125
120
}
126
121
127
122
private void ExceptionEncountered ( object sender , ExceptionEventArgs e )
128
123
{
129
124
if ( e . Exception is IOException )
125
+ {
130
126
mLogger . LogError ( "IOException" ) ;
131
-
132
- /*if (e.Exception is SocketException)
133
- return;*/
127
+ return ;
128
+ }
129
+
134
130
135
131
if ( e . Exception is OperationCanceledException )
132
+ {
136
133
mLogger . LogError ( "OperationCanceledException" ) ;
137
-
134
+ return ;
135
+ }
138
136
139
137
mLogger . LogError ( "Error happened {errorMessage}" , e . Exception ) ;
140
138
}
@@ -183,7 +181,6 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
183
181
mLogger . LogInformation ( "Exit by client request" ) ;
184
182
185
183
KillProcess ( ) ;
186
- //IsOutputEnded = true;
187
184
break ;
188
185
}
189
186
}
@@ -196,9 +193,7 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
196
193
{
197
194
try
198
195
{
199
- //var data = Encoding.UTF8.GetBytes(e.Data);
200
196
mUdpEndpoint . SendAsync ( mCurrentClientParam . ip , Port , e . Data ) ;
201
-
202
197
mLogger . LogDebug ( "{data}" , e . Data ) ;
203
198
204
199
}
@@ -216,25 +211,11 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
216
211
{
217
212
try
218
213
{
219
- //var data = Encoding.UTF8.GetBytes(e.Data);
220
- mUdpEndpoint . SendAsync ( mCurrentClientParam . ip , Port , e . Data ) ;
214
+ if ( mTcpServer . IsClientConnected ( mCurrentClientParam . Guid ) )
215
+ mUdpEndpoint . SendAsync ( mCurrentClientParam . ip , Port , e . Data ) ;
221
216
222
-
223
217
mLogger . LogDebug ( "{data}" , e . Data ) ;
224
218
}
225
- /*catch (TaskCanceledException)
226
- {
227
- mLogger.LogError("Task was canceled");
228
- IsOutputEnded = true;
229
- }
230
- catch (TimeoutException)
231
- {
232
- mLogger.LogError("Task timeout");
233
- }
234
- catch (OperationCanceledException)
235
- {
236
- mLogger.LogError("Operation canceled");
237
- }*/
238
219
catch ( Exception exp )
239
220
{
240
221
mLogger . LogError ( "Catch happened {exp}" , exp ) ;
@@ -244,7 +225,6 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
244
225
else
245
226
{
246
227
mLogger . LogInformation ( "Output ended happened" ) ;
247
- //await Task.Delay(100);
248
228
IsOutputEnded = true ;
249
229
}
250
230
}
@@ -281,14 +261,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
281
261
while ( ! stoppingToken . IsCancellationRequested )
282
262
{
283
263
while ( IsOutputEnded )
284
- //while (IsOutputEnded && IsProcessEnded)
285
264
{
286
265
IsOutputEnded = false ;
287
- //IsProcessEnded = false;
288
-
289
- //if (mProcess == null)
290
- // return;
291
-
292
266
ExitData exitData = new ( ) ;
293
267
294
268
try
@@ -308,7 +282,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
308
282
} ;
309
283
310
284
mProcess . Dispose ( ) ;
311
- //mProcess = null;
312
285
}
313
286
}
314
287
catch ( Exception exception )
@@ -348,18 +321,6 @@ private bool IsOutputEnded
348
321
}
349
322
}
350
323
351
- /*private bool IsProcessEnded
352
- {
353
- get { return mIsProcessEnded; }
354
- set
355
- {
356
- if(value == mIsProcessEnded)
357
- return;
358
-
359
- mIsProcessEnded = value;
360
- }
361
- }*/
362
-
363
324
private string InterpreterPath
364
325
{
365
326
get { return mInterpreterPath ; }
@@ -454,18 +415,19 @@ private int Port
454
415
455
416
private void KillProcess ( )
456
417
{
457
- IsOutputEnded = true ;
458
-
459
418
try
460
419
{
461
- if ( mProcess . HasExited )
462
- return ;
463
-
464
420
mProcess . CancelErrorRead ( ) ;
465
421
mProcess . CancelOutputRead ( ) ;
466
422
467
423
mProcess . Kill ( entireProcessTree : true ) ;
468
424
mLogger . LogInformation ( "KillProcess(): {id}," , mProcess . Id ) ;
425
+
426
+ IsOutputEnded = true ;
427
+ }
428
+ catch ( InvalidOperationException )
429
+ {
430
+ mLogger . LogError ( "KillProcess(): InvalidOperationException happened" ) ;
469
431
}
470
432
catch ( Exception ex )
471
433
{
@@ -579,8 +541,6 @@ protected virtual void Dispose(bool disposing)
579
541
if ( disposing )
580
542
{
581
543
KillProcess ( ) ;
582
- //sOutputEnded = true;
583
-
584
544
UnSubscribe ( ) ;
585
545
mTcpServer . Stop ( ) ;
586
546
mTcpServer . Dispose ( ) ;
0 commit comments