aboutsummaryrefslogtreecommitdiff
path: root/Sources/swiftGopherClient/gopherClient.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/swiftGopherClient/gopherClient.swift')
-rw-r--r--Sources/swiftGopherClient/gopherClient.swift50
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() {