Skip to content

Instantly share code, notes, and snippets.

@JVital2013
Last active October 31, 2023 18:14
Show Gist options
  • Save JVital2013/a2108f546f443ecf60f6f68873fa9400 to your computer and use it in GitHub Desktop.
Save JVital2013/a2108f546f443ecf60f6f68873fa9400 to your computer and use it in GitHub Desktop.
#####
# PowerShell implementation of nanomsg inspired by sam210723 https://github.com/sam210723/goesrecv-monitor/blob/master/goesrecv-monitor/Symbols.cs
# Logs all VCIDs and their packet counters to troubleshoot drops
# Edit the 3 lines below to match your goesrecv host, port, and location to save log file
#####
$ip = "192.168.1.137"
$port = "5004"
$logfile = "allpackets.csv"
#Initialize nanomsg subscription to goesrecv
[byte[]] $nninit = 0x00, 0x53, 0x50, 0x00, 0x00, 0x21, 0x00, 0x00
#Expected goesrecv response to nanomsg subscription
[byte[]] $nnires = 0x00, 0x53, 0x50, 0x00, 0x00, 0x20, 0x00, 0x00
#Set up other variables
$socket = New-Object System.Net.Sockets.Socket -ArgumentList $([System.Net.Sockets.SocketType]::Stream), $([System.Net.Sockets.ProtocolType]::Tcp)
$res = [System.Byte[]]::CreateInstance([System.Byte], 8)
$dres = [System.Byte[]]::CreateInstance([System.Byte], 65536)
$buffer = [System.Byte[]]::CreateInstance([System.Byte], 65536)
$num = 0
$totalBytes = 0
#Connect to goesrecv SDR sample publisher
try
{
Write-Output "Connecting to goesresv host..."
$socket.Connect($ip, $port)
$socket.Send($nninit) | Out-Null
$socket.Receive($res) | Out-Null
if(-not [System.Linq.Enumerable]::SequenceEqual($nnires, $res))
{
Write-Warning "Could Not Connect to host"
$socket.Close()
exit
}
}
catch
{
Write-Warning "Could Not Connect to host"
exit
}
Write-Output "Connected to goesresv host!"
#Create log file (if specified)
if(-not [String]::IsNullOrWhiteSpace($logfile)) {Set-Content -Path $logfile -Value "VCID,Packet Counter"}
#Listen for packets forever
$bytesBeforeHeader = 0
$packetCount = 0
do {
try
{
#Recieve all available bytes
$num = $socket.Receive($dres)
$remainingBytesToWrite = $num
$startReadingAt = 0
#Loop through all the nanomsg headers
while($remainingBytesToWrite -gt $bytesBeforeHeader)
{
#Write any information before the header
if($bytesBeforeHeader -gt 0)
{
[System.Buffer]::BlockCopy($dres, $startReadingAt, $buffer, $totalBytes, $bytesBeforeHeader)
$totalBytes += $bytesBeforeHeader
}
#Get next nanomsg packet length
[System.Array]::Copy($dres, $bytesBeforeHeader + $startReadingAt, $res, 0, 8) | Out-Null
if([Bitconverter]::IsLittleEndian) {[array]::Reverse($res)}
$startReadingAt += $bytesBeforeHeader + 8
$remainingBytesToWrite = $num - $startReadingAt
$bytesBeforeHeader = [BitConverter]::ToUInt64($res, 0)
}
#No more headers in bytes we have; write the rest of the bytes
[System.Buffer]::BlockCopy($dres, $startReadingAt, $buffer, $totalBytes, $remainingBytesToWrite)
$bytesBeforeHeader -= $remainingBytesToWrite
$totalBytes += $remainingBytesToWrite
}
catch
{
Write-Warning "Error parsing packet stream"
$socket.Close()
exit
}
#Parse for current channel
$currentOffset = 0
while($totalBytes -ge 892)
{
$vcid = $buffer[$currentOffset + 1] -band 0x3f
$counter = ($buffer[$currentOffset + 2] -shl 16) -bor ($buffer[$currentOffset + 3] -shl 8) -bor $buffer[$currentOffset + 4]
Add-Content -Path $logfile -Value "$vcid,$counter"
$currentOffset += 892
$totalBytes -= 892
}
} while($num -ne 0)
#Clean Up after no packets were received from the server
Write-Warning "Connection to the server closed"
$socket.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment