Skip to content

feat(TcpSocket): support multiple DataPackageAdapter#542

Merged
ArgoZhang merged 16 commits intomasterfrom
refactor-socket
Aug 26, 2025
Merged

feat(TcpSocket): support multiple DataPackageAdapter#542
ArgoZhang merged 16 commits intomasterfrom
refactor-socket

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Aug 26, 2025

Link issues

fixes #541

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Support multiple data package adapters on TCP socket clients by adding AddDataPackageAdapter and RemoveDataPackageAdapter extension methods, overloading SetDataPackageAdapter with handler and generic converter variants, refactoring DataPackageAdapter constructor, standardizing license headers, and updating unit tests.

New Features:

  • Introduce AddDataPackageAdapter and RemoveDataPackageAdapter methods to allow multiple data adapters and callbacks per TCP client
  • Add overloads of SetDataPackageAdapter to configure adapters directly with IDataPackageHandler and with generic data converters

Enhancements:

  • Refactor DataPackageAdapter to accept its handler via constructor and simplify callback wiring
  • Standardize license headers and file metadata across TcpSocket and DataAdapter source files

Tests:

  • Migrate existing unit tests to the new DataPackageAdapter constructor
  • Add unit tests AddDataPackageAdapter_Ok, SetDataPackageAdapter_Ok, and SetDataPackageAdapter_Generic for multi-adapter support

Copilot AI review requested due to automatic review settings August 26, 2025 05:49
@bb-auto bb-auto Bot added the enhancement New feature or request label Aug 26, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Aug 26, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Aug 26, 2025

Reviewer's Guide

This PR adds support for multiple DataPackageAdapter instances on an ITcpSocketClient by introducing Add/Remove adapter methods with caching and callback management, refactors the DataPackageAdapter implementation to use constructor injection and simplify handler setup, updates unit tests to cover the new APIs and adjust adapter construction, and refreshes license headers across extension files.

Sequence diagram for adding a DataPackageAdapter to ITcpSocketClient

sequenceDiagram
    participant Client as ITcpSocketClient
    participant Extensions as ITcpSocketClientExtensions
    participant Adapter as IDataPackageAdapter
    participant Callback as Callback
    Extensions->>Client: AddDataPackageAdapter(Adapter, Callback)
    Extensions->>Adapter: set ReceivedCallBack = Callback
    Extensions->>Client: subscribe ReceivedCallBack
    Client->>Adapter: HandlerAsync(buffer) on data received
    Adapter->>Callback: Callback(processed data)
Loading

Sequence diagram for removing a DataPackageAdapter from ITcpSocketClient

sequenceDiagram
    participant Client as ITcpSocketClient
    participant Extensions as ITcpSocketClientExtensions
    participant Adapter as IDataPackageAdapter
    participant Callback as Callback
    Extensions->>Client: RemoveDataPackageAdapter(Callback)
    Extensions->>Client: unsubscribe ReceivedCallBack
    Extensions->>Adapter: remove from cache
Loading

Class diagram for DataPackageAdapter and IDataPackageAdapter changes

classDiagram
    class IDataPackageAdapter {
        +Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack
        +ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token)
    }
    class DataPackageAdapter {
        +DataPackageAdapter(IDataPackageHandler? DataPackageHandler = null)
        +Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack
        +ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token)
    }
    IDataPackageAdapter <|.. DataPackageAdapter
    class IDataPackageHandler {
        +Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack
        +ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token)
    }
    DataPackageAdapter --> IDataPackageHandler: optional constructor param
Loading

Class diagram for ITcpSocketClientExtensions adapter management

classDiagram
    class ITcpSocketClientExtensions {
        +void AddDataPackageAdapter(ITcpSocketClient client, IDataPackageAdapter adapter, Func<ReadOnlyMemory<byte>, ValueTask> callback)
        +void RemoveDataPackageAdapter(ITcpSocketClient client, Func<ReadOnlyMemory<byte>, ValueTask> callback)
        +void SetDataPackageAdapter(ITcpSocketClient client, IDataPackageAdapter adapter, Func<ReadOnlyMemory<byte>, ValueTask> callback)
        +void SetDataPackageAdapter<TEntity>(ITcpSocketClient client, IDataPackageAdapter adapter, IDataConverter<TEntity> converter, Func<TEntity?, Task> callback)
        +void SetDataPackageAdapter<TEntity>(ITcpSocketClient client, IDataPackageAdapter adapter, Func<TEntity?, Task> callback)
        +void SetDataPackageAdapter(ITcpSocketClient client, IDataPackageHandler handler, Func<ReadOnlyMemory<byte>, ValueTask> callback)
        +void SetDataPackageAdapter<TEntity>(ITcpSocketClient client, IDataPackageHandler handler, IDataConverter<TEntity> converter, Func<TEntity?, Task> callback)
        +void SetDataPackageAdapter<TEntity>(ITcpSocketClient client, IDataPackageHandler handler, Func<TEntity?, Task> callback)
    }
    class ITcpSocketClient {
        +event Func<ReadOnlyMemory<byte>, ValueTask> ReceivedCallBack
    }
    ITcpSocketClientExtensions --> ITcpSocketClient: manages adapters
