Skip to content

Commit 28ecc62

Browse files
committed
🚧 Ipc
1 parent 4f6b4cf commit 28ecc62

File tree

5 files changed

+38
-190
lines changed

5 files changed

+38
-190
lines changed

src/BD.Common8.Bcl/IOPath/IOPath.Directory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public static bool DirTryDelete(string dirPath, bool noRecursive = false)
6565
Directory.Delete(dirPath, !noRecursive);
6666
return true;
6767
}
68+
catch (DirectoryNotFoundException)
69+
{
70+
return true;
71+
}
6872
#if DEBUG
6973
#pragma warning disable CS0168 // 声明了变量,但从未使用过
7074
catch (Exception ex)

src/BD.Common8.Bcl/IOPath/IOPath.FileSystem.cs

Lines changed: 1 addition & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string CacheDirectory
4949

5050
#if !NETFRAMEWORK
5151

52-
static readonly object lock_GetCacheFilePath = new();
52+
static readonly System.Threading.Lock lock_GetCacheFilePath = new();
5353

5454
/// <summary>
5555
/// 根据缓存子文件夹名称与文件扩展名获取一个缓存文件路径
@@ -82,18 +82,6 @@ public static string GetCacheFilePath(string dirName, string fileNamePrefix, str
8282
}
8383
}
8484

85-
/// <summary>
86-
/// 尝试延时一段时间后删除文件
87-
/// </summary>
88-
/// <param name="filePath">要删除的文件路径</param>
89-
/// <param name="millisecondsDelay">延时等待的毫秒数</param>
90-
[Obsolete("use TryDeleteInDelayAsync")]
91-
public static async void TryDeleteInDelay(string filePath, int millisecondsDelay = 9000)
92-
{
93-
await Task.Delay(millisecondsDelay);
94-
FileTryDelete(filePath);
95-
}
96-
9785
/// <summary>
9886
/// 尝试延时一段时间后删除文件
9987
/// </summary>
@@ -105,35 +93,6 @@ public static async Task TryDeleteInDelayAsync(string filePath, int milliseconds
10593
FileTryDelete(filePath);
10694
}
10795

