Skip to content

Class UdpClientAsync

Namespace: Tool.Sockets.UdpHelper
Assembly: Tool.Net.dll

封装一个底层异步Udp对象(客户端)IpV4

csharp
public class UdpClientAsync : NetworkConnect<IUdpCore>, INetworkConnect<IUdpCore>, INetworkConnect, INetworkCore, IDisposable

Inheritance

objectEnClientEventDriveNetworkConnect<IUdpCore>UdpClientAsync

Implements

INetworkConnect<IUdpCore>, INetworkConnect, INetworkCore, IDisposable

Inherited Members

NetworkConnect<IUdpCore>.LocalPoint, NetworkConnect<IUdpCore>.Connected, NetworkConnect<IUdpCore>.Server, NetworkConnect<IUdpCore>.Millisecond, NetworkConnect<IUdpCore>.IsClose, NetworkConnect<IUdpCore>.IsThreadPool, NetworkConnect<IUdpCore>.BufferSize, NetworkConnect<IUdpCore>.Close(), NetworkConnect<IUdpCore>.ConnectAsync(string, int), NetworkConnect<IUdpCore>.CreateSendBytes(int), NetworkConnect<IUdpCore>.Dispose(), NetworkConnect<IUdpCore>.OnComplete(in UserKey, EnClient), NetworkConnect<IUdpCore>.Reconnection(), NetworkConnect<IUdpCore>.SendAsync(SendBytes<IUdpCore>), NetworkConnect<IUdpCore>.SetCompleted(CompletedEvent<EnClient>), NetworkConnect<IUdpCore>.SetReceived(ReceiveEvent<IUdpCore>), EnClientEventDrive.OnInterceptor(EnClient, bool), EnClientEventDrive.OnIsQueue(EnClient, bool), EnClientEventDrive.IsEvent(EnClient), EnClientEventDrive.IsQueue(EnClient), EnClientEventDrive.OpenAllEvent(), EnClientEventDrive.OpenAllQueue(), EnClientEventDrive.CloseAllEvent(), EnClientEventDrive.CloseAllQueue(), object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ObjectExtension.Add<T>(object, object, object), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), P2pClientAsync.P2PConnectAsync(UdpClientAsync, Ipv4Port, Ipv4Port, int), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object)

Remarks

代码由逆血提供支持

Constructors

UdpClientAsync()

创建一个 客户端类

csharp
public UdpClientAsync()

UdpClientAsync(NetBufferSize)

创建一个 客户端类,确认模式和设置流大小

csharp
public UdpClientAsync(NetBufferSize bufferSize)

Parameters

bufferSize NetBufferSize

包大小枚举

UdpClientAsync(NetBufferSize, bool)

创建一个 客户端类,确认模式和设置流大小

csharp
public UdpClientAsync(NetBufferSize bufferSize, bool OnlyData)

Parameters

bufferSize NetBufferSize

包大小枚举

OnlyData bool

是否启动框架模式

Properties

Client

UDP 服务对象

csharp
public Socket Client { get; }

Property Value

Socket

Connected

获取当前是否已连接到远程主机。

csharp
public override bool Connected { get; }

Property Value

bool

IsClose

标识服务端连接是否关闭

csharp
public override bool IsClose { get; }

Property Value

bool

Keep

获取当前心跳信息

csharp
public KeepAlive Keep { get; }

Property Value

KeepAlive

LocalPoint

当前设备的连接信息

csharp
public override Ipv4Port LocalPoint { get; }

Property Value

Ipv4Port

Millisecond

监听控制毫秒

csharp
public override int Millisecond { get; set; }

Property Value

int

OnlyData

是否保证数据唯一性,开启后将采用框架验证保证其每次的数据唯一性,(如果不满足数据条件将直接与其断开连接)

csharp
public bool OnlyData { get; }

Property Value

bool

ReceiveTimeout

监听最大等待时长(默认60秒)

不得小于5秒

csharp
public int ReceiveTimeout { get; init; }

Property Value

int

ReplyDelay

回复消息延迟时间(警告:当前设置仅在开启了OnlyData模式生效,超时未回复会重发,重发最大次数10,依然没有回复将抛出异常!)小于20将不生效使用默认值

csharp
public int ReplyDelay { get; init; }

Property Value

int

Server

服务器创建时的信息

csharp
public override UserKey Server { get; }

Property Value

UserKey

UdpCore

UDP 核心控制器

csharp
public IUdpCore UdpCore { get; }

Property Value

IUdpCore

Methods

AddKeepAlive(byte)

添加持久化消息(心跳),防止特殊情况下的断开连接

csharp
public void AddKeepAlive(byte TimeInterval)

Parameters

TimeInterval byte

Close()

UDP关闭

csharp
public override void Close()

ConnectAsync(int)

异步连接,连接ip地址为127.0.0.1

csharp
public Task ConnectAsync(int port)

Parameters

port int

要连接服务端的端口

Returns

Task

ConnectAsync(string, int)

发送数据目标

csharp
public override Task ConnectAsync(string ip, int port)

Parameters

ip string

要连接的服务器的ip地址

port int

要连接的服务器的端口

Returns

Task

Exceptions

Exception

ConnectAsync(Ipv4Port)

异步连接

csharp
public Task ConnectAsync(Ipv4Port ipv4Port)

Parameters

ipv4Port Ipv4Port

要连接的服务器的ip地址端口

Returns

Task

CreateSendBytes(int)

创建数据包对象

csharp
public override SendBytes<IUdpCore> CreateSendBytes(int length = 0)

Parameters

length int

数据包大小

Returns

SendBytes<IUdpCore>

Dispose()

回收UDP相关资源

csharp
public override void Dispose()

OnComplete(in UserKey, EnClient)

可供开发重写的实现类

csharp
public override ValueTask<IGetQueOnEnum> OnComplete(in UserKey IpPort, EnClient enAction)

Parameters

IpPort UserKey

IP:端口

enAction EnClient

消息类型

Returns

ValueTask<IGetQueOnEnum>

Reconnection()

重连,返回是否重连,如果没有断开是不会重连的

csharp
public override Task<bool> Reconnection()

Returns

Task<bool>

SendAsync(string)

异步发送消息

csharp
public ValueTask SendAsync(string msg)

Parameters

msg string

文本数据

Returns

ValueTask

SendAsync(ArraySegment<byte>)

异步发送消息

csharp
public ValueTask SendAsync(ArraySegment<byte> listData)

Parameters

listData ArraySegment<byte>

数据包

Returns

ValueTask

SendAsync(SendBytes<IUdpCore>)

开始异步发送数据

csharp
public override ValueTask SendAsync(SendBytes<IUdpCore> sendBytes)

Parameters

sendBytes SendBytes<IUdpCore>

数据包对象

Returns

ValueTask

Exceptions

ArgumentException

SetCompleted(CompletedEvent<EnClient>)

连接、发送、关闭事件

csharp
public override void SetCompleted(CompletedEvent<EnClient> Completed)

Parameters

Completed CompletedEvent<EnClient>

SetReceived(ReceiveEvent<IUdpCore>)

接收到数据事件

csharp
public override void SetReceived(ReceiveEvent<IUdpCore> Received)

Parameters

Received ReceiveEvent<IUdpCore>

基于Apache-2.0协议开源