7
7
using System . Collections . Generic ;
8
8
using System . Diagnostics ;
9
9
using System . IO ;
10
+ using System . Net . Sockets ;
10
11
using System . Runtime . InteropServices ;
11
12
using System . Threading ;
12
13
using System . Threading . Tasks ;
@@ -25,13 +26,17 @@ public class RemotePythonExecutionService : BackgroundService
25
26
26
27
#region Var
27
28
28
- private readonly WatsonTcpServer mTcpServer ;
29
+ private WatsonTcpServer mTcpServer ;
29
30
private Process mProcess ;
30
31
private bool mIsOutputEnded = false ;
31
32
private bool mIsProcessEnded = false ;
32
33
public Guid CurrentConnectionGuid ;
33
34
private bool mIsDisposed ;
34
35
36
+ private string mInterpreterPath = string . Empty ;
37
+ private string mWorkingDirrectoryPath = string . Empty ;
38
+ private string mSourceCodeSavePath = string . Empty ;
39
+
35
40
#endregion
36
41
37
42
#region ~
@@ -41,20 +46,20 @@ public RemotePythonExecutionService(IServiceProvider serviceProvider)
41
46
mLogger = serviceProvider . GetRequiredService < ILogger < RemotePythonExecutionService > > ( ) ;
42
47
mAppSettingsMonitor = serviceProvider . GetRequiredService < IOptionsMonitor < AppSettings > > ( ) ;
43
48
49
+ Ip = mAppSettingsMonitor . CurrentValue . ServerSettings . Ip ;
50
+ Port = mAppSettingsMonitor . CurrentValue . ServerSettings . Port ;
44
51
45
- string ip = mAppSettingsMonitor . CurrentValue . ServerSettings . Ip ;
46
- int port = mAppSettingsMonitor . CurrentValue . ServerSettings . Port ;
47
52
48
- mTcpServer = new WatsonTcpServer ( ip , port ) ;
53
+ SetPath ( mAppSettingsMonitor . CurrentValue ) ;
54
+ //SetServerAddress(mAppSettingsMonitor.CurrentValue);
55
+
56
+ mTcpServer = new WatsonTcpServer ( Ip , Port ) ;
49
57
mAppSettingsMonitor . OnChange ( OnChangeSettings ) ;
50
58
51
59
Subscribe ( ) ;
52
-
53
60
mTcpServer . Start ( ) ;
61
+ mLogger . LogInformation ( "Server runing on {ip}:{port}" , Ip , Port ) ;
54
62
55
- SetPath ( mAppSettingsMonitor . CurrentValue ) ;
56
-
57
- mLogger . LogInformation ( "Server runing on {ip}:{port}" , ip , port ) ;
58
63
}
59
64
60
65
#endregion
@@ -67,22 +72,27 @@ private void Subscribe()
67
72
mTcpServer . Events . ClientDisconnected += ClientDisconnected ;
68
73
mTcpServer . Events . MessageReceived += MessageReceived ;
69
74
mTcpServer . Events . ExceptionEncountered += ExceptionEncountered ;
75
+ //mTcpServer.Events.ServerStopped += ServerStopped;
70
76
}
71
77
72
78
private void UnSubscribe ( )
73
79
{
74
80
mTcpServer . Events . ClientConnected -= ClientConnected ;
75
81
mTcpServer . Events . ClientDisconnected -= ClientDisconnected ;
76
82
mTcpServer . Events . MessageReceived -= MessageReceived ;
77
- mTcpServer . Events . ExceptionEncountered -= ExceptionEncountered ;
83
+ mTcpServer . Events . ExceptionEncountered -= ExceptionEncountered ;
84
+ //mTcpServer.Events.ServerStopped -= ServerStopped;
85
+
78
86
}
79
87
80
88
#endregion
81
89
82
90
#region Events
91
+
83
92
private void OnChangeSettings ( AppSettings settings , string arg2 )
84
93
{
85
94
SetPath ( settings ) ;
95
+ SetServerAddress ( settings ) ;
86
96
}
87
97
88
98
private void ClientConnected ( object sender , ConnectionEventArgs e )
@@ -107,9 +117,18 @@ private void ExceptionEncountered(object sender, ExceptionEventArgs e)
107
117
if ( e . Exception is IOException )
108
118
return ;
109
119
120
+ if ( e . Exception is SocketException )
121
+ return ;
122
+
110
123
mLogger . LogError ( "Error happened {errorMessage}" , e . Exception ) ;
111
124
}
112
125
126
+ private void ServerStopped ( object sender , EventArgs e )
127
+ {
128
+ mIsOutputEnded = true ;
129
+ mIsProcessEnded = true ;
130
+ }
131
+
113
132
private void MessageReceived ( object sender , MessageReceivedEventArgs e )
114
133
{
115
134
Dictionary < string , object > metadata = e . Metadata ;
@@ -179,6 +198,10 @@ private async void ProcessOutputDataReceived(object sender, DataReceivedEventArg
179
198
mLogger . LogDebug ( "{data}" , e . Data ) ;
180
199
mIsOutputEnded = false ;
181
200
}
201
+ catch ( TaskCanceledException )
202
+ {
203
+ mLogger . LogError ( "Task was canceled" ) ;
204
+ }
182
205
catch ( Exception exp )
183
206
{
184
207
mLogger . LogError ( "Catch happened {exp}" , exp ) ;
@@ -244,10 +267,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
244
267
245
268
#endregion
246
269
247
-
248
270
#region Private fields
249
271
250
- private string mInterpreterPath = string . Empty ;
251
272
private string InterpreterPath
252
273
{
253
274
get { return mInterpreterPath ; }
@@ -264,7 +285,6 @@ private string InterpreterPath
264
285
}
265
286
}
266
287
267
- private string mWorkingDirrectoryPath = string . Empty ;
268
288
private string WorkingDirrectoryPath
269
289
{
270
290
get { return mWorkingDirrectoryPath ; }
@@ -280,8 +300,7 @@ private string WorkingDirrectoryPath
280
300
mLogger . LogDebug ( "New path for {name} register with values {value}" , nameof ( WorkingDirrectoryPath ) , WorkingDirrectoryPath ) ;
281
301
}
282
302
}
283
-
284
- private string mSourceCodeSavePath = string . Empty ;
303
+
285
304
private string SourceCodeSavePath
286
305
{
287
306
get { return mSourceCodeSavePath ; }
@@ -299,9 +318,76 @@ private string SourceCodeSavePath
299
318
300
319
}
301
320
321
+ private string mIp = string . Empty ;
322
+ private string Ip
323
+ {
324
+ get { return mIp ; }
325
+ set
326
+ {
327
+ if ( string . IsNullOrEmpty ( value ) )
328
+ {
329
+ mIp = "0.0.0.0" ;
330
+ return ;
331
+ }
332
+
333
+ if ( value . Equals ( mIp ) )
334
+ return ;
335
+
336
+ mIp = value ;
337
+ mLogger . LogDebug ( "New ip for server register with values {value}" , Ip ) ;
338
+
339
+ OnServerAddressChange ( ) ;
340
+ }
341
+ }
342
+
343
+
344
+ private int mPort ;
345
+
346
+ private int Port
347
+ {
348
+ get { return mPort ; }
349
+ set
350
+ {
351
+ if ( value . Equals ( mPort ) )
352
+ return ;
353
+
354
+ mPort = value ;
355
+ mLogger . LogDebug ( "New port for server register with values {value}" , Port ) ;
356
+
357
+ OnServerAddressChange ( ) ;
358
+ }
359
+ }
360
+
302
361
#endregion
362
+
303
363
#region Private methods
304
364
365
+ private void OnServerAddressChange ( )
366
+ {
367
+ if ( mTcpServer == null )
368
+ return ;
369
+
370
+ if ( mTcpServer . IsListening )
371
+ {
372
+ if ( mTcpServer . Connections != 0 )
373
+ {
374
+ mIsOutputEnded = true ;
375
+ mIsProcessEnded = true ;
376
+ //mTcpServer.DisconnectClientsAsync();
377
+ }
378
+
379
+
380
+ mTcpServer . Stop ( ) ;
381
+ UnSubscribe ( ) ;
382
+ //mTcpServer?.Dispose();
383
+ }
384
+
385
+ mTcpServer = new WatsonTcpServer ( Ip , Port ) ;
386
+ Subscribe ( ) ;
387
+ mTcpServer . Start ( ) ;
388
+ mLogger . LogInformation ( "Server runing on {ip}:{port}" , Ip , Port ) ;
389
+ }
390
+
305
391
private void SetPath ( AppSettings appSettings )
306
392
{
307
393
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -317,6 +403,12 @@ private void SetPath(AppSettings appSettings)
317
403
WorkingDirrectoryPath = appSettings . PythonPaths . WorkingDirrectoryPath . Linux ;
318
404
}
319
405
406
+ private void SetServerAddress ( AppSettings appSettings )
407
+ {
408
+ Ip = appSettings . ServerSettings . Ip ;
409
+ Port = appSettings . ServerSettings . Port ;
410
+ }
411
+
320
412
private void SaveCodeAndStartProcess ( string code , bool withDebug )
321
413
{
322
414
File . WriteAllText ( SourceCodeSavePath , code ) ;
0 commit comments