<# 参数外部互操作入口:调用者提供固定提交的 lib60870-C 源码,脚本不下载依赖。 分别构建本项目子站和外部库主站,在 ACK 关闭/开启两种模式下运行相同报文用例。 本脚本独立于默认构建;客户端核对线格式与读回值,子站日志补充实际收尾计数。 #> param( [Parameter(Mandatory=$true)][string]$Lib60870Root, [string]$Compiler = 'gcc' ) $ErrorActionPreference = 'Stop' $project = Split-Path -Parent $PSScriptRoot $out = Join-Path $project '.build/parameter-interop' $external = (Resolve-Path -LiteralPath $Lib60870Root).Path $revision = & git -C $external rev-parse HEAD if ($LASTEXITCODE -ne 0 -or $revision -ne '7a388e3e133999e1ca77ba7521d55d074b7cd2bc') { throw 'Supply lib60870-C at commit 7a388e3e133999e1ca77ba7521d55d074b7cd2bc' } if (Test-Path -LiteralPath (Join-Path $external 'lib60870-C/CMakeLists.txt')) { $external = Join-Path $external 'lib60870-C' } New-Item -ItemType Directory -Path $out -Force | Out-Null # Windows PowerShell 重定向日志时会把原生命令 stderr 提示包装为 ErrorRecord; # CMake 成功时也可能报告可选 TLS 缺失,因此这里以原生退出码判定构建成败。 function Invoke-CMake([string[]]$Arguments) { $savedPreference = $ErrorActionPreference try { $ErrorActionPreference = 'Continue' & cmake @Arguments $code = $LASTEXITCODE } finally { $ErrorActionPreference = $savedPreference } if ($code -ne 0) { throw "External CMake failed with exit code $code" } } Invoke-CMake @('-S', $external, '-B', (Join-Path $out 'lib60870-build'), '-G', 'MinGW Makefiles', '-DBUILD_EXAMPLES=OFF', '-DBUILD_TESTS=OFF', '-DCMAKE_BUILD_TYPE=Release') Invoke-CMake @('--build', (Join-Path $out 'lib60870-build'), '-j', '4') # 子站仍持有输出文件时也可读取端口公告和最终证据,明确使用 UTF-8 解码。 function Read-LiveLog([string]$Path) { $stream = [IO.File]::Open($Path, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite) $reader = New-Object IO.StreamReader($stream, [Text.Encoding]::UTF8) try { return $reader.ReadToEnd() } finally { $reader.Dispose() } } Push-Location $project try { $sources = @(rg --files 104 common -g '*.c' | Where-Object { $_ -notmatch 'IEC104\.c$|IEC104_Log\.c$|IEC104_Link_Master_StartStop\.c$' }) $server = Join-Path $out 'parameter-server.exe' $client = Join-Path $out 'parameter-lib60870-client.exe' & $Compiler -std=c11 -O2 -Wall -Wextra -Werror -I104 -I104/business -Icommon test/parameter_interop_slave.c @sources -lws2_32 -o $server if ($LASTEXITCODE -ne 0) { throw 'Parameter test station build failed' } & $Compiler -std=c11 -O2 -Wall -Wextra -Werror "-I$external/src/inc/api" "-I$external/src/hal/inc" test/parameter_lib60870_client.c (Join-Path $out 'lib60870-build/src/liblib60870.a') -lws2_32 -o $client if ($LASTEXITCODE -ne 0) { throw 'External parameter master build failed' } # 两轮各启一个新子站,运行值和暂存从相同初值开始,客户端不依赖模式分支。 foreach ($ack in @(0, 1)) { $stdout = Join-Path $out "server-$ack.stdout.txt" $stderr = Join-Path $out "server-$ack.stderr.txt" $process = Start-Process -FilePath $server -ArgumentList @('0', "$ack", '12000') -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdout -RedirectStandardError $stderr # 保持原生句柄打开,即使子进程先退出,Windows PowerShell 仍能读取 ExitCode。 $null = $process.Handle try { $port = $null for ($n = 0; $n -lt 100 -and -not $port; $n++) { if ($process.HasExited) { throw "Parameter server exited early: $stderr" } if (Test-Path -LiteralPath $stdout) { $log = Read-LiveLog $stdout if ($log -match 'PORT (\d+) ACK') { $port = $Matches[1] } } if (-not $port) { Start-Sleep -Milliseconds 50 } } if (-not $port) { throw 'Parameter server did not announce its port' } Write-Output "Testing external master, ACK mode $ack, port $port" & $client $port if ($LASTEXITCODE -ne 0) { throw "Parameter interoperability failed (ACK=$ack)" } if (-not $process.WaitForExit(20000)) { throw 'Parameter server did not finish' } $process.Refresh() if ($process.ExitCode -ne 0) { throw "Parameter server failed (exit=$($process.ExitCode)): $stderr" } $log = Read-LiveLog $stdout # 主站收到肯定确认不等同于从站已执行 COMMIT;退出统计提供收尾侧证据。 if ($log -notmatch 'PARAMETER committed=(\d+) aborted=(\d+)') { throw 'Missing server completion evidence' } if ($ack -eq 1 -and [int]$Matches[1] -lt 12) { throw 'ACK mode did not commit the expected preparations' } Write-Output $log } finally { if (-not $process.HasExited) { Stop-Process -Id $process.Id -Force } $process.Dispose() } } } finally { Pop-Location }