mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-23 16:19:37 -05:00
Improves TCP Stream reader to read messages with correct size
This commit is contained in:
parent
dff16d8f0d
commit
c5e4c4eade
1 changed files with 18 additions and 16 deletions
|
|
@ -2,27 +2,29 @@ function Read-IcingaTCPStream()
|
||||||
{
|
{
|
||||||
param(
|
param(
|
||||||
[System.Net.Sockets.TcpClient]$Client = @{},
|
[System.Net.Sockets.TcpClient]$Client = @{},
|
||||||
[System.Net.Security.SslStream]$Stream = $null
|
[System.Net.Security.SslStream]$Stream = $null,
|
||||||
|
[int]$ReadLength = 0
|
||||||
);
|
);
|
||||||
|
|
||||||
# Get the maxium size of our buffer
|
if ($ReadLength -eq 0) {
|
||||||
[byte[]]$bytes = New-Object byte[] $Client.ReceiveBufferSize;
|
$ReadLength = $Client.ReceiveBufferSize;
|
||||||
# Read the content of our SSL stream
|
|
||||||
$Stream.Read($bytes, 0, $Client.ReceiveBufferSize);
|
|
||||||
|
|
||||||
# Now ready the actual content size of the received message
|
|
||||||
[int]$count = 0;
|
|
||||||
for ($i=0; $i -le $Client.ReceiveBufferSize; $i++) {
|
|
||||||
if ($bytes[$i] -ne 0) {
|
|
||||||
$count = $count + 1;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($null -eq $Stream) {
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get the maxium size of our buffer
|
||||||
|
[byte[]]$bytes = New-Object byte[] $ReadLength;
|
||||||
|
|
||||||
|
# Read the content of our SSL stream
|
||||||
|
$MessgeSize = $Stream.Read($bytes, 0, $ReadLength);
|
||||||
|
|
||||||
|
Write-IcingaDebugMessage -Message 'Network Stream message size and content in bytes' -Objects $MessgeSize, $bytes;
|
||||||
|
|
||||||
# Resize our array to the correct size
|
# Resize our array to the correct size
|
||||||
[byte[]]$resized = New-Object byte[] $count;
|
[byte[]]$resized = New-Object byte[] $MessgeSize;
|
||||||
[array]::Copy($bytes, 0, $resized, 0, $count);
|
[array]::Copy($bytes, 0, $resized, 0, $MessgeSize);
|
||||||
|
|
||||||
# Return our message content
|
# Return our message content
|
||||||
return [System.Text.Encoding]::UTF8.GetString($resized);
|
return [System.Text.Encoding]::UTF8.GetString($resized);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue