swiftlib_sdlan/Sources/Punchnet/SDLNoticeClient.swift
2025-07-17 16:11:01 +08:00

90 lines
2.6 KiB
Swift

//
// SDLNoticeClient.swift
// Tun
//
// Created by on 2024/5/20.
//
import Foundation
//
// SDLanServer.swift
// Tun
//
// Created by on 2024/1/31.
//
import Foundation
@preconcurrency import NIOCore
import NIOPosix
// sn-server
class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
var channel: Channel?
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
private let remoteAddress: SocketAddress
init() {
self.remoteAddress = try! SocketAddress(ipAddress: "127.0.0.1", port: 50195)
}
//
func start() {
let bootstrap = DatagramBootstrap(group: self.group)
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
//
channel.pipeline.addHandler(self)
}
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) {
}
// ,
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
context.fireChannelRead(data)
}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
// 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.channel = nil
}
public func channelInactive(context: ChannelHandlerContext) {
self.channel = nil
context.close(promise: nil)
}
//
func send(data: Data) {
guard let channel = self.channel else {
return
}
let remoteAddress = self.remoteAddress
let allocator = channel.allocator
channel.eventLoop.execute { [allocator] in
let buffer = allocator.buffer(bytes: data)
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
}
}
deinit {
try? self.group.syncShutdownGracefully()
}
}