Skip to content

Commit

Permalink
[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Bro…
Browse files Browse the repository at this point in the history
…adcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”;

无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
  • Loading branch information
nnhy committed Oct 9, 2024
1 parent a8fd36a commit 96c34fc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 4 additions & 0 deletions NewLife.Core/Net/NetUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ private static NetType ParseType(String? value)
/// <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 辅助
Expand Down
7 changes: 6 additions & 1 deletion NewLife.Core/Net/UdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ protected override Boolean OnOpen()
// 启用地址重用后,即使旧进程未退出,新进程也可以监听,但只有旧进程退出后,新进程才能接受对该端口的连接请求
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)
{
Expand Down Expand Up @@ -218,6 +220,9 @@ internal override Boolean OnReceiveAsync(SocketAsyncEventArgs se)
// 每次接收以后,这个会被设置为远程地址,这里重置一下,以防万一
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);
}
Expand Down
11 changes: 2 additions & 9 deletions NewLife.Core/Net/UdpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -83,6 +75,7 @@ public UdpSession(UdpServer server, IPAddress? local, IPEndPoint remote)
Remote = new NetUri(NetType.Udp, remote);
Tracer = server.Tracer;

Local = server.Local.Clone();
if (local != null) Local.Address = local;

// 检查并开启广播
Expand Down

0 comments on commit 96c34fc

Please sign in to comment.