cvs: php4 /pear/Net Socket.php
| From: | Stig Bakken | Date: | Mon, 28 Jan 2002 22:26:20 +0000 |
| Subject: | cvs: php4 /pear/Net Socket.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-2225@lists.php.net to get a copy of this message | ||
ssb Mon Jan 28 17:26:20 2002 EDT
Modified files:
/php4/pear/Net Socket.php
Log:
* doc comments, add setTimeout() and getStatus(), thanks to
Mads Mohr Christensen <mohr@slamkode.dk>
Index: php4/pear/Net/Socket.php diff -u php4/pear/Net/Socket.php:1.9 php4/pear/Net/Socket.php:1.10 --- php4/pear/Net/Socket.php:1.9 Tue Dec 11 10:31:38 2001 +++ php4/pear/Net/Socket.php Mon Jan 28 17:26:20 2002 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: Socket.php,v 1.9 2001/12/11 15:31:38 sebastian Exp $ +// $Id: Socket.php,v 1.10 2002/01/28 22:26:20 ssb Exp $ // require_once 'PEAR.php'; @@ -40,7 +40,7 @@ var $blocking = true; /** Whether the socket is persistent. */ - var $persistent; + var $persistent = false; /** The IP address to connect to. */ var $addr = ''; @@ -83,7 +83,7 @@ * @access public * @return mixed true on success or error object */ - function connect($addr, $port, $persistent = false, $timeout = false) { + function connect($addr, $port, $persistent = null, $timeout = null) { if (is_resource($this->fp)) { @fclose($this->fp); $this->fp = null; @@ -95,8 +95,13 @@ $this->addr = gethostbyname($addr); } $this->port = $port % 65536; - $this->timeout = $timeout; - $openfunc = $persistent ? 'pfsockopen' : 'fsockopen'; + if ($persistent !== null) { + $this->persistent = $persistent; + } + if ($timeout !== null) { + $this->timeout = $timeout; + } + $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; $errno = 0; $errstr = ''; if ($this->timeout) { @@ -108,11 +113,10 @@ if (!$fp) { return new PEAR_Error($errstr, $errno); } - - socket_set_blocking($fp, $this->blocking); + $this->fp = $fp; - $this->persistent = $persistent; - return true; + + return $this->setBlocking($this->blocking); } // }}} @@ -154,16 +158,68 @@ * * @param $mode bool true for blocking sockets, false for nonblocking * @access public + * @return mixed true on success or an error object otherwise */ function setBlocking($mode) { - $this->blocking = $mode; if (is_resource($this->fp)) { - set_socket_blocking($this->fp, $mode); + $this->blocking = $mode; + socket_set_blocking($this->fp, $this->blocking); + return true; } + return new PEAR_Error("not connected"); + } + // }}} + + // {{{ setTimeout() + /** + * Sets the timeout value on socket descriptor, + * expressed in the sum of seconds and microseconds + * + * @param $seconds int seconds + * @param $microseconds int microseconds + * @access public + * @return mixed true on success or an error object otherwise + */ + function setTimeout($seconds, $microseconds) { + if (is_resource($this->fp)) { + socket_set_timeout($this->fp, $seconds, $microseconds); + return true; + } + return new PEAR_Error("not connected"); + } + // }}} + + // {{{ getStatus() + /** + * Returns information about an existing socket resource. + * Currently returns four entries in the result array: + * + * <p> + * timed_out (bool) - The socket timed out waiting for data<br> + * blocked (bool) - The socket was blocked<br> + * eof (bool) - Indicates EOF event<br> + * unread_bytes (int) - Number of bytes left in the socket buffer<br> + * </p> + * + * @access public + * @return mixed Array containing information about existing socket resource or an error object otherwise + */ + function getStatus() { + if (is_resource($this->fp)) { + return socket_get_status($this->fp); + } + return new PEAR_Error("not connected"); } // }}} // {{{ gets() + /** + * Get a specified line of data + * + * @access public + * @return $size bytes of data from the socket, or a PEAR_Error if + * not connected. + */ function gets($size) { if (is_resource($this->fp)) { return fgets($this->fp, $size); @@ -193,6 +249,12 @@ // }}} // {{{ write() + /** + * Write a specified amount of data. + * + * @access public + * @return mixed true on success or an error object otherwise + */ function write($data) { if (is_resource($this->fp)) { return fwrite($this->fp, $data); @@ -210,31 +272,51 @@ */ function writeLine ($data) { if (is_resource($this->fp)) { - return fwrite($this->fp, "$data\r\n"); + return $this->write($data . "\r\n"); } return new PEAR_Error("not connected"); } // }}} // {{{ eof() + /** + * Tests for end-of-file on a socket descriptor + * + * @access public + * @return bool + */ function eof() { return (is_resource($this->fp) && feof($this->fp)); } // }}} // {{{ readByte() + /** + * Reads a byte of data + * + * @access public + * @return 1 byte of data from the socket, or a PEAR_Error if + * not connected. + */ function readByte() { if (is_resource($this->fp)) { - return ord(fread($this->fp, 1)); + return ord($this->read(1)); } return new PEAR_Error("not connected"); } // }}} // {{{ readWord() + /** + * Reads a word of data + * + * @access public + * @return 1 word of data from the socket, or a PEAR_Error if + * not connected. + */ function readWord() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 2); + $buf = $this->read(2); return (ord($buf[0]) + (ord($buf[1]) << 8)); } return new PEAR_Error("not connected"); @@ -242,9 +324,16 @@ // }}} // {{{ readInt() + /** + * Reads an int of data + * + * @access public + * @return 1 int of data from the socket, or a PEAR_Error if + * not connected. + */ function readInt() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 4); + $buf = $this->read(4); return (ord($buf[0]) + (ord($buf[1]) << 8) + (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); } @@ -253,10 +342,17 @@ // }}} // {{{ readString() + /** + * Reads a zeroterminated string of data + * + * @access public + * @return string, or a PEAR_Error if + * not connected. + */ function readString() { if (is_resource($this->fp)) { $string = ''; - while (($char = fread($this->fp, 1)) != "\x00") { + while (($char = $this->read(1)) != "\x00") { $string .= $char; } return $string; @@ -266,9 +362,16 @@ // }}} // {{{ readIPAddress() + /** + * Reads an IP Address and returns it in a dot formated string + * + * @access public + * @return Dot formated string, or a PEAR_Error if + * not connected. + */ function readIPAddress() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 4); + $buf = $this->read(4); return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), ord($buf[2]), ord($buf[3])); } @@ -283,14 +386,15 @@ * * @access public * @return All available data up to a newline, without that - * newline, or until the end of the socket. + * newline, or until the end of the socket, or a PEAR_Error if + * not connected. */ function readLine() { if (is_resource($this->fp)) { $line = ''; $timeout = time() + $this->timeout; - while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { - $line .= fgets($this->fp, $this->lineLength); + while (!$this->eof() && (!$this->timeout || time() < $timeout)) { + $line .= $this->gets($this->lineLength); if (strlen($line) >= 2 && (substr($line, -2) == "\r\n" || substr($line, -1) == "\n")) { @@ -309,13 +413,14 @@ * socket is in blocking mode until the socket closes. * * @access public - * @return All data until the socket closes. + * @return All data until the socket closes, or a PEAR_Error if + * not connected. */ function readAll() { if (is_resource($this->fp)) { $data = ''; - while (!feof($this->fp)) - $data .= fread($this->fp, $this->lineLength); + while (!$this->eof()) + $data .= $this->read($this->lineLength); return $data; } return new PEAR_Error("not connected");
Index: php4/pear/Net/Socket.php diff -u php4/pear/Net/Socket.php:1.9 php4/pear/Net/Socket.php:1.10 --- php4/pear/Net/Socket.php:1.9 Tue Dec 11 10:31:38 2001 +++ php4/pear/Net/Socket.php Mon Jan 28 17:26:20 2002 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: Socket.php,v 1.9 2001/12/11 15:31:38 sebastian Exp $ +// $Id: Socket.php,v 1.10 2002/01/28 22:26:20 ssb Exp $ // require_once 'PEAR.php'; @@ -40,7 +40,7 @@ var $blocking = true; /** Whether the socket is persistent. */ - var $persistent; + var $persistent = false; /** The IP address to connect to. */ var $addr = ''; @@ -83,7 +83,7 @@ * @access public * @return mixed true on success or error object */ - function connect($addr, $port, $persistent = false, $timeout = false) { + function connect($addr, $port, $persistent = null, $timeout = null) { if (is_resource($this->fp)) { @fclose($this->fp); $this->fp = null; @@ -95,8 +95,13 @@ $this->addr = gethostbyname($addr); } $this->port = $port % 65536; - $this->timeout = $timeout; - $openfunc = $persistent ? 'pfsockopen' : 'fsockopen'; + if ($persistent !== null) { + $this->persistent = $persistent; + } + if ($timeout !== null) { + $this->timeout = $timeout; + } + $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; $errno = 0; $errstr = ''; if ($this->timeout) { @@ -108,11 +113,10 @@ if (!$fp) { return new PEAR_Error($errstr, $errno); } - - socket_set_blocking($fp, $this->blocking); + $this->fp = $fp; - $this->persistent = $persistent; - return true; + + return $this->setBlocking($this->blocking); } // }}} @@ -154,16 +158,68 @@ * * @param $mode bool true for blocking sockets, false for nonblocking * @access public + * @return mixed true on success or an error object otherwise */ function setBlocking($mode) { - $this->blocking = $mode; if (is_resource($this->fp)) { - set_socket_blocking($this->fp, $mode); + $this->blocking = $mode; + socket_set_blocking($this->fp, $this->blocking); + return true; } + return new PEAR_Error("not connected"); + } + // }}} + + // {{{ setTimeout() + /** + * Sets the timeout value on socket descriptor, + * expressed in the sum of seconds and microseconds + * + * @param $seconds int seconds + * @param $microseconds int microseconds + * @access public + * @return mixed true on success or an error object otherwise + */ + function setTimeout($seconds, $microseconds) { + if (is_resource($this->fp)) { + socket_set_timeout($this->fp, $seconds, $microseconds); + return true; + } + return new PEAR_Error("not connected"); + } + // }}} + + // {{{ getStatus() + /** + * Returns information about an existing socket resource. + * Currently returns four entries in the result array: + * + * <p> + * timed_out (bool) - The socket timed out waiting for data<br> + * blocked (bool) - The socket was blocked<br> + * eof (bool) - Indicates EOF event<br> + * unread_bytes (int) - Number of bytes left in the socket buffer<br> + * </p> + * + * @access public + * @return mixed Array containing information about existing socket resource or an error object otherwise + */ + function getStatus() { + if (is_resource($this->fp)) { + return socket_get_status($this->fp); + } + return new PEAR_Error("not connected"); } // }}} // {{{ gets() + /** + * Get a specified line of data + * + * @access public + * @return $size bytes of data from the socket, or a PEAR_Error if + * not connected. + */ function gets($size) { if (is_resource($this->fp)) { return fgets($this->fp, $size); @@ -193,6 +249,12 @@ // }}} // {{{ write() + /** + * Write a specified amount of data. + * + * @access public + * @return mixed true on success or an error object otherwise + */ function write($data) { if (is_resource($this->fp)) { return fwrite($this->fp, $data); @@ -210,31 +272,51 @@ */ function writeLine ($data) { if (is_resource($this->fp)) { - return fwrite($this->fp, "$data\r\n"); + return $this->write($data . "\r\n"); } return new PEAR_Error("not connected"); } // }}} // {{{ eof() + /** + * Tests for end-of-file on a socket descriptor + * + * @access public + * @return bool + */ function eof() { return (is_resource($this->fp) && feof($this->fp)); } // }}} // {{{ readByte() + /** + * Reads a byte of data + * + * @access public + * @return 1 byte of data from the socket, or a PEAR_Error if + * not connected. + */ function readByte() { if (is_resource($this->fp)) { - return ord(fread($this->fp, 1)); + return ord($this->read(1)); } return new PEAR_Error("not connected"); } // }}} // {{{ readWord() + /** + * Reads a word of data + * + * @access public + * @return 1 word of data from the socket, or a PEAR_Error if + * not connected. + */ function readWord() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 2); + $buf = $this->read(2); return (ord($buf[0]) + (ord($buf[1]) << 8)); } return new PEAR_Error("not connected"); @@ -242,9 +324,16 @@ // }}} // {{{ readInt() + /** + * Reads an int of data + * + * @access public + * @return 1 int of data from the socket, or a PEAR_Error if + * not connected. + */ function readInt() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 4); + $buf = $this->read(4); return (ord($buf[0]) + (ord($buf[1]) << 8) + (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); } @@ -253,10 +342,17 @@ // }}} // {{{ readString() + /** + * Reads a zeroterminated string of data + * + * @access public + * @return string, or a PEAR_Error if + * not connected. + */ function readString() { if (is_resource($this->fp)) { $string = ''; - while (($char = fread($this->fp, 1)) != "\x00") { + while (($char = $this->read(1)) != "\x00") { $string .= $char; } return $string; @@ -266,9 +362,16 @@ // }}} // {{{ readIPAddress() + /** + * Reads an IP Address and returns it in a dot formated string + * + * @access public + * @return Dot formated string, or a PEAR_Error if + * not connected. + */ function readIPAddress() { if (is_resource($this->fp)) { - $buf = fread($this->fp, 4); + $buf = $this->read(4); return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), ord($buf[2]), ord($buf[3])); } @@ -283,14 +386,15 @@ * * @access public * @return All available data up to a newline, without that - * newline, or until the end of the socket. + * newline, or until the end of the socket, or a PEAR_Error if + * not connected. */ function readLine() { if (is_resource($this->fp)) { $line = ''; $timeout = time() + $this->timeout; - while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { - $line .= fgets($this->fp, $this->lineLength); + while (!$this->eof() && (!$this->timeout || time() < $timeout)) { + $line .= $this->gets($this->lineLength); if (strlen($line) >= 2 && (substr($line, -2) == "\r\n" || substr($line, -1) == "\n")) { @@ -309,13 +413,14 @@ * socket is in blocking mode until the socket closes. * * @access public - * @return All data until the socket closes. + * @return All data until the socket closes, or a PEAR_Error if + * not connected. */ function readAll() { if (is_resource($this->fp)) { $data = ''; - while (!feof($this->fp)) - $data .= fread($this->fp, $this->lineLength); + while (!$this->eof()) + $data .= $this->read($this->lineLength); return $data; } return new PEAR_Error("not connected");