diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2023-12-16 22:00:38 -0700 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2023-12-16 22:00:38 -0700 |
commit | c808fa9ab35fde98d592acf74e74055f5f4a18c5 (patch) | |
tree | dfa19c9bd55e21dd3571b7be68288e071d2058bc /Sources/swiftGopherClient/gopherClient.swift | |
parent | 9e3cb79bbfe4eb55ef820b6afed2129412eaabe9 (diff) |
Refactor
Diffstat (limited to 'Sources/swiftGopherClient/gopherClient.swift')
-rw-r--r-- | Sources/swiftGopherClient/gopherClient.swift | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/Sources/swiftGopherClient/gopherClient.swift b/Sources/swiftGopherClient/gopherClient.swift index e508285..eabbbc4 100644 --- a/Sources/swiftGopherClient/gopherClient.swift +++ b/Sources/swiftGopherClient/gopherClient.swift @@ -7,12 +7,18 @@ import Foundation import NIO +import NIOTransportServices +import GopherHelpers public class GopherClient { private let group: EventLoopGroup public init() { - self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) + if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { + self.group = NIOTSEventLoopGroup() + } else { + self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) + } } deinit { @@ -20,22 +26,40 @@ public class GopherClient { } public func sendRequest(to host: String, port: Int = 70, message: String, completion: @escaping (Result<[gopherItem], Error>) -> Void) { - let bootstrap = ClientBootstrap(group: group) - .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) - .channelInitializer { channel in - channel.pipeline.addHandler(GopherRequestResponseHandler(message: message, completion: completion)) + if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { + let bootstrap = NIOTSConnectionBootstrap(group: group) + .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) + .channelInitializer { channel in + channel.pipeline.addHandler(GopherRequestResponseHandler(message: message, completion: completion)) + } + bootstrap.connect(host: host, port: port).whenComplete { result in + switch result { + case .success(let channel): + channel.closeFuture.whenComplete { _ in + print("Connection closed") + } + case .failure(let error): + completion(.failure(error)) + } } - - bootstrap.connect(host: host, port: port).whenComplete { result in - switch result { - case .success(let channel): - channel.closeFuture.whenComplete { _ in - print("Connection closed") + } else { + let bootstrap = ClientBootstrap(group: group) + .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) + .channelInitializer { channel in + channel.pipeline.addHandler(GopherRequestResponseHandler(message: message, completion: completion)) + } + bootstrap.connect(host: host, port: port).whenComplete { result in + switch result { + case .success(let channel): + channel.closeFuture.whenComplete { _ in + print("Connection closed") + } + case .failure(let error): + completion(.failure(error)) } - case .failure(let error): - completion(.failure(error)) } } + } private func shutdownEventLoopGroup() { |