Loading

File-Level Changes

Change Details Files
Enable multiple DataPackageAdapter management in ITcpSocketClient extensions
  • Introduce static _cache to track per-client adapters and callbacks
  • Add AddDataPackageAdapter and RemoveDataPackageAdapter methods with subscribe/unsubscribe logic
  • Enhance SetDataPackageAdapter to clear existing adapters from cache and remove their callbacks
  • Provide overloads for handler-based and generic converter-based SetDataPackageAdapter signatures
src/extensions/BootstrapBlazor.TcpSocket/Extensions/ITcpSocketClientExtensions.cs
Refactor DataPackageAdapter to constructor injection and simplify handler setup
  • Convert DataPackageAdapter to primary-constructor accepting IDataPackageHandler
  • Remove setter property for DataPackageHandler and streamline ReceivedCallBack initialization
  • Use null-coalescing assignment for DataPackageHandler.ReceivedCallBack
  • Delete DataPackageHandler property from IDataPackageAdapter interface
src/extensions/BootstrapBlazor.Socket/DataAdapter/DataPackageAdapter.cs
src/extensions/BootstrapBlazor.Socket/DataAdapter/IDataPackageAdapter.cs
Update unit tests for constructor changes and new adapter APIs
  • Replace object-initializer adapter creation with new DataPackageAdapter(handler) constructor
  • Add tests for AddDataPackageAdapter, RemoveDataPackageAdapter and multiple adapters
  • Add tests for SetDataPackageAdapter overloads, including generic converter scenarios
test/UnitTestTcpSocket/TcpSocketFactoryTest.cs
Refresh license headers across extension source files
  • Update copyright and license comments to Apache 2.0 and unify website URLs
src/extensions/BootstrapBlazor.TcpSocket/Extensions/TcpSocketUtility.cs
src/extensions/BootstrapBlazor.TcpSocket/DefaultTcpSocketClient.cs
src/extensions/BootstrapBlazor.TcpSocket/DefaultTcpSocketClientProvider.cs
src/extensions/BootstrapBlazor.TcpSocket/DefaultTcpSocketFactory.cs
src/extensions/BootstrapBlazor.TcpSocket/Extensions/TcpSocketExtensions.cs
src/extensions/BootstrapBlazor.TcpSocket/ITcpSocketClient.cs
src/extensions/BootstrapBlazor.TcpSocket/ITcpSocketClientProvider.cs
src/extensions/BootstrapBlazor.TcpSocket/ITcpSocketFactory.cs
src/extensions/BootstrapBlazor.TcpSocket/TcpSocketClientOptions.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#541 Enable TcpSocket to support multiple DataPackageAdapter instances, allowing multiple adapters to be added and removed for a single client.
#541 Update the ITcpSocketClientExtensions API to provide methods for adding and removing DataPackageAdapter instances, and ensure correct callback management for multiple adapters.
#541 Add or update tests to verify that multiple DataPackageAdapter instances can be used simultaneously and callbacks are invoked as expected.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit e4236c0 into master Aug 26, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the refactor-socket branch August 26, 2025 05:50
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Variable 'cb' is used before its declaration. (link)

General comments:

  • The static _cache dictionary in Add/RemoveDataPackageAdapter is neither thread-safe nor cleaned up when clients are disposed; consider using a ConcurrentDictionary and removing client entries when no adapters remain to prevent memory leaks and race conditions.
  • The DataPackageAdapter class uses a primary-constructor syntax for the IDataPackageHandler parameter but doesn’t explicitly store it; verify that DataPackageHandler is correctly backed by a field since the interface no longer exposes it and primary constructors on regular classes may not be supported in all C# versions.
  • The new tests wait for callbacks but don’t assert the received buffer contents or confirm that RemoveDataPackageAdapter unsubscribes callbacks; add assertions to verify both adapter callbacks run as expected and that removal stops further invocations.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The static _cache dictionary in Add/RemoveDataPackageAdapter is neither thread-safe nor cleaned up when clients are disposed; consider using a ConcurrentDictionary and removing client entries when no adapters remain to prevent memory leaks and race conditions.
