param( [int] $Port = 7788, [string] $Log = "$PSScriptRoot\sc_request.log" ) function Say($msg) { for ($i = 0; $i -lt 5; $i++) { try { "$msg" | Out-File -FilePath $Log -Append -Encoding utf8; return } catch { Start-Sleep -Milliseconds 100 } } } "" | Out-File -FilePath $Log -Encoding utf8 Say "[req] starting on 127.0.0.1:$Port" $listener = New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Loopback, $Port) $listener.Server.SetSocketOption('Socket', 'ReuseAddress', $true) $bound = $false for ($i = 0; $i -lt 30 -and -not $bound; $i++) { try { $listener.Start(); $bound = $true } catch { Start-Sleep -Seconds 1 } } if (-not $bound) { Say "[req] could not bind"; exit 1 } Say "[req] listening" $client = $listener.AcceptTcpClient() Say "[req] === shard connected ===" $stream = $client.GetStream() $reader = New-Object System.IO.StreamReader($stream) $writer = New-Object System.IO.StreamWriter($stream) $writer.AutoFlush = $true # Give the shard a beat to send its hello, then fire the requests. Start-Sleep -Milliseconds 500 $requests = @( '{"kind":"account.roster","reqId":"r-roster","account":"whitlocktech"}', '{"kind":"char.request","reqId":"r-darrow","account":"whitlocktech","slot":0}', '{"kind":"vendor.snapshot","reqId":"r-vendor","account":"seed_000"}', '{"kind":"char.request","reqId":"r-bad","account":"does_not_exist","slot":0}', '{"kind":"char.request","reqId":"r-serial","serial":"0x24C"}' ) foreach ($r in $requests) { $writer.WriteLine($r) Say "[req] -> $r" Start-Sleep -Milliseconds 400 } # Read replies for a few seconds. $deadline = (Get-Date).AddSeconds(8) while ((Get-Date) -lt $deadline) { if ($stream.DataAvailable) { $line = $reader.ReadLine() if ($null -ne $line) { Say "[req] <- $line" } } else { Start-Sleep -Milliseconds 100 } } Say "[req] done" $client.Close() $listener.Stop()