Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Jun 28, 2025

Link issues

fixes #6328

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

Introduce an ISocketClientProvider abstraction and refactor the TCP socket client implementation to use provider-based dependency injection and centralized SocketClientOptions configuration.

New Features:

  • Add ISocketClientProvider interface and DefaultSocketClientProvider implementation for managing TCP socket connections via DI
  • Expose SocketClientOptions on ITcpSocketClient to centralize socket configuration

Enhancements:

  • Refactor TcpSocketClientBase to rely on ISocketClientProvider and IServiceProvider instead of a generic client type
  • Simplify DefaultTcpSocketClient by removing custom creation logic and leveraging the provider pattern
  • Update DI setup in TcpSocketExtensions to register ISocketClientProvider alongside ITcpSocketFactory
  • Change default local endpoint fallback to IPAddress.Any in SocketClientOptions and Utility

Tests:

  • Add unit tests for DefaultSocketClientProvider behavior and SocketClientOptions validation
  • Update existing TcpSocketClient tests to use options-based timeouts and buffer settings

@bb-auto bb-auto bot added the enhancement New feature or request label Jun 28, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 28, 2025

Reviewer's Guide

Overhauls the TCP socket client to adopt a provider-based design (ISocketClientProvider) with dependency injection, consolidating configuration in SocketClientOptions and updating factories, interfaces, and unit tests to align with the new model.

ER diagram for changes to TCP socket client data types