108-
/// <summary>
109-
/// 启动进程后尝试延时一段时间后删除文件
110-
/// </summary>
111-
/// <param name="process">启动的进程</param>
112-
/// <param name="filePath">要删除的文件路径</param>
113-
/// <param name="millisecondsDelay">延时等待的毫秒数</param>
114-
/// <param name="processWaitMillisecondsDelay">启动的进程等待退出的毫秒数</param>
115-
[Obsolete("use TryDeleteInDelayAsync")]
116-
public static void TryDeleteInDelay(Process? process, string filePath, int millisecondsDelay = 9000, int processWaitMillisecondsDelay = 9000)
117-
{
118-
if (process != null)
119-
{
120-
var waitForExitResult = process.TryWaitForExit(processWaitMillisecondsDelay);
121-
if (!waitForExitResult)
122-
{
123-
try
124-
{
125-
process.KillEntireProcessTree();
126-
}
127-
catch
128-
{
129-
}
130-
TryDeleteInDelay(filePath, millisecondsDelay);
131-
return;
132-
}
133-
}
134-
FileTryDelete(filePath);
135-
}
136-
13796
/// <summary>
13897
/// 启动进程后尝试延时一段时间后删除文件
13998
/// </summary>
@@ -196,140 +155,6 @@ protected static void InitFileSystem(Func<string> getAppDataDirectory, Func<stri
196155
IOPath.getAppDataDirectory = getAppDataDirectory;
197156
IOPath.getCacheDirectory = getCacheDirectory;
198157
}
199-
200-
#if !NETFRAMEWORK
201-
/// <summary>
202-
/// 带迁移的初始化文件系统,使用 <see cref="Directory.Move(string, string)"/> 或 xcopy 进行移动,如果迁移失败则回退源目录
203-
/// </summary>
204-
/// <param name="destAppDataPath">新的 AppData 文件夹路径</param>
205-
/// <param name="destCachePath">新的 Cache 文件夹路径</param>
206-
/// <param name="sourceAppDataPath">旧的 AppData 文件夹路径</param>
207-
/// <param name="sourceCachePath">旧的 Cache 文件夹路径</param>
208-
protected static void InitFileSystemWithMigrations(
209-
string destAppDataPath, string destCachePath,
210-
string sourceAppDataPath, string sourceCachePath)
211-
{
212-
bool ExistsNotEmptyDir(string path)
213-
{
214-
var exists = Directory.Exists(path);
215-
if (OSHelper.IsPublishToStore)
216-
{
217-
if (path == destCachePath || path == sourceCachePath)
218-
{
219-
return false;
220-
}
221-
}
222-
return exists && Directory.EnumerateFileSystemEntries(path).Any(); // 文件夹存在且不为空文件夹
223-
}
224-
225-
var paths = new[] { destAppDataPath, destCachePath, };
226-
var dict_paths = paths.ToDictionary(x => x, x => ExistsNotEmptyDir(x));
227-
228-
if (dict_paths.Values.All(x => !x))
229-
{
230-
var old_paths = new[] { sourceAppDataPath, sourceCachePath, };
231-
if (old_paths.All(x => Directory.Exists(x) && Directory.EnumerateFileSystemEntries(x).Any())) // 迁移之前根目录上的文件夹
232-
{
233-
var isNotFirst = false;
234-
for (int i = 0; i < old_paths.Length; i++)
235-
{
236-
var path = paths[i];
237-
var old_path = old_paths[i];
238-
try
239-
{
240-
if (!isNotFirst)
241-
{
242-
try
243-
{
244-
// 尝试搜索之前版本的进程将其结束
245-
var currentProcess = Process.GetCurrentProcess();
246-
var query = from x in Process.GetProcessesByName(currentProcess.ProcessName)
247-
where x != currentProcess
248-
let m = x.TryGetMainModule()
249-
where m != null && m.FileName != currentProcess.TryGetMainModule()?.FileName
250-
select x;
251-
var process = query.ToArray();
252-
foreach (var proces in process)
253-
{
254-
try
255-
{
256-
#if NETCOREAPP3_0_OR_GREATER
257-
proces.Kill(true);
258-
#else
259-
proces.Kill();
260-
#endif
261-
}
262-
catch
263-
{
264-
}
265-
}
266-
}
267-
catch
268-
{
269-
}
270-
isNotFirst = true;
271-
}
272-
MoveDirectory(old_path, path);
273-
dict_paths[path] = true;
274-
}
275-
catch
276-
{
277-
if (!OSHelper.IsPublishToStore)
278-
{
279-
// 跨卷移动失败或其他原因失败,使用旧的目录,并尝试删除创建的空文件夹
280-
DirTryDelete(path);
281-
}
282-
paths[i] = old_path;
283-
}
284-
}
285-
}
286-
}
287-
288-
foreach (var item in dict_paths)
289-
{
290-
if (!item.Value)
291-
{
292-
Directory.CreateDirectory(item.Key);
293-
}
294-
}
295-
296-
InitFileSystem(GetAppDataDirectory, GetCacheDirectory);
297-
string GetAppDataDirectory() => paths[0];
298-
string GetCacheDirectory() => paths[1];
299-
}
300-
#endif
301-
302-
/// <summary>
303-
/// 初始化文件系统,但优先使用旧目录上的文件夹,如果存在的话(允许空文件夹),不会进行文件迁移
304-
/// </summary>
305-
/// <param name="destAppDataPath">新的 AppData 文件夹路径</param>
306-
/// <param name="destCachePath">新的 Cache 文件夹路径</param>
307-
/// <param name="sourceAppDataPath">旧的 AppData 文件夹路径</param>
308-
/// <param name="sourceCachePath">旧的 Cache 文件夹路径</param>
309-
protected static void InitFileSystemUseDestFirst(
310-
string destAppDataPath, string destCachePath,
311-
string sourceAppDataPath, string sourceCachePath)
312-
{
313-
var paths = new[] { destAppDataPath, destCachePath, };
314-
var old_paths = new[] { sourceAppDataPath, sourceCachePath, };
315-
316-
for (int i = 0; i < old_paths.Length; i++)
317-
{
318-
var item = old_paths[i];
319-
if (Directory.Exists(item))
320-
{
321-
paths[i] = item;
322-
}
323-
else
324-
{
325-
DirCreateByNotExists(paths[i]);
326-
}
327-
}
328-
329-
InitFileSystem(GetAppDataDirectory, GetCacheDirectory);
330-
string GetAppDataDirectory() => paths[0];
331-
string GetCacheDirectory() => paths[1];
332-
}
333158
}
334159

