diff options
| author | Navan Chauhan <navanchauhan@gmail.com> | 2024-07-27 23:59:24 -0600 | 
|---|---|---|
| committer | Navan Chauhan <navanchauhan@gmail.com> | 2024-07-27 23:59:24 -0600 | 
| commit | e4fc81f11bd4f72383697c76a75d117f47e9a808 (patch) | |
| tree | a4015e7a15fc1ee1f54a0538fc5433b16c79847e /Sources | |
| parent | 79913541f514a54a475afac1963830dd4129ae15 (diff) | |
add async version
Diffstat (limited to 'Sources')
| -rw-r--r-- | Sources/swiftGopherClient/gopherClient.swift | 50 | 
1 files changed, 50 insertions, 0 deletions
| diff --git a/Sources/swiftGopherClient/gopherClient.swift b/Sources/swiftGopherClient/gopherClient.swift index 802d646..68cca6c 100644 --- a/Sources/swiftGopherClient/gopherClient.swift +++ b/Sources/swiftGopherClient/gopherClient.swift @@ -105,6 +105,56 @@ public class GopherClient {      #endif    } +     +    @available(iOS 13.0, *) +    @available(macOS 10.15, *) +    public func sendRequest(to host: String, port: Int = 70, message: String) async throws -> [gopherItem] { +        return try await withCheckedThrowingContinuation { continuation in +            let bootstrap = self.createBootstrap(message: message) { result in +                continuation.resume(with: result) +            } +             +            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): +                    continuation.resume(throwing: error) +                } +            } +        } +    } +     +    private func createBootstrap( +        message: String, +        completion: @escaping (Result<[gopherItem], Error>) -> Void +    ) -> NIOClientTCPBootstrapProtocol { +        let handler = GopherRequestResponseHandler(message: message, completion: completion) + +        #if os(Linux) +        return ClientBootstrap(group: eventLoopGroup) +            .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) +            .channelInitializer { channel in +                channel.pipeline.addHandler(handler) +            } +        #else +        if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, visionOS 1.0, *) { +            return NIOTSConnectionBootstrap(group: group) +                .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) +                .channelInitializer { channel in +                    channel.pipeline.addHandler(handler) +                } +        } else { +            return ClientBootstrap(group: group) +                .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) +                .channelInitializer { channel in +                    channel.pipeline.addHandler(handler) +                } +        } +        #endif +    }    /// Shuts down the event loop group, releasing any resources.    /// | 
