Skip to content

refactor(websocket): migrate TCP receive path to std::span and update protocol API #318

Description

@kcenon

Parent Issue

Part of #315 (TCP receive std::span callback migration)

Summary

Migrate WebSocket receive flow to use tcp_socket's std::span<const uint8_t> callback so WebSocket receive does not require per-TCP-read std::vector construction just to hand off bytes to the protocol layer.

Background

websocket_socket currently registers a vector-based receive callback:

  • tcp_socket_->set_receive_callback([...] (const std::vector<uint8_t>& data) { on_tcp_receive(data); }) (src/internal/websocket_socket.cpp:72)

websocket_protocol::process_data() also currently takes const std::vector<uint8_t>& (include/kcenon/network/internal/websocket_protocol.h:127).

With the new span callback in tcp_socket (#316), WebSocket should consume bytes as a view and let the protocol layer append into its internal buffer, avoiding an intermediate vector allocation on every read.

Proposed Solution

1) Update websocket_protocol API to accept a view

Change:

  • websocket_protocol::process_data(const std::vector<uint8_t>&)
    To:
  • websocket_protocol::process_data(std::span<const uint8_t>)

Implementation: append incoming bytes into buffer_ (already a std::vector<uint8_t>), then parse frames as today.

2) Update websocket_socket to register span callback

  • Use tcp_socket_->set_receive_callback_view(...)
  • Update websocket_socket::on_tcp_receive to accept std::span<const uint8_t> and forward to protocol_.process_data(span).

3) Update tests and docs

  • Update tests/unit/websocket_protocol_test.cpp call sites for the new signature
  • Update any docs/examples referencing process_data(vector) (e.g. docs/FEATURES_KO.md)

Files to Update

  • include/kcenon/network/internal/websocket_protocol.h
  • src/internal/websocket_protocol.cpp
  • include/kcenon/network/internal/websocket_socket.h
  • src/internal/websocket_socket.cpp
  • tests/unit/websocket_protocol_test.cpp
  • Docs as needed (search for process_data( usage)

Acceptance Criteria

  • WebSocket receive path uses tcp_socket span callback
  • websocket_protocol::process_data accepts std::span<const uint8_t>
  • No per-read std::vector is required solely to adapt TCP bytes into the protocol layer
  • Unit tests pass (websocket_protocol_test and any WebSocket integration tests)

Test Plan

  • Run:
    • ctest --output-on-failure -R websocket
    • ctest --output-on-failure (full suite as time permits)

Metadata

Metadata

Assignees

Labels

asyncAsynchronous operationsenhancementNew feature or requestperformancePerformance improvementsrefactoringCode refactoring and improvements

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions