Skip to content

SignalR features: expose WebSocket handshake state and send buffer for external hosts - #336

Open
sergiovision wants to merge 2 commits into
chronoxor:mainfrom
sergiovision:feature/public-websocket-engine-state
Open

SignalR features: expose WebSocket handshake state and send buffer for external hosts#336
sergiovision wants to merge 2 commits into
chronoxor:mainfrom
sergiovision:feature/public-websocket-engine-state

Conversation

@sergiovision

@sergiovision sergiovision commented Sep 11, 2026

Copy link
Copy Markdown

Summary

WebSocket is already a self-contained handshake + framing engine (PerformServerUpgrade,
PrepareReceiveFrame, PrepareSendFrame) that WsSession/WssSession drive over their own
socket. Three members it needs are internal, which prevents reusing the engine over a stream owned
by a different HTTP host — for example an ASP.NET Core / Kestrel upgraded connection that serves
SignalR-style hub paths (/hub/...) on the same port as a REST API, so no second WebSocket port has
to be opened and proxied.

This change also introduces significant performance and reliability optimizations for WebSocket framing, masking, and buffer management.

Changes

  1. Expose WebSocket Engine Members for External Hosts:

    • WebSocket.WsHandshaked — handshake state, set by PerformServerUpgrade / PerformClientUpgrade (internalpublic)
    • WebSocket.WsSendLock — lock that guards PrepareSendFrame + reading the send buffer (internalpublic)
    • WebSocket.WsSendBuffer — the framed output produced by PrepareSendFrame (internalpublic)
  2. WebSocket Performance & Reliability Improvements:

    • Vector/32-bit chunked XOR masking & unmasking in WebSocket.MaskBuffer for fast payload processing.
    • Fast direct memory copy path for unmasked server-to-client frames (buffer.CopyTo).
    • Batch byte reading in PrepareReceiveFrame header parsing, replacing byte-by-byte loops.
    • Optimized Sec-WebSocket-Accept handshake validation using direct base64 comparison against computed SHA1 digest.
    • Avoid repeated string allocations by reusing static WsGuid constant.
  3. Buffer & HTTP Parsing Optimizations:

    • Optimized Buffer.Reserve with a 256-byte minimum allocation floor to reduce reallocation churn on small messages.
    • Safe null/empty span and array handling in Buffer.Append.
    • Safe BodyBytes copy in HttpRequest and HttpResponse.

Test plan

  • Existing WsSession/WssSession/WsClient/WssClient call sites are unchanged.
  • All WebSocket frame preparation, unmasking, and framing tests pass.

Sergei Zhuravlev added 2 commits September 11, 2026 11:17
…nagement

- Use 32-bit chunked XOR in MaskBuffer for fast masking/unmasking
- Direct memory copy for unmasked server-to-client frames
- Batch append bytes during frame header parsing instead of byte-by-byte loops
- Fast base64 validation for Sec-WebSocket-Accept handshake header
- Optimized Buffer.Reserve with minimum allocation floor to prevent realloc churn
- Add null/empty checks for span and array appends in Buffer
- Safe BodyBytes copy in HttpRequest and HttpResponse
- Bump project version to 10.0.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant