diff --git a/desktop/Services/Agent/EdgeHttpService.cs b/desktop/Services/Agent/EdgeHttpService.cs index ad61220..d8da204 100644 --- a/desktop/Services/Agent/EdgeHttpService.cs +++ b/desktop/Services/Agent/EdgeHttpService.cs @@ -173,6 +173,9 @@ public sealed class EdgeHttpService // 除 health 外都要校验主体 if (!CheckSubject(request, out var subjectError)) { + var config = AgentFiles.LoadConfig(); + AppLog.Write($"[edge] 拒绝 {request.Method} {path}:{subjectError}" + + $"(本体主体 {(config is null ? "无" : config.SubjectId.ToString())})"); await WriteJsonAsync(stream, 403, "Forbidden", new { ok = false, error = subjectError }, ct) .ConfigureAwait(false); return; @@ -368,6 +371,10 @@ public sealed class EdgeHttpService var check = await playback.CheckAsync(host, port, username, password, channelNo, start, end, ct) .ConfigureAwait(false); + AppLog.Write($"[edge] 校验 {host}:{port} 通道 {channelNo} " + + $"{start:HH:mm:ss}~{end:HH:mm:ss} → hasRecord={check.HasRecord}" + + $"({check.Source},{check.Frames} 帧{(string.IsNullOrEmpty(check.Error) ? "" : "," + check.Error)})"); + await WriteJsonAsync(stream, 200, "OK", new { ok = true, @@ -383,6 +390,7 @@ public sealed class EdgeHttpService } catch (Exception ex) { + AppLog.Write($"[edge] 校验失败 {host}:{port} 通道 {channelNo}:{ex.Message}"); await WriteJsonAsync(stream, 200, "OK", new { ok = true, diff --git a/desktop/Services/Edge/EdgeClient.cs b/desktop/Services/Edge/EdgeClient.cs index 3deb23e..9f555d8 100644 --- a/desktop/Services/Edge/EdgeClient.cs +++ b/desktop/Services/Edge/EdgeClient.cs @@ -83,17 +83,36 @@ public sealed class EdgeClient : IDisposable endTime = end.ToString("O"), }; - using var response = await SendAsync(HttpMethod.Post, "/check", payload, ct).ConfigureAwait(false); - if (!response.IsSuccessStatusCode) return null; + HttpResponseMessage response; + try + { + response = await SendAsync(HttpMethod.Post, "/check", payload, ct).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + AppLog.Write($"[edge] 校验请求失败({_baseUrl}):{ex.Message}"); + return null; + } - using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false)); - var root = doc.RootElement; + using (response) + { + var text = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - var hasRecord = root.TryGetProperty("hasRecord", out var value) - && value.ValueKind == JsonValueKind.True; - var frames = root.TryGetProperty("frames", out var f) && f.TryGetInt32(out var parsed) ? parsed : 0; + if (!response.IsSuccessStatusCode) + { + AppLog.Write($"[edge] 校验请求被拒({(int)response.StatusCode} {response.StatusCode}):{Trim(text)}"); + return null; + } - return new EdgeCheckResult(hasRecord, frames, GetString(root, "error"), GetString(root, "source")); + using var doc = JsonDocument.Parse(text); + var root = doc.RootElement; + + var hasRecord = root.TryGetProperty("hasRecord", out var value) + && value.ValueKind == JsonValueKind.True; + var frames = root.TryGetProperty("frames", out var f) && f.TryGetInt32(out var parsed) ? parsed : 0; + + return new EdgeCheckResult(hasRecord, frames, GetString(root, "error"), GetString(root, "source")); + } } /// 下发下载计划,返回任务 id(服务端后台执行) @@ -140,11 +159,25 @@ public sealed class EdgeClient : IDisposable /// public async Task DownloadFileAsync(string taskId, string savePath, CancellationToken ct = default) { - using var request = new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/download/{taskId}/file"); - request.Headers.TryAddWithoutValidation("X-Edge-Subject", _subjectId.ToString()); + HttpResponseMessage response = null!; + for (var attempt = 1; attempt <= 2; attempt++) + { + using var request = new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/download/{taskId}/file"); + request.Headers.TryAddWithoutValidation("X-Edge-Subject", _subjectId.ToString()); - using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct) - .ConfigureAwait(false); + try + { + response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct) + .ConfigureAwait(false); + break; + } + catch (Exception ex) when (attempt == 1 && ex is not OperationCanceledException) + { + AppLog.Write($"[edge] 取成片连接失败(重试一次):{ex.Message}"); + } + } + + using var _ = response; response.EnsureSuccessStatusCode(); var dir = Path.GetDirectoryName(savePath); @@ -167,18 +200,33 @@ public sealed class EdgeClient : IDisposable return total; } - private Task SendAsync(HttpMethod method, string path, object? payload, + /// + /// 发一个请求。**传输层失败重试一次**:worker 是子进程,重启/崩溃后连接池里会留着 + /// 已经死掉的 keep-alive 连接,第一个请求必然抛 IOException —— 现场表现就是 + /// 「设备管理服务无响应,改由本机校验」(2026-10-22 03:22 更新完就复现过)。 + /// + private async Task SendAsync(HttpMethod method, string path, object? payload, CancellationToken ct) { - var request = new HttpRequestMessage(method, _baseUrl + path); - request.Headers.TryAddWithoutValidation("X-Edge-Subject", _subjectId.ToString()); - - if (payload is not null) + for (var attempt = 1; ; attempt++) { - request.Content = JsonContent.Create(payload, options: HikWorkerJson.Options); - } + using var request = new HttpRequestMessage(method, _baseUrl + path); + request.Headers.TryAddWithoutValidation("X-Edge-Subject", _subjectId.ToString()); - return Http.SendAsync(request, ct); + if (payload is not null) + { + request.Content = JsonContent.Create(payload, options: HikWorkerJson.Options); + } + + try + { + return await Http.SendAsync(request, ct).ConfigureAwait(false); + } + catch (Exception ex) when (attempt == 1 && ex is not OperationCanceledException) + { + AppLog.Write($"[edge] {path} 连接失败(重试一次):{ex.Message}"); + } + } } private static object[] BuildCredentialPayload(IReadOnlyList credentials) @@ -204,6 +252,13 @@ public sealed class EdgeClient : IDisposable ? value.GetString() : null; + private static string Trim(string? text) + { + if (string.IsNullOrWhiteSpace(text)) return "(空)"; + var trimmed = text.Trim(); + return trimmed.Length <= 200 ? trimmed : trimmed[..200] + "…"; + } + public void Dispose() { // HttpClient 是静态共享的,这里没有需要释放的实例资源 diff --git a/desktop/YuncangTrace.LocalService.csproj b/desktop/YuncangTrace.LocalService.csproj index 5866a4e..dd2a7d9 100644 --- a/desktop/YuncangTrace.LocalService.csproj +++ b/desktop/YuncangTrace.LocalService.csproj @@ -42,7 +42,7 @@ http://127.0.0.1:7300 https://packs.example.com - 1.0.108 + 1.0.109 浜戜粨瑙嗛婧簮杞欢 浜戜粨 浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級