Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.

Commit f5ee572

Browse files
authored
feat(Performance): refactor send/receive improve performance (#26)
* refactor: 重构代码 * doc: 更新 readme 文档 * chore: 更新 xml 配置文件 * refactor: 删除 SendState 状态类 * refactor: 移除 UnsupportedOSPlatform 方法 * refactor: Sender 重构 * refactor: 重构 factory * chore: 更新依赖 * refactor: 精简代码 * refactor: 更改构造方法 * feat: 移除 EnableLog 设置 * feat: 增加 ThrowIfNotConnected 扩展 * refactor: 增加 TcpSocketClientOptions 扩展方法 * refactor: 精简代码功能代码下沉 * refactor: DefaultTcpSocketClient 参数重构 * doc: 更新注释 * refactor: 精简 Receiver 代码 * feat: 更改 ReceiveAsync 接口 * refactor: 增加 Options 属性 * refactor: 更新 Options 参数 * refactor: 更改顺序 * refactor: 移除 Options 属性 * refactor: 更新代码 * fix: 增加是否连接返回值 * test: 更新单元测试 * test: 更新单元测试 * chore: 更新依赖 * refactor: 移除 ITcpSocketClientProvider 精简代码 * test: 更新单元测试 * test: 更新单元测试 * chore: bump version 9.0.8 * chore: 更新更新依赖包 * refactor: 增加配置 Options 逻辑 * refactor: 增加 Callback 回调防止死锁 * refactor: 移动数据适配器到接口 ITcpSocketClient * test: 精简单元测试 * refactor: 删除不需要的单元测试 * refactor: 移动适配器相关代码到扩展方法中 * test: 删除冗余单元测试 * test: 增加 SendAsync 单元测试覆盖率 * refactor: 移除 ConfigureDataConverters 方法 * chore: 更新依赖版本 9.0.3 * test: 精简单元测试 * fix: 修复 Callback 未释放问题 * feat: 增加连接超时逻辑 * test: 增加单元测试 * feat: 增加连接超时功能 * test: 增加单元测试 * feat: 重构 GetOrCreate 方法 * test: 更新单元测试 * test: 增加接收取消单元测试 * test: 增加单元测试
1 parent eadd805 commit f5ee572

20 files changed

Lines changed: 421 additions & 2223 deletions

src/Longbow.TcpSocket/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 95 additions & 144 deletions
Large diffs are not rendered by default.

src/Longbow.TcpSocket/Extensions/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,20 @@ public static class ServiceCollectionExtensions
1717
/// 增加 ITcpSocketFactory 服务
1818
/// </summary>
1919
/// <param name="services"></param>
20+
/// <param name="configureOptions"></param>
2021
/// <returns></returns>
2122
[UnsupportedOSPlatform("browser")]
22-
public static IServiceCollection AddTcpSocketFactory(this IServiceCollection services)
23+
public static IServiceCollection AddTcpSocketFactory(this IServiceCollection services, Action<TcpSocketClientOptions>? configureOptions = null)
2324
{
2425
// 添加 ITcpSocketFactory 服务
2526
services.TryAddSingleton<ITcpSocketFactory, DefaultTcpSocketFactory>();
2627

2728
// 增加 ISocketClientProvider 服务
28-
services.TryAddTransient<ITcpSocketClientProvider, DefaultTcpSocketClientProvider>();
29+
services.TryAddTransient<ITcpSocketClient, DefaultTcpSocketClient>();
2930

30-
return services;
31-
}
31+
// 增加全局配置
32+
services.Configure<TcpSocketClientOptions>(op => configureOptions?.Invoke(op));
3233

33-
/// <summary>
34-
/// 配置第三方数据模型与 <see cref="DataConverterCollection"/> 数据转换器集合配置扩展方法
35-
/// </summary>
36-
/// <param name="services"></param>
37-
/// <param name="configureOptions"></param>
38-
/// <returns></returns>
39-
public static IServiceCollection ConfigureDataConverters(this IServiceCollection services, Action<DataConverterCollection> configureOptions)
40-
{
41-
services.Configure(configureOptions);
4234
return services;
4335
}
4436
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://github.com/LongbowExtensions/
4+
5+
namespace Longbow.TcpSocket;
6+
7+
static class TcpSocketClientOptionsExtensions
8+
{
9+
public static TcpSocketClientOptions CopyTo(this TcpSocketClientOptions source) => new()
10+
{
11+
ReceiveBufferSize = source.ReceiveBufferSize,
12+
IsAutoReceive = source.IsAutoReceive,
13+
ConnectTimeout = source.ConnectTimeout,
14+
SendTimeout = source.SendTimeout,
15+
ReceiveTimeout = source.ReceiveTimeout,
16+
LocalEndPoint = source.LocalEndPoint,
17+
IsAutoReconnect = source.IsAutoReconnect,
18+
ReconnectInterval = source.ReconnectInterval,
19+
NoDelay = source.NoDelay
20+
};
21+
}

src/Longbow.TcpSocket/Extensions/TcpSocketUtility.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System.Net;
66
using System.Net.Sockets;
7-
using System.Runtime.Versioning;
87

98
namespace Longbow.TcpSocket;
109

@@ -27,7 +26,6 @@ public static class TcpSocketUtility
2726
/// string as an IP address or resolve it as a hostname.</param>
2827
/// <returns>An <see cref="IPAddress"/> object representing the parsed or resolved IP address. If the input cannot be parsed
2928
/// or resolved, the method returns a default IP address.</returns>
30-
[UnsupportedOSPlatform("browser")]
3129
public static IPAddress ConvertToIPAddress(string ipString)
3230
{
3331
if (string.IsNullOrEmpty(ipString))
@@ -48,7 +46,6 @@ public static IPAddress ConvertToIPAddress(string ipString)
4846
}
4947

5048
[ExcludeFromCodeCoverage]
51-
[UnsupportedOSPlatform("browser")]
5249
private static IPAddress IPAddressByHostName => Dns.GetHostAddresses(Dns.GetHostName(), AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Any;
5350

5451
/// <summary>
@@ -59,7 +56,6 @@ public static IPAddress ConvertToIPAddress(string ipString)
5956
/// <param name="port">The port number associated with the endpoint. Must be between 0 and 65535.</param>
6057
/// <returns>An <see cref="IPEndPoint"/> representing the specified IP address and port.</returns>
6158
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="port"/> is less than 0 or greater than 65535.</exception>
62-
[UnsupportedOSPlatform("browser")]
6359
public static IPEndPoint ConvertToIpEndPoint(string ipString, int port)
6460
{
6561
if (port < 0 || port > 65535)

0 commit comments

Comments
 (0)