Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/socket.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/compatibility.php');
00021 class SimpleStickyError {
00022     var $_error = 'Constructor not chained';
00023 
00028     function SimpleStickyError() {
00029         $this->_clearError();
00030     }
00031 
00037     function isError() {
00038         return ($this->_error != '');
00039     }
00040 
00047     function getError() {
00048         return $this->_error;
00049     }
00050 
00056     function _setError($error) {
00057         $this->_error = $error;
00058     }
00059 
00064     function _clearError() {
00065         $this->_setError('');
00066     }
00067 }
00068 
00074 class SimpleSocket extends SimpleStickyError {
00075     var $_handle;
00076     var $_is_open = false;
00077     var $_sent = '';
00078     var $lock_size;
00079 
00088     function SimpleSocket($host, $port, $timeout, $block_size = 255) {
00089         $this->SimpleStickyError();
00090         if (! ($this->_handle = $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
00091             $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
00092             return;
00093         }
00094         $this->_is_open = true;
00095         $this->_block_size = $block_size;
00096         SimpleTestCompatibility::setTimeout($this->_handle, $timeout);
00097     }
00098 
00105     function write($message) {
00106         if ($this->isError() || ! $this->isOpen()) {
00107             return false;
00108         }
00109         $count = fwrite($this->_handle, $message);
00110         if (! $count) {
00111             if ($count === false) {
00112                 $this->_setError('Cannot write to socket');
00113                 $this->close();
00114             }
00115             return false;
00116         }
00117         fflush($this->_handle);
00118         $this->_sent .= $message;
00119         return true;
00120     }
00121 
00130     function read() {
00131         if ($this->isError() || ! $this->isOpen()) {
00132             return false;
00133         }
00134         $raw = @fread($this->_handle, $this->_block_size);
00135         if ($raw === false) {
00136             $this->_setError('Cannot read from socket');
00137             $this->close();
00138         }
00139         return $raw;
00140     }
00141 
00147     function isOpen() {
00148         return $this->_is_open;
00149     }
00150 
00157     function close() {
00158         $this->_is_open = false;
00159         return fclose($this->_handle);
00160     }
00161 
00167     function getSent() {
00168         return $this->_sent;
00169     }
00170 
00180     function _openSocket($host, $port, &$error_number, &$error, $timeout) {
00181         return @fsockopen($host, $port, $error_number, $error, $timeout);
00182     }
00183 }
00184 
00190 class SimpleSecureSocket extends SimpleSocket {
00191 
00199     function SimpleSecureSocket($host, $port, $timeout) {
00200         $this->SimpleSocket($host, $port, $timeout);
00201     }
00202 
00212     function _openSocket($host, $port, &$error_number, &$error, $timeout) {
00213         return parent::_openSocket("tls://$host", $port, $error_number, $error, $timeout);
00214     }
00215 }
00216 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations