Created
May 30, 2025 06:34
-
-
Save jacobsapps/27228bf3ed5cd0386d898b1eb66600b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public func connect(url: URL, timeout: Double = 10, certificatePinning: CertificatePinning? = nil) { | |
| guard let parts = url.getParts() else { | |
| delegate?.connectionChanged(state: .failed(FoundationTransportError.invalidRequest)) | |
| return | |
| } | |
| self.certPinner = certificatePinning | |
| self.isTLS = parts.isTLS | |
| var readStream: Unmanaged<CFReadStream>? | |
| var writeStream: Unmanaged<CFWriteStream>? | |
| let h = parts.host as NSString | |
| CFStreamCreatePairWithSocketToHost(nil, h, UInt32(parts.port), &readStream, &writeStream) | |
| inputStream = readStream!.takeRetainedValue() | |
| outputStream = writeStream!.takeRetainedValue() | |
| guard let inStream = inputStream, let outStream = outputStream else { | |
| return | |
| } | |
| inStream.delegate = self | |
| outStream.delegate = self | |
| if isTLS { | |
| let key = CFStreamPropertyKey(rawValue: kCFStreamPropertySocketSecurityLevel) | |
| CFReadStreamSetProperty(inStream, key, kCFStreamSocketSecurityLevelNegotiatedSSL) | |
| CFWriteStreamSetProperty(outStream, key, kCFStreamSocketSecurityLevelNegotiatedSSL) | |
| } | |
| onConnect?(inStream, outStream) | |
| isOpen = false | |
| CFReadStreamSetDispatchQueue(inStream, workQueue) | |
| CFWriteStreamSetDispatchQueue(outStream, workQueue) | |
| inStream.open() | |
| outStream.open() | |
| workQueue.asyncAfter(deadline: .now() + timeout, execute: { [weak self] in | |
| guard let s = self else { return } | |
| if !s.isOpen { | |
| s.delegate?.connectionChanged(state: .failed(FoundationTransportError.timeout)) | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment