更新Hosting机制。

新增交换大小端序
优化内存池写入和读取端序
This commit is contained in:
若汝棋茗
2023-12-15 20:29:01 +08:00
parent f3e0edfb2d
commit 0cd88f1a6f
13 changed files with 1118 additions and 578 deletions

View File

@@ -3,7 +3,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildProjectName).snk</AssemblyOriginatorKeyFile>
<Version>2.0.0-beta.239</Version>
<Version>2.0.0-beta.241</Version>
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Company>若汝棋茗</Company>
@@ -34,6 +34,9 @@
<PropertyGroup Condition="'$(TargetFramework)'=='net481'">
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net461'">
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Include="LICENSE.txt">
<Pack>True</Pack>

View File

@@ -55,29 +55,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region TcpDmtpClient
/// <summary>
/// 添加TcpDmtpClient服务。
/// 添加Scoped TcpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedTcpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpDmtpClient
where TImpClient : class, TClient
{
return services.AddTcpClient<TClient, TImpClient>(actionConfig);
return services.AddScopedTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加TcpDmtpClient服务。并使用<see cref="ITcpDmtpClient"/>注册服务。
/// 添加Scoped TcpDmtpClient服务。并使用<see cref="ITcpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedTcpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTcpDmtpClient<ITcpDmtpClient, TcpDmtpClient>(actionConfig);
return services.AddScopedTcpDmtpClient<ITcpDmtpClient, TcpDmtpClient>(actionConfig);
}
/// <summary>
/// 添加单例TcpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonTcpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpDmtpClient
where TImpClient : class, TClient
{
return services.AddSingletonTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加单例TcpDmtpClient服务。并使用<see cref="ITcpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonTcpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddSingletonTcpDmtpClient<ITcpDmtpClient, TcpDmtpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态TcpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpDmtpClient
where TImpClient : class, TClient
{
return services.AddTransientTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态TcpDmtpClient服务。并使用<see cref="ITcpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTransientTcpDmtpClient<ITcpDmtpClient, TcpDmtpClient>(actionConfig);
}
#endregion TcpDmtpClient
@@ -115,29 +167,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region HttpClient
/// <summary>
/// 添加HttpClient服务。
/// 添加Scoped HttpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddHttpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedHttpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpClient
where TImpClient : class, TClient
{
return services.AddTcpClient<TClient, TImpClient>(actionConfig);
return services.AddScopedTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加HttpClient服务。并使用<see cref="IHttpClient"/>注册服务。
/// 添加Scoped HttpClient服务。并使用<see cref="IHttpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddHttpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedHttpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddHttpClient<IHttpClient, HttpClient>(actionConfig);
return services.AddScopedHttpClient<IHttpClient, HttpClient>(actionConfig);
}
/// <summary>
/// 添加单例HttpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonHttpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpClient
where TImpClient : class, TClient
{
return services.AddSingletonTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加单例HttpClient服务。并使用<see cref="IHttpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonHttpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddSingletonHttpClient<IHttpClient, HttpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态HttpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientHttpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpClient
where TImpClient : class, TClient
{
return services.AddTransientTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态HttpClient服务。并使用<see cref="IHttpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientHttpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTransientHttpClient<IHttpClient, HttpClient>(actionConfig);
}
#endregion HttpClient
@@ -175,29 +279,81 @@ namespace Microsoft.Extensions.DependencyInjection
#region HttpDmtpClient
/// <summary>
/// 添加HttpDmtpClient服务。
/// 添加Scoped HttpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddHttpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedHttpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpDmtpClient
where TImpClient : class, TClient
{
return services.AddTcpClient<TClient, TImpClient>(actionConfig);
return services.AddScopedTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加HttpDmtpClient服务。并使用<see cref="IHttpDmtpClient"/>注册服务。
/// 添加Scoped HttpDmtpClient服务。并使用<see cref="IHttpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddHttpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddScopedHttpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddHttpDmtpClient<IHttpDmtpClient, HttpDmtpClient>(actionConfig);
return services.AddScopedHttpDmtpClient<IHttpDmtpClient, HttpDmtpClient>(actionConfig);
}
/// <summary>
/// 添加单例HttpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonHttpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpDmtpClient
where TImpClient : class, TClient
{
return services.AddSingletonTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加单例HttpDmtpClient服务。并使用<see cref="IHttpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonHttpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddSingletonHttpDmtpClient<IHttpDmtpClient, HttpDmtpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态HttpDmtpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientHttpDmtpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, IHttpDmtpClient
where TImpClient : class, TClient
{
return services.AddTransientTcpClient<TClient, TImpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态HttpDmtpClient服务。并使用<see cref="IHttpDmtpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientHttpDmtpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTransientHttpDmtpClient<IHttpDmtpClient, HttpDmtpClient>(actionConfig);
}
#endregion HttpDmtpClient

View File

@@ -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<IResolver>(this);
services.AddSingleton<IResolver>(privoder =>
{
this.m_serviceProvider ??= privoder;
return this;
});
}
/// <inheritdoc/>

View File

@@ -18,13 +18,23 @@ namespace TouchSocket.Core
public enum EndianType
{
/// <summary>
/// 小端模式
/// 小端模式即DCBA
/// </summary>
Little,
/// <summary>
/// 大端模式
/// 大端模式。即ABCD
/// </summary>
Big
Big,
/// <summary>
/// 以交换小端格式。即CDAB
/// </summary>
LittleSwap,
/// <summary>
/// 以交换大端BADC
/// </summary>
BigSwap
}
}

View File

@@ -10,6 +10,7 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
using Newtonsoft.Json.Linq;
using System;
using System.Runtime.CompilerServices;
@@ -19,32 +20,79 @@ namespace TouchSocket.Core
/// 将基数据类型转换为指定端的一个字节数组,
/// 或将一个字节数组转换为指定端基数据类型。
/// </summary>
public class TouchSocketBitConverter
public sealed class TouchSocketBitConverter
{
/// <summary>
/// 以大端
/// </summary>
public static TouchSocketBitConverter BigEndian;
public static readonly TouchSocketBitConverter BigEndian;
/// <summary>
/// 以交换大端
/// </summary>
public static readonly TouchSocketBitConverter BigSwapEndian;
/// <summary>
/// 以小端
/// </summary>
public static TouchSocketBitConverter LittleEndian;
public static readonly TouchSocketBitConverter LittleEndian;
/// <summary>
/// 以交换小端
/// </summary>
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;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="endianType"></param>
public TouchSocketBitConverter(EndianType endianType)
{
this.EndianType = endianType;
}
/// <summary>
/// 按照枚举值选择默认的端序。
/// </summary>
/// <param name="endianType"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
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("没有该选项");
}
}
/// <summary>
/// 以默认小端,可通过<see cref="TouchSocketBitConverter.DefaultEndianType"/>重新指定默认端。
/// </summary>
public static TouchSocketBitConverter Default { get; private set; }
private static EndianType m_defaultEndianType;
/// <summary>
/// 默认大小端切换。
/// </summary>
@@ -64,21 +112,20 @@ namespace TouchSocket.Core
Default = BigEndian;
break;
case EndianType.LittleSwap:
Default = LittleSwapEndian;
break;
case EndianType.BigSwap:
Default = BigSwapEndian;
break;
default:
break;
}
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="endianType"></param>
public TouchSocketBitConverter(EndianType endianType)
{
this.EndianType = endianType;
}
/// <summary>
/// 指定大小端。
/// </summary>
@@ -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);
}
/// <summary>
/// 将布尔数组转为字节数组。不足位补0.
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
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;
}
/// <summary>
/// 转换为指定端模式的bool数据。
/// </summary>
@@ -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
/// <summary>反转多字节的数据信息</summary>
/// <param name="value">数据字节</param>
/// <param name="offset">起始索引默认值为0</param>
/// <returns>实际字节信息</returns>
[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;
}
/// <summary>反转多字节的数据信息</summary>
/// <param name="value">数据字节</param>
/// <param name="offset">起始索引默认值为0</param>
/// <returns>实际字节信息</returns>
[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
}
}

