[英]how to pipe data from NSOutputStream to NSInputStream in objective-c
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.
库1将是库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. 情况1:库1接收其输入流上的数据我想在另一个虚拟输出流上写入数据,该输出流将通过管道传输到库2上的输入流。
Case 2: Library 2 wants to send some data, so it will write data onto its outputstream. 案例2:库2想要发送一些数据,因此它会将数据写入其输出流。 This should be piped to a dummy input stream from where data will be read and written onto the output stream of library 1.
这应该通过管道传输到虚拟输入流,数据将被读取并写入库1的输出流。
How do i create pipes for these NSStreams in objective-c ? 如何在objective-c中为这些NSStream创建管道?
Thanks in advance for your inputs. 提前感谢您的意见。
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. 您写入outputStream的内容将从inputStream中读取。
Swift version of Gabriele Mondada's answer Swift版本的Gabriele Mondada的答案
var readStream:Unmanaged<CFReadStream>?
var writeStream:Unmanaged<CFWriteStream>?
CFStreamCreateBoundPair(nil, &readStream, &writeStream, 4096)
let inputStream:NSInputStream = readStream!.takeRetainedValue()
let outputStream:NSOutputStream = writeStream!.takeRetainedValue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.