Skip to content

Commit fbb85bc

Browse files
committed
Refact
1 parent 263edcb commit fbb85bc

File tree

2 files changed

+40
-45
lines changed

2 files changed

+40
-45
lines changed

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public RemotePythonExecutionService(IServiceProvider serviceProvider)
6969

7070
Subscribe();
7171
mTcpServer.Start();
72-
mLogger.LogInformation("Server runing on {ip}:{port}", Ip, Port);
72+
mLogger.LogInformation("Servers runing on {ip}:{port}", Ip, Port);
7373
}
7474

7575
#endregion
@@ -94,6 +94,7 @@ private void UnSubscribe()
9494
mTcpServer.Events.ClientDisconnected -= ClientDisconnected;
9595
mTcpServer.Events.MessageReceived -= MessageReceived;
9696
mTcpServer.Events.ExceptionEncountered -= ExceptionEncountered;
97+
9798
AppDomain.CurrentDomain.ProcessExit -= AppProcessExit;
9899
}
99100

@@ -109,35 +110,31 @@ private void OnChangeSettings(AppSettings settings, string arg2)
109110

110111
private void ClientConnected(object sender, ConnectionEventArgs e)
111112
{
112-
mLogger.LogInformation("Client connected. Connection id: {Guid}", e.Client.Guid);
113113
var ip = e.Client.IpPort.Split(':').FirstOrDefault();
114114
mCurrentClientParam = (e.Client.Guid, ip);
115115

116116
IsOutputEnded = false;
117-
IsProcessEnded = false;
117+
mLogger.LogInformation("Client connected. Connection id: {Guid}. Client ip: {ip}", e.Client.Guid, ip);
118118
}
119119

120120
private void ClientDisconnected(object sender, DisconnectionEventArgs e)
121121
{
122122
mLogger.LogInformation("Client disconnected. Connection id: {Guid}", e.Client.Guid);
123123

124124
KillProcess();
125-
IsOutputEnded = true;
126125
}
127126

128127
private void ExceptionEncountered(object sender, ExceptionEventArgs e)
129128
{
130129
if (e.Exception is IOException)
131-
return;
130+
mLogger.LogError("IOException");
132131

133132
/*if (e.Exception is SocketException)
134-
return;
133+
return;*/
135134

136135
if (e.Exception is OperationCanceledException)
137-
{
138136
mLogger.LogError("OperationCanceledException");
139-
return;
140-
}*/
137+
141138

142139
mLogger.LogError("Error happened {errorMessage}", e.Exception);
143140
}
@@ -186,7 +183,7 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
186183
mLogger.LogInformation("Exit by client request");
187184

188185
KillProcess();
189-
mIsOutputEnded = true;
186+
//IsOutputEnded = true;
190187
break;
191188
}
192189
}
@@ -199,12 +196,11 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
199196
{
200197
try
201198
{
202-
var data = Encoding.UTF8.GetBytes(e.Data);
203-
204-
mUdpEndpoint.SendAsync(mCurrentClientParam.ip, 19000, data);
199+
//var data = Encoding.UTF8.GetBytes(e.Data);
200+
mUdpEndpoint.SendAsync(mCurrentClientParam.ip, Port, e.Data);
205201

206202
mLogger.LogDebug("{data}", e.Data);
207-
IsOutputEnded = false;
203+
208204
}
209205
catch (Exception exp)
210206
{
@@ -220,13 +216,13 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
220216
{
221217
try
222218
{
223-
var data = Encoding.UTF8.GetBytes(e.Data);
224-
mUdpEndpoint.SendAsync(mCurrentClientParam.ip, 19000, data);
219+
//var data = Encoding.UTF8.GetBytes(e.Data);
220+
mUdpEndpoint.SendAsync(mCurrentClientParam.ip, Port, e.Data);
225221

226222

227223
mLogger.LogDebug("{data}", e.Data);
228224
}
229-
catch (TaskCanceledException)
225+
/*catch (TaskCanceledException)
230226
{
231227
mLogger.LogError("Task was canceled");
232228
IsOutputEnded = true;
@@ -238,7 +234,7 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
238234
catch (OperationCanceledException)
239235
{
240236
mLogger.LogError("Operation canceled");
241-
}
237+
}*/
242238
catch (Exception exp)
243239
{
244240
mLogger.LogError("Catch happened {exp}", exp);
@@ -247,16 +243,15 @@ private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
247243
}
248244
else
249245
{
250-
mLogger.LogDebug("Output ended happened");
246+
mLogger.LogInformation("Output ended happened");
251247
//await Task.Delay(100);
252248
IsOutputEnded = true;
253249
}
254250
}
255251

256252
private void ProcessExited(object sender, EventArgs e)
257253
{
258-
mLogger.LogDebug("Process ended happened");
259-
IsProcessEnded = true;
254+
mLogger.LogInformation("Process ended happened");
260255
}
261256

262257
private void AppProcessExit(object sender, EventArgs e)
@@ -285,13 +280,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
285280
{
286281
while (!stoppingToken.IsCancellationRequested)
287282
{
288-
while (IsOutputEnded && IsProcessEnded)
283+
while (IsOutputEnded)
284+
//while (IsOutputEnded && IsProcessEnded)
289285
{
290286
IsOutputEnded = false;
291-
IsProcessEnded = false;
287+
//IsProcessEnded = false;
292288

293-
/*if (mProcess == null)
294-
return;*/
289+
//if (mProcess == null)
290+
// return;
295291

296292
ExitData exitData = new();
297293

@@ -311,8 +307,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
311307
ExitCode = mProcess.ExitCode
312308
};
313309

314-
315-
//mProcess.Dispose();
310+
mProcess.Dispose();
316311
//mProcess = null;
317312
}
318313
}
@@ -326,6 +321,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
326321

327322
if (mTcpServer.IsClientConnected(mCurrentClientParam.Guid))
328323
{
324+
mLogger.LogInformation("Send exit data");
329325
await mTcpServer.SendAsync(mCurrentClientParam.Guid, string.Empty, exitDictonary, token: stoppingToken);
330326
await mTcpServer.DisconnectClientAsync(mCurrentClientParam.Guid, MessageStatus.Removed, true, stoppingToken);
331327
}
@@ -336,12 +332,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
336332
}
337333
}
338334

