Tim Hauptmann
11/11/2019, 9:43 AMprivate var readStream: Unmanaged<CFReadStream>?
private var writeStream: Unmanaged<CFWriteStream>?
private var inputStream: InputStream?
private var outputStream: OutputStream?
func connect(
hostname: String,
port: Int32,
callback: @escaping (_ dataTransferObject: DiagnoseViewDataTransferObject) -> Void
) {
let host = CFStringCreateWithCString(kCFAllocatorDefault, hostname, CFStringBuiltInEncodings.UTF8.rawValue)
CFStreamCreatePairWithSocketToHost(
kCFAllocatorDefault,
host,
443,
&self.readStream,
&self.writeStream
)
inputStream = readStream?.takeRetainedValue()
outputStream = writeStream?.takeRetainedValue()
inputStream!.delegate = self
outputStream!.delegate = self
}
----------------
In Kotlin I have the following:
private var readStream: CValuesRef<CFReadStreamRefVar>? = null
private var writeStream: CValuesRef<CFWriteStreamRefVar>? = null
private var inputStream: NSInputStream? = null
private var outputStream: NSOutputStream? = null
fun connect(
hostname: String,
port: Int,
callback: (dataTransferObject: DiagnoseViewDataTransferObject) -> Unit
) {
val host = CFStringCreateWithCString(kCFAllocatorDefault, hostname, kCFStringEncodingUTF8)
CFStreamCreatePairWithSocketToHost(
alloc = kCFAllocatorDefault,
host = host,
port = 443,
readStream = this.readStream,
writeStream = this.writeStream
)
inputStream = readStream as NSInputStream?
outputStream = writeStream as NSOutputStream?
inputStream!!.delegate = this //crashes here with Nullpointer
outputStream!!.delegate = this
}
Someone can help me how to use CFStreamCreatePairWithSocketToHost from Kotlin?marstran
11/11/2019, 10:27 AMTim Hauptmann
11/11/2019, 10:35 AMrusshwolf
11/11/2019, 12:45 PMreadStream
so it's always null
. So you only ever set inputStream
to null
. Also your types don't match, so I don't think the as NSInputStream?
cast will never succeed unless it's a usage of CValuesRef
I don't know about (which is possible because I haven't done a ton with K/N value references)Tim Hauptmann
11/11/2019, 1:18 PMCFReadStream
and CFWriteStream
. The call to 'CFStreamCreatePairWithSocketToHost' initializes them. This works in Swift, but not in Kotlin. Presumably I use the wrong types?
The cast does not give me the warning "This cast can never succeed". But maybe it's wrong too.Artyom Degtyarev [JB]
11/11/2019, 1:19 PMCFReadStreamRef
and send pointer to the function rather than create a CValuesRef as you done. Also I agree, that these cast shouldn’t work as-is, one got to manage with bridging as described in the Apple documentation(https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html)