I have 2 libraries that I want to integrate and make them talk to each other. Each of them listen on their own input and output streams. Library 1 will be the transport layer for library 2.
Case 1: Library 1 receives data on its input stream I want to write data on another dummy outputstream which will be piped to the input stream on library 2.
Case 2: Library 2 wants to send some data, so it will write data onto its outputstream. This should be piped to a dummy input stream from where data will be read and written onto the output stream of library 1.
How do i create pipes for these NSStreams in objective-c ?
Thanks in advance for your inputs.
Swift version of Gabriele Mondada's answer
var readStream:Unmanaged<CFReadStream>?
var writeStream:Unmanaged<CFWriteStream>?
CFStreamCreateBoundPair(nil, &readStream, &writeStream, 4096)
let inputStream:NSInputStream = readStream!.takeRetainedValue()
let outputStream:NSOutputStream = writeStream!.takeRetainedValue()
Here is how to create a simple pipe:
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreateBoundPair(NULL, &readStream, &writeStream, 4096);
NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
What you write to outputStream will be readable from inputStream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With