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

更新WaitingOptions,支持udp指定发送

上级 bd00562a
......@@ -20,20 +20,18 @@ namespace TouchSocket.Sockets
/// <summary>
/// 等待型客户端。
/// </summary>
public interface IWaitingClient<TClient> : IWaitSender where TClient : IClient, IDefaultSender, ISender
public interface IWaitingClient<TClient> : IWaitSender,IDisposable where TClient : IClient, IDefaultSender, ISender
{
/// <summary>
/// 等待设置。
/// </summary>
public WaitingOptions WaitingOptions { get; set; }
public WaitingOptions WaitingOptions { get;}
/// <summary>
/// 客户端终端
/// </summary>
TClient Client { get; }
/// <summary>
/// 发送字节流
/// </summary>
......
......@@ -18,7 +18,7 @@ using TouchSocket.Resources;
namespace TouchSocket.Sockets
{
internal class WaitingClient<TClient> : IWaitingClient<TClient> where TClient : IClient, IDefaultSender, ISender
internal class WaitingClient<TClient> : DisposableObject, IWaitingClient<TClient> where TClient : IClient, IDefaultSender, ISender
{
private readonly WaitData<ResponsedData> m_waitData;
......@@ -74,8 +74,7 @@ namespace TouchSocket.Sockets
{
breaked = false;
m_waitData.Reset();
if (this.WaitingOptions.BreakTrigger && this.Client is ITcpClientBase tcpClient)
if (WaitingOptions.BreakTrigger && this.Client is ITcpClientBase tcpClient)
{
tcpClient.Disconnected += this.OnDisconnected;
}
......@@ -89,13 +88,27 @@ namespace TouchSocket.Sockets
Client.OnHandleRawBuffer += OnHandleRawBuffer;
}
if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.SendAdapter)
if (WaitingOptions.RemoteIPHost != null && Client is IUdpSession session)
{
Client.Send(buffer, offset, length);
if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.SendAdapter)
{
session.Send(WaitingOptions.RemoteIPHost.EndPoint, buffer, offset, length);
}
else
{
session.DefaultSend(WaitingOptions.RemoteIPHost.EndPoint, buffer, offset, length);
}
}
else
{
Client.DefaultSend(buffer, offset, length);
if (WaitingOptions.AdapterFilter == AdapterFilter.AllAdapter || WaitingOptions.AdapterFilter == AdapterFilter.SendAdapter)
{
Client.Send(buffer, offset, length);
}
else
{
Client.DefaultSend(buffer, offset, length);
}
}
m_waitData.SetCancellationToken(token);
......@@ -328,6 +341,13 @@ namespace TouchSocket.Sockets
});
}
protected override void Dispose(bool disposing)
{
this.Client = default;
this.m_waitData.SafeDispose();
base.Dispose(disposing);
}
private void OnDisconnected(ITcpClientBase client, DisconnectEventArgs e)
{
breaked = true;
......@@ -356,7 +376,6 @@ namespace TouchSocket.Sockets
{
responsedData = new ResponsedData(null, requestInfo);
}
return !m_waitData.Set(responsedData);
}
}
......
......@@ -27,7 +27,7 @@ namespace TouchSocket.Sockets
/// <param name="client"></param>
/// <param name="waitingOptions"></param>
/// <returns></returns>
public static IWaitingClient<TClient> GetWaitingClient<TClient>(this TClient client, WaitingOptions waitingOptions) where TClient : IClient, IDefaultSender, ISender
public static IWaitingClient<TClient> GetWaitingClient<TClient>(this TClient client, WaitingOptions waitingOptions) where TClient : ITcpClientBase
{
WaitingClient<TClient> waitingClient = new WaitingClient<TClient>(client, waitingOptions);
return waitingClient;
......
......@@ -14,49 +14,53 @@
namespace TouchSocket.Sockets
{
/// <summary>
/// 等待设置
/// 适配器筛选
/// </summary>
public class WaitingOptions
public enum AdapterFilter
{
/// <summary>
/// 当Client为Tcp系时。是否在断开连接时以异常返回结果。
/// 发送和接收都经过适配器
/// </summary>
public bool ThrowBreakException { get; set; } = true;
AllAdapter,
/// <summary>
/// 当Client为Tcp系时。是否在断开连接时立即触发结果。默认会返回null。当<see cref="ThrowBreakException"/>为<see langword="true"/>时,会触发异常。
/// 发送经过适配器,接收不经过
/// </summary>
public bool BreakTrigger { get; set; }
SendAdapter,
/// <summary>
/// 适配器筛选
/// 发送不经过适配器,接收经过
/// </summary>
public AdapterFilter AdapterFilter { get; set; } = AdapterFilter.AllAdapter;
WaitAdapter,
/// <summary>
/// 全都不经过适配器。
/// </summary>
NoneAll
}
/// <summary>
/// 适配器筛选
/// 等待设置
/// </summary>
public enum AdapterFilter
public class WaitingOptions
{
/// <summary>
/// 发送和接收都经过适配器
/// 适配器筛选
/// </summary>
AllAdapter,
public AdapterFilter AdapterFilter { get; set; } = AdapterFilter.AllAdapter;
/// <summary>
/// 发送经过适配器,接收不经过
/// 当Client为Tcp系时。是否在断开连接时立即触发结果。默认会返回null。当<see cref="ThrowBreakException"/>为<see langword="true"/>时,会触发异常。
/// </summary>
SendAdapter,
public bool BreakTrigger { get; set; }
/// <summary>
/// 发送不经过适配器,接收经过
/// 远程地址(仅在Udp模式下生效)
/// </summary>
WaitAdapter,
public IPHost RemoteIPHost { get; set; }
/// <summary>
/// 全都不经过适配器
/// 当Client为Tcp系时。是否在断开连接时以异常返回结果
/// </summary>
NoneAll
public bool ThrowBreakException { get; set; } = true;
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>D:\MyStore\13_Doc\Keys\TouchSocket.snk</AssemblyOriginatorKeyFile>
<Version>1.2.12</Version>
<Version>1.2.13</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.
先完成此消息的编辑!
想要评论请 注册