| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Socks; |
| 4 |
|
| 5 |
use React\Promise\Deferred; |
| 6 |
use \InvalidArgumentException; |
| 7 |
use \UnexpectedValueException; |
| 8 |
|
| 9 |
/** |
| 10 |
* @internal |
| 11 |
*/ |
| 12 |
final class StreamReader |
| 13 |
{ |
| 14 |
const RET_DONE = true; |
| 15 |
const RET_INCOMPLETE = null; |
| 16 |
|
| 17 |
private $buffer = ''; |
| 18 |
private $queue = array(); |
| 19 |
|
| 20 |
public function write($data) |
| 21 |
{ |
| 22 |
$this->buffer .= $data; |
| 23 |
|
| 24 |
do { |
| 25 |
$current = reset($this->queue); |
| 26 |
|
| 27 |
if ($current === false) { |
| 28 |
break; |
| 29 |
} |
| 30 |
|
| 31 |
/* @var $current Closure */ |
| 32 |
|
| 33 |
$ret = $current($this->buffer); |
| 34 |
|
| 35 |
if ($ret === self::RET_INCOMPLETE) { |
| 36 |
// current is incomplete, so wait for further data to arrive |
| 37 |
break; |
| 38 |
} else { |
| 39 |
// current is done, remove from list and continue with next |
| 40 |
array_shift($this->queue); |
| 41 |
} |
| 42 |
} while (true); |
| 43 |
} |
| 44 |
|
| 45 |
public function readBinary($structure) |
| 46 |
{ |
| 47 |
$length = 0; |
| 48 |
$unpack = ''; |
| 49 |
foreach ($structure as $name=>$format) { |
| 50 |
if ($length !== 0) { |
| 51 |
$unpack .= '/'; |
| 52 |
} |
| 53 |
$unpack .= $format . $name; |
| 54 |
|
| 55 |
if ($format === 'C') { |
| 56 |
++$length; |
| 57 |
} else if ($format === 'n') { |
| 58 |
$length += 2; |
| 59 |
} else if ($format === 'N') { |
| 60 |
$length += 4; |
| 61 |
} else { |
| 62 |
throw new InvalidArgumentException('Invalid format given'); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $this->readLength($length)->then(function ($response) use ($unpack) { |
| 67 |
return unpack($unpack, $response); |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
public function readLength($bytes) |
| 72 |
{ |
| 73 |
$deferred = new Deferred(); |
| 74 |
|
| 75 |
$this->readBufferCallback(function (&$buffer) use ($bytes, $deferred) { |
| 76 |
if (strlen($buffer) >= $bytes) { |
| 77 |
$deferred->resolve((string)substr($buffer, 0, $bytes)); |
| 78 |
$buffer = (string)substr($buffer, $bytes); |
| 79 |
|
| 80 |
return StreamReader::RET_DONE; |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
return $deferred->promise(); |
| 85 |
} |
| 86 |
|
| 87 |
public function readByte() |
| 88 |
{ |
| 89 |
return $this->readBinary(array( |
| 90 |
'byte' => 'C' |
| 91 |
))->then(function ($data) { |
| 92 |
return $data['byte']; |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
public function readByteAssert($expect) |
| 97 |
{ |
| 98 |
return $this->readByte()->then(function ($byte) use ($expect) { |
| 99 |
if ($byte !== $expect) { |
| 100 |
throw new UnexpectedValueException('Unexpected byte encountered'); |
| 101 |
} |
| 102 |
return $byte; |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
public function readStringNull() |
| 107 |
{ |
| 108 |
$deferred = new Deferred(); |
| 109 |
$string = ''; |
| 110 |
|
| 111 |
$that = $this; |
| 112 |
$readOne = function () use (&$readOne, $that, $deferred, &$string) { |
| 113 |
$that->readByte()->then(function ($byte) use ($deferred, &$string, $readOne) { |
| 114 |
if ($byte === 0x00) { |
| 115 |
$deferred->resolve($string); |
| 116 |
} else { |
| 117 |
$string .= chr($byte); |
| 118 |
$readOne(); |
| 119 |
} |
| 120 |
}); |
| 121 |
}; |
| 122 |
$readOne(); |
| 123 |
|
| 124 |
return $deferred->promise(); |
| 125 |
} |
| 126 |
|
| 127 |
public function readBufferCallback(/* callable */ $callable) |
| 128 |
{ |
| 129 |
if (!is_callable($callable)) { |
| 130 |
throw new InvalidArgumentException('Given function must be callable'); |
| 131 |
} |
| 132 |
|
| 133 |
if ($this->queue) { |
| 134 |
$this->queue []= $callable; |
| 135 |
} else { |
| 136 |
$this->queue = array($callable); |
| 137 |
|
| 138 |
if ($this->buffer !== '') { |
| 139 |
// this is the first element in the queue and the buffer is filled => trigger write procedure |
| 140 |
$this->write(''); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public function getBuffer() |
| 146 |
{ |
| 147 |
return $this->buffer; |
| 148 |
} |
| 149 |
} |