[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”;

无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
This commit is contained in:
石头 2024-10-10 00:36:00 +08:00
parent a8fd36ae18
commit 96c34fc4ca
3 changed files with 12 additions and 10 deletions

View File

@ -213,6 +213,10 @@ public class NetUri
/// <summary>获取该域名下所有IP节点含端口</summary>
/// <returns></returns>
public IPEndPoint[] GetEndPoints() => GetAddresses().Select(e => new IPEndPoint(e, Port)).ToArray();
/// <summary>克隆</summary>
/// <returns></returns>
public NetUri Clone() => new() { Type = Type, Host = Host, Port = Port, Address = Address };
#endregion
#region

View File

@ -85,7 +85,9 @@ public class UdpServer : SessionBase, ISocketServer, ILogFeature
// 启用地址重用后,即使旧进程未退出,新进程也可以监听,但只有旧进程退出后,新进程才能接受对该端口的连接请求
if (ReuseAddress) sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
// 无需设置SocketOptionName.PacketInformation在ReceiveMessageFromAsync时会自动设置
//if (sock.AddressFamily == AddressFamily.InterNetwork)
// sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
}
catch (Exception ex)
{
@ -218,6 +220,9 @@ public class UdpServer : SessionBase, ISocketServer, ILogFeature
// 每次接收以后,这个会被设置为远程地址,这里重置一下,以防万一
se.RemoteEndPoint = new IPEndPoint(IPAddress.Any.GetRightAny(Local.EndPoint.AddressFamily), 0);
// 在StarAgent中此时可能收到广播包SocketFlags是Broadcast需要清空否则报错“参考的对象类型不支持尝试的操作”
se.SocketFlags = SocketFlags.None;
//return Client.ReceiveFromAsync(se);
return Client.ReceiveMessageFromAsync(se);
}

View File

@ -23,16 +23,8 @@ public class UdpSession : DisposeBase, ISocketSession, ITransport, ILogFeature
/// <summary>底层Socket</summary>
Socket? ISocket.Client => Server?.Client;
///// <summary>数据流</summary>
//public Stream Stream { get; set; }
private NetUri? _Local;
/// <summary>本地地址</summary>
public NetUri Local
{
get => _Local ??= Server.Local;
set => Server.Local = _Local = value;
}
public NetUri Local { get; set; }
/// <summary>端口</summary>
public Int32 Port { get => Local.Port; set => Local.Port = value; }
@ -83,6 +75,7 @@ public class UdpSession : DisposeBase, ISocketSession, ITransport, ILogFeature
Remote = new NetUri(NetType.Udp, remote);
Tracer = server.Tracer;
Local = server.Local.Clone();
if (local != null) Local.Address = local;
// 检查并开启广播