How to convert Stream.streamError to CFStreamError

My App is opening streams like so:

Code Block swift
var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(nil, "192.168.42.1" as NSString,
UInt32(7878),
&readStream, &writeStream)
self.inputStream = readStream!.takeRetainedValue()
self.outputStream = writeStream!.takeRetainedValue()
if let inputStream = self.inputStream, let outputStream = self.outputStream {
self.reader = StreamReader("WiFi", self.responses, onOpened: { successful in
})
self.writer = StreamWriter("WiFi")
CFReadStreamSetDispatchQueue(inputStream, self.workQueue)
CFWriteStreamSetDispatchQueue(outputStream, self.workQueue)
inputStream.delegate = self.reader
inputStream.open()
self.writer!.outputStream = outputStream
outputStream.delegate = self.writer
outputStream.open()
}


The StreamReader class has a onOpened callback which is empty here but controls further app flow, like so (in StreamReader):

Code Block swift
case Stream.Event.errorOccurred:
    let e: CFStreamError = aStream.streamError as! CFStreamError
    os_log(.default, log: log, "error occured: %{public}s", aStream.streamError?.localizedDescription ?? "")
    os_log(.default, log: log, "error occured: %{public}d", aStream.streamStatus.rawValue)
    if !opened {
        self.onOpened?(false)
    }
    self.onStopped?()


I try to read the raw error code, in order to comply with the local network privacy changes as mentioned in https://developer.apple.com/forums/thread/661606 for example.

However, without a way to read the current status, I rely on the return code of the stream callback Stream.Event.errorOccurred.

Also really thankful if someone could point me to good documentation on this.
Is there a reason you’re using CFStream for this? NWConnection is a lot easier.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
How to convert Stream.streamError to CFStreamError
 
 
Q