4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
6
using System . Web ;
7
+ using log4net ;
7
8
using Microsoft . AspNetCore . Hosting ;
8
9
using Toastify . Common ;
9
10
using Toastify . Threading ;
@@ -14,9 +15,11 @@ namespace Toastify.Core.Auth
14
15
{
15
16
public class AuthHttpServer : IAuthHttpServer , IDisposable
16
17
{
18
+ private static readonly ILog logger = LogManager . GetLogger ( typeof ( AuthHttpServer ) ) ;
19
+
17
20
private readonly IWebHost webHost ;
18
21
private readonly NamedPipeServerStream pipe ;
19
- private readonly CancellationTokenSource cts ;
22
+ private CancellationTokenSource cts ;
20
23
21
24
private Thread receiveThread ;
22
25
@@ -40,13 +43,21 @@ public AuthHttpServer()
40
43
. UseStartup < AuthHttpServerStartup > ( )
41
44
. UseUrls ( url )
42
45
. Build ( ) ;
43
-
44
- this . cts = new CancellationTokenSource ( ) ;
45
46
}
46
47
47
48
public async Task Start ( )
48
49
{
49
- await this . webHost . StartAsync ( this . cts . Token ) . ConfigureAwait ( false ) ;
50
+ this . cts ? . Cancel ( ) ;
51
+ this . cts = new CancellationTokenSource ( ) ;
52
+
53
+ try
54
+ {
55
+ await this . webHost . StartAsync ( this . cts . Token ) . ConfigureAwait ( false ) ;
56
+ }
57
+ catch
58
+ {
59
+ // ignore
60
+ }
50
61
51
62
this . receiveThread = ThreadManager . Instance . CreateThread ( this . ReceiveThread ) ;
52
63
this . receiveThread . IsBackground = true ;
@@ -58,15 +69,18 @@ public Task Stop()
58
69
{
59
70
return this . webHost . StopAsync ( this . cts . Token ) ;
60
71
}
61
-
72
+
62
73
private async void ReceiveThread ( )
63
74
{
64
75
try
65
76
{
77
+ logger . Debug ( $ "{ nameof ( this . ReceiveThread ) } started ({ this . receiveThread . Name } )") ;
78
+
66
79
await this . pipe . WaitForConnectionAsync ( this . cts . Token ) . ConfigureAwait ( false ) ;
67
80
if ( this . cts . IsCancellationRequested )
68
81
return ;
69
82
83
+ logger . Debug ( $ "[{ nameof ( this . ReceiveThread ) } ] Pipe connection established!") ;
70
84
StringStream ss = new StringStream ( this . pipe ) ;
71
85
string responseString = ss . ReadString ( ) ;
72
86
var response = HttpUtility . ParseQueryString ( responseString ) ;
@@ -80,11 +94,13 @@ private async void ReceiveThread()
80
94
finally
81
95
{
82
96
this . pipe . Close ( ) ;
97
+ logger . Debug ( $ "{ nameof ( this . ReceiveThread ) } ended!") ;
83
98
}
84
99
}
85
100
86
101
private void OnAuthorizationFinished ( string code , string state , string error )
87
102
{
103
+ logger . Debug ( $ "Authorization finished! Error: \" { error } \" ") ;
88
104
this . AuthorizationFinished ? . Invoke ( this , new AuthEventArgs ( code , state , error ) ) ;
89
105
}
90
106
@@ -99,7 +115,7 @@ public void Dispose(TimeSpan timeout)
99
115
{
100
116
try
101
117
{
102
- this . cts . Cancel ( ) ;
118
+ this . cts ? . Cancel ( ) ;
103
119
this . receiveThread . Join ( timeout ) ;
104
120
}
105
121
catch
@@ -110,7 +126,7 @@ public void Dispose(TimeSpan timeout)
110
126
this . pipe ? . Dispose ( ) ;
111
127
this . webHost ? . Dispose ( ) ;
112
128
113
- this . cts . Dispose ( ) ;
129
+ this . cts ? . Dispose ( ) ;
114
130
}
115
131
116
132
#endregion
0 commit comments