Source for file socket.php

Documentation is available at socket.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage MockObjects
  6.  *  @version    $Id: socket.php 1723 2008-04-08 00:34:10Z lastcraft $
  7.  */
  8.  
  9. /**#@+
  10.  * include SimpleTest files
  11.  */
  12. require_once(dirname(__FILE__'/compatibility.php');
  13. /**#@-*/
  14.  
  15.  *    Stashes an error for later. Useful for constructors
  16.  *    until PHP gets exceptions.
  17.  *    @package SimpleTest
  18.  *    @subpackage WebTester
  19.  */
  20.     var $_error = 'Constructor not chained';
  21.  
  22.     /**
  23.      *    Sets the error to empty.
  24.      *    @access public
  25.      */
  26.     function SimpleStickyError({
  27.         $this->_clearError();
  28.     }
  29.  
  30.     /**
  31.      *    Test for an outstanding error.
  32.      *    @return boolean           True if there is an error.
  33.      *    @access public
  34.      */
  35.     function isError({
  36.         return ($this->_error != '');
  37.     }
  38.  
  39.     /**
  40.      *    Accessor for an outstanding error.
  41.      *    @return string     Empty string if no error otherwise
  42.      *                        the error message.
  43.      *    @access public
  44.      */
  45.     function getError({
  46.         return $this->_error;
  47.     }
  48.  
  49.     /**
  50.      *    Sets the internal error.
  51.      *    @param string       Error message to stash.
  52.      *    @access protected
  53.      */
  54.     function _setError($error{
  55.         $this->_error = $error;
  56.     }
  57.  
  58.     /**
  59.      *    Resets the error state to no error.
  60.      *    @access protected
  61.      */
  62.     function _clearError({
  63.         $this->_setError('');
  64.     }
  65. }
  66.  
  67. /**
  68.  *    Wrapper for TCP/IP socket.
  69.  *    @package SimpleTest
  70.  *    @subpackage WebTester
  71.  */
  72. class SimpleSocket extends SimpleStickyError {
  73.     var $_handle;
  74.     var $_is_open = false;
  75.     var $_sent = '';
  76.     var $lock_size;
  77.  
  78.     /**
  79.      *    Opens a socket for reading and writing.
  80.      *    @param string $host          Hostname to send request to.
  81.      *    @param integer $port         Port on remote machine to open.
  82.      *    @param integer $timeout      Connection timeout in seconds.
  83.      *    @param integer $block_size   Size of chunk to read.
  84.      *    @access public
  85.      */
  86.     function SimpleSocket($host$port$timeout$block_size 255{
  87.         $this->SimpleStickyError();
  88.         if (($this->_handle = $this->_openSocket($host$port$error_number$error$timeout))) {
  89.             $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
  90.             return;
  91.         }
  92.         $this->_is_open = true;
  93.         $this->_block_size $block_size;
  94.         SimpleTestCompatibility::setTimeout($this->_handle$timeout);
  95.     }
  96.  
  97.     /**
  98.      *    Writes some data to the socket and saves alocal copy.
  99.      *    @param string $message       String to send to socket.
  100.      *    @return boolean              True if successful.
  101.      *    @access public
  102.      */
  103.     function write($message{
  104.         if ($this->isError(|| $this->isOpen()) {
  105.             return false;
  106.         }
  107.         $count fwrite($this->_handle$message);
  108.         if ($count{
  109.             if ($count === false{
  110.                 $this->_setError('Cannot write to socket');
  111.                 $this->close();
  112.             }
  113.             return false;
  114.         }
  115.         fflush($this->_handle);
  116.         $this->_sent .= $message;
  117.         return true;
  118.     }
  119.  
  120.     /**
  121.      *    Reads data from the socket. The error suppresion
  122.      *    is a workaround for PHP4 always throwing a warning
  123.      *    with a secure socket.
  124.      *    @return integer/boolean           Incoming bytes. False
  125.      *                                      on error.
  126.      *    @access public
  127.      */
  128.     function read({
  129.         if ($this->isError(|| $this->isOpen()) {
  130.             return false;
  131.         }
  132.         $raw @fread($this->_handle$this->_block_size);
  133.         if ($raw === false{
  134.             $this->_setError('Cannot read from socket');
  135.             $this->close();
  136.         }
  137.         return $raw;
  138.     }
  139.  
  140.     /**
  141.      *    Accessor for socket open state.
  142.      *    @return boolean           True if open.
  143.      *    @access public
  144.      */
  145.     function isOpen({
  146.         return $this->_is_open;
  147.     }
  148.  
  149.     /**
  150.      *    Closes the socket preventing further reads.
  151.      *    Cannot be reopened once closed.
  152.      *    @return boolean           True if successful.
  153.      *    @access public
  154.      */
  155.     function close({
  156.         $this->_is_open = false;
  157.         return fclose($this->_handle);
  158.     }
  159.  
  160.     /**
  161.      *    Accessor for content so far.
  162.      *    @return string        Bytes sent only.
  163.      *    @access public
  164.      */
  165.     function getSent({
  166.         return $this->_sent;
  167.     }
  168.  
  169.     /**
  170.      *    Actually opens the low level socket.
  171.      *    @param string $host          Host to connect to.
  172.      *    @param integer $port         Port on host.
  173.      *    @param integer $error_number Recipient of error code.
  174.      *    @param string $error         Recipoent of error message.
  175.      *    @param integer $timeout      Maximum time to wait for connection.
  176.      *    @access protected
  177.      */
  178.     function _openSocket($host$port&$error_number&$error$timeout{
  179.         return @fsockopen($host$port$error_number$error$timeout);
  180.     }
  181. }
  182.  
  183. /**
  184.  *    Wrapper for TCP/IP socket over TLS.
  185.  *    @package SimpleTest
  186.  *    @subpackage WebTester
  187.  */
  188. class SimpleSecureSocket extends SimpleSocket {
  189.  
  190.     /**
  191.      *    Opens a secure socket for reading and writing.
  192.      *    @param string $host      Hostname to send request to.
  193.      *    @param integer $port     Port on remote machine to open.
  194.      *    @param integer $timeout  Connection timeout in seconds.
  195.      *    @access public
  196.      */
  197.     function SimpleSecureSocket($host$port$timeout{
  198.         $this->SimpleSocket($host$port$timeout);
  199.     }
  200.  
  201.     /**
  202.      *    Actually opens the low level socket.
  203.      *    @param string $host          Host to connect to.
  204.      *    @param integer $port         Port on host.
  205.      *    @param integer $error_number Recipient of error code.
  206.      *    @param string $error         Recipient of error message.
  207.      *    @param integer $timeout      Maximum time to wait for connection.
  208.      *    @access protected
  209.      */
  210.     function _openSocket($host$port&$error_number&$error$timeout{
  211.         return parent::_openSocket("tls://$host"$port$error_number$error$timeout);
  212.     }
  213. }
  214. ?>

Documentation generated on Sun, 04 May 2008 09:22:10 -0500 by phpDocumentor 1.3.0