6

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.

abligh
  • 24,573
  • 4
  • 47
  • 84
  • Subclass both with wrapper classes. One intercepts the data and queues it, the other draws from the queue. – Hot Licks Jan 30 '15 at 20:20
  • Sharath any luck on this? I've been looking to do a similar thing. – Joshua Book Feb 05 '15 at 22:44
  • the scenario for me was that, both libraries were working of stream events independently. I wrote a bridge class: I made the bridge such that when data is received on library 1 it would get the NSData and create a input stream with [NSInputStream inputStreamWithData:data] and this as the input stream for library 2. For the case where data was to be sent i directly sent data on library 2 output stream, collected it in bridge via stream events and write on the library 1 output stream – Sharath Ambati Feb 06 '15 at 23:02

2 Answers2

3

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.

Gabriele Mondada
  • 497
  • 4
  • 14
  • this is crappy. Who needs such pair? Usually you have two streams associated with something: file, socket, memory, xml parser, so ready sockets should be piped together. – Marek R Mar 12 '16 at 22:56
  • I need it. Thanks. See also my answer for a version in Swift – Dale Sep 23 '16 at 04:42
  • 1
    @MarekR Say I have some source of data. I pass it an `OutputStream` to write to. I want to upload these data to a server; the HTTP library takes an `InputStream`. So all I need is a bridge between the two streams. – Raphael Feb 24 '17 at 11:19
  • @Raphael you missed my point, you have to do lots of boiler plate code, to feed one stream to another. This pair is useful only in one case, to pass data between threads (each stream scheduled on different event loop), in other cases it is completely useless. – Marek R Mar 16 '17 at 13:28
3

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()
Dale
  • 3,193
  • 24
  • 29
  • See also http://stackoverflow.com/questions/24028995/toll-free-bridging-and-pointer-access-in-swift – Dale Sep 23 '16 at 04:45