Skip to content

Commit 6cd3e20

Browse files
committed
Close #10
1 parent fbb85bc commit 6cd3e20

File tree

1 file changed

+15
-55
lines changed

1 file changed

+15
-55
lines changed

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
using System.Diagnostics;
1010
using System.IO;
1111
using System.Linq;
12-
using System.Net.Sockets;
1312
using System.Runtime.InteropServices;
1413
using System.Text;
1514
using System.Threading;
1615
using System.Threading.Tasks;
1716
using WatsonTcp;
18-
using static System.Runtime.InteropServices.JavaScript.JSType;
1917

2018
namespace RemotePythonExecution.Services
2119
{
@@ -33,7 +31,6 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
3331

3432
private WatsonTcpServer mTcpServer;
3533
private Process mProcess;
36-
//private Guid CurrentConnectionGuid;
3734
private UdpEndpoint mUdpEndpoint;
3835
private (Guid Guid, string ip) mCurrentClientParam;
3936

@@ -43,7 +40,6 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
4340
private string mWorkingDirrectoryPath = string.Empty;
4441
private string mSourceCodeSavePath = string.Empty;
4542

46-
private bool mIsProcessEnded = false;
4743
private bool mIsOutputEnded = false;
4844

4945

@@ -120,21 +116,23 @@ private void ClientConnected(object sender, ConnectionEventArgs e)
120116
private void ClientDisconnected(object sender, DisconnectionEventArgs e)
121117
{
122118
mLogger.LogInformation("Client disconnected. Connection id: {Guid}", e.Client.Guid);
123-
124119
KillProcess();
125120
}
126121

127122
private void ExceptionEncountered(object sender, ExceptionEventArgs e)
128123
{
129124
if (e.Exception is IOException)
125+
{
130126
mLogger.LogError("IOException");
131-
132-
/*if (e.Exception is SocketException)
133-
return;*/
127+
return;
128+
}
129+
134130

135131
if (e.Exception is OperationCanceledException)
132+
{
136133
mLogger.LogError("OperationCanceledException");
137-
134+
return;
135+
}
138136

139137
mLogger.LogError("Error happened {errorMessage}", e.Exception);
140138
}
@@ -183,7 +181,6 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
183181
mLogger.LogInformation("Exit by client request");
184182

185183
KillProcess();
186-
//IsOutputEnded = true;
187184
break;
188185
}
189186
}
@@ -196,9 +193,7 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
196193
{
197194
try
198195
{
199-
//var data = Encoding.UTF8.GetBytes(e.Data);
200196
mUdpEndpoint.SendAsync(mCurrentClientParam.ip, Port, e.Data);
201-
202197
mLogger.LogDebug("{data}", e.Data);
203198

204199
}
@@ -216,25 +211,11 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
216211
{
217212
try
218213
{
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);
221216

222-
223217
mLogger.LogDebug("{data}", e.Data);
224218
}
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-
}*/
238219
catch (Exception exp)
239220
{
240221
mLogger.LogError("Catch happened {exp}", exp);
@@ -244,7 +225,6 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
244225
else
245226
{
246227
mLogger.LogInformation("Output ended happened");
247-
//await Task.Delay(100);
248228
IsOutputEnded = true;
249229
}
250230
}
@@ -281,14 +261,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
281261
while (!stoppingToken.IsCancellationRequested)
282262
{
283263
while (IsOutputEnded)
284-
//while (IsOutputEnded && IsProcessEnded)
285264
{
286265
IsOutputEnded = false;
287-
//IsProcessEnded = false;
288-
289-
//if (mProcess == null)
290-
// return;
291-
292266
ExitData exitData = new();
293267

294268
try
@@ -308,7 +282,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
308282
};
309283

310284
mProcess.Dispose();
311-
//mProcess = null;
312285
}
313286
}
314287
catch(Exception exception)
@@ -348,18 +321,6 @@ private bool IsOutputEnded
348321
}
349322
}
350323

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-
363324
private string InterpreterPath
364325
{
365326
get { return mInterpreterPath; }
@@ -454,18 +415,19 @@ private int Port
454415

455416
private void KillProcess()
456417
{
457-
IsOutputEnded = true;
458-
459418
try
460419
{
461-
if (mProcess.HasExited)
462-
return;
463-
464420
mProcess.CancelErrorRead();
465421
mProcess.CancelOutputRead();
466422

467423
mProcess.Kill(entireProcessTree: true);
468424
mLogger.LogInformation("KillProcess(): {id},", mProcess.Id);
425+
426+
IsOutputEnded = true;
427+
}
428+
catch (InvalidOperationException)
429+
{
430+
mLogger.LogError("KillProcess(): InvalidOperationException happened");
469431
}
470432
catch (Exception ex)
471433
{
@@ -579,8 +541,6 @@ protected virtual void Dispose(bool disposing)
579541
if (disposing)
580542
{
581543
KillProcess();
582-
//sOutputEnded = true;
583-
584544
UnSubscribe();
585545
mTcpServer.Stop();
586546
mTcpServer.Dispose();

0 commit comments

Comments
 (0)