diff --git a/TouchSocketVersion.props b/TouchSocketVersion.props index 044e45370..b755f78b5 100644 --- a/TouchSocketVersion.props +++ b/TouchSocketVersion.props @@ -1,7 +1,7 @@  - 3.1.17 + 3.1.18 $(BaseVersion) diff --git a/src/TouchSocket.Dmtp/Features/Rpc/Actor/DmtpRpcActor.cs b/src/TouchSocket.Dmtp/Features/Rpc/Actor/DmtpRpcActor.cs index e270b1e27..28b467a15 100644 --- a/src/TouchSocket.Dmtp/Features/Rpc/Actor/DmtpRpcActor.cs +++ b/src/TouchSocket.Dmtp/Features/Rpc/Actor/DmtpRpcActor.cs @@ -304,8 +304,9 @@ public class DmtpRpcActor : DisposableObject, IDmtpRpcActor //调用方不关心结果 return; } - else if (rpcMethod != null && rpcMethod.HasCallContext) + else { + //首先移除调用上下文。 this.m_callContextDic.TryRemove(rpcRequestPackage.Sign, out _); } diff --git a/src/TouchSocket.Http/WebSockets/Components/WebSocketClientBase.cs b/src/TouchSocket.Http/WebSockets/Components/WebSocketClientBase.cs index 641fe1ced..5156cb3ab 100644 --- a/src/TouchSocket.Http/WebSockets/Components/WebSocketClientBase.cs +++ b/src/TouchSocket.Http/WebSockets/Components/WebSocketClientBase.cs @@ -11,6 +11,7 @@ //------------------------------------------------------------------------------ using System; +using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; using TouchSocket.Core; @@ -162,7 +163,16 @@ public abstract class WebSocketClientBase : HttpClientBase { if (dataFrame.IsClose) { - var msg = dataFrame.PayloadData?.ToString(); + var bytes = dataFrame.PayloadData; + bytes.SeekToStart(); + if (bytes.Length >= 2) + { + var closeStatus = (WebSocketCloseStatus)bytes.ReadUInt16(EndianType.Big); + this.m_webSocket.CloseStatus = closeStatus; + } + + var msg = bytes.ReadToSpan(bytes.CanReadLength).ToString(System.Text.Encoding.UTF8); + await this.PrivateWebSocketClosing(new ClosedEventArgs(false, msg)).ConfigureAwait(EasyTask.ContinueOnCapturedContext); await this.m_webSocket.CloseAsync(msg).ConfigureAwait(EasyTask.ContinueOnCapturedContext); return; diff --git a/src/TouchSocket.Rpc/Common/RpcMethod.cs b/src/TouchSocket.Rpc/Common/RpcMethod.cs index 00f6e1fb3..424c1e210 100644 --- a/src/TouchSocket.Rpc/Common/RpcMethod.cs +++ b/src/TouchSocket.Rpc/Common/RpcMethod.cs @@ -15,6 +15,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using TouchSocket.Core; namespace TouchSocket.Rpc; @@ -246,11 +247,11 @@ public sealed class RpcMethod : Method /// /// 筛选器 /// - /// 全局筛选器 + /// 全局筛选器 /// - public IReadOnlyList GetFilters(IReadOnlyList g_actionFilters) + public IReadOnlyList GetFilters(IReadOnlyList filters, IResolver resolver) { - if (this.m_hasFilters[0] || this.m_hasFilters[1] || this.m_hasFilters[2] || this.m_hasFilters[3] || (g_actionFilters?.Any()) == true) + if (this.m_hasFilters[0] || this.m_hasFilters[1] || this.m_hasFilters[2] || this.m_hasFilters[3] || filters.Count > 0) { var actionFilters = new List(); //注册方法 @@ -299,17 +300,15 @@ public sealed class RpcMethod : Method } } - if (g_actionFilters?.Any() == true) + //全局筛选器 + foreach (var filterType in filters) { - //全局筛选器 - foreach (var filter in g_actionFilters) - { - this.AddActionFilter(filter, ref actionFilters); - } + var filter = resolver.Resolve(filterType); + this.AddActionFilter(Unsafe.As(ref filter), ref actionFilters); } return actionFilters; } - return new IRpcActionFilter[0]; + return []; } private void AddActionFilter(IRpcActionFilter filter, ref List filters) diff --git a/src/TouchSocket.Rpc/Common/RpcStore.cs b/src/TouchSocket.Rpc/Common/RpcStore.cs index cb5a3b4c8..ef51905ac 100644 --- a/src/TouchSocket.Rpc/Common/RpcStore.cs +++ b/src/TouchSocket.Rpc/Common/RpcStore.cs @@ -40,6 +40,8 @@ public sealed class RpcStore /// public Type[] ServerTypes => this.m_serverTypes.Keys.ToArray(); + public IReadOnlyList Filters => this.m_filters; + /// /// 获取所有已注册的函数。 /// @@ -110,41 +112,31 @@ public sealed class RpcStore #region 全局筛选器 - private readonly ConcurrentList m_filters = new ConcurrentList(); - - /// - /// 获取全局筛选器对象 - /// - /// - /// - public IReadOnlyList GetFilters(ICallContext callContext) - { - return m_filters.Select(s=>(IRpcActionFilter) callContext.Resolver.Resolve(s)).ToList(); - } + private readonly List m_filters = new(); /// /// 添加全局筛选器 /// /// - public void Filter() where TFilter : class, IRpcActionFilter + public void AddFilter() where TFilter : class, IRpcActionFilter { var filterType = typeof(TFilter); - this.Filter(filterType); + this.AddFilter(filterType); } /// /// 添加全局过滤器 /// - public void Filter(Type filterType) + public void AddFilter(Type filterType) { if (!typeof(IRpcActionFilter).IsAssignableFrom(filterType)) { throw new RpcException($"注册类型必须与{nameof(IRpcActionFilter)}有继承关系"); } - if (!m_filters.Any(s => s.FullName == filterType.FullName)) + if (!this.m_filters.Any(s => s == filterType)) { - m_filters.Add(filterType); + this.m_filters.Add(filterType); this.m_registrator.RegisterTransient(filterType); } } diff --git a/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs b/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs index 6c87848cb..747f9dccc 100644 --- a/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs +++ b/src/TouchSocket.Rpc/RpcServerProvider/InternalRpcServerProvider.cs @@ -42,7 +42,7 @@ internal sealed class InternalRpcServerProvider : IRpcServerProvider return new InvokeResult(InvokeStatus.UnFound); } - var filters = method.GetFilters(m_rpcStore.GetFilters(callContext)); + var filters = method.GetFilters(this.m_rpcStore.Filters, callContext.Resolver); try { for (var i = 0; i < filters.Count; i++) diff --git a/src/TouchSocket/Components/Core/TcpCore.cs b/src/TouchSocket/Components/Core/TcpCore.cs index 1b627cd97..805506535 100644 --- a/src/TouchSocket/Components/Core/TcpCore.cs +++ b/src/TouchSocket/Components/Core/TcpCore.cs @@ -417,23 +417,21 @@ internal sealed class TcpCore : DisposableObject #endif else { - var offset = 0; - while (length > 0 && !this.m_cancellationToken.IsCancellationRequested) + var sentLength = 0; + while (sentLength < length && !this.m_cancellationToken.IsCancellationRequested) { - var result = await this.m_socketSender.SendAsync(this.m_socket, memory[offset..]).ConfigureAwait(EasyTask.ContinueOnCapturedContext); + var result = await this.m_socketSender.SendAsync(this.m_socket, memory.Slice(sentLength)) + .ConfigureAwait(EasyTask.ContinueOnCapturedContext); if (result.SocketError != null) { throw result.SocketError; } - if (result.BytesTransferred == 0 && length > 0) + if (result.BytesTransferred == 0 && memory.Length > 0) { throw new Exception(TouchSocketResource.IncompleteDataTransmission); } - offset += result.BytesTransferred; - length -= result.BytesTransferred; + sentLength += result.BytesTransferred; } - - //this.m_socketSender.Reset(); } this.m_sentCounter.Increment(length); }