diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index 7f5e2101d..5516f395e 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -3,7 +3,7 @@
logo.ico
True
$(MSBuildProjectName).snk
- 2.0.0-beta.239
+ 2.0.0-beta.241
false
true
若汝棋茗
@@ -34,6 +34,9 @@
8.0
+
+ 8.0
+
True
diff --git a/src/TouchSocket.AspNetCore/Extensions/AspNetCoreExtension.cs b/src/TouchSocket.AspNetCore/Extensions/AspNetCoreExtension.cs
index 634100756..5f7be1774 100644
--- a/src/TouchSocket.AspNetCore/Extensions/AspNetCoreExtension.cs
+++ b/src/TouchSocket.AspNetCore/Extensions/AspNetCoreExtension.cs
@@ -55,29 +55,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region TcpDmtpClient
///
- /// 添加TcpDmtpClient服务。
+ /// 添加Scoped TcpDmtpClient服务。
///
///
///
///
///
///
- public static IServiceCollection AddTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedTcpDmtpClient(this IServiceCollection services, Action actionConfig)
where TClient : class, ITcpDmtpClient
where TImpClient : class, TClient
{
- return services.AddTcpClient(actionConfig);
+ return services.AddScopedTcpClient(actionConfig);
}
///
- /// 添加TcpDmtpClient服务。并使用注册服务。
+ /// 添加Scoped TcpDmtpClient服务。并使用注册服务。
///
///
///
///
- public static IServiceCollection AddTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedTcpDmtpClient(this IServiceCollection services, Action actionConfig)
{
- return services.AddTcpDmtpClient(actionConfig);
+ return services.AddScopedTcpDmtpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例TcpDmtpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, ITcpDmtpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddSingletonTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例TcpDmtpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddSingletonTcpDmtpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态TcpDmtpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, ITcpDmtpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddTransientTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态TcpDmtpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientTcpDmtpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddTransientTcpDmtpClient(actionConfig);
}
#endregion TcpDmtpClient
@@ -115,29 +167,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region HttpClient
///
- /// 添加HttpClient服务。
+ /// 添加Scoped HttpClient服务。
///
///
///
///
///
///
- public static IServiceCollection AddHttpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedHttpClient(this IServiceCollection services, Action actionConfig)
where TClient : class, IHttpClient
where TImpClient : class, TClient
{
- return services.AddTcpClient(actionConfig);
+ return services.AddScopedTcpClient(actionConfig);
}
///
- /// 添加HttpClient服务。并使用注册服务。
+ /// 添加Scoped HttpClient服务。并使用注册服务。
///
///
///
///
- public static IServiceCollection AddHttpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedHttpClient(this IServiceCollection services, Action actionConfig)
{
- return services.AddHttpClient(actionConfig);
+ return services.AddScopedHttpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例HttpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonHttpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, IHttpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddSingletonTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例HttpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonHttpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddSingletonHttpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态HttpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientHttpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, IHttpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddTransientTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态HttpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientHttpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddTransientHttpClient(actionConfig);
}
#endregion HttpClient
@@ -175,29 +279,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region HttpDmtpClient
///
- /// 添加HttpDmtpClient服务。
+ /// 添加Scoped HttpDmtpClient服务。
///
///
///
///
///
///
- public static IServiceCollection AddHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedHttpDmtpClient(this IServiceCollection services, Action actionConfig)
where TClient : class, IHttpDmtpClient
where TImpClient : class, TClient
{
- return services.AddTcpClient(actionConfig);
+ return services.AddScopedTcpClient(actionConfig);
}
///
- /// 添加HttpDmtpClient服务。并使用注册服务。
+ /// 添加Scoped HttpDmtpClient服务。并使用注册服务。
///
///
///
///
- public static IServiceCollection AddHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddScopedHttpDmtpClient(this IServiceCollection services, Action actionConfig)
{
- return services.AddHttpDmtpClient(actionConfig);
+ return services.AddScopedHttpDmtpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例HttpDmtpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, IHttpDmtpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddSingletonTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加单例HttpDmtpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddSingletonHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddSingletonHttpDmtpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态HttpDmtpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, IHttpDmtpClient
+ where TImpClient : class, TClient
+ {
+ return services.AddTransientTcpClient(actionConfig);
+ }
+
+ ///
+ /// 添加瞬态HttpDmtpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientHttpDmtpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddTransientHttpDmtpClient(actionConfig);
}
#endregion HttpDmtpClient
diff --git a/src/TouchSocket.Core.DependencyInjection/Container/AspNetCoreContainer.cs b/src/TouchSocket.Core.DependencyInjection/Container/AspNetCoreContainer.cs
index cd64f10c2..c6f9f1e3c 100644
--- a/src/TouchSocket.Core.DependencyInjection/Container/AspNetCoreContainer.cs
+++ b/src/TouchSocket.Core.DependencyInjection/Container/AspNetCoreContainer.cs
@@ -11,6 +11,7 @@
//------------------------------------------------------------------------------
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -34,7 +35,11 @@ namespace TouchSocket.Core.AspNetCore
public AspNetCoreContainer(IServiceCollection services)
{
this.m_services = services ?? throw new ArgumentNullException(nameof(services));
- services.AddSingleton(this);
+ services.AddSingleton(privoder =>
+ {
+ this.m_serviceProvider ??= privoder;
+ return this;
+ });
}
///
diff --git a/src/TouchSocket.Core/Common/Enum/EndianType.cs b/src/TouchSocket.Core/Common/Enum/EndianType.cs
index eed1d8a2c..8418bb45c 100644
--- a/src/TouchSocket.Core/Common/Enum/EndianType.cs
+++ b/src/TouchSocket.Core/Common/Enum/EndianType.cs
@@ -18,13 +18,23 @@ namespace TouchSocket.Core
public enum EndianType
{
///
- /// 小端模式
+ /// 小端模式,即DCBA
///
Little,
///
- /// 大端模式
+ /// 大端模式。即ABCD
///
- Big
+ Big,
+
+ ///
+ /// 以交换小端格式。即CDAB
+ ///
+ LittleSwap,
+
+ ///
+ /// 以交换大端,即:BADC
+ ///
+ BigSwap
}
}
\ No newline at end of file
diff --git a/src/TouchSocket.Core/Common/TouchSocketBitConverter.cs b/src/TouchSocket.Core/Common/TouchSocketBitConverter.cs
index 2367632a6..27a7e91d5 100644
--- a/src/TouchSocket.Core/Common/TouchSocketBitConverter.cs
+++ b/src/TouchSocket.Core/Common/TouchSocketBitConverter.cs
@@ -10,6 +10,7 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
+using Newtonsoft.Json.Linq;
using System;
using System.Runtime.CompilerServices;
@@ -19,32 +20,79 @@ namespace TouchSocket.Core
/// 将基数据类型转换为指定端的一个字节数组,
/// 或将一个字节数组转换为指定端基数据类型。
///
- public class TouchSocketBitConverter
+ public sealed class TouchSocketBitConverter
{
///
/// 以大端
///
- public static TouchSocketBitConverter BigEndian;
+ public static readonly TouchSocketBitConverter BigEndian;
+
+ ///
+ /// 以交换大端
+ ///
+ public static readonly TouchSocketBitConverter BigSwapEndian;
///
/// 以小端
///
- public static TouchSocketBitConverter LittleEndian;
+ public static readonly TouchSocketBitConverter LittleEndian;
+
+ ///
+ /// 以交换小端
+ ///
+ public static readonly TouchSocketBitConverter LittleSwapEndian;
+
+ private static EndianType m_defaultEndianType;
static TouchSocketBitConverter()
{
BigEndian = new TouchSocketBitConverter(EndianType.Big);
LittleEndian = new TouchSocketBitConverter(EndianType.Little);
+ BigSwapEndian = new TouchSocketBitConverter(EndianType.BigSwap);
+ LittleSwapEndian = new TouchSocketBitConverter(EndianType.LittleSwap);
DefaultEndianType = EndianType.Little;
}
+ ///
+ /// 构造函数
+ ///
+ ///
+ public TouchSocketBitConverter(EndianType endianType)
+ {
+ this.EndianType = endianType;
+ }
+
+ ///
+ /// 按照枚举值选择默认的端序。
+ ///
+ ///
+ ///
+ ///
+ public static TouchSocketBitConverter GetBitConverter(EndianType endianType)
+ {
+ switch (endianType)
+ {
+ case EndianType.Little:
+ return LittleEndian;
+
+ case EndianType.Big:
+ return BigEndian;
+
+ case EndianType.LittleSwap:
+ return LittleSwapEndian;
+
+ case EndianType.BigSwap:
+ return BigSwapEndian;
+ default:
+ throw new Exception("没有该选项");
+ }
+ }
+
///
/// 以默认小端,可通过重新指定默认端。
///
public static TouchSocketBitConverter Default { get; private set; }
- private static EndianType m_defaultEndianType;
-
///
/// 默认大小端切换。
///
@@ -64,21 +112,20 @@ namespace TouchSocket.Core
Default = BigEndian;
break;
+ case EndianType.LittleSwap:
+ Default = LittleSwapEndian;
+ break;
+
+ case EndianType.BigSwap:
+ Default = BigSwapEndian;
+ break;
+
default:
break;
}
}
}
- ///
- /// 构造函数
- ///
- ///
- public TouchSocketBitConverter(EndianType endianType)
- {
- this.EndianType = endianType;
- }
-
///
/// 指定大小端。
///
@@ -92,7 +139,6 @@ namespace TouchSocket.Core
public bool IsSameOfSet()
{
return !(BitConverter.IsLittleEndian ^ (this.EndianType == EndianType.Little));
- //return true;
}
#region ushort
@@ -147,7 +193,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat8(bytes, 0);
}
return bytes;
@@ -167,9 +213,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[8];
- Array.Copy(buffer, offset, bytes, 0, 8);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat8(buffer, offset);
return BitConverter.ToUInt64(bytes, 0);
}
}
@@ -188,6 +232,30 @@ namespace TouchSocket.Core
return BitConverter.GetBytes(value);
}
+ ///
+ /// 将布尔数组转为字节数组。不足位补0.
+ ///
+ ///
+ ///
+ ///
+ public byte[] GetBytes(bool[] values)
+ {
+ if (values is null)
+ {
+ throw new ArgumentNullException(nameof(values));
+ }
+
+ var numArray = new byte[values.Length % 8 == 0 ? values.Length / 8 : (values.Length / 8) + 1];
+ for (var index = 0; index < values.Length; ++index)
+ {
+ if (values[index])
+ {
+ numArray[index / 8] = numArray[index / 8].SetBit((short)(index % 8), 1);
+ }
+ }
+ return numArray;
+ }
+
///
/// 转换为指定端模式的bool数据。
///
@@ -295,7 +363,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat4(bytes, 0);
}
return bytes;
@@ -315,9 +383,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[4];
- Array.Copy(buffer, offset, bytes, 0, bytes.Length);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat4(buffer, offset);
return BitConverter.ToInt32(bytes, 0);
}
}
@@ -336,7 +402,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat8(bytes, 0);
}
return bytes;
@@ -356,9 +422,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[8];
- Array.Copy(buffer, offset, bytes, 0, bytes.Length);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat8(buffer, offset);
return BitConverter.ToInt64(bytes, 0);
}
}
@@ -377,7 +441,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat4(bytes, 0);
}
return bytes;
@@ -397,9 +461,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[4];
- Array.Copy(buffer, offset, bytes, 0, bytes.Length);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat4(buffer, offset);
return BitConverter.ToUInt32(bytes, 0);
}
}
@@ -418,7 +480,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat4(bytes, 0);
}
return bytes;
@@ -438,9 +500,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[4];
- Array.Copy(buffer, offset, bytes, 0, bytes.Length);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat4(buffer, offset);
return BitConverter.ToSingle(bytes, 0);
}
}
@@ -459,7 +519,7 @@ namespace TouchSocket.Core
var bytes = BitConverter.GetBytes(value);
if (!this.IsSameOfSet())
{
- Array.Reverse(bytes);
+ bytes = this.ByteTransDataFormat8(bytes, 0);
}
return bytes;
@@ -479,9 +539,7 @@ namespace TouchSocket.Core
}
else
{
- var bytes = new byte[8];
- Array.Copy(buffer, offset, bytes, 0, bytes.Length);
- Array.Reverse(bytes);
+ var bytes = this.ByteTransDataFormat8(buffer, offset);
return BitConverter.ToDouble(bytes, 0);
}
}
@@ -527,5 +585,107 @@ namespace TouchSocket.Core
}
#endregion decimal
+
+ #region Tool
+
+ /// 反转多字节的数据信息
+ /// 数据字节
+ /// 起始索引,默认值为0
+ /// 实际字节信息
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private byte[] ByteTransDataFormat4(byte[] value, int offset)
+ {
+ byte[] numArray = new byte[4];
+ switch (EndianType)
+ {
+ case EndianType.Big:
+ numArray[0] = value[offset + 3];
+ numArray[1] = value[offset + 2];
+ numArray[2] = value[offset + 1];
+ numArray[3] = value[offset];
+ break;
+
+ case EndianType.BigSwap:
+ numArray[0] = value[offset + 2];
+ numArray[1] = value[offset + 3];
+ numArray[2] = value[offset];
+ numArray[3] = value[offset + 1];
+ break;
+
+ case EndianType.LittleSwap:
+ numArray[0] = value[offset + 1];
+ numArray[1] = value[offset];
+ numArray[2] = value[offset + 3];
+ numArray[3] = value[offset + 2];
+ break;
+
+ case EndianType.Little:
+ numArray[0] = value[offset];
+ numArray[1] = value[offset + 1];
+ numArray[2] = value[offset + 2];
+ numArray[3] = value[offset + 3];
+ break;
+ }
+ return numArray;
+ }
+
+ /// 反转多字节的数据信息
+ /// 数据字节
+ /// 起始索引,默认值为0
+ /// 实际字节信息
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private byte[] ByteTransDataFormat8(byte[] value, int offset)
+ {
+ byte[] numArray = new byte[8];
+ switch (EndianType)
+ {
+ case EndianType.Big:
+ numArray[0] = value[offset + 7];
+ numArray[1] = value[offset + 6];
+ numArray[2] = value[offset + 5];
+ numArray[3] = value[offset + 4];
+ numArray[4] = value[offset + 3];
+ numArray[5] = value[offset + 2];
+ numArray[6] = value[offset + 1];
+ numArray[7] = value[offset];
+ break;
+
+ case EndianType.BigSwap:
+ numArray[0] = value[offset + 6];
+ numArray[1] = value[offset + 7];
+ numArray[2] = value[offset + 4];
+ numArray[3] = value[offset + 5];
+ numArray[4] = value[offset + 2];
+ numArray[5] = value[offset + 3];
+ numArray[6] = value[offset];
+ numArray[7] = value[offset + 1];
+ break;
+
+ case EndianType.LittleSwap:
+ numArray[0] = value[offset + 1];
+ numArray[1] = value[offset];
+ numArray[2] = value[offset + 3];
+ numArray[3] = value[offset + 2];
+ numArray[4] = value[offset + 5];
+ numArray[5] = value[offset + 4];
+ numArray[6] = value[offset + 7];
+ numArray[7] = value[offset + 6];
+ break;
+
+ case EndianType.Little:
+ numArray[0] = value[offset];
+ numArray[1] = value[offset + 1];
+ numArray[2] = value[offset + 2];
+ numArray[3] = value[offset + 3];
+ numArray[4] = value[offset + 4];
+ numArray[5] = value[offset + 5];
+ numArray[6] = value[offset + 6];
+ numArray[7] = value[offset + 7];
+ break;
+ }
+ return numArray;
+ }
+
+ #endregion Tool
}
}
\ No newline at end of file
diff --git a/src/TouchSocket.Core/Config/SetupConfigObject.cs b/src/TouchSocket.Core/Config/SetupConfigObject.cs
index 0c859c86e..d29443c95 100644
--- a/src/TouchSocket.Core/Config/SetupConfigObject.cs
+++ b/src/TouchSocket.Core/Config/SetupConfigObject.cs
@@ -85,11 +85,6 @@ namespace TouchSocket.Core
{
registrator = new Container();
}
- //if (!registrator.IsRegistered(typeof(IPluginManager)))
- //{
- // registrator.RegisterSingleton();
- //}
-
if (!registrator.IsRegistered(typeof(ILog)))
{
registrator.RegisterSingleton(new LoggerGroup());
diff --git a/src/TouchSocket.Core/Container/ContainerExtension.cs b/src/TouchSocket.Core/Container/ContainerExtension.cs
index 0127d6f27..6e50e784a 100644
--- a/src/TouchSocket.Core/Container/ContainerExtension.cs
+++ b/src/TouchSocket.Core/Container/ContainerExtension.cs
@@ -570,7 +570,7 @@ namespace TouchSocket.Core
///
///
///
- public static object ResolveWithoutRoot(this IResolver resolver, Type fromType)
+ public static object ResolveWithoutRoot(this IServiceProvider resolver, Type fromType)
{
object[] ops = null;
var ctor = fromType.GetConstructors().FirstOrDefault(x => x.IsDefined(typeof(DependencyInjectAttribute), true));
@@ -612,11 +612,11 @@ namespace TouchSocket.Core
{
var attribute = parameters[i].GetCustomAttribute();
var type = attribute.Type ?? parameters[i].ParameterType;
- ps[i] = resolver.Resolve(type, attribute.Key);
+ ps[i] = resolver.GetService(type);
}
else
{
- ps[i] = resolver.Resolve(parameters[i].ParameterType);
+ ps[i] = resolver.GetService(parameters[i].ParameterType);
}
}
}
@@ -635,7 +635,7 @@ namespace TouchSocket.Core
///
///
///
- public static T ResolveWithoutRoot(this IResolver resolver)
+ public static T ResolveWithoutRoot(this IServiceProvider resolver)
{
return (T)ResolveWithoutRoot(resolver, typeof(T));
}
diff --git a/src/TouchSocket.Core/Pool/ByteManager/ByteBlock.cs b/src/TouchSocket.Core/Pool/ByteManager/ByteBlock.cs
index b43f84a4e..0d139cb44 100644
--- a/src/TouchSocket.Core/Pool/ByteManager/ByteBlock.cs
+++ b/src/TouchSocket.Core/Pool/ByteManager/ByteBlock.cs
@@ -10,6 +10,7 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
+using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.IO;
@@ -588,8 +589,8 @@ namespace TouchSocket.Core
}
var length = this.ReadInt32();
var data = new byte[length];
- Array.Copy(this.Buffer, this.Pos, data, 0, length);
- this.Pos += length;
+ Array.Copy(this.Buffer, this.m_position, data, 0, length);
+ this.m_position += length;
return data;
}
@@ -697,131 +698,131 @@ namespace TouchSocket.Core
#region Int32
///
- /// 从当前流位置读取一个值
+ /// 从当前流位置读取一个默认端序的值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public int ReadInt32(bool? bigEndian = null)
+ public int ReadInt32()
{
- int value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt32(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt32(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt32(this.Buffer, this.Pos); break;
- }
+ var value = TouchSocketBitConverter.Default.ToInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
///
- /// 写入值
+ /// 从当前流位置读取一个指定端序的值
+ ///
+ ///
+ public int ReadInt32(EndianType endianType)
+ {
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
+ ///
+ /// 写入默认端序的值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(int value, bool? bigEndian = null)
+ public void Write(int value)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
+ }
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ ///
+ /// 写入指定端序的值
+ ///
+ ///
+ /// 指定端序
+ public void Write(int value, EndianType endianType)
+ {
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int32
#region Int16
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public short ReadInt16()
+ {
+ var value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public short ReadInt16(bool? bigEndian = null)
+ /// 指定端序
+ public short ReadInt16(EndianType endianType)
{
- short value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt16(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt16(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(short value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(short value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(short value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int16
#region Int64
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public long ReadInt64()
+ {
+ long value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public long ReadInt64(bool? bigEndian = null)
+ /// 指定端序
+ public long ReadInt64(EndianType endianType)
{
- long value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt64(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(long value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(long value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(long value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int64
@@ -834,7 +835,7 @@ namespace TouchSocket.Core
public bool ReadBoolean()
{
var value = TouchSocketBitConverter.Default.ToBoolean(this.Buffer, this.Pos);
- this.Pos += 1;
+ this.m_position += 1;
return value;
}
@@ -878,7 +879,7 @@ namespace TouchSocket.Core
else
{
var str = Encoding.UTF8.GetString(this.Buffer, this.Pos, len);
- this.Pos += len;
+ this.m_position += len;
return str;
}
}
@@ -921,264 +922,264 @@ namespace TouchSocket.Core
#region Char
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public char ReadChar()
+ {
+ char value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public char ReadChar(bool? bigEndian = null)
+ /// 指定端序
+ public char ReadChar(EndianType endianType)
{
- char value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToChar(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToChar(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToChar(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(char value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(char value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(char value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Char
#region Double
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public double ReadDouble()
+ {
+ double value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public double ReadDouble(bool? bigEndian = null)
+ /// 指定端序
+ public double ReadDouble(EndianType endianType)
{
- double value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToDouble(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToDouble(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToDouble(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(double value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(double value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(double value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Double
#region Float
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public float ReadFloat()
+ {
+ float value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public float ReadFloat(bool? bigEndian = null)
+ /// 指定端序
+ public float ReadFloat(EndianType endianType)
{
- float value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToSingle(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToSingle(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos); break;
- }
- this.Pos += 4;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToSingle(this.Buffer, this.Pos);
+ this.m_position += 4;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(float value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(float value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(float value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Float
#region UInt16
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public ushort ReadUInt16()
+ {
+ var value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public ushort ReadUInt16(bool? bigEndian = null)
+ /// 指定端序
+ public ushort ReadUInt16(EndianType endianType)
{
- ushort value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt16(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt16(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(ushort value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(ushort value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(ushort value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt16
#region UInt32
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public uint ReadUInt32()
+ {
+ uint value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public uint ReadUInt32(bool? bigEndian = null)
+ /// 指定端序
+ public uint ReadUInt32(EndianType endianType)
{
- uint value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt32(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt32(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos); break;
- }
- this.Pos += 4;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(uint value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(uint value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(uint value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt32
#region UInt64
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public ulong ReadUInt64()
+ {
+ ulong value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public ulong ReadUInt64(bool? bigEndian = null)
+ /// 指定端序
+ public ulong ReadUInt64(EndianType endianType)
{
- ulong value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt64(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt64(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(ulong value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(ulong value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(ulong value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true:
- this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
- break;
-
- case false:
- this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value));
- break;
-
- default:
- this.Write(TouchSocketBitConverter.Default.GetBytes(value));
- break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt64
@@ -1252,8 +1253,8 @@ namespace TouchSocket.Core
///
public DateTime ReadDateTime()
{
- var value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
- this.Pos += 8;
+ var value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return DateTime.FromBinary(value);
}
@@ -1263,7 +1264,7 @@ namespace TouchSocket.Core
///
public void Write(DateTime value)
{
- this.Write(TouchSocketBitConverter.Default.GetBytes(value.ToBinary()));
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value.ToBinary()));
}
#endregion DateTime
@@ -1275,8 +1276,8 @@ namespace TouchSocket.Core
///
public TimeSpan ReadTimeSpan()
{
- var value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
- this.Pos += 8;
+ var value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return TimeSpan.FromTicks(value);
}
@@ -1286,7 +1287,7 @@ namespace TouchSocket.Core
///
public void Write(TimeSpan value)
{
- this.Write(TouchSocketBitConverter.Default.GetBytes(value.Ticks));
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value.Ticks));
}
#endregion TimeSpan
@@ -1342,7 +1343,7 @@ namespace TouchSocket.Core
throw new Exception("未定义的序列化类型");
}
- this.Pos += length;
+ this.m_position += length;
return obj;
}
diff --git a/src/TouchSocket.Core/Pool/ByteManager/ValueByteBlock.cs b/src/TouchSocket.Core/Pool/ByteManager/ValueByteBlock.cs
index ff98723bc..4022bb407 100644
--- a/src/TouchSocket.Core/Pool/ByteManager/ValueByteBlock.cs
+++ b/src/TouchSocket.Core/Pool/ByteManager/ValueByteBlock.cs
@@ -516,8 +516,8 @@ namespace TouchSocket.Core
}
var length = this.ReadInt32();
var data = new byte[length];
- Array.Copy(this.Buffer, this.Pos, data, 0, length);
- this.Pos += length;
+ Array.Copy(this.Buffer, this.m_position, data, 0, length);
+ this.m_position += length;
return data;
}
@@ -579,110 +579,177 @@ namespace TouchSocket.Core
#endregion BytesPackage
+ #region ByteBlock
+
+ ///
+ /// 从当前流位置读取一个值。
+ ///
+ /// 注意,使用该方式读取到的内存块,会脱离释放周期,所以最好在使用完成后自行释放。
+ ///
+ ///
+ public ByteBlock ReadByteBlock()
+ {
+ if (this.ReadIsNull())
+ {
+ return default;
+ }
+
+ if (!this.TryReadBytesPackageInfo(out var pos, out var len))
+ {
+ return default;
+ }
+ var byteBlock = new ByteBlock(len);
+ byteBlock.Write(this.Buffer, pos, len);
+ this.m_position += len;
+ return byteBlock;
+ }
+
+ ///
+ /// 写入值
+ ///
+ public void WriteByteBlock(ByteBlock byteBlock)
+ {
+ if (byteBlock is null)
+ {
+ this.WriteNull();
+ }
+ else
+ {
+ this.WriteNotNull();
+ this.WriteBytesPackage(byteBlock.Buffer, 0, byteBlock.Len);
+ }
+ }
+
+ #endregion ByteBlock
+
#region Int32
///
- /// 从当前流位置读取一个值
+ /// 从当前流位置读取一个默认端序的值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public int ReadInt32(bool? bigEndian = null)
+ public int ReadInt32()
{
- int value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt32(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt32(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt32(this.Buffer, this.Pos); break;
- }
+ var value = TouchSocketBitConverter.Default.ToInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
///
- /// 写入值
+ /// 从当前流位置读取一个指定端序的值
+ ///
+ ///
+ public int ReadInt32(EndianType endianType)
+ {
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
+ ///
+ /// 写入默认端序的值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(int value, bool? bigEndian = null)
+ public void Write(int value)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value));
+ }
+
+ ///
+ /// 写入指定端序的值
+ ///
+ ///
+ /// 指定端序
+ public void Write(int value, EndianType endianType)
+ {
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int32
#region Int16
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public short ReadInt16()
+ {
+ var value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public short ReadInt16(bool? bigEndian = null)
+ /// 指定端序
+ public short ReadInt16(EndianType endianType)
{
- short value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt16(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt16(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(short value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(short value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(short value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int16
#region Int64
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public long ReadInt64()
+ {
+ long value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public long ReadInt64(bool? bigEndian = null)
+ /// 指定端序
+ public long ReadInt64(EndianType endianType)
{
- long value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToInt64(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(long value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(long value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(long value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int64
@@ -695,7 +762,7 @@ namespace TouchSocket.Core
public bool ReadBoolean()
{
var value = TouchSocketBitConverter.Default.ToBoolean(this.Buffer, this.Pos);
- this.Pos += 1;
+ this.m_position += 1;
return value;
}
@@ -739,7 +806,7 @@ namespace TouchSocket.Core
else
{
var str = Encoding.UTF8.GetString(this.Buffer, this.Pos, len);
- this.Pos += len;
+ this.m_position += len;
return str;
}
}
@@ -782,216 +849,264 @@ namespace TouchSocket.Core
#region Char
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public char ReadChar()
+ {
+ char value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public char ReadChar(bool? bigEndian = null)
+ /// 指定端序
+ public char ReadChar(EndianType endianType)
{
- char value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToChar(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToChar(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToChar(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(char value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(char value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(char value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Char
#region Double
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public double ReadDouble()
+ {
+ double value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public double ReadDouble(bool? bigEndian = null)
+ /// 指定端序
+ public double ReadDouble(EndianType endianType)
{
- double value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToDouble(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToDouble(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToDouble(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(double value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(double value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(double value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Double
#region Float
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public float ReadFloat()
+ {
+ float value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public float ReadFloat(bool? bigEndian = null)
+ /// 指定端序
+ public float ReadFloat(EndianType endianType)
{
- float value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToSingle(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToSingle(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos); break;
- }
- this.Pos += 4;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToSingle(this.Buffer, this.Pos);
+ this.m_position += 4;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(float value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(float value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(float value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Float
#region UInt16
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public ushort ReadUInt16()
+ {
+ var value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public ushort ReadUInt16(bool? bigEndian = null)
+ /// 指定端序
+ public ushort ReadUInt16(EndianType endianType)
{
- ushort value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt16(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt16(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos); break;
- }
- this.Pos += 2;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt16(this.Buffer, this.Pos);
+ this.m_position += 2;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(ushort value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(ushort value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(ushort value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt16
#region UInt32
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public uint ReadUInt32()
+ {
+ uint value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public uint ReadUInt32(bool? bigEndian = null)
+ /// 指定端序
+ public uint ReadUInt32(EndianType endianType)
{
- uint value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt32(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt32(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos); break;
- }
- this.Pos += 4;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt32(this.Buffer, this.Pos);
+ this.m_position += 4;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(uint value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(uint value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(uint value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt32
#region UInt64
+ ///
+ /// 从当前流位置读取一个默认端序的值
+ ///
+ public ulong ReadUInt64()
+ {
+ ulong value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
+ return value;
+ }
+
///
/// 从当前流位置读取一个值
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public ulong ReadUInt64(bool? bigEndian = null)
+ /// 指定端序
+ public ulong ReadUInt64(EndianType endianType)
{
- ulong value;
- switch (bigEndian)
- {
- case true: value = TouchSocketBitConverter.BigEndian.ToUInt64(this.Buffer, this.Pos); break;
- case false: value = TouchSocketBitConverter.LittleEndian.ToUInt64(this.Buffer, this.Pos); break;
- default: value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos); break;
- }
- this.Pos += 8;
+ var value = TouchSocketBitConverter.GetBitConverter(endianType).ToUInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return value;
}
+ ///
+ /// 写入默认端序的值
+ ///
+ ///
+ public void Write(ulong value)
+ {
+ this.Write(TouchSocketBitConverter.Default.GetBytes(value));
+ }
+
///
/// 写入值
///
///
- /// 是否为指定大端编码。允许true(大端),false(小端),null(默认端序)三种赋值。默认为null。
- public void Write(ulong value, bool? bigEndian = null)
+ /// 指定端序
+ public void Write(ulong value, EndianType endianType)
{
- switch (bigEndian)
- {
- case true: this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value)); break;
- case false: this.Write(TouchSocketBitConverter.LittleEndian.GetBytes(value)); break;
- default: this.Write(TouchSocketBitConverter.Default.GetBytes(value)); break;
- }
+ this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion UInt64
@@ -1065,8 +1180,8 @@ namespace TouchSocket.Core
///
public DateTime ReadDateTime()
{
- var value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
- this.Pos += 8;
+ var value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return DateTime.FromBinary(value);
}
@@ -1076,7 +1191,7 @@ namespace TouchSocket.Core
///
public void Write(DateTime value)
{
- this.Write(TouchSocketBitConverter.Default.GetBytes(value.ToBinary()));
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value.ToBinary()));
}
#endregion DateTime
@@ -1088,8 +1203,8 @@ namespace TouchSocket.Core
///
public TimeSpan ReadTimeSpan()
{
- var value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
- this.Pos += 8;
+ var value = TouchSocketBitConverter.BigEndian.ToInt64(this.Buffer, this.Pos);
+ this.m_position += 8;
return TimeSpan.FromTicks(value);
}
@@ -1099,7 +1214,7 @@ namespace TouchSocket.Core
///
public void Write(TimeSpan value)
{
- this.Write(TouchSocketBitConverter.Default.GetBytes(value.Ticks));
+ this.Write(TouchSocketBitConverter.BigEndian.GetBytes(value.Ticks));
}
#endregion TimeSpan
@@ -1155,7 +1270,7 @@ namespace TouchSocket.Core
throw new Exception("未定义的序列化类型");
}
- this.Pos += length;
+ this.m_position += length;
return obj;
}
diff --git a/src/TouchSocket.Core/WaitPool/WaitHandlePool.cs b/src/TouchSocket.Core/WaitPool/WaitHandlePool.cs
index 69c770f96..efa4bda06 100644
--- a/src/TouchSocket.Core/WaitPool/WaitHandlePool.cs
+++ b/src/TouchSocket.Core/WaitPool/WaitHandlePool.cs
@@ -26,9 +26,9 @@ namespace TouchSocket.Core
private readonly ConcurrentDictionary> m_waitDicAsync;
private readonly ConcurrentQueue> m_waitQueue;
private readonly ConcurrentQueue> m_waitQueueAsync;
-
+ private long m_maxSign = long.MaxValue;
+ private long m_minSign = long.MinValue;
private long m_waitCount;
-
private long m_waitReverseCount;
///
@@ -42,6 +42,16 @@ namespace TouchSocket.Core
this.m_waitQueueAsync = new ConcurrentQueue>();
}
+ ///
+ /// 最大Sign
+ ///
+ public long MaxSign { get => m_maxSign; set => m_maxSign = value; }
+
+ ///
+ /// 最小Sign
+ ///
+ public long MinSign { get => m_minSign; set => m_minSign = value; }
+
///
/// 取消全部
///
@@ -368,19 +378,6 @@ namespace TouchSocket.Core
return result;
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private long GetSign(bool reverse)
- {
- if (reverse)
- {
- return Interlocked.Decrement(ref this.m_waitReverseCount);
- }
- else
- {
- return Interlocked.Increment(ref this.m_waitCount);
- }
- }
-
///
///
///
@@ -400,5 +397,20 @@ namespace TouchSocket.Core
this.m_waitQueue.Clear();
base.Dispose(disposing);
}
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private long GetSign(bool reverse)
+ {
+ if (reverse)
+ {
+ Interlocked.CompareExchange(ref this.m_waitReverseCount, 0, this.m_minSign);
+ return Interlocked.Decrement(ref this.m_waitReverseCount);
+ }
+ else
+ {
+ Interlocked.CompareExchange(ref this.m_waitCount, 0, this.m_maxSign);
+ return Interlocked.Increment(ref this.m_waitCount);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/TouchSocket.Hosting/Extensions/ServiceCollectionExtensions.cs b/src/TouchSocket.Hosting/Extensions/ServiceCollectionExtensions.cs
index 4421a6683..ddb879d29 100644
--- a/src/TouchSocket.Hosting/Extensions/ServiceCollectionExtensions.cs
+++ b/src/TouchSocket.Hosting/Extensions/ServiceCollectionExtensions.cs
@@ -11,6 +11,7 @@
//------------------------------------------------------------------------------
using System;
+using System.ComponentModel;
using TouchSocket.Core;
using TouchSocket.Core.AspNetCore;
using TouchSocket.Hosting;
@@ -24,26 +25,109 @@ namespace Microsoft.Extensions.DependencyInjection
///
public static class ServiceCollectionExtensions
{
- #region HostedService
-
+ #region SetupConfig
///
- /// 添加Client类型的HostedService服务。
- ///
- /// 这类服务必须实现与。同时,服务不做其他处理。例如连接,需要自己调用。或者做无人值守重连。
- ///
+ /// 添加SingletonSetupConfigObject服务。
///
- ///
- ///
+ ///
+ ///
///
///
///
- public static IServiceCollection AddClientHostedService(this IServiceCollection services, Action actionConfig)
- where TObjectClient : class, ISetupConfigObject, ICloseObject
- where TObjectImpClient : class, TObjectClient
+ public static IServiceCollection AddSingletonSetupConfigObject(this IServiceCollection services, Action actionConfig)
+ where TObjectService : class, ISetupConfigObject
+ where TObjectImpService : class, TObjectService
{
- return AddSetupConfigObjectHostedService, TObjectClient, TObjectImpClient>(services, actionConfig);
+ var aspNetCoreContainer = new AspNetCoreContainer(services);
+ var config = new TouchSocketConfig();
+ config.SetRegistrator(aspNetCoreContainer);
+ actionConfig.Invoke(config);
+ if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action actionContainer)
+ {
+ actionContainer.Invoke(aspNetCoreContainer);
+ }
+
+ services.AddSingleton(privoder =>
+ {
+ var imp = privoder.ResolveWithoutRoot();
+ config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
+ aspNetCoreContainer.BuildResolver(privoder);
+ config.SetResolver(aspNetCoreContainer);
+ imp.Setup(config);
+ return imp;
+ });
+ return services;
}
+ ///
+ /// 添加TransientSetupConfigObject服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientSetupConfigObject(this IServiceCollection services, Action actionConfig)
+ where TObjectService : class, ISetupConfigObject
+ where TObjectImpService : class, TObjectService
+ {
+ var aspNetCoreContainer = new AspNetCoreContainer(services);
+ var config = new TouchSocketConfig();
+ config.SetRegistrator(aspNetCoreContainer);
+ actionConfig.Invoke(config);
+ if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action actionContainer)
+ {
+ actionContainer.Invoke(aspNetCoreContainer);
+ }
+
+ services.AddTransient(privoder =>
+ {
+ var imp = privoder.ResolveWithoutRoot();
+ config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
+ aspNetCoreContainer.BuildResolver(privoder);
+ config.SetResolver(aspNetCoreContainer);
+ imp.Setup(config);
+ return imp;
+ });
+ return services;
+ }
+
+ ///
+ /// 添加ScopedSetupConfigObject服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddScopedSetupConfigObject(this IServiceCollection services, Action actionConfig)
+ where TObjectService : class, ISetupConfigObject
+ where TObjectImpService : class, TObjectService
+ {
+ var aspNetCoreContainer = new AspNetCoreContainer(services);
+ var config = new TouchSocketConfig();
+ config.SetRegistrator(aspNetCoreContainer);
+ actionConfig.Invoke(config);
+ if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action actionContainer)
+ {
+ actionContainer.Invoke(aspNetCoreContainer);
+ }
+
+ services.AddScoped(privoder =>
+ {
+ var imp = privoder.ResolveWithoutRoot();
+ config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
+ aspNetCoreContainer.BuildResolver(privoder);
+ config.SetResolver(aspNetCoreContainer);
+ imp.Setup(config);
+ return imp;
+ });
+ return services;
+ }
+ #endregion
+
+ #region HostedService
+
///
/// 添加Service类型的HostedService服务。这类服务必须实现与
///
@@ -159,31 +243,84 @@ namespace Microsoft.Extensions.DependencyInjection
#region TcpClient
///
- /// 添加TcpClient服务。
+ /// 添加单例TcpClient服务。
///
///
///
///
///
///
- public static IServiceCollection AddTcpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddSingletonTcpClient(this IServiceCollection services, Action actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
- return AddClientHostedService(services, actionConfig);
+ return AddSingletonSetupConfigObject(services, actionConfig);
}
///
- /// 添加TcpClient服务。并使用注册服务。
+ /// 添加单例TcpClient服务。并使用注册服务。
///
///
///
///
- public static IServiceCollection AddTcpClient(this IServiceCollection services, Action actionConfig)
+ public static IServiceCollection AddSingletonTcpClient(this IServiceCollection services, Action actionConfig)
{
- return services.AddTcpClient(actionConfig);
+ return services.AddSingletonTcpClient(actionConfig);
}
+
+ ///
+ /// 添加瞬态TcpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientTcpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, ITcpClient
+ where TImpClient : class, TClient
+ {
+ return AddTransientSetupConfigObject(services, actionConfig);
+ }
+
+ ///
+ /// 添加瞬态TcpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddTransientTcpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddTransientTcpClient(actionConfig);
+ }
+
+
+ ///
+ /// 添加Scoped TcpClient服务。
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddScopedTcpClient(this IServiceCollection services, Action actionConfig)
+ where TClient : class, ITcpClient
+ where TImpClient : class, TClient
+ {
+ return AddScopedSetupConfigObject(services, actionConfig);
+ }
+
+ ///
+ /// 添加Scoped TcpClient服务。并使用注册服务。
+ ///
+ ///
+ ///
+ ///
+ public static IServiceCollection AddScopedTcpClient(this IServiceCollection services, Action actionConfig)
+ {
+ return services.AddScopedTcpClient(actionConfig);
+ }
#endregion TcpClient
}
}
\ No newline at end of file
diff --git a/src/TouchSocket.Hosting/HostedServices/ClientHost.cs b/src/TouchSocket.Hosting/HostedServices/ClientHost.cs
deleted file mode 100644
index b96163a07..000000000
--- a/src/TouchSocket.Hosting/HostedServices/ClientHost.cs
+++ /dev/null
@@ -1,54 +0,0 @@
-//------------------------------------------------------------------------------
-// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
-// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
-// CSDN博客:https://blog.csdn.net/qq_40374647
-// 哔哩哔哩视频:https://space.bilibili.com/94253567
-// Gitee源代码仓库:https://gitee.com/RRQM_Home
-// Github源代码仓库:https://github.com/RRQM
-// API首页:http://rrqm_home.gitee.io/touchsocket/
-// 交流QQ群:234762506
-// 感谢您的下载和使用
-//------------------------------------------------------------------------------
-
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
-using Microsoft.Extensions.Logging;
-using System;
-using System.Threading;
-using System.Threading.Tasks;
-using TouchSocket.Core;
-using TouchSocket.Core.AspNetCore;
-using TouchSocket.Sockets;
-
-namespace TouchSocket.Hosting.Sockets.HostService
-{
- internal class ClientHost : SetupConfigObjectHostedService where TClient : ISetupConfigObject,ICloseObject
- {
- private ILogger> m_logger;
-
- public override async Task StartAsync(CancellationToken cancellationToken)
- {
- try
- {
- await base.StartAsync(cancellationToken);
- this.m_logger.LogInformation($"{typeof(TClient)}启动");
- }
- catch (Exception ex)
- {
- this.m_logger.LogError(ex, ex.Message);
- }
- }
-
- public override async Task StopAsync(CancellationToken cancellationToken)
- {
- this.ConfigObject.Close();
- await EasyTask.CompletedTask;
- }
-
- protected override void OnSetResolver(IResolver resolver)
- {
- base.OnSetResolver(resolver);
- this.m_logger = resolver.GetService>>();
- }
- }
-}
\ No newline at end of file
diff --git a/src/TouchSocket/Components/Tcp/TcpClient.cs b/src/TouchSocket/Components/Tcp/TcpClient.cs
index 4eaf2877c..b1c2b955e 100644
--- a/src/TouchSocket/Components/Tcp/TcpClient.cs
+++ b/src/TouchSocket/Components/Tcp/TcpClient.cs
@@ -256,7 +256,7 @@ namespace TouchSocket.Sockets
#region 断开操作
///
- public virtual void Close(string msg = TouchSocketCoreUtility.Empty)
+ public virtual void Close(string msg)
{
lock (this.GetTcpCore())
{