fix udp client

This commit is contained in:
anlicheng 2025-07-17 16:11:01 +08:00
parent 20ef0ca985
commit aaa1b3812d
2 changed files with 17 additions and 22 deletions

View File

@ -125,8 +125,6 @@ public class SDLContext: @unchecked Sendable {
self.udpHole = nil self.udpHole = nil
self.readTask?.cancel() self.readTask?.cancel()
exit(-1)
} }
private func startSuperClient() async throws { private func startSuperClient() async throws {
@ -188,13 +186,13 @@ public class SDLContext: @unchecked Sendable {
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage) let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
self.noticeClient.send(data: alertNotice.binaryData) self.noticeClient.send(data: alertNotice.binaryData)
} }
SDLLogger.log("Get a SuperNak message exit", level: .error) NSLog("[SDLContext] Get a SuperNak message exit")
default: default:
() ()
} }
case .closed: case .closed:
SDLLogger.log("[SDLContext] super client closed", level: .debug) NSLog("[SDLContext] super client closed")
await self.arpServer.clear() await self.arpServer.clear()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
Task {@MainActor in Task {@MainActor in
@ -205,10 +203,10 @@ public class SDLContext: @unchecked Sendable {
switch evt { switch evt {
case .natChanged(let natChangedEvent): case .natChanged(let natChangedEvent):
let dstMac = natChangedEvent.mac let dstMac = natChangedEvent.mac
NSLog("natChangedEvent, dstMac: \(dstMac)") NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)")
await sessionManager.removeSession(dstMac: dstMac) await sessionManager.removeSession(dstMac: dstMac)
case .sendRegister(let sendRegisterEvent): case .sendRegister(let sendRegisterEvent):
NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)") NSLog("[SDLContext] sendRegisterEvent, ip: \(sendRegisterEvent)")
let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp) let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp)
if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) { if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) {
// register // register
@ -391,7 +389,7 @@ public class SDLContext: @unchecked Sendable {
networkSettings.dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "114.114.114.114"]) networkSettings.dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "114.114.114.114"])
} }
SDLLogger.log("[SDLContext] Tun started at network ip: \(netAddress.ipAddress), mask: \(netAddress.maskAddress)", level: .debug) NSLog("[SDLContext] Tun started at network ip: \(netAddress.ipAddress), mask: \(netAddress.maskAddress)")
let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress]) let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress])
// //
@ -407,9 +405,9 @@ public class SDLContext: @unchecked Sendable {
await self.holerManager.cleanup() await self.holerManager.cleanup()
self.startReader() self.startReader()
SDLLogger.log("[SDLContext] setTunnelNetworkSettings success, start read packet", level: .debug) NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet")
} catch let err { } catch let err {
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error) NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)")
exit(-1) exit(-1)
} }
} }

View File

@ -23,7 +23,7 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
public typealias InboundIn = AddressedEnvelope<ByteBuffer> public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer> public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
var context: ChannelHandlerContext? var channel: Channel?
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
private let remoteAddress: SocketAddress private let remoteAddress: SocketAddress
@ -40,17 +40,14 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
channel.pipeline.addHandler(self) channel.pipeline.addHandler(self)
} }
let channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait() self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
SDLLogger.log("[SDLNoticeClient] started and listening on: \(channel.localAddress!)", level: .debug) SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.channel?.localAddress!)", level: .debug)
// This will never unblock as we don't close the channel
try! channel.closeFuture.wait()
} }
// -- MARK: ChannelInboundHandler Methods // -- MARK: ChannelInboundHandler Methods
public func channelActive(context: ChannelHandlerContext) { public func channelActive(context: ChannelHandlerContext) {
self.context = context
} }
// , // ,
@ -62,27 +59,27 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
// As we are not really interested getting notified on success or failure we just pass nil as promise to // As we are not really interested getting notified on success or failure we just pass nil as promise to
// reduce allocations. // reduce allocations.
context.close(promise: nil) context.close(promise: nil)
self.context = nil self.channel = nil
} }
public func channelInactive(context: ChannelHandlerContext) { public func channelInactive(context: ChannelHandlerContext) {
self.context = nil self.channel = nil
context.close(promise: nil) context.close(promise: nil)
} }
// //
func send(data: Data) { func send(data: Data) {
guard let context = self.context else { guard let channel = self.channel else {
return return
} }
let remoteAddress = self.remoteAddress let remoteAddress = self.remoteAddress
let allocator = context.channel.allocator let allocator = channel.allocator
context.eventLoop.execute { [allocator] in channel.eventLoop.execute { [allocator] in
let buffer = allocator.buffer(bytes: data) let buffer = allocator.buffer(bytes: data)
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer) let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
context.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil) channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
} }
} }