//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在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 System.Collections.Generic;
using System.Net;
using TouchSocket.Core;
namespace TouchSocket.Sockets
{
///
/// Udp数据处理适配器
///
public abstract class UdpDataHandlingAdapter
{
internal IUdpSession m_owner;
///
/// 是否允许发送对象。
///
public abstract bool CanSendRequestInfo { get; }
///
/// 拼接发送
///
public abstract bool CanSplicingSend { get; }
///
/// 获取或设置适配器能接收的最大数据包长度。默认1024*1024 Byte。
///
public int MaxPackageSize { get; set; } = 1024 * 1024;
///
/// 适配器拥有者。
///
public IUdpSession Owner => this.m_owner;
///
/// 当接收数据处理完成后,回调该函数执行接收
///
public Action ReceivedCallBack { get; set; }
///
/// 当接收数据处理完成后,回调该函数执行发送
///
public Action SendCallBack { get; set; }
///
/// 收到数据的切入点,该方法由框架自动调用。
///
///
///
public void ReceivedInput(EndPoint remoteEndPoint, ByteBlock byteBlock)
{
try
{
this.PreviewReceived(remoteEndPoint, byteBlock);
}
catch (Exception ex)
{
this.OnError(ex.Message);
}
}
///
/// 发送数据的切入点,该方法由框架自动调用。
///
///
public void SendInput(IRequestInfo requestInfo)
{
this.PreviewSend(requestInfo);
}
///
/// 发送数据的切入点,该方法由框架自动调用。
///
///
///
///
///
public void SendInput(EndPoint endPoint, byte[] buffer, int offset, int length)
{
this.PreviewSend(endPoint, buffer, offset, length);
}
///
/// 发送数据的切入点,该方法由框架自动调用。
///
///
///
public void SendInput(EndPoint endPoint, IList> transferBytes)
{
this.PreviewSend(endPoint, transferBytes);
}
///
/// 处理已经经过预先处理后的数据
///
///
/// 以二进制形式传递
/// 以解析实例传递
protected void GoReceived(EndPoint remoteEndPoint, ByteBlock byteBlock, IRequestInfo requestInfo)
{
this.ReceivedCallBack.Invoke(remoteEndPoint, byteBlock, requestInfo);
}
///
/// 发送已经经过预先处理后的数据
///
///
///
///
///
protected void GoSend(EndPoint endPoint, byte[] buffer, int offset, int length)
{
this.SendCallBack.Invoke(endPoint, buffer, offset, length);
}
///
/// 在解析时发生错误。
///
/// 错误异常
/// 是否调用
/// 是否记录日志
protected virtual void OnError(string error, bool reset = true, bool log = true)
{
if (reset)
{
this.Reset();
}
if (log && this.m_owner != null && this.m_owner.Logger != null)
{
this.m_owner.Logger.Error(error);
}
}
///
/// 当接收到数据后预先处理数据,然后调用处理数据
///
///
///
protected abstract void PreviewReceived(EndPoint remoteEndPoint, ByteBlock byteBlock);
///
/// 当发送数据前预先处理数据
///
///
protected abstract void PreviewSend(IRequestInfo requestInfo);
///
/// 当发送数据前预先处理数据
///
///
/// 数据
/// 偏移
/// 长度
protected abstract void PreviewSend(EndPoint endPoint, byte[] buffer, int offset, int length);
///
/// 组合发送预处理数据,
/// 当属性SplicingSend实现为True时,系统才会调用该方法。
///
///
/// 代发送数据组合
protected abstract void PreviewSend(EndPoint endPoint, IList> transferBytes);
///
/// 重置解析器到初始状态,一般在被触发时,由返回值指示是否调用。
///
protected abstract void Reset();
}
}