How to reopen closed stream?
andig opened this issue · 3 comments
I'm trying to read from a pipe. If that closes, reopening the pipe should be attempted. I've tried to do this by subsclassing the Stream
:
public function handleClose()
{
parent::handleClose();
$this->stream = fopen($file, 'r');
$this->resume();
}
However, the loop terminates. Any hints would be appreciated.
You can't. Once a stream has been closed it's destroyed. If a user re-connects they're creating a new stream. You need a protocol on top of TCP to re-identify the user. If you're using HTTP (for example) cookies are often used to identify a user.
My intent was to reopen a pipe (file handle) when it closes for whatever reason. I need to read the pipe, tokenize and convert it and feed the results as REST service.
I was thinking about wrapping the file stream with a tokenizer stream and put the result somewhere, worst case global var. All async. I did not find a good way though to express this logic (build streams, feed to loop).
I have now instead built a specialized stream that uses the end
event of the wrapped file stream to create ankher file stream and continue (end is swallowed). It doesn't feel very clean though.
when it closes for whatever reason
I'm not sure I follow, can you elaborate on this a bit?
A stream is usually considered an ordered series of bytes, so once the stream closes/ends, you have to take additional measures to resume to the exact position in the stream. As streams are often used to communicate between distributed systems (or components thereof), this is anything but trivial.
Depending on your problem domain, there may be additional protocol details or constraints which may make this possible. However, I'm not sure there's much we can do here in a generic way?