diff --git a/CHANGELOG.md b/CHANGELOG.md index 476d943..76666a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,10 +38,7 @@ * split `stubbles\peer\Socket` into two classes * `stubbles\peer\Socket::connect()` now returns an instance of `stubbles\peer\Stream` * all functionallity to read and write on a socket is now in `stubbles\peer\Stream` - * split `stubbles\peer\BsdSocket` into two classes - * `stubbles\peer\Socket::Bsdconnect()` now returns an instance of `stubbles\peer\BsdSocketConnection` - * all functionallity to read and write on a socket is now in `stubbles\peer\BsdSocketConnection` - * `stubbles\peer\BsdSocket` is not an instance of `stubbles\peer\Socket` any more + * removed `stubbles\peer\BsdSocket` * `stubbles\peer\IpAddress::openSocket()` and `stubbles\peer\IpAddress::openSecureSocket()` now return `stubbles\peer\Stream` diff --git a/src/main/php/peer/BsdSocket.php b/src/main/php/peer/BsdSocket.php deleted file mode 100644 index 49ca564..0000000 --- a/src/main/php/peer/BsdSocket.php +++ /dev/null @@ -1,226 +0,0 @@ - 'SOCK_STREAM', - SOCK_DGRAM => 'SOCK_DGRAM', - SOCK_RAW => 'SOCK_RAW', - SOCK_SEQPACKET => 'SOCK_SEQPACKET', - SOCK_RDM => 'SOCK_RDM' - ]; - - /** - * constructor - * - * Port can be null for SocketDomain::$AF_UNIX, all other domains require - * a port. - * - * @param \stubbles\peer\SocketDomain $domain one of SocketDomain::$AF_INET, SocketDomain::$AF_INET6 or SocketDomain::$AF_UNIX - * @param string $host host to connect socket to - * @param int $port optional port to connect socket to, defaults to 80 - * @throws \InvalidArgumentException - */ - public function __construct(SocketDomain $domain, $host, $port = null) - { - if ($domain->requiresPort() && empty($port)) { - throw new \InvalidArgumentException( - 'Domain ' . $domain->name() . ' requires a port' - ); - } - - $this->host = $host; - $this->port = $port; - $this->domain = $domain; - } - - /** - * sets the socket type - * - * @param int $type one of SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET or SOCK_RDM - * @return \stubbles\peer\BsdSocket - * @throws \InvalidArgumentException - */ - public function setType($type) - { - if (!in_array($type, array_keys(self::$types))) { - throw new \InvalidArgumentException( - 'Type must be one of SOCK_STREAM, SOCK_DGRAM, SOCK_RAW,' - . ' SOCK_SEQPACKET or SOCK_RDM.' - ); - } - - $this->type = $type; - return $this; - } - - /** - * returns the socket type - * - * @return int - */ - public function type() - { - return $this->type; - } - - /** - * enables tcp protocol - * - * @return \stubbles\peer\BsdSocket - */ - public function useTcp() - { - $this->protocol = SOL_TCP; - return $this; - } - - /** - * checks whether socket uses tcp - * - * @return bool - */ - public function isTcp() - { - return SOL_TCP === $this->protocol; - } - - /** - * enables udp protocol - * - * @return \stubbles\peer\BsdSocket - */ - public function useUdp() - { - $this->protocol = SOL_UDP; - return $this; - } - - /** - * checks whether socket uses udp - * - * @return bool - */ - public function isUdp() - { - return SOL_UDP === $this->protocol; - } - - /** - * sets an option - * - * @param int $level protocol level of option - * @param int $name option name - * @param mixed $value option value - * @return \stubbles\peer\BsdSocket - */ - public function setOption($level, $name, $value) - { - if (!isset($this->options[$level])) { - $this->options[$level] = []; - } - - $this->options[$level][$name] = $value; - return $this; - } - - /** - * returns an option - * - * @param int $level protocol level of option - * @param int $name option name - * @param mixed $default value to return if option not set - * @return mixed - */ - public function option($level, $name, $default) - { - if (isset($this->options[$level]) && isset($this->options[$level][$name])) { - return $this->options[$level][$name]; - } - - return $default; - } - - /** - * opens a socket connection - * - * @return \stubbles\peer\BsdSocketConnection - * @throws \stubbles\peer\ConnectionException - */ - public function connect() - { - $socket = @socket_create( - $this->domain->value(), - $this->type, - $this->protocol - ); - if (false === $socket) { - throw new ConnectionException( - sprintf( - 'Create of %s socket (type %s, protocol %s) failed.', - $this->domain->name(), - self::$types[$this->type], - getprotobynumber($this->protocol) - ) - ); - } - - $connection = new BsdSocketConnection( - $this->domain->connect($socket, $this->host, $this->port) - ); - return $connection->setOptions($this->options); - } -} diff --git a/src/main/php/peer/BsdSocketConnection.php b/src/main/php/peer/BsdSocketConnection.php deleted file mode 100644 index 06e989e..0000000 --- a/src/main/php/peer/BsdSocketConnection.php +++ /dev/null @@ -1,215 +0,0 @@ -resource = $resource; - } - - /** - * destructor - */ - public function __destruct() - { - // on unit tests resource might be closed from outside - if (is_resource($this->resource)) { - socket_close($this->resource); - } - } - - /** - * sets a lis of options on the connection - * - * @param array $options - * @return $this - */ - public function setOptions(array $options) - { - foreach ($options as $level => $pairs) { - foreach ($pairs as $name => $value) { - $this->setOption($level, $name, $value); - } - } - - return $this; - } - - /** - * sets an option - * - * @param int $level protocol level of option - * @param int $name option name - * @param mixed $value option value - * @return $this - */ - public function setOption($level, $name, $value) - { - if (!socket_set_option($this->resource, $level, $name, $value)) { - throw new ConnectionException( - 'Failed to set option ' . $name - . ' on level ' . $level . ' to value ' . $value - ); - } - return $this; - } - - /** - * returns an option - * - * @param int $level protocol level of option - * @param int $name option name - * @return mixed - * @throws \stubbles\peer\ConnectionException - */ - public function option($level, $name) - { - $option = socket_get_option($this->resource, $level, $name); - if (false === $option) { - throw new ConnectionException( - 'Failed to retrieve option ' . $name . ' on level ' . $level - ); - } - - return $option; - } - - /** - * returns last error - * - * @return string - */ - public function lastError() - { - $e = socket_last_error($this->fp); - return $e . ': ' . socket_strerror($e); - } - - /** - * read from socket - * - * @param int $length length of data to read - * @return string - * @throws \LogicException - */ - public function read($length = 4096) - { - return $this->doRead($length, PHP_NORMAL_READ); - } - - /** - * read a whole line from socket - * - * @param int $length length of data to read - * @return string - */ - public function readLine($length = 4096) - { - return rtrim($this->read($length)); - } - - /** - * read binary data from socket - * - * @param int $length length of data to read - * @return string - * @throws \LogicException - */ - public function readBinary($length = 1024) - { - return $this->doRead($length, PHP_BINARY_READ); - } - - /** - * write data to socket and returns the amount of written bytes - * - * @param string $data data to write - * @return int - * @throws \stubbles\peer\ConnectionException - * @throws \LogicException - */ - public function write($data) - { - $length = socket_write($this->resource, $data, strlen($data)); - if (false === $length) { - throw new ConnectionException('"Writing of ' . strlen($data) . ' bytes failed.'); - } - - return $length; - } - - /** - * helper method to do the actual reading - * - * @param int $length length of data to read - * @param int $type one of PHP_BINARY_READ or PHP_NORMAL_READ - * @return string - * @throws \stubbles\peer\ConnectionException - */ - protected function doRead($length, $type) - { - $result = socket_read($this->resource, $length, $type); - if (false === $result) { - throw new ConnectionException('Read failed: ' . $this->lastError()); - } - - if (empty($result)) { - $this->eof = true; - $result = null; - } - - return $result; - } - - /** - * check if we reached end of data - * - * @return bool - */ - public function eof() - { - return $this->eof; - } -} diff --git a/src/test/php/peer/BsdSocketConnectionTest.php b/src/test/php/peer/BsdSocketConnectionTest.php deleted file mode 100644 index 09a4164..0000000 --- a/src/test/php/peer/BsdSocketConnectionTest.php +++ /dev/null @@ -1,69 +0,0 @@ -getMockBuilder( - 'stubbles\peer\BsdSocketConnection' - )->disableOriginalConstructor() - ->setMethods(['doRead']) - ->getMock(); - } - - /** - * @test - */ - public function readReturnsData() - { - $bsdSocket = $this->createBsdSocketMock(); - $bsdSocket->method('doRead') - ->with(equalTo(4096), equalTo(PHP_NORMAL_READ)) - ->will(returnValue("foo\n")); - assertEquals("foo\n", $bsdSocket->read()); - } - - /** - * @test - */ - public function readLineReturnsDataWithoutLinebreak() - { - $bsdSocket = $this->createBsdSocketMock(); - $bsdSocket->method('doRead') - ->with(equalTo(4096), equalTo(PHP_NORMAL_READ)) - ->will(returnValue("foo\n")); - assertEquals('foo', $bsdSocket->readLine()); - } - - /** - * @test - */ - public function readBinaryReturnsData() - { - $bsdSocket = $this->createBsdSocketMock(); - $bsdSocket->method('doRead') - ->with(equalTo(1024), equalTo(PHP_BINARY_READ)) - ->will(returnValue("foo\n")); - assertEquals("foo\n", $bsdSocket->readBinary()); - } -} diff --git a/src/test/php/peer/BsdSocketTest.php b/src/test/php/peer/BsdSocketTest.php deleted file mode 100644 index dd6f559..0000000 --- a/src/test/php/peer/BsdSocketTest.php +++ /dev/null @@ -1,141 +0,0 @@ -type()); - } - - /** - * @test - */ - public function typeCanBeSetToSOCK_DGRAM() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals(SOCK_DGRAM, $bsdSocket->setType(SOCK_DGRAM)->type()); - } - - /** - * @test - */ - public function typeCanBeSetToSOCK_RAW() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals(SOCK_RAW, $bsdSocket->setType(SOCK_RAW)->type()); - } - - /** - * @test - */ - public function typeCanBeSetToSOCK_SEQPACKET() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals( - SOCK_SEQPACKET, - $bsdSocket->setType(SOCK_SEQPACKET)->type() - ); - } - - /** - * @test - */ - public function typeCanBeSetToSOCK_RDM() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals(SOCK_RDM, $bsdSocket->setType(SOCK_RDM)->type()); - } - - /** - * trying to set an invalid type throws an illegal argument exception - * - * @test - * @expectedException InvalidArgumentException - */ - public function invalidTypeThrowsIllegalArgumentException() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - $bsdSocket->setType('invalid'); - } - - /** - * @test - */ - public function protocolDefaultsToTcp() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_INET, 'example.com', 80); - assertTrue($bsdSocket->isTcp()); - assertFalse($bsdSocket->isUdp()); - } - - /** - * @test - */ - public function protocolCanBeSetToTcp() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_INET, 'example.com', 80); - assertTrue($bsdSocket->useTcp()->isTcp()); - assertFalse($bsdSocket->isUdp()); - } - - /** - * @test - */ - public function protocolCanBeSetToUdp() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_INET, 'example.com', 80); - assertTrue($bsdSocket->useUdp()->isUdp()); - assertFalse($bsdSocket->isTcp()); - } - - /** - * @test - */ - public function returnsDefaultForOptionNotSet() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals( - 'default', - $bsdSocket->option('bar', 'baz', 'default') - ); - } - - /** - * @test - */ - public function returnsValueFromOptionAlreadySet() - { - $bsdSocket = createBsdSocket(SocketDomain::$AF_UNIX, '/tmp/mysocket'); - assertEquals( - 'foo', - $bsdSocket->setOption('bar', 'baz', 'foo') - ->option('bar', 'baz', 'default') - ); - } -}