Created
November 15, 2012 06:56
-
-
Save cameronjacobson/4077115 to your computer and use it in GitHub Desktop.
concept for react:// streamWrapper (reactphp/react)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ReactStream { | |
private $components; | |
private $position; | |
private $queue; | |
function stream_open($path, $mode, $options, &$opened_path) | |
{ | |
$this->components = parse_url($path); | |
$this->position = 0; | |
$this->queue = new SplQueue(); | |
$this->queue->setIteratorMode(SplQueue::IT_MODE_FIFO | SplQueue::IT_MODE_DELETE); | |
return true; | |
} | |
function stream_read($count) | |
{ | |
return $this->queue->dequeue(); | |
} | |
function stream_write($data) | |
{ | |
$this->queue->enqueue($data); | |
return strlen($data); | |
} | |
function stream_cast($cast_as){ | |
//TODO: functionality which requires native stream will call this | |
switch($cast_as){ | |
case STREAM_CAST_FOR_SELECT: | |
//return ? | |
break; | |
case STREAM_CAST_AS_STREAM: | |
default: | |
//return ? | |
break; | |
} | |
} | |
function stream_tell() | |
{ | |
return $this->position; | |
} | |
function stream_close(){ | |
unset($this->queue); | |
} | |
function stream_eof() | |
{ | |
return empty($this->queue); | |
} | |
function stream_seek($offset, $whence) | |
{ | |
return false; | |
} | |
function stream_metadata($path, $option, $var) | |
{ | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('ReactStream.php'); | |
stream_wrapper_register('react','ReactStream',STREAM_IS_URL); | |
$react_instance = fopen("react://host:port","rw"); | |
// send task payload internally, or to another react instance | |
$task_payload = serialize(['task1', 'data1', 'data2']); | |
fwrite($react_instance,$task_payload); | |
// this would be the receiving end of the payload | |
$task_payload = unserialize(fread($react_instance, 1024)); | |
print_r($task_payload); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment