提交 16d96c37 编写于 作者: 若汝棋茗

发布0.7.0 Aspnetcore

上级 988cfdfe
......@@ -22,7 +22,7 @@ namespace TouchSocket.Core.AspNetCore
public class AspNetCoreContainer : IContainer
{
private readonly IServiceCollection m_services;
readonly Container m_container = new Container();
private readonly Container m_container = new Container();
/// <summary>
/// 初始化一个IServiceCollection的容器。
......@@ -41,7 +41,7 @@ namespace TouchSocket.Core.AspNetCore
/// <returns></returns>
public bool IsRegistered(Type fromType, string key = "")
{
return ((IScopedContainer)this.m_container).IsRegistered(fromType, key);
return ((IContainerProvider)this.m_container).IsRegistered(fromType, key);
}
/// <summary>
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using Microsoft.Extensions.DependencyInjection;
using TouchSocket.Core.AspNetCore;
namespace TouchSocket.Core.Config
{
/// <summary>
/// AspNetCoreConfigExtension
/// </summary>
public static class AspNetCoreConfigExtension
{
/// <summary>
/// 使用<see cref="AspNetCoreContainer"/>作为容器。
/// </summary>
/// <param name="config"></param>
/// <param name="services"></param>
/// <returns></returns>
public static TouchSocketConfig UseAspNetCoreContainer(this TouchSocketConfig config, IServiceCollection services)
{
config.SetContainer(new AspNetCoreContainer(services));
return config;
}
}
}
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using TouchSocket.Core;
using TouchSocket.Core.Config;
using TouchSocket.Core.Dependency;
using TouchSocket.Http;
using TouchSocket.Sockets;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// HttpServiceCollectionExtensions
/// </summary>
public static class HttpServiceCollectionExtensions
{
#region HttpService
/// <summary>
/// 添加HttpService服务。
/// </summary>
/// <typeparam name="ServiceInterface">注册服务的接口</typeparam>
/// <param name="service"></param>
/// <param name="tcpService">实例化的服务器</param>
/// <returns></returns>
public static IHttpService AddHttpService<ServiceInterface>(this IServiceCollection service, ITcpService tcpService) where ServiceInterface : IHttpService
{
tcpService.Config.Container.RegisterSingleton<ServiceInterface>(tcpService);
return (ServiceInterface)tcpService;
}
/// <summary>
/// 添加HttpService服务。
/// </summary>
/// <typeparam name="ServiceInterface">注册服务的接口</typeparam>
/// <param name="service"></param>
/// <param name="configAction">设置配置相关信息</param>
/// <returns></returns>
public static IHttpService AddHttpService<ServiceInterface>(this IServiceCollection service, Action<TouchSocketConfig> configAction) where ServiceInterface : IHttpService
{
var config = new TouchSocketConfig()
.UseAspNetCoreContainer(service);
configAction?.Invoke(config);
IHttpService tcpService = config.Container.Resolve<HttpService>();
tcpService.Setup(config)
.Start();
return AddHttpService<ServiceInterface>(service, tcpService);
}
/// <summary>
/// 以<see cref="IHttpService"/>作为注入接口,添加HttpService服务。
/// </summary>
/// <param name="service"></param>
/// <param name="configAction">设置配置相关信息</param>
/// <returns></returns>
public static IHttpService AddHttpService(this IServiceCollection service, Action<TouchSocketConfig> configAction)
{
return AddHttpService<IHttpService>(service, configAction);
}
/// <summary>
/// 以<see cref="IHttpService"/>作为注入接口,添加HttpService服务。
/// </summary>
/// <param name="service"></param>
/// <param name="port">监听端口</param>
/// <returns></returns>
public static IHttpService AddHttpService(this IServiceCollection service, int port)
{
return AddHttpService<IHttpService>(service, config =>
{
config.SetListenIPHosts(new IPHost[] { new IPHost(port) });
});
}
#endregion HttpService
}
}
......@@ -30,15 +30,20 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// </summary>
public class WSTouchRpcClient : DisposableObject, IWSTouchRpcClient, IRpcActor
{
private readonly byte[] m_buffer = new byte[1024 * 64];
private readonly ActionMap m_actionMap;
private readonly byte[] m_buffer = new byte[1024 * 64];
private readonly RpcActor m_rpcActor;
private ClientWebSocket m_client;
private TouchSocketConfig m_config;
private int m_failCount;
private IPHost m_remoteIPHost;
private RpcActor m_rpcActor;
private Timer m_timer;
private RpcStore m_rpcStore;
private Timer m_timer;
/// <summary>
/// 最后活动时间
/// </summary>
public DateTime LastActiveTime { get;private set; }
/// <summary>
/// 创建一个WSTouchRpcClient实例。
......@@ -66,6 +71,11 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// </summary>
public event ClientDisconnectedEventHandler<WSTouchRpcClient> Disconnected;
/// <summary>
/// 方法映射表
/// </summary>
public ActionMap ActionMap { get => this.m_actionMap; }
/// <summary>
/// 客户端配置
/// </summary>
......@@ -80,12 +90,6 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <inheritdoc/>
/// </summary>
public string ID => this.m_rpcActor.ID;
/// <summary>
/// 方法映射表
/// </summary>
public ActionMap ActionMap { get => m_actionMap; }
/// <summary>
/// <inheritdoc/>
/// </summary>
......@@ -124,7 +128,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <summary>
/// RpcStore
/// </summary>
public RpcStore RpcStore { get => m_rpcStore;}
public RpcStore RpcStore { get => this.m_rpcStore; }
/// <summary>
/// <inheritdoc/>
......@@ -181,7 +185,8 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
this.m_client.SafeDispose();
this.m_client = new ClientWebSocket();
await this.m_client.ConnectAsync(this.RemoteIPHost.Uri, default);
this.BeginReceive(null);
_ = this.BeginReceive(null);
}
if (this.IsHandshaked)
......@@ -189,7 +194,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
return;
}
this.m_rpcActor.Handshake(this.Config.GetValue<string>(TouchRpcConfigExtensions.VerifyTokenProperty), default,
this.m_rpcActor.Handshake(this.Config.GetValue<string>(TouchRpcConfigExtensions.VerifyTokenProperty), default,
timeout, this.Config.GetValue<Metadata>(TouchRpcConfigExtensions.MetadataProperty));
}
......@@ -439,19 +444,17 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
this.Disconnected?.Invoke(this, e);
}
private async void BeginReceive(ByteBlock byteBlock)
private async Task BeginReceive(ByteBlock byteBlock)
{
try
{
if (byteBlock == null)
{
byteBlock = new ByteBlock();
}
byteBlock ??= new ByteBlock();
var result = await this.m_client.ReceiveAsync(this.m_buffer, default);
if (result.Count == 0)
{
this.BreakOut("远程终端主动关闭", false);
}
this.LastActiveTime = DateTime.Now;
byteBlock.Write(this.m_buffer, 0, result.Count);
if (result.EndOfMessage)
{
......@@ -466,14 +469,14 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
{
byteBlock.SafeDispose();
}
this.BeginReceive(null);
await this.BeginReceive(null);
}
else
{
this.BeginReceive(byteBlock);
await this.BeginReceive(byteBlock);
}
}
catch (System.Exception ex)
catch (Exception ex)
{
this.BreakOut(ex.Message, false);
}
......@@ -776,6 +779,30 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
private void OnRpcActorHandshaked(RpcActor actor, VerifyOptionEventArgs e)
{
this.m_timer.SafeDispose();
if (this.Config.GetValue<HeartbeatValue>(TouchRpcConfigExtensions.HeartbeatFrequencyProperty) is HeartbeatValue heartbeat)
{
this.m_timer = new Timer((obj) =>
{
if (DateTime.Now.TimeOfDay - this.LastActiveTime.TimeOfDay < TimeSpan.FromMilliseconds(heartbeat.Interval))
{
return;
}
if (this.Ping())
{
this.m_failCount = 0;
}
else
{
if (++this.m_failCount > heartbeat.MaxFailCount)
{
this.Close("自动心跳失败次数达到最大,已清理连接。");
this.m_timer.SafeDispose();
}
}
}, null, heartbeat.Interval, heartbeat.Interval);
}
if (this.UsePlugin && this.PluginsManager.Raise<ITouchRpcPlugin>(nameof(ITouchRpcPlugin.OnHandshaked), this, e))
{
return;
......@@ -818,6 +845,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
private async void RpcActorSend(RpcActor actor, bool isAsync, ArraySegment<byte>[] transferBytes)
{
this.LastActiveTime = DateTime.Now;
using ByteBlock byteBlock = new ByteBlock();
foreach (var item in transferBytes)
{
......@@ -829,13 +857,14 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
#endregion 内部委托绑定
#region 事件触发
/// <summary>
/// 当文件传输结束之后。并不意味着完成传输,请通过<see cref="FileTransferStatusEventArgs.Result"/>属性值进行判断。
/// </summary>
/// <param name="e"></param>
protected virtual void OnFileTransfered(FileTransferStatusEventArgs e)
{
}
/// <summary>
......@@ -844,7 +873,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="e"></param>
protected virtual void OnFileTransfering(FileOperationEventArgs e)
{
}
/// <summary>
......@@ -853,26 +882,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="e"></param>
protected virtual void OnHandshaked(VerifyOptionEventArgs e)
{
this.m_timer.SafeDispose();
if (this.Config.GetValue<HeartbeatValue>(TouchRpcConfigExtensions.HeartbeatFrequencyProperty) is HeartbeatValue heartbeat)
{
this.m_timer = new Timer((obj) =>
{
if (this.Ping())
{
this.m_failCount = 0;
}
else
{
if (++this.m_failCount > heartbeat.MaxFailCount)
{
this.Close("自动心跳失败次数达到最大,已清理连接。");
this.m_timer.SafeDispose();
}
}
}, null, heartbeat.Interval, heartbeat.Interval);
}
}
/// <summary>
......@@ -882,7 +892,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="byteBlock"></param>
protected virtual void OnReceived(short protocol, ByteBlock byteBlock)
{
}
/// <summary>
......@@ -891,7 +901,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="e"></param>
protected virtual void OnStreamTransfered(StreamStatusEventArgs e)
{
}
/// <summary>
......@@ -900,7 +910,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="e"></param>
protected virtual void OnStreamTransfering(StreamOperationEventArgs e)
{
}
#endregion 事件触发
......
......@@ -33,7 +33,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
{
#region SocketClient
private ConcurrentDictionary<string, WSTouchRpcSocketClient> m_tokenDic = new ConcurrentDictionary<string, WSTouchRpcSocketClient>();
private readonly ConcurrentDictionary<string, WSTouchRpcSocketClient> m_tokenDic = new ConcurrentDictionary<string, WSTouchRpcSocketClient>();
/// <summary>
/// 数量
......@@ -183,7 +183,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <summary>
/// 方法映射表
/// </summary>
public ActionMap ActionMap { get => m_actionMap; }
public ActionMap ActionMap { get => this.m_actionMap; }
/// <summary>
/// <inheritdoc/>
......@@ -641,7 +641,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
this.OnReceived(client, protocol, byteBlock);
}
private void OnRpcServiceResetID(RpcActor actor, WaitSetID arg2)
{
this.ResetID(arg2.OldID, arg2.NewID);
......
......@@ -10,9 +10,7 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.Threading;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Core.Plugins;
namespace TouchSocket.Rpc.TouchRpc.AspNetCore
......
......@@ -25,7 +25,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
private readonly RequestDelegate m_next;
private readonly IWSTouchRpcService m_rpcService;
private string m_url = "/wstouchrpc";
private ILogger<WSTouchRpcMiddleware> m_logger;
private readonly ILogger<WSTouchRpcMiddleware> m_logger;
/// <summary>
/// 实例化一个中间件
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using TouchSocket.Core;
using TouchSocket.Core.Config;
using TouchSocket.Core.Dependency;
using TouchSocket.Sockets;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// SocketsServiceCollectionExtensions
/// </summary>
public static class SocketsServiceCollectionExtensions
{
#region TcpService
/// <summary>
/// 添加TcpService服务。
/// </summary>
/// <typeparam name="ServiceInterface">注册服务的接口</typeparam>
/// <param name="service"></param>
/// <param name="tcpService">实例化的服务器</param>
/// <returns></returns>
public static ITcpService AddTcpService<ServiceInterface>(this IServiceCollection service, ITcpService tcpService) where ServiceInterface : ITcpService
{
tcpService.Config.Container.RegisterSingleton<ServiceInterface>(tcpService);
return (ServiceInterface)tcpService;
}
/// <summary>
/// 添加TcpService服务。
/// </summary>
/// <typeparam name="ServiceInterface">注册服务的接口</typeparam>
/// <param name="service"></param>
/// <param name="configAction">设置配置相关信息</param>
/// <returns></returns>
public static ITcpService AddTcpService<ServiceInterface>(this IServiceCollection service, Action<TouchSocketConfig> configAction) where ServiceInterface : ITcpService
{
var config = new TouchSocketConfig()
.UseAspNetCoreContainer(service);
configAction?.Invoke(config);
ITcpService tcpService = config.Container.Resolve<TcpService>();
tcpService.Setup(config)
.Start();
return AddTcpService<ServiceInterface>(service, tcpService);
}
/// <summary>
/// 以<see cref="ITcpService"/>作为注入接口,添加TcpService服务。
/// </summary>
/// <param name="service"></param>
/// <param name="configAction">设置配置相关信息</param>
/// <returns></returns>
public static ITcpService AddTcpService(this IServiceCollection service, Action<TouchSocketConfig> configAction)
{
return AddTcpService<ITcpService>(service, configAction);
}
/// <summary>
/// 以<see cref="ITcpService"/>作为注入接口,添加TcpService服务。
/// </summary>
/// <param name="service"></param>
/// <param name="port">监听端口</param>
/// <returns></returns>
public static ITcpService AddTcpService(this IServiceCollection service, int port)
{
return AddTcpService<ITcpService>(service, config =>
{
config.SetListenIPHosts(new IPHost[] { new IPHost(port) });
});
}
#endregion TcpService
}
}
......@@ -5,7 +5,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.6.0</Version>
<Version>0.7.0</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
......@@ -35,7 +35,7 @@ API:https://www.yuque.com/rrqm/touchsocket/index</Description>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6|AnyCPU'">
......@@ -77,6 +77,6 @@ API:https://www.yuque.com/rrqm/touchsocket/index</Description>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.26" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
<PackageReference Include="TouchSocket" Version="0.6.0" />
<PackageReference Include="TouchSocket" Version="0.7.0" />
</ItemGroup>
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册