Index: branches/RC/core/kernel/utility/socket.php =================================================================== diff -u -N -r8929 -r10098 --- branches/RC/core/kernel/utility/socket.php (.../socket.php) (revision 8929) +++ branches/RC/core/kernel/utility/socket.php (.../socket.php) (revision 10098) @@ -5,51 +5,51 @@ * */ class kSocket extends kBase { - + /** * Socket file pointer. * @var resource $fp */ var $fp = null; - + /** * Whether the socket is blocking. Defaults to true. * @var boolean $blocking */ var $blocking = true; - + /** * Whether the socket is persistent. Defaults to false. * @var boolean $persistent */ var $persistent = false; - + /** * The IP address to connect to. * @var string $addr */ var $addr = ''; - + /** * The port number to connect to. * @var integer $port */ var $port = 0; - + /** * Number of seconds to wait on socket connections before assuming * there's no more data. Defaults to no timeout. * @var integer $timeout */ var $timeout = false; - + /** * Number of bytes to read at a time in readLine() and * readAll(). Defaults to 2048. * @var integer $lineLength */ var $lineLength = 2048; - + /** * Connect to the specified port. If called when the socket is * already connected, it disconnects and connects again. @@ -71,7 +71,7 @@ @fclose($this->fp); $this->fp = null; } - + // convert hostname to ip address if (!$addr) { return $this->raiseError('host address cannot be empty'); @@ -80,36 +80,36 @@ } else { $this->addr = @gethostbyname($addr); } - + $this->port = $port % 65536; - + if ($persistent !== null) { $this->persistent = $persistent; } - + if ($timeout !== null) { $this->timeout = $timeout; } - + $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; $errno = 0; $errstr = ''; - + if ($this->timeout) { $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); } else { $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); } - + if (!$fp) { return $this->raiseError($errstr, Array($errno)); } - + $this->fp = $fp; - + return $this->setBlocking($this->blocking); } - + /** * Disconnects from the peer, closes the socket. * @@ -121,12 +121,12 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + @fclose($this->fp); $this->fp = null; return true; } - + /** * Find out if the socket is in blocking mode. * @@ -137,7 +137,7 @@ { return $this->blocking; } - + /** * Sets whether the socket connection should be blocking or * not. A read call to a non-blocking socket will return immediately @@ -153,12 +153,12 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + $this->blocking = $mode; socket_set_blocking($this->fp, $this->blocking); return true; } - + /** * Sets the timeout value on socket descriptor, * expressed in the sum of seconds and microseconds @@ -173,10 +173,10 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + return socket_set_timeout($this->fp, $seconds, $microseconds); } - + /** * Returns information about an existing socket resource. * Currently returns four entries in the result array: @@ -196,10 +196,10 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + return socket_get_status($this->fp); } - + /** * Get a specified line of data * @@ -212,10 +212,10 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + return @fgets($this->fp, $size); } - + /** * Read a specified amount of data. This is guaranteed to return, * and has the added benefit of getting everything in one fread() @@ -232,10 +232,10 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + return @fread($this->fp, $size); } - + /** * Write a specified amount of data. * @@ -251,14 +251,14 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + if (is_null($blocksize) && !OS_WINDOWS) { return fwrite($this->fp, $data); } else { if (is_null($blocksize)) { $blocksize = 1024; } - + $pos = 0; $size = strlen($data); while ($pos < $size) { @@ -268,11 +268,11 @@ } $pos += $written; } - + return $pos; } } - + /** * Write a line of data to the socket, followed by a trailing "\r\n". * @@ -284,10 +284,10 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + return fwrite($this->fp, $data . "\r\n"); } - + /** * Tests for end-of-file on a socket descriptor. * @@ -298,7 +298,7 @@ { return (is_resource($this->fp) && feof($this->fp)); } - + /** * Read until either the end of the socket or a newline, whichever * comes first. Strips the trailing newline from the returned data. @@ -313,7 +313,7 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + $line = ''; $timeout = time() + $this->timeout; while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { @@ -324,7 +324,7 @@ } return $line; } - + /** * Read until the socket closes, or until there is no more data in * the inner PHP buffer. If the inner buffer is empty, in blocking @@ -343,19 +343,19 @@ if (!is_resource($this->fp)) { return $this->raiseError('not connected'); } - + $data = ''; while (!feof($this->fp)) { $data .= @fread($this->fp, $this->lineLength); } return $data; } - + function raiseError($text, $params = Array()) { trigger_error(vsprintf($text, $params), E_USER_WARNING); return false; } } - + ?> \ No newline at end of file