339-
public override Task StopAsync(CancellationToken cancellationToken)
340-
{
341-
mLogger.LogError("Host stopped");
342-
343-
return base.StopAsync(cancellationToken);
344-
}
345335
#endregion
346336

347337
#region Private fields
@@ -358,7 +348,7 @@ private bool IsOutputEnded
358348
}
359349
}
360350

361-
private bool IsProcessEnded
351+
/*private bool IsProcessEnded
362352
{
363353
get { return mIsProcessEnded; }
364354
set
@@ -368,7 +358,7 @@ private bool IsProcessEnded
368358
369359
mIsProcessEnded = value;
370360
}
371-
}
361+
}*/
372362

373363
private string InterpreterPath
374364
{
@@ -416,7 +406,6 @@ private string SourceCodeSavePath
416406
mSourceCodeSavePath = value;
417407
mLogger.LogDebug("New path for {name} register with values {value}", nameof(SourceCodeSavePath), SourceCodeSavePath);
418408
}
419-
420409
}
421410

422411
private string mIp = string.Empty;
@@ -464,9 +453,8 @@ private int Port
464453
#region Private methods
465454

466455
private void KillProcess()
467-
{
468-
if(mProcess == null)
469-
return;
456+
{
457+
IsOutputEnded = true;
470458

471459
try
472460
{
@@ -477,6 +465,7 @@ private void KillProcess()
477465
mProcess.CancelOutputRead();
478466

479467
mProcess.Kill(entireProcessTree: true);
468+
mLogger.LogInformation("KillProcess(): {id},", mProcess.Id);
480469
}
481470
catch (Exception ex)
482471
{
@@ -494,17 +483,23 @@ private void OnServerAddressChange()
494483
if(mTcpServer.Connections != 0)
495484
{
496485
KillProcess();
497-
IsOutputEnded = true;
498486
}
499487

500488
mTcpServer.Stop();
489+
490+
mTcpServer.Dispose();
491+
mUdpEndpoint.Dispose();
492+
501493
UnSubscribe();
502494
}
503495

504496
mTcpServer = new WatsonTcpServer(Ip, Port);
497+
mUdpEndpoint = new UdpEndpoint(Ip, Port);
498+
505499
Subscribe();
506500
mTcpServer.Start();
507-
mLogger.LogInformation("Server runing on {ip}:{port}", Ip, Port);
501+
502+
mLogger.LogInformation("Servers runing on {ip}:{port}", Ip, Port);
508503
}
509504

510505
private void SetPath(AppSettings appSettings)
@@ -561,7 +556,7 @@ private void StartProcess(bool withDebug = false)
561556
mProcess = new()
562557
{
563558
StartInfo = proccesInfo,
564-
EnableRaisingEvents = true,
559+
EnableRaisingEvents = true
565560
};
566561

567562
mProcess.Exited += ProcessExited;
@@ -571,7 +566,7 @@ private void StartProcess(bool withDebug = false)
571566

572567
mProcess.BeginOutputReadLine();
573568
mProcess.BeginErrorReadLine();
574-
569+
575570
mProcess.WaitForExit();
576571
}
577572

@@ -584,7 +579,7 @@ protected virtual void Dispose(bool disposing)
584579
if (disposing)
585580
{
586581
KillProcess();
587-
IsOutputEnded = true;
582+
//sOutputEnded = true;
588583

589584
UnSubscribe();
590585
mTcpServer.Stop();

src/RemotePythonExecution/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Serilog": {
33
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
44
"MinimumLevel": {
5-
"Default": "Debug",
5+
"Default": "Information",
66
"Override": {
77
"Microsoft": "Warning",
88
"System": "Warning"

0 commit comments

Comments
 (0)