Skip to content

Commit

Permalink
bug fix and improvement
Browse files Browse the repository at this point in the history
now HttpClient will use system default proxy settings
resolved the issue that LaunchWrapper may not return the correct exit code
  • Loading branch information
laolarou726 committed Jul 31, 2023
1 parent adc37fc commit 425acfd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 60 deletions.
41 changes: 5 additions & 36 deletions ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,15 @@ public static class HttpClientHelper

static HttpClient HttpClientFactory()
{
var handlers = new RedirectHandler(new RetryHandler(new HttpClientHandler { AllowAutoRedirect = false }));
var handlers = new RedirectHandler(new RetryHandler(new HttpClientHandler
{
AllowAutoRedirect = false,
Proxy = HttpClient.DefaultProxy
}));
var httpClient = new HttpClient(handlers);

httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Ua);

return httpClient;
}

/*
public static void Init()
{
var arr = new[] { DefaultClientName, DataClientName, HeadClientName, MultiPartClientName };
foreach (var name in arr)
ServiceHelper.ServiceCollection
.AddHttpClient(name, client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd(Ua);
if (name == MultiPartClientName)
client.DefaultRequestHeaders.ConnectionClose = false;
})
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
AllowAutoRedirect = false
})
.AddHttpMessageHandler<RedirectHandler>()
.AddHttpMessageHandler<RetryHandler>();
ServiceHelper.UpdateServiceProvider();
HttpClientFactory = ServiceHelper.ServiceProvider.GetRequiredService<IHttpClientFactory>();
}
/// <summary>
/// 获取一个HttpClient实例
/// </summary>
/// <returns></returns>
public static HttpClient GetNewClient(string name)
{
return HttpClientFactory.CreateClient(name);
}
*/
}
24 changes: 23 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/ProcessorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading;
using System;
using System.Diagnostics;
using System.Threading;

namespace ProjBobcat.Class.Helper;

Expand All @@ -21,4 +23,24 @@ public static bool SetMaxThreads()

return changeSucceeded;
}

/// <summary>
/// 尝试获取进程的退出码
/// </summary>
/// <param name="proc"></param>
/// <param name="code"></param>
/// <returns></returns>
public static bool TryGetProcessExitCode(Process proc, out int code)
{
try
{
code = proc.ExitCode;
return true;
}
catch (InvalidOperationException)
{
code = 0;
return false;
}
}
}
26 changes: 13 additions & 13 deletions ProjBobcat/ProjBobcat/Class/LaunchWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using ProjBobcat.Class.Helper;
using ProjBobcat.Class.Model;
using ProjBobcat.Class.Model.Auth;
using ProjBobcat.DefaultComponent.Launch.GameCore;
Expand All @@ -13,7 +14,7 @@ namespace ProjBobcat.Class;
/// </summary>
public class LaunchWrapper : IDisposable
{
bool disposedValue;
bool _disposedValue;

/// <summary>
/// 构造函数
Expand Down Expand Up @@ -83,16 +84,11 @@ void DisposeManaged()
Process.Exited -= ProcessOnExited;
}

void ProcessOnExited(object sender, EventArgs e)
void ProcessOnExited(object? sender, EventArgs e)
{
try
{
ExitCode = Process?.ExitCode ?? -1;
}
catch (InvalidOperationException)
{
ExitCode = -1;
}
if (Process == null) return;

ExitCode = ProcessorHelper.TryGetProcessExitCode(Process, out var code) ? code : 0;
}

void ProcessOnErrorDataReceived(object sender, DataReceivedEventArgs e)
Expand Down Expand Up @@ -150,13 +146,17 @@ void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs e)

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing) Process?.Dispose();
if (disposing)
{
DisposeManaged();
Process?.Dispose();
}

// TODO: 释放未托管的资源(未托管的对象)并重写终结器
// TODO: 将大型字段设置为 null
disposedValue = true;
_disposedValue = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ await Task.Run(() =>
OnGameExit(launchWrapper, new GameExitEventArgs
{
Exception = task.Exception,
ExitCode = launchWrapper.ExitCode
ExitCode = launchWrapper.ExitCode == 0
? ProcessorHelper.TryGetProcessExitCode(launchWrapper.Process, out var code) ? code : 0
: launchWrapper.ExitCode
});
});

Expand Down
6 changes: 1 addition & 5 deletions ProjBobcat/ProjBobcat/Handler/RedirectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public class RedirectHandler : DelegatingHandler
{
readonly int _maxRetries = 20;

public RedirectHandler()
{
}

public RedirectHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
Expand All @@ -30,7 +26,7 @@ public RedirectHandler(HttpMessageHandler innerHandler, int maxRetries) : base(i
async Task<HttpResponseMessage?> CreateRedirectResponse(HttpRequestMessage request,
HttpResponseMessage? response, CancellationToken cancellationToken)
{
var redirectUri = response?.Headers?.Location;
var redirectUri = response?.Headers.Location;

if (redirectUri == null) return null;
if (request.RequestUri == null) return null;
Expand Down
4 changes: 0 additions & 4 deletions ProjBobcat/ProjBobcat/Handler/RetryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public class RetryHandler : DelegatingHandler
{
readonly int _maxRetries = 5;

public RetryHandler()
{
}

public RetryHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
Expand Down

0 comments on commit 425acfd

Please sign in to comment.