diff --git a/src/DNode/Connection.php b/src/DNode/Connection.php new file mode 100644 index 0000000..a34bb9a --- /dev/null +++ b/src/DNode/Connection.php @@ -0,0 +1,32 @@ +bufferSize != $this->lastBufferSize) { + $this->adjustReadBufferSize($stream); + } + + $data = fread($stream, $this->bufferSize); + + if ('' === $data || false === $data) { + $this->end(); + } else { + $this->emit('data', array($data, $this)); + } + } + + protected function adjustReadBufferSize($stream) + { + $this->lastBufferSize = $this->bufferSize; + if (0 !== stream_set_read_buffer($stream, 0)) { + throw new \RuntimeException('Unable to set read buffer on stream'); + } + } +} \ No newline at end of file diff --git a/src/DNode/DNode.php b/src/DNode/DNode.php index bd76131..c6c3a93 100644 --- a/src/DNode/DNode.php +++ b/src/DNode/DNode.php @@ -3,7 +3,6 @@ use Evenement\EventEmitter; use React\EventLoop\LoopInterface; use React\Socket\Server; -use React\Socket\Connection; use React\Socket\ConnectionInterface; class DNode extends EventEmitter @@ -30,17 +29,31 @@ public function using($middleware) public function connect() { $params = $this->protocol->parseArgs(func_get_args()); + + if (!isset($params['scheme'])) { + $params['scheme'] = 'tcp'; + } + if (!isset($params['host'])) { $params['host'] = '127.0.0.1'; } if (!isset($params['port'])) { - throw new \Exception("For now we only support TCP connections to a defined port"); + throw new \Exception("For now we only support connections to a defined port"); + } + + if (!in_array($params['scheme'], stream_get_transports())) { + $e = new \InvalidArgumentException("Scheme {$params['scheme']} is not supported... are you missing an extension?"); + + $this->emit('error', array($e)); } - $client = @stream_socket_client("tcp://{$params['host']}:{$params['port']}"); + $url = "{$params['scheme']}://{$params['host']}:{$params['port']}"; + $client = @stream_socket_client($url); + if (!$client) { - $e = new \RuntimeException("No connection to DNode server in tcp://{$params['host']}:{$params['port']}"); + $e = new \RuntimeException("No connection to DNode server in $url"); + $this->emit('error', array($e)); if (!count($this->listeners('error'))) { diff --git a/src/DNode/Protocol.php b/src/DNode/Protocol.php index c7b1865..e625002 100644 --- a/src/DNode/Protocol.php +++ b/src/DNode/Protocol.php @@ -73,9 +73,11 @@ public function parseArgs($args) { continue; } - foreach ($arg as $key => $value) { - $params[$key] = $value; - } + $arg = (array) $arg; + } + + if (is_array($arg)) { + $params = array_merge($params, $arg); continue; } diff --git a/tests/DNode/ProtocolTest.php b/tests/DNode/ProtocolTest.php index 27c6bfc..30ea643 100644 --- a/tests/DNode/ProtocolTest.php +++ b/tests/DNode/ProtocolTest.php @@ -66,6 +66,11 @@ public function provideParseArgs() $obj->foo = 'bar'; $obj->baz = 'qux'; + $arr = array( + 'quux' => 'corge', + 'grault' => 'garply' + ); + return array( 'string number becomes port' => array( array('port' => '8080'), @@ -83,6 +88,10 @@ public function provideParseArgs() array('port' => 8080), array(8080), ), + 'array becomes merged' => array( + array('quux' => 'corge', 'grault' => 'garply'), + array($arr), + ), 'Closure becomes block' => array( array('block' => $closure), array($closure), @@ -102,11 +111,11 @@ public function provideParseArgs() * @test * @covers DNode\Protocol::parseArgs * @expectedException InvalidArgumentException - * @expectedExceptionMessage Not sure what to do about array arguments + * @expectedExceptionMessage Not sure what to do about boolean arguments */ public function parseArgsShouldRejectInvalidArgs() { - $args = array(array('wat')); + $args = array(true); $this->protocol->parseArgs($args); } }