提交 15a68459 编写于 作者: 若汝棋茗

修复ws协议的touchrpc连接问题

上级 8c34dade
......@@ -10,9 +10,14 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.Reflection;
namespace TouchSocket.Rpc.TouchRpc.AspNetCore
{
internal class WSTouchRpcUtility
{
static WSTouchRpcUtility()
{
}
}
}
\ No newline at end of file
......@@ -576,7 +576,6 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
OnRouting(client, e);
}
private void OnRpcServiceHandshaked(RpcActor actor, VerifyOptionEventArgs e)
{
WSTouchRpcSocketClient client = (WSTouchRpcSocketClient)actor.Caller;
......@@ -656,10 +655,6 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
private void RpcServiceOutputSend(RpcActor actor, ArraySegment<byte>[] arg3)
{
WSTouchRpcSocketClient client = (WSTouchRpcSocketClient)actor.Caller;
if (!client.CanSend)
{
return;
}
client.RpcActorSend(arg3);
}
......@@ -741,7 +736,6 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <param name="e"></param>
protected virtual void OnRouting(WSTouchRpcSocketClient client, PackageRouterEventArgs e)
{
}
/// <summary>
......@@ -784,6 +778,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
#endregion 事件
#region 小文件
/// <inheritdoc/>
public PullSmallFileResult PullSmallFile(string targetId, string path, Metadata metadata = null, int timeout = 5000, CancellationToken token = default)
{
......@@ -835,7 +830,8 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
return Task.FromResult(new Result(ResultCode.Error, TouchSocketStatus.ClientNotFind.GetDescription(targetId)));
}
}
#endregion
#endregion 小文件
#region 发送
......
......@@ -296,12 +296,10 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
internal async void RpcActorSend(ArraySegment<byte>[] transferBytes)
{
using ByteBlock byteBlock = new ByteBlock();
foreach (var item in transferBytes)
for (int i = 0; i < transferBytes.Length; i++)
{
byteBlock.Write(item.Array, item.Offset, item.Count);
await m_client.SendAsync(transferBytes[i], WebSocketMessageType.Binary, i == transferBytes.Length - 1, CancellationToken.None);
}
await m_client.SendAsync(byteBlock.Buffer, System.Net.WebSockets.WebSocketMessageType.Binary, true, CancellationToken.None);
}
internal void SetRpcActor(RpcActor rpcActor)
......@@ -371,6 +369,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
private async Task BeginReceive()
{
ByteBlock byteBlock = null;
int bufferLength = this.Config.GetValue(TouchSocketConfigExtension.BufferLengthProperty);
try
{
while (true)
......@@ -381,10 +380,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
byteBlock.SafeDispose();
break;
}
if (byteBlock == null)
{
byteBlock = new ByteBlock();
}
byteBlock ??= new ByteBlock(bufferLength);
byteBlock.Write(m_buffer, 0, result.Count);
if (result.EndOfMessage)
{
......
......@@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace TouchSocket.Rpc.TouchRpc.AspNetCore
{
/// <summary>
......@@ -51,7 +52,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task<Task> Invoke(HttpContext context)
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Equals(Url, StringComparison.CurrentCultureIgnoreCase))
{
......@@ -71,11 +72,10 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
{
context.Response.StatusCode = 400;
}
return Task.CompletedTask;
}
else
{
return m_next(context);
await m_next(context);
}
}
}
......
......@@ -5,7 +5,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>D:\MyStore\13_Doc\Keys\TouchSocket.snk</AssemblyOriginatorKeyFile>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
......@@ -69,6 +69,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="1.2.4" />
<PackageReference Include="TouchSocket" Version="1.2.11" />
</ItemGroup>
</Project>
......@@ -147,9 +147,9 @@ namespace TouchSocket.Rpc.TouchRpc
{
m_client.SafeDispose();
m_client = new ClientWebSocket();
await m_client.ConnectAsync(RemoteIPHost.Uri, default);
await m_client.ConnectAsync(RemoteIPHost.Uri, default).ConfigureAwait(false);
_ = BeginReceive(null);
BeginReceive();
}
if (IsHandshaked)
......@@ -300,42 +300,48 @@ namespace TouchSocket.Rpc.TouchRpc
Disconnected?.Invoke(this, e);
}
private async Task BeginReceive(ByteBlock byteBlock)
private void BeginReceive()
{
try
Task.Factory.StartNew(async () =>
{
byteBlock ??= new ByteBlock();
var result = await m_client.ReceiveAsync(m_buffer, default);
if (result.Count == 0)
try
{
BreakOut("远程终端主动关闭", false);
}
LastActiveTime = DateTime.Now;
byteBlock.Write(m_buffer.Array, 0, result.Count);
if (result.EndOfMessage)
{
try
{
m_rpcActor.InputReceivedData(byteBlock);
}
catch
{
}
finally
ByteBlock byteBlock = null;
int bufferLength = this.Config.GetValue(TouchSocketConfigExtension.BufferLengthProperty);
while (true)
{
byteBlock.SafeDispose();
byteBlock ??= new ByteBlock(bufferLength);
var result = await m_client.ReceiveAsync(m_buffer, default);
if (result.Count == 0)
{
BreakOut("远程终端主动关闭", false);
return;
}
LastActiveTime = DateTime.Now;
byteBlock.Write(m_buffer.Array, 0, result.Count);
if (result.EndOfMessage)
{
try
{
m_rpcActor.InputReceivedData(byteBlock);
}
catch
{
}
finally
{
byteBlock.SafeDispose();
byteBlock = default;
}
}
}
await BeginReceive(null);
}
else
catch (Exception ex)
{
await BeginReceive(byteBlock);
BreakOut(ex.Message, false);
}
}
catch (Exception ex)
{
BreakOut(ex.Message, false);
}
}, TaskCreationOptions.LongRunning);
}
private void BreakOut(string msg, bool manual)
......@@ -569,6 +575,7 @@ namespace TouchSocket.Rpc.TouchRpc
OnReceived(protocol, byteBlock);
}
private void OnRpcActorRouting(RpcActor actor, PackageRouterEventArgs e)
{
if (UsePlugin && PluginsManager.Raise<ITouchRpcPlugin>(nameof(ITouchRpcPlugin.OnRouting), this, e))
......
......@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>D:\MyStore\13_Doc\Keys\TouchSocket.snk</AssemblyOriginatorKeyFile>
<Version>1.2.10</Version>
<Version>1.2.11</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2023 若汝棋茗</Copyright>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册