Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 094d7ca

Browse files
committed
Things
1 parent f84f7d1 commit 094d7ca

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

Toastify/src/Core/Auth/AuthHttpServer.cs

+23-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using System.Web;
7+
using log4net;
78
using Microsoft.AspNetCore.Hosting;
89
using Toastify.Common;
910
using Toastify.Threading;
@@ -14,9 +15,11 @@ namespace Toastify.Core.Auth
1415
{
1516
public class AuthHttpServer : IAuthHttpServer, IDisposable
1617
{
18+
private static readonly ILog logger = LogManager.GetLogger(typeof(AuthHttpServer));
19+
1720
private readonly IWebHost webHost;
1821
private readonly NamedPipeServerStream pipe;
19-
private readonly CancellationTokenSource cts;
22+
private CancellationTokenSource cts;
2023

2124
private Thread receiveThread;
2225

@@ -40,13 +43,21 @@ public AuthHttpServer()
4043
.UseStartup<AuthHttpServerStartup>()
4144
.UseUrls(url)
4245
.Build();
43-
44-
this.cts = new CancellationTokenSource();
4546
}
4647

4748
public async Task Start()
4849
{
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+
}
5061

5162
this.receiveThread = ThreadManager.Instance.CreateThread(this.ReceiveThread);
5263
this.receiveThread.IsBackground = true;
@@ -58,15 +69,18 @@ public Task Stop()
5869
{
5970
return this.webHost.StopAsync(this.cts.Token);
6071
}
61-
72+
6273
private async void ReceiveThread()
6374
{
6475
try
6576
{
77+
logger.Debug($"{nameof(this.ReceiveThread)} started ({this.receiveThread.Name})");
78+
6679
await this.pipe.WaitForConnectionAsync(this.cts.Token).ConfigureAwait(false);
6780
if (this.cts.IsCancellationRequested)
6881
return;
6982

83+
logger.Debug($"[{nameof(this.ReceiveThread)}] Pipe connection established!");
7084
StringStream ss = new StringStream(this.pipe);
7185
string responseString = ss.ReadString();
7286
var response = HttpUtility.ParseQueryString(responseString);
@@ -80,11 +94,13 @@ private async void ReceiveThread()
8094
finally
8195
{
8296
this.pipe.Close();
97+
logger.Debug($"{nameof(this.ReceiveThread)} ended!");
8398
}
8499
}
85100

86101
private void OnAuthorizationFinished(string code, string state, string error)
87102
{
103+
logger.Debug($"Authorization finished! Error: \"{error}\"");
88104
this.AuthorizationFinished?.Invoke(this, new AuthEventArgs(code, state, error));
89105
}
90106

@@ -99,7 +115,7 @@ public void Dispose(TimeSpan timeout)
99115
{
100116
try
101117
{
102-
this.cts.Cancel();
118+
this.cts?.Cancel();
103119
this.receiveThread.Join(timeout);
104120
}
105121
catch
@@ -110,7 +126,7 @@ public void Dispose(TimeSpan timeout)
110126
this.pipe?.Dispose();
111127
this.webHost?.Dispose();
112128

113-
this.cts.Dispose();
129+
this.cts?.Dispose();
114130
}
115131

116132
#endregion

Toastify/src/Core/Auth/AuthHttpServerStartup.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO.Pipes;
33
using System.Net;
4+
using System.Security.Policy;
45
using System.Security.Principal;
56
using System.Web;
67
using log4net;
@@ -40,6 +41,8 @@ public void ConfigureServices(IServiceCollection services)
4041
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4142
{
4243
Uri url = new Uri(this.Configuration["url"]);
44+
logger.Debug($"Configuring {nameof(AuthHttpServerStartup)}... URL: {url}");
45+
4346
app.Use(async (context, next) =>
4447
{
4548
logger.Debug($"[{nameof(AuthHttpServerStartup)}] {context.Request.Path.Value}{context.Request.QueryString.Value}");

Toastify/src/Core/Auth/ToastifyWebAuth.cs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected override void Authorize()
7979

8080
public override Task<IToken> GetToken()
8181
{
82+
this.abortAuthEvent.Reset();
8283
return this.authHttpServer == null ? Task.FromResult((IToken)null) : base.GetToken();
8384
}
8485

0 commit comments

Comments
 (0)