- The DataPackageAdapter class uses a primary-constructor syntax for the IDataPackageHandler parameter but doesn’t explicitly store it; verify that DataPackageHandler is correctly backed by a field since the interface no longer exposes it and primary constructors on regular classes may not be supported in all C# versions.
- The new tests wait for callbacks but don’t assert the received buffer contents or confirm that RemoveDataPackageAdapter unsubscribes callbacks; add assertions to verify both adapter callbacks run as expected and that removal stops further invocations.

## Individual Comments

### Comment 1
<location> `src/extensions/BootstrapBlazor.TcpSocket/Extensions/ITcpSocketClientExtensions.cs:54` </location>
<code_context>
         return client.ConnectAsync(endPoint, token);
     }

+    private static readonly Dictionary<ITcpSocketClient, List<(IDataPackageAdapter Adapter, Func<ReadOnlyMemory<byte>, ValueTask> Callback)>> _cache = [];
+
+    /// <summary>
</code_context>

<issue_to_address>
Consider thread safety for the static _cache dictionary.

Since _cache is modified and read by multiple extension methods, this can cause race conditions in multi-threaded use. Use a thread-safe collection like ConcurrentDictionary or implement locking to prevent concurrent access issues.
</issue_to_address>

### Comment 2
<location> `src/extensions/BootstrapBlazor.TcpSocket/Extensions/ITcpSocketClientExtensions.cs:66` </location>
<code_context>
+    {
+        if (_cache.TryGetValue(client, out var list))
+        {
+            list.Add((adapter, cb));
+        }
+        else
</code_context>

<issue_to_address>
Variable 'cb' is used before its declaration.

'cb' is referenced in list.Add and _cache.Add before it is declared as an async local function, which will cause a compilation error. Please declare 'cb' before using it.
</issue_to_address>

### Comment 3
<location> `src/extensions/BootstrapBlazor.TcpSocket/Extensions/ITcpSocketClientExtensions.cs:64` </location>
<code_context>
+    /// <param name="callback"></param>
+    public static void RemoveDataPackageAdapter(this ITcpSocketClient client, Func<ReadOnlyMemory<byte>, ValueTask> callback)
+    {
+        if (_cache.TryGetValue(client, out var list))
+        {
+            var items = list.Where(i => i.Adapter.ReceivedCallBack == callback).ToList();
+            foreach (var c in items)
+            {
</code_context>

<issue_to_address>
Callback removal logic may not match added callbacks.

Since the callback added is a wrapper function, direct comparison with the original callback may fail. Store and compare the actual delegate instance used in ReceivedCallBack to ensure correct removal.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

return client.ConnectAsync(endPoint, token);
}

private static readonly Dictionary<ITcpSocketClient, List<(IDataPackageAdapter Adapter, Func<ReadOnlyMemory<byte>, ValueTask> Callback)>> _cache = [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Consider thread safety for the static _cache dictionary.

Since _cache is modified and read by multiple extension methods, this can cause race conditions in multi-threaded use. Use a thread-safe collection like ConcurrentDictionary or implement locking to prevent concurrent access issues.

{
if (_cache.TryGetValue(client, out var list))
{
list.Add((adapter, cb));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Variable 'cb' is used before its declaration.

'cb' is referenced in list.Add and _cache.Add before it is declared as an async local function, which will cause a compilation error. Please declare 'cb' before using it.

Comment on lines +64 to +73
if (_cache.TryGetValue(client, out var list))
{
list.Add((adapter, cb));
}
else
{
_cache.Add(client, [(adapter, cb)]);
}

client.ReceivedCallBack += cb;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Callback removal logic may not match added callbacks.

Since the callback added is a wrapper function, direct comparison with the original callback may fail. Store and compare the actual delegate instance used in ReceivedCallBack to ensure correct removal.

@ArgoZhang ArgoZhang review requested due to automatic review settings March 23, 2026 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(TcpSocket): support multiple DataPackageAdapter

1 participant