erDiagram
    ITcpSocketClient ||--o{ TcpSocketClientBase : implements
    TcpSocketClientBase ||--o{ DefaultTcpSocketClient : extends
    ISocketClientProvider ||--o{ DefaultSocketClientProvider : implements
    TcpSocketClientBase }o--|| ISocketClientProvider : uses
    TcpSocketClientBase }o--|| SocketClientOptions : has
    ISocketClientProvider {
        bool IsConnected
        IPEndPoint LocalEndPoint
    }
    SocketClientOptions {
        int ReceiveBufferSize
        bool IsAutoReceive
        int ConnectTimeout
        int SendTimeout
        int ReceiveTimeout
        IPEndPoint LocalEndPoint
    }
Loading

Class diagram for the new ISocketClientProvider-based TCP socket client architecture

classDiagram
    class ITcpSocketClient {
        +bool IsConnected
        +SocketClientOptions Options
        +IPEndPoint LocalEndPoint
        +Func<ReadOnlyMemory<byte>, ValueTask> ReceivedCallBack
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<Memory<byte>> ReceiveAsync(CancellationToken)
        +ValueTask DisposeAsync()
    }

    class TcpSocketClientBase {
        #ISocketClientProvider? SocketClientProvider
        #ILogger? Logger
        +IServiceProvider? ServiceProvider
        +SocketClientOptions Options
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<Memory<byte>> ReceiveAsync(CancellationToken)
        +ValueTask DisposeAsync()
    }

    class DefaultTcpSocketClient {
    }

    class ISocketClientProvider {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +ValueTask ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<int> ReceiveAsync(Memory<byte>, CancellationToken)
        +ValueTask CloseAsync()
    }

    class DefaultSocketClientProvider {
    }

    ITcpSocketClient <|.. TcpSocketClientBase
    TcpSocketClientBase <|-- DefaultTcpSocketClient
    ISocketClientProvider <|.. DefaultSocketClientProvider
    TcpSocketClientBase --> ISocketClientProvider : uses
    DefaultTcpSocketClient --> TcpSocketClientBase
    DefaultSocketClientProvider ..|> ISocketClientProvider
Loading

Class diagram for SocketClientOptions and its usage

classDiagram
    class SocketClientOptions {
        +int ReceiveBufferSize
        +bool IsAutoReceive
        +int ConnectTimeout
        +int SendTimeout
        +int ReceiveTimeout
        +IPEndPoint LocalEndPoint
    }
    TcpSocketClientBase --> SocketClientOptions : has
    DefaultTcpSocketClient --> SocketClientOptions : has
Loading

File-Level Changes

Change Details Files
Refactor TcpSocketClientBase to use SocketClientOptions and ISocketClientProvider
  • Replaced generic TSocketClient and abstract factory method with injected ISocketClientProvider
  • Switched all connect/send/receive calls to use provider and options for timeouts and buffers
  • Added ServiceProvider and lazy Logger resolution via DI
  • Removed per-property fields (ConnectTimeout, SendTimeout, etc.) in favor of Options
  • Updated DisposeAsync and logging to use provider and DI
src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs
src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs
Introduce ISocketClientProvider and register via DI
  • Renamed ISocketClient to ISocketClientProvider with LocalEndPoint setter
  • Implemented DefaultSocketClientProvider to manage TcpClient lifecycle
  • Registered ISocketClientProvider in AddBootstrapBlazorTcpSocketFactory extension
src/BootstrapBlazor/Services/TcpSocket/ISocketClientProvider.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultSocketClientProvider.cs
src/BootstrapBlazor/Services/TcpSocket/Extensions/TcpSocketExtensions.cs
Update factory to use new provider and DI
  • Changed DefaultTcpSocketFactory to inject ServiceProvider into clients
  • Switched to AddSingleton for ITcpSocketFactory registration
  • Removed direct Logger assignment in factory
src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs
Adjust unit tests for Options-based configuration and add provider tests
  • Replaced direct timeout, buffer size, and IsAutoReceive properties with client.Options access
  • Added DefaultSocketClientProviderTest to validate provider behavior
  • Removed obsolete DefaultSocketClientTest
test/UnitTest/Services/TcpSocketFactoryTest.cs
test/UnitTest/Services/DefaultSocketClientProviderTest.cs
Change default local endpoint to IPAddress.Any
  • Updated default LocalEndPoint in SocketClientOptions from Loopback to Any
  • Adjusted Utility fallback IPAddressByHostName to default to IPAddress.Any
src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs
src/BootstrapBlazor/Utils/Utility.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6328 Add ISocketClientProvider service to allow for managing socket connections, data transmission, and reception over TCP sockets.

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

@bb-auto bb-auto bot added this to the 9.7.0 milestone Jun 28, 2025
sourcery-ai[bot]
sourcery-ai bot previously approved these changes Jun 28, 2025
Copy link
Contributor

@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 @ArgoZhang - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs:42` </location>
<code_context>
+    /// Gets or sets the service provider used to resolve dependencies.
     /// </summary>
-    public int ReceiveBufferSize { get; set; } = 1024 * 64;
+    [NotNull]
+    public IServiceProvider? ServiceProvider { get; set; }

     /// <summary>
</code_context>

<issue_to_address>
The [NotNull] attribute on a public settable property may not provide the intended guarantees.

Consider enforcing non-null assignment through constructor injection or property validation, as [NotNull] does not guarantee non-null values for public settable properties.

Suggested implementation:

```csharp
    /// Gets the service provider used to resolve dependencies.
    /// </summary>

```

```csharp
    public IServiceProvider ServiceProvider { get; }

```

You will need to:
1. Add a constructor to the class that takes an `IServiceProvider` parameter and assigns it to the `ServiceProvider` property.
2. Update all usages and instantiations of this class to provide a non-null `IServiceProvider` via the constructor.
</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.

@codecov
Copy link

codecov bot commented Jun 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (f43449c) to head (874ccf3).

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #6329   +/-   ##
========================================
  Coverage   99.99%   100.00%           
========================================
  Files         717       717           
  Lines       31568     31557   -11     
  Branches     4455      4457    +2     
========================================
- Hits        31567     31557   -10     
+ Partials        1         0    -1     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ArgoZhang ArgoZhang merged commit 618b956 into main Jun 28, 2025
5 checks passed
@ArgoZhang ArgoZhang deleted the feat-socket branch June 28, 2025 15:32
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(ISocketClientProvider): add ISocketClientProvider service

2 participants