icinga-powershell-framework/lib/webserver/Read-IcingaTCPStream.psm1

32 lines
918 B
PowerShell
Raw Normal View History

2020-03-24 07:42:14 -04:00
function Read-IcingaTCPStream()
{
param(
[System.Net.Sockets.TcpClient]$Client = @{},
[System.Net.Security.SslStream]$Stream = $null,
[int]$ReadLength = 0
2020-03-24 07:42:14 -04:00
);
if ($ReadLength -eq 0) {
$ReadLength = $Client.ReceiveBufferSize;
}
if ($null -eq $Stream) {
return $null;
}
2020-03-24 07:42:14 -04:00
# Get the maxium size of our buffer
[byte[]]$bytes = New-Object byte[] $ReadLength;
2020-03-24 07:42:14 -04:00
# Read the content of our SSL stream
$MessgeSize = $Stream.Read($bytes, 0, $ReadLength);
2020-03-24 07:42:14 -04:00
Write-IcingaDebugMessage -Message 'Network Stream message size and content in bytes' -Objects $MessgeSize, $bytes;
2020-03-24 07:42:14 -04:00
# Resize our array to the correct size
[byte[]]$resized = New-Object byte[] $MessgeSize;
[array]::Copy($bytes, 0, $resized, 0, $MessgeSize);
2020-03-24 07:42:14 -04:00
# Return our message content
return [System.Text.Encoding]::UTF8.GetString($resized);
}