Repository navigation
Expand file tree
/
Copy pathRead-ModuleWebSocket.ps1
More file actions
118 lines (97 loc) · 5.79 KB
/
Copy pathRead-ModuleWebSocket.ps1
File metadata and controls
118 lines (97 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<#
.SYNOPSIS
Read messages from an open WebSocket and enqueue them for processing.
.DESCRIPTION
`Read-ModuleWebSocket` continuously reads messages from the active
`ClientWebSocket`, converts the received bytes into UTF8 strings and
pushes complete messages to the configured reader worker queue for the
connection name. It updates thread processing state and handles
exceptions by emitting structured module return values.
.PARAMETER Name
| Name | Type | Description | Default |
|------|---------------|------------------------------------------|---------|
| Name | System.String | Logical name of the WebSocket connection | |
.PARAMETER ClientWebSocket
| Name | Type | Description | Default |
|-----------------|---------------------------------------|--------------------------------------|---------|
| ClientWebSocket | System.Net.WebSockets.ClientWebSocket | The active client WebSocket instance | |
.PARAMETER CancellationTokenSource
| Name | Type | Description | Default |
|-------------------------|------------------------------------------|---------------------------------------------|---------|
| CancellationTokenSource | System.Threading.CancellationTokenSource | Token source used to cancel read operations | |
.OUTPUTS
System.Object -- Emits structured `Write-FunctionReturnValue` values on errors;
otherwise received messages are placed into the named reader worker queue for
downstream processing.
.EXAMPLE
PS> Read-ModuleWebSocket -Name 'MyWebSocket' -ClientWebSocket $ws -CancellationTokenSource $cts
.NOTES
This function is designed to run inside a dedicated worker thread spawned by
`Start-ModuleWebSocketHandler`. It stores connection references via
`Update-ModuleWebSocketThread` into `$Global:ModuleName.Private.WebSocket` and
reads from the `ClientWebSocket` in a blocking loop. Received messages are
placed in the `ReaderWorker` BlockingCollection under
`$Global:ModuleName.Public.WebSocket.<Name>`. The loop runs until
`Test-ThreadRunning -CheckWebSocket` returns `$false`.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Read-ModuleWebSocket()
{
param (
[string]$Name = $null,
[System.Net.WebSockets.ClientWebSocket]$ClientWebSocket = $null,
[System.Threading.CancellationTokenSource]$CancellationTokenSource = $null
);
$GlobalVariable = Get-GlobalRuntimeVariable;
if ((Update-ModuleWebSocketThread -Name $Name -ClientWebSocket $ClientWebSocket -CancellationTokenSource $CancellationTokenSource) -ne $GlobalVariable.Public.ReturnCodes.WebSocketUpdatedSuccessfully.Code) {
return;
}
[string]$WebSocketName = $GlobalVariable.Private.WebSocket.Name;
$WebSocketBuffer = [Net.WebSockets.WebSocket]::CreateClientBuffer(2048, 2048);
# If the WebSocket is not open, we don't need to proceed
while (Test-ThreadRunning -CheckWebSocket) {
try {
$ResponseMsgBuilder = [System.Text.StringBuilder]::new();
[bool]$EndOfMessage = $false;
# Loop this to ensure we receive the full message, as WebSocket messages can be fragmented and we need to wait until we received the full message before we can process it
do {
$RequestProgress = $GlobalVariable.Private.WebSocket.ClientWebSocket.ReceiveAsync(
$WebSocketBuffer,
$GlobalVariable.Private.WebSocket.CancellationTokenSource.Token
);
# Wait for the read to complete, also this waits for a message to be available
While ((Test-ModuleWebSocket) -And $RequestProgress.IsCompleted -eq $FALSE) {
Start-Sleep -Milliseconds 10;
}
if (-not $RequestProgress.IsCompleted -or $RequestProgress.IsFaulted -or $RequestProgress.IsCanceled) {
break;
}
# Set this thread as processing to avoid being killed by the thread manager, once we received a message
Set-ThreadProcessing;
$ResponseMsgBuilder.Append([System.Text.Encoding]::UTF8.GetString($WebSocketBuffer, 0, $RequestProgress.Result.Count)) | Out-Null;
[bool]$EndOfMessage = $RequestProgress.Result.EndOfMessage;
} while (-not $EndOfMessage -and (Test-ModuleWebSocket));
$RequestProgress.Dispose();
$RequestProgress = $null;
# Log the received message for debugging purposes
[string]$ResponseMsg = $ResponseMsgBuilder.ToString();
Write-LoggingEntry -Message 'Received WebSocket message' -Data @{ 'Name' = $WebSocketName; 'Message' = $ResponseMsg; } -Severity 'Debug' | Out-Null;
# Push the received message to the reader worker queue
$GlobalVariable.Public.WebSocket.$WebSocketName.ReaderWorker.Add($ResponseMsg) | Out-Null;
} catch {
Write-FunctionReturnValue -ReturnValue 'WebSocketReadError' -Data @{ 'Name' = $WebSocketName; 'Exception' = $_.Exception } | Out-Null;
} finally {
$WebSocketBuffer = $null;
$ResponseMsgBuilder = $null;
$ResponseMsg = $null;
# Clear our request progress which would otherwise cause a memory leak as in case we
# cannot establish a connection to the remote server, this function is called frequently
# and will then cause endless request progress states to be created
if ($null -ne $RequestProgress) {
$RequestProgress.Dispose();
$RequestProgress = $null;
}
}
}
}