how to pipe data from NSOutputStream to NSInputStream in objective-c
Asked Answered
F

2

7

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.

Forgave answered 30/1, 2015 at 19:3 Comment(3)
Subclass both with wrapper classes. One intercepts the data and queues it, the other draws from the queue.Leclaire
Sharath any luck on this? I've been looking to do a similar thing.Reorientation
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 streamForgave
A
4

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.

Assamese answered 12/8, 2015 at 9:43 Comment(4)
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.Godunov
I need it. Thanks. See also my answer for a version in SwiftMerideth
@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.Ostrich
@Ostrich 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.Godunov
M
4

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()
Merideth answered 23/9, 2016 at 4:43 Comment(1)
See also #24029495Merideth

© 2022 - 2024 — McMap. All rights reserved.