View File

@@ -85,11 +85,6 @@ namespace TouchSocket.Core
{
registrator = new Container();
}
//if (!registrator.IsRegistered(typeof(IPluginManager)))
//{
// registrator.RegisterSingleton<IPluginManager, PluginManager>();
//}
if (!registrator.IsRegistered(typeof(ILog)))
{
registrator.RegisterSingleton<ILog>(new LoggerGroup());

View File

@@ -570,7 +570,7 @@ namespace TouchSocket.Core
/// <param name="fromType"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
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<DependencyInjectAttribute>();
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
/// <param name="resolver"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static T ResolveWithoutRoot<T>(this IResolver resolver)
public static T ResolveWithoutRoot<T>(this IServiceProvider resolver)
{
return (T)ResolveWithoutRoot(resolver, typeof(T));
}

View File

@@ -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
/// <summary>
/// 从当前流位置读取一个<see cref="int"/>值
/// 从当前流位置读取一个默认端序的<see cref="int"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
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;
}
/// <summary>
/// 写入<see cref="int"/>值
/// 从当前流位置读取一个指定端序的<see cref="int"/>值
/// </summary>
/// <param name="endianType"></param>
public int ReadInt32(EndianType endianType)
{
var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 写入默认端序的<see cref="int"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
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;
}
/// <summary>
/// 写入指定端序的<see cref="int"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="endianType">指定端序</param>
public void Write(int value, EndianType endianType)
{
this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int32
#region Int16
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="short"/>值
/// </summary>
public short ReadInt16()
{
var value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="short"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public short ReadInt16(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="short"/>值
/// </summary>
/// <param name="value"></param>
public void Write(short value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="short"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(short value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="long"/>值
/// </summary>
public long ReadInt64()
{
long value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="long"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public long ReadInt64(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="long"/>值
/// </summary>
/// <param name="value"></param>
public void Write(long value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="long"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(long value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="char"/>值
/// </summary>
public char ReadChar()
{
char value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="char"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public char ReadChar(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="char"/>值
/// </summary>
/// <param name="value"></param>
public void Write(char value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="char"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(char value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="double"/>值
/// </summary>
public double ReadDouble()
{
double value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="double"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public double ReadDouble(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="double"/>值
/// </summary>
/// <param name="value"></param>
public void Write(double value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="double"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(double value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="float"/>值
/// </summary>
public float ReadFloat()
{
float value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="float"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public float ReadFloat(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="float"/>值
/// </summary>
/// <param name="value"></param>
public void Write(float value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="float"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(float value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="ushort"/>值
/// </summary>
public ushort ReadUInt16()
{
var value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="ushort"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public ushort ReadUInt16(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="ushort"/>值
/// </summary>
/// <param name="value"></param>
public void Write(ushort value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="ushort"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(ushort value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="uint"/>值
/// </summary>
public uint ReadUInt32()
{
uint value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="uint"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public uint ReadUInt32(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="uint"/>值
/// </summary>
/// <param name="value"></param>
public void Write(uint value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="uint"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(uint value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="ulong"/>值
/// </summary>
public ulong ReadUInt64()
{
ulong value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="ulong"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public ulong ReadUInt64(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="ulong"/>值
/// </summary>
/// <param name="value"></param>
public void Write(ulong value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="ulong"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(ulong value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// </summary>
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
/// <param name="value"></param>
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
/// </summary>
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
/// <param name="value"></param>
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;
}

View File

@@ -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
/// <summary>
/// 从当前流位置读取一个<see cref="ByteBlock"/>值。
/// <para>
/// 注意,使用该方式读取到的内存块,会脱离释放周期,所以最好在使用完成后自行释放。
/// </para>
/// </summary>
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;
}
/// <summary>
/// 写入<see cref="ByteBlock"/>值
/// </summary>
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
/// <summary>
/// 从当前流位置读取一个<see cref="int"/>值
/// 从当前流位置读取一个默认端序的<see cref="int"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
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;
}
/// <summary>
/// 写入<see cref="int"/>值
/// 从当前流位置读取一个指定端序的<see cref="int"/>值
/// </summary>
/// <param name="endianType"></param>
public int ReadInt32(EndianType endianType)
{
var value = TouchSocketBitConverter.GetBitConverter(endianType).ToInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 写入默认端序的<see cref="int"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
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));
}
/// <summary>
/// 写入指定端序的<see cref="int"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="endianType">指定端序</param>
public void Write(int value, EndianType endianType)
{
this.Write(TouchSocketBitConverter.GetBitConverter(endianType).GetBytes(value));
}
#endregion Int32
#region Int16
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="short"/>值
/// </summary>
public short ReadInt16()
{
var value = TouchSocketBitConverter.Default.ToInt16(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="short"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public short ReadInt16(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="short"/>值
/// </summary>
/// <param name="value"></param>
public void Write(short value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="short"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(short value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="long"/>值
/// </summary>
public long ReadInt64()
{
long value = TouchSocketBitConverter.Default.ToInt64(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="long"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public long ReadInt64(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="long"/>值
/// </summary>
/// <param name="value"></param>
public void Write(long value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="long"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(long value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="char"/>值
/// </summary>
public char ReadChar()
{
char value = TouchSocketBitConverter.Default.ToChar(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="char"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public char ReadChar(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="char"/>值
/// </summary>
/// <param name="value"></param>
public void Write(char value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="char"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(char value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="double"/>值
/// </summary>
public double ReadDouble()
{
double value = TouchSocketBitConverter.Default.ToDouble(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="double"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public double ReadDouble(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="double"/>值
/// </summary>
/// <param name="value"></param>
public void Write(double value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="double"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(double value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="float"/>值
/// </summary>
public float ReadFloat()
{
float value = TouchSocketBitConverter.Default.ToSingle(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="float"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public float ReadFloat(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="float"/>值
/// </summary>
/// <param name="value"></param>
public void Write(float value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="float"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(float value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="ushort"/>值
/// </summary>
public ushort ReadUInt16()
{
var value = TouchSocketBitConverter.Default.ToUInt16(this.Buffer, this.Pos);
this.m_position += 2;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="ushort"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public ushort ReadUInt16(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="ushort"/>值
/// </summary>
/// <param name="value"></param>
public void Write(ushort value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="ushort"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(ushort value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="uint"/>值
/// </summary>
public uint ReadUInt32()
{
uint value = TouchSocketBitConverter.Default.ToUInt32(this.Buffer, this.Pos);
this.m_position += 4;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="uint"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public uint ReadUInt32(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="uint"/>值
/// </summary>
/// <param name="value"></param>
public void Write(uint value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="uint"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(uint value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// <summary>
/// 从当前流位置读取一个默认端序的<see cref="ulong"/>值
/// </summary>
public ulong ReadUInt64()
{
ulong value = TouchSocketBitConverter.Default.ToUInt64(this.Buffer, this.Pos);
this.m_position += 8;
return value;
}
/// <summary>
/// 从当前流位置读取一个<see cref="ulong"/>值
/// </summary>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public ulong ReadUInt64(bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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;
}
/// <summary>
/// 写入默认端序的<see cref="ulong"/>值
/// </summary>
/// <param name="value"></param>
public void Write(ulong value)
{
this.Write(TouchSocketBitConverter.Default.GetBytes(value));
}
/// <summary>
/// 写入<see cref="ulong"/>值
/// </summary>
/// <param name="value"></param>
/// <param name="bigEndian">是否为指定大端编码。允许true大端false小端null默认端序三种赋值。默认为null。</param>
public void Write(ulong value, bool? bigEndian = null)
/// <param name="endianType">指定端序</param>
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
/// </summary>
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
/// <param name="value"></param>
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
/// </summary>
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
/// <param name="value"></param>
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;
}

View File

@@ -26,9 +26,9 @@ namespace TouchSocket.Core
private readonly ConcurrentDictionary<long, WaitDataAsync<T>> m_waitDicAsync;
private readonly ConcurrentQueue<WaitData<T>> m_waitQueue;
private readonly ConcurrentQueue<WaitDataAsync<T>> m_waitQueueAsync;
private long m_maxSign = long.MaxValue;
private long m_minSign = long.MinValue;
private long m_waitCount;
private long m_waitReverseCount;
/// <summary>
@@ -42,6 +42,16 @@ namespace TouchSocket.Core
this.m_waitQueueAsync = new ConcurrentQueue<WaitDataAsync<T>>();
}
/// <summary>
/// 最大Sign
/// </summary>
public long MaxSign { get => m_maxSign; set => m_maxSign = value; }
/// <summary>
/// 最小Sign
/// </summary>
public long MinSign { get => m_minSign; set => m_minSign = value; }
/// <summary>
/// 取消全部
/// </summary>
@@ -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);
}
}
/// <summary>
/// <inheritdoc/>
/// </summary>
@@ -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);
}
}
}
}

View File

@@ -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
/// </summary>
public static class ServiceCollectionExtensions
{
#region HostedService
#region SetupConfig
/// <summary>
/// 添加Client类型的HostedService服务。
/// <para>
/// 这类服务必须实现<see cref="ISetupConfigObject"/>与<see cref="ICloseObject"/>。同时,服务不做其他处理。例如连接,需要自己调用。或者做无人值守重连。
/// </para>
/// 添加SingletonSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectClient"></typeparam>
/// <typeparam name="TObjectImpClient"></typeparam>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddClientHostedService<TObjectClient, TObjectImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectClient : class, ISetupConfigObject, ICloseObject
where TObjectImpClient : class, TObjectClient
public static IServiceCollection AddSingletonSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectService : class, ISetupConfigObject
where TObjectImpService : class, TObjectService
{
return AddSetupConfigObjectHostedService<ClientHost<TObjectClient>, 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<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddSingleton<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
/// <summary>
/// 添加TransientSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> 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<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddTransient<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
/// <summary>
/// 添加ScopedSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> 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<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddScoped<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
#endregion
#region HostedService
/// <summary>
/// 添加Service类型的HostedService服务。这类服务必须实现<see cref="ISetupConfigObject"/>与<see cref="IService"/>
/// </summary>
@@ -159,31 +243,84 @@ namespace Microsoft.Extensions.DependencyInjection
#region TcpClient
/// <summary>
/// 添加TcpClient服务。
/// 添加单例TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddSingletonTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddClientHostedService<TClient, TImpClient>(services, actionConfig);
return AddSingletonSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// 添加单例TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
public static IServiceCollection AddSingletonTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTcpClient<ITcpClient, TcpClient>(actionConfig);
return services.AddSingletonTcpClient<ITcpClient, TcpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddTransientSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加瞬态TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTransientTcpClient<ITcpClient, TcpClient>(actionConfig);
}
/// <summary>
/// 添加Scoped TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddScopedSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加Scoped TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddScopedTcpClient<ITcpClient, TcpClient>(actionConfig);
}
#endregion TcpClient
}
}

View File

@@ -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<TClient> : SetupConfigObjectHostedService<TClient> where TClient : ISetupConfigObject,ICloseObject
{
private ILogger<ClientHost<TClient>> 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<ILogger<ClientHost<TClient>>>();
}
}
}

View File

@@ -256,7 +256,7 @@ namespace TouchSocket.Sockets
#region
/// <inheritdoc/>
public virtual void Close(string msg = TouchSocketCoreUtility.Empty)
public virtual void Close(string msg)
{
lock (this.GetTcpCore())
{