Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Jun 28, 2025

Link issues

fixes #6326

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

Genericize the TCP socket client framework to support custom socket client implementations, introduce configurable client options, and enhance connection, send, and receive operations with timeout, cancellation, and logging support.

New Features:

  • Make TcpSocketClientBase generic by introducing a TSocketClient type parameter and abstract CreateSocketClient method
  • Add SocketClientOptions class and update ITcpSocketFactory.GetOrCreate to accept a configuration delegate
  • Define ISocketClient interface and provide DefaultSocketClient as the default implementation

Bug Fixes:

Enhancements:

  • Unify timeout and cancellation logic in ConnectAsync, SendAsync, and ReceiveAsync methods
  • Integrate Microsoft.Extensions.Logging into socket clients for structured logging
  • Simplify DefaultTcpSocketClient by delegating core operations to the generic base class

Tests:

  • Expand unit tests to cover the new factory signature, generic client behavior, timeout and cancellation scenarios, and default socket client implementation

@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

Refactored the TCP socket client to use a generic base with a pluggable ISocketClient, introduced configuration via SocketClientOptions and an updated factory API, extracted raw socket logic into DefaultSocketClient, centralized logging/error handling/cleanup in the base class, and revised tests to cover the new design.

ER diagram for SocketClientOptions configuration

erDiagram
    SOCKET_CLIENT_OPTIONS {
        int ReceiveBufferSize
        bool IsAutoReceive
        int ConnectTimeout
        int SendTimeout
        int ReceiveTimeout
        IPEndPoint LocalEndPoint
    }
    DEFAULT_TCP_SOCKET_CLIENT ||--o| SOCKET_CLIENT_OPTIONS : configures
Loading

Class diagram for new TCP socket client architecture

classDiagram
    class ITcpSocketClient {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +Func<Memory<byte>, Task> ReceivedCallBack
        +IDataPackageHandler DataPackageHandler
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<Memory<byte>> ReceiveAsync(CancellationToken)
        +ValueTask CloseAsync()
    }
    class TcpSocketClientBase~TSocketClient~ {
        #TSocketClient? Client
        +ILogger? Logger
        +IPEndPoint LocalEndPoint
        +bool IsConnected
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<Memory<byte>> ReceiveAsync(CancellationToken)
        +ValueTask CloseAsync()
        #abstract TSocketClient CreateSocketClient(IPEndPoint)
    }
    class ISocketClient {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<int> ReceiveAsync(Memory<byte>, CancellationToken)
        +ValueTask CloseAsync()
    }
    class DefaultSocketClient {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<int> ReceiveAsync(Memory<byte>, CancellationToken)
        +ValueTask CloseAsync()
    }
    class DefaultTcpSocketClient {
        +DefaultTcpSocketClient(SocketClientOptions)
        +DefaultSocketClient CreateSocketClient(IPEndPoint)
    }
    class SocketClientOptions {
        +int ReceiveBufferSize
        +bool IsAutoReceive
        +int ConnectTimeout
        +int SendTimeout
        +int ReceiveTimeout
        +IPEndPoint LocalEndPoint
    }
    ITcpSocketClient <|.. TcpSocketClientBase~TSocketClient~
    ISocketClient <|.. DefaultSocketClient
    TcpSocketClientBase~DefaultSocketClient~ <|-- DefaultTcpSocketClient
    DefaultTcpSocketClient o-- SocketClientOptions
    TcpSocketClientBase~TSocketClient~ o-- ISocketClient
Loading

Class diagram for updated ITcpSocketFactory API

classDiagram
    class ITcpSocketFactory {
        +ITcpSocketClient GetOrCreate(string name, Action<SocketClientOptions> valueFactory)
        +void Remove(string name)
    }
    class DefaultTcpSocketFactory {
        +ITcpSocketClient GetOrCreate(string name, Action<SocketClientOptions> valueFactory)
    }
    ITcpSocketFactory <|.. DefaultTcpSocketFactory
    DefaultTcpSocketFactory o-- DefaultTcpSocketClient
    DefaultTcpSocketClient o-- SocketClientOptions
Loading

Class diagram for ISocketClient interface and DefaultSocketClient implementation

classDiagram
    class ISocketClient {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<int> ReceiveAsync(Memory<byte>, CancellationToken)
        +ValueTask CloseAsync()
    }
    class DefaultSocketClient {
        +bool IsConnected
        +IPEndPoint LocalEndPoint
        +ValueTask<bool> ConnectAsync(IPEndPoint, CancellationToken)
        +ValueTask<bool> SendAsync(ReadOnlyMemory<byte>, CancellationToken)
        +ValueTask<int> ReceiveAsync(Memory<byte>, CancellationToken)
        +ValueTask CloseAsync()
    }
    ISocketClient <|.. DefaultSocketClient
Loading

File-Level Changes

Change Details Files
Generic base class integrated with ISocketClient
  • Added generic TSocketClient parameter and protected Client property
  • Introduced ISocketClient interface and CreateSocketClient hook
  • Refactored ConnectAsync/SendAsync/ReceiveAsync to delegate to Client
src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs
src/BootstrapBlazor/Services/TcpSocket/ISocketClient.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultSocketClient.cs
Options-based client configuration and factory update
  • Added SocketClientOptions for buffer/timeouts/auto-receive/endpoints
  • Changed ITcpSocketFactory.GetOrCreate to accept Action
  • Updated DefaultTcpSocketFactory to build clients from options
src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs
src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs
src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs
Extracted raw socket operations into DefaultSocketClient
  • Implemented ISocketClient in DefaultSocketClient with Connect/Send/Receive/Close
  • Moved low-level TcpClient logic out of the base class
src/BootstrapBlazor/Services/TcpSocket/DefaultSocketClient.cs
src/BootstrapBlazor/Services/TcpSocket/ISocketClient.cs
Centralized logging, error handling, and cleanup
  • Added ILogger property and Log method in base class
  • Wrapped operations in try/catch with timeout and error logs
  • Implemented DisposeAsync to cancel tasks and close client
src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs
Updated and expanded unit tests for new design
  • Rewrote factory tests to use SocketClientOptions
  • Added DefaultSocketClient and SocketClientOptions tests
  • Enhanced send/receive tests with mock error/timeout handlers
test/UnitTest/Services/TcpSocketFactoryTest.cs
test/UnitTest/Services/DefaultSocketClientTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6326 Update the generic class TcpSocketClientBase to enhance its functionality and flexibility.
#6326 Ensure the LocalEndPoint property is non-nullable and always initialized.

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!


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

Attention: Patch coverage is 99.53052% with 1 line in your changes missing coverage. Please review.

Project coverage is 99.99%. Comparing base (5d1457b) to head (eda3733).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...apBlazor/Services/TcpSocket/TcpSocketClientBase.cs 99.35% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6327   +/-   ##
=======================================
  Coverage   99.99%   99.99%           
=======================================
  Files         715      717    +2     
  Lines       31514    31568   +54     
  Branches     4449     4455    +6     
=======================================
+ Hits        31513    31567   +54     
  Partials        1        1           
Flag Coverage Δ
BB 99.99% <99.53%> (?)

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 f43449c into main Jun 28, 2025
4 of 5 checks passed
@ArgoZhang ArgoZhang deleted the feat-socket branch June 28, 2025 10:25
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(TcpSocketClientBase): update generic class

2 participants