335160
/// <summary>

src/BD.Common8.Http.ClientFactory/Services/SerializableService.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,33 @@ public virtual SystemTextJsonSerializerOptions UseJsonSerializerOptions
203203
/// <returns></returns>
204204
protected virtual ApiRspCode GetApiRspCodeByClientException(Exception ex)
205205
{
206-
if (ex is HttpRequestException && ex.InnerException is SocketException socketException)
206+
if (ex is HttpRequestException reqEx)
207207
{
208-
switch (socketException.SocketErrorCode)
208+
if (ex.InnerException is SocketException socketException)
209209
{
210-
case SocketError.TimedOut:
211-
return ApiRspCode.Timeout;
212-
case SocketError.ConnectionRefused:
213-
{
214-
// System.Net.Http.HttpRequestException: 由于目标计算机积极拒绝,无法连接。 (localhost:443)
215-
// ---> System.Net.Sockets.SocketException (10061): 由于目标计算机积极拒绝,无法连接。
216-
// at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
217-
return ApiRspCode.ConnectionRefused;
218-
}
210+
switch (socketException.SocketErrorCode)
211+
{
212+
case SocketError.TimedOut:
213+
return ApiRspCode.Timeout;
214+
case SocketError.ConnectionRefused:
215+
{
216+
// System.Net.Http.HttpRequestException: 由于目标计算机积极拒绝,无法连接。 (localhost:443)
217+
// ---> System.Net.Sockets.SocketException (10061): 由于目标计算机积极拒绝,无法连接。
218+
// at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
219+
return ApiRspCode.ConnectionRefused;
220+
}
221+
}
219222
}
223+
#if !NETFRAMEWORK
224+
else if (reqEx.StatusCode.HasValue)
225+
{
226+
var statusCode = reqEx.StatusCode.Value;
227+
if (!(((int)statusCode >= 200) && ((int)statusCode <= 299)))
228+
{
229+
return (ApiRspCode)statusCode;
230+
}
231+
}
232+
#endif
220233
}
221234

222235
if (ex is TaskCanceledException && ex.InnerException is TimeoutException)

src/BD.Common8.Ipc.Client/Services/Implementation/IpcClientService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ protected virtual async ValueTask DisposeAsyncCore()
246246
ApiRspCode apiRspCode = default;
247247
if (hubConnection == null || hubConnection.State != HubConnectionState.Connected)
248248
{
249-
apiRspCode = ApiRspCode.Timeout;
249+
apiRspCode = GetApiRspCodeByClientException(ex);
250+
if (apiRspCode == ApiRspCode.ClientException)
251+
{
252+
apiRspCode = ApiRspCode.Timeout;
253+
}
250254
}
251255

252256
var apiRspBase = GetApiRspBase<TResponseBody>(typeResponseBody);

src/BD.Common8.Ipc.Server/Services/Implementation/IpcServerService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void Build()
9999
{
100100
var builder = WebApplication.CreateEmptyBuilder(new WebApplicationOptions());
101101
builder.WebHost.UseKestrelCore();
102+
builder.WebHost.UseKestrelHttpsConfiguration();
102103
builder.WebHost.ConfigureKestrel(options =>
103104
{
104105
if (ListenLocalhost)
@@ -437,6 +438,7 @@ protected virtual void ConfigureHub(HttpConnectionDispatcherOptions options)
437438
public HubEndpointConventionBuilder MapHub<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods)] THub>([StringSyntax("Route")] string hubUrl) where THub : Hub
438439
{
439440
hubTypes.Add(hubUrl, typeof(THub));
441+
//Console.WriteLine($"hubUrl: {hubUrl}, THub: {typeof(THub)}");
440442
return app!.MapHub<THub>(hubUrl, ConfigureHub);
441443
}
442444

@@ -457,8 +459,8 @@ protected virtual async Task OnError(HttpContext ctx)
457459
protected virtual void OnError(IExceptionHandlerFeature exceptionHandlerPathFeature)
458460
{
459461
#if DEBUG
460-
Console.WriteLine("OnError: ");
461-
Console.WriteLine(exceptionHandlerPathFeature?.Error);
462+
Console.Error.WriteLine("OnError: ");
463+
Console.Error.WriteLine(exceptionHandlerPathFeature?.Error);
462464
#endif
463465
}
464466

0 commit comments

Comments
 (0)