diff --git a/Sources/Punchnet/SDLContext.swift b/Sources/Punchnet/SDLContext.swift index ebd27b1..9c18cd1 100644 --- a/Sources/Punchnet/SDLContext.swift +++ b/Sources/Punchnet/SDLContext.swift @@ -125,8 +125,6 @@ public class SDLContext: @unchecked Sendable { self.udpHole = nil self.readTask?.cancel() - - exit(-1) } private func startSuperClient() async throws { @@ -188,13 +186,13 @@ public class SDLContext: @unchecked Sendable { let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage) self.noticeClient.send(data: alertNotice.binaryData) } - SDLLogger.log("Get a SuperNak message exit", level: .error) + NSLog("[SDLContext] Get a SuperNak message exit") default: () } case .closed: - SDLLogger.log("[SDLContext] super client closed", level: .debug) + NSLog("[SDLContext] super client closed") await self.arpServer.clear() DispatchQueue.main.asyncAfter(deadline: .now() + 5) { Task {@MainActor in @@ -205,10 +203,10 @@ public class SDLContext: @unchecked Sendable { switch evt { case .natChanged(let natChangedEvent): let dstMac = natChangedEvent.mac - NSLog("natChangedEvent, dstMac: \(dstMac)") + NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)") await sessionManager.removeSession(dstMac: dstMac) case .sendRegister(let sendRegisterEvent): - NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)") + NSLog("[SDLContext] sendRegisterEvent, ip: \(sendRegisterEvent)") let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp) if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) { // 发送register包 @@ -391,7 +389,7 @@ public class SDLContext: @unchecked Sendable { 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]) // 设置路由表 @@ -407,9 +405,9 @@ public class SDLContext: @unchecked Sendable { await self.holerManager.cleanup() self.startReader() - SDLLogger.log("[SDLContext] setTunnelNetworkSettings success, start read packet", level: .debug) + NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet") } catch let err { - SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error) + NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)") exit(-1) } } diff --git a/Sources/Punchnet/SDLNoticeClient.swift b/Sources/Punchnet/SDLNoticeClient.swift index 9523be6..7c0e904 100644 --- a/Sources/Punchnet/SDLNoticeClient.swift +++ b/Sources/Punchnet/SDLNoticeClient.swift @@ -23,7 +23,7 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable { public typealias InboundIn = AddressedEnvelope public typealias OutboundOut = AddressedEnvelope - var context: ChannelHandlerContext? + var channel: Channel? private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) private let remoteAddress: SocketAddress @@ -40,17 +40,14 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable { channel.pipeline.addHandler(self) } - let channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait() - SDLLogger.log("[SDLNoticeClient] started and listening on: \(channel.localAddress!)", level: .debug) - - // This will never unblock as we don't close the channel - try! channel.closeFuture.wait() + self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait() + SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.channel?.localAddress!)", level: .debug) } // -- MARK: ChannelInboundHandler Methods 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 // reduce allocations. context.close(promise: nil) - self.context = nil + self.channel = nil } public func channelInactive(context: ChannelHandlerContext) { - self.context = nil + self.channel = nil context.close(promise: nil) } // 处理写入逻辑 func send(data: Data) { - guard let context = self.context else { + guard let channel = self.channel else { return } 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 envelope = AddressedEnvelope(remoteAddress: remoteAddress, data: buffer) - context.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil) + channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil) } }