home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Socket / Connection.php next >
Encoding:
PHP Script  |  2008-07-02  |  9.1 KB  |  368 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: Connection                               |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Connection.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::Connection
  18. * @author       Michael Wallner <mike@php.net>
  19. * @package      System_Socket
  20. * @category     System
  21. * @example      simple-debugger.php     Simple example for a HTTP connection
  22. */
  23.  
  24. if (!defined('SYSTEM_SOCKET_ROOT')) {
  25.     define('SYSTEM_SOCKET_ROOT', dirname(__FILE__) . '/..');
  26. }
  27.  
  28. require_once SYSTEM_SOCKET_ROOT . '/Socket/Manager.php';
  29.  
  30. /** 
  31. * System_Socket_Connection
  32. *
  33. * @author   Michael Wallner <mike@php.net>
  34. * @version  $Revision: 1.1 $
  35. * @access   public
  36. */
  37. class System_Socket_Connection extends System_Socket_Manager
  38. {
  39.     /**
  40.     * Connection ID
  41.     * @access   protected
  42.     * @var      string
  43.     */
  44.     var $ID = null;
  45.     
  46.     /**
  47.     * Peer host
  48.     * @access public
  49.     * @readonly
  50.     */
  51.     var $peerHost = '';
  52.     
  53.     /**
  54.     * Peer port
  55.     * @access public
  56.     * @readonly
  57.     */
  58.     var $peerPort = 0;
  59.     
  60.     /**
  61.     * Constructor
  62.     * 
  63.     * Instantiate a new System_Socket_Connection object with the underlying
  64.     * System_Socket object $socket.
  65.     *
  66.     * @access public
  67.     * @return object  System_Socket_Connection
  68.     * @param  object  $socket System_Socekt object
  69.     */
  70.     function System_Socket_Connection(&$socket)
  71.     {
  72.         System_Socket_Connection::__construct($socket);
  73.     }
  74.     
  75.     /**
  76.     * ZE2 Constructor
  77.     * @ignore
  78.     */
  79.     function __construct(&$socket)
  80.     {
  81.         parent::__construct($socket);
  82.         
  83.         if ($this->hasSocket) {
  84.             @socket_getPeername(
  85.                 $this->Socket->getResource(), 
  86.                 $this->peerHost, 
  87.                 $this->peerPort
  88.             );
  89.             
  90.             $this->ID = md5($this->peerHost . $this->peerPort . $this->Socket->getResource());
  91.         }
  92.     }
  93.     
  94.     /**
  95.     * Get connection ID
  96.     * 
  97.     * Each connection is represented by a unique id.
  98.     *
  99.     * @access   public
  100.     * @return   string
  101.     */
  102.     function getID()
  103.     {
  104.         return $this->ID;
  105.     }
  106.     
  107.     /**
  108.     * Check if wer're connected
  109.     * 
  110.     * Check if the System_Socket_Connection is still connected.  Actually we
  111.     * check if the underlying socket resource handle is still valid.
  112.     *
  113.     * @access   public
  114.     * @return   bool
  115.     */
  116.     function isConnected()
  117.     {
  118.         return ($this->hasSocket && $this->Socket->hasResource());
  119.     }
  120.     
  121.     /**
  122.     * Set socket blocking
  123.     * 
  124.     * Set the blocking mode of the underlying System_Socket.
  125.     * 
  126.     * @access   public
  127.     * @return   bool
  128.     */
  129.     function setBlocking($blocking)
  130.     {
  131.         return $this->notify(__FUNCTION__, 
  132.             $this->Socket->setBlocking($blocking), $blocking);
  133.     }
  134.     
  135.     /**
  136.     * Get socket blocking
  137.     * 
  138.     * Get the blocking mode of the underlying System_Socket.
  139.     * 
  140.     * @access   public
  141.     * @return   bool
  142.     */
  143.     function getBlocking()
  144.     {
  145.         return $this->notify(__FUNCTION__, $this->Socket->getBlocking());
  146.     }
  147.     
  148.     /**
  149.     * Write through connection
  150.     *
  151.     * Write data through the socket connection.
  152.     *
  153.     * @access public
  154.     * @return mixed  int bytes written or false
  155.     * @param  string $data
  156.     */
  157.     function write($data)
  158.     {
  159.         return $this->notify(__FUNCTION__, 
  160.             @socket_write($this->Socket->getResource(), $data), $data);
  161.     }
  162.     
  163.     /**
  164.     * Write char (chr(byte))
  165.     * 
  166.     * @access   public
  167.     * @return   mixed
  168.     * @param    string  $char
  169.     */
  170.     function writeChar($char)
  171.     {
  172.         $this->notify(__FUNCTION__, $this->write($char), $char);
  173.     }
  174.     
  175.     /**
  176.     * Write byte
  177.     * 
  178.     * @access   public
  179.     * @return   mixed
  180.     * @param    int     $byte
  181.     */
  182.     function writeByte($byte)
  183.     {
  184.         return $this->notify(__FUNCTION__, $this->write(chr($byte)), $byte);
  185.     }
  186.     
  187.     /**
  188.     * Write data with ending delimter
  189.     * 
  190.     * Write a string terminated by the specified delimiter
  191.     * 
  192.     * @access   public
  193.     * @return   mixed
  194.     * @oaram    string  $data   data to write
  195.     * @param    string  $delim  delimiter to append
  196.     * @param    bool    $escape whether to escape all delimiter characters
  197.     *                           ($delim) conatained in $data
  198.     */
  199.     function writeDelim($data, $delim, $escape = true)
  200.     {
  201.         if ($escape) {
  202.             $data = addCSlashes($data, $delim);
  203.         }
  204.         return $this->notify(__FUNCTION__, 
  205.             $this->write($data . $delim), $data, $delim, $escape);
  206.     }
  207.     
  208.     /**
  209.     * Write a line
  210.     * 
  211.     * Write a line of data terminated with the specified linefeed character.
  212.     * 
  213.     * @access   public
  214.     * @return   mixed
  215.     * @param    string  $line   line of data
  216.     * @param    string  $LF     linefeed character
  217.     * @param    bool    $escape whether to escape all linefeed
  218.     *                           characters ($LF) contained in $line
  219.     */
  220.     function writeLine($line = '', $LF = "\n", $escape = true)
  221.     {
  222.         return $this->notify(__FUNCTION__, 
  223.             $this->writeDelim($line, $LF, $escape), $line, $LF, $escape);
  224.     }
  225.     
  226.     /**
  227.     * Write integer
  228.     * 
  229.     * Write an 32 bit network byte order integer (4 bytes).
  230.     * 
  231.     * @access   public
  232.     * @return   mixed
  233.     * @param    int     $int
  234.     */
  235.     function writeInt($int)
  236.     {
  237.         return $this->notify(__FUNCTION__, 
  238.             $this->write(pack('V', $int)), $int);
  239.     }
  240.     
  241.     /**
  242.     * Write string terminated by NUL
  243.     * 
  244.     * Write a string which will be terminated by a NUL byte.
  245.     * 
  246.     * @access   public
  247.     * @return   mixed
  248.     * @param    string  $string the string to write
  249.     * @param    bool    $escape wheter to escape all NULs in $string
  250.     */
  251.     function writeString($string, $escape = true)
  252.     {
  253.         return $this->notify(__FUNCTION__, 
  254.             $this->writeDelim($string, "\0", $escape), $string, $escape);
  255.     }
  256.     
  257.     /**
  258.     * Read through connection
  259.     *
  260.     * Attempts to read $length length of data through the socket connection.
  261.     * 
  262.     * @access public
  263.     * @return mixed read data or false
  264.     * @param  int   bytes to read
  265.     */
  266.     function read($length = 4096)
  267.     {
  268.         return $this->notify(__FUNCTION__, 
  269.             @socket_read($this->Socket->getResource(), abs($length)), $length);
  270.     }
  271.     
  272.     /**
  273.     * Read char
  274.     * 
  275.     * Reads a single character from the socket resource.
  276.     * 
  277.     * @access   public
  278.     * @return   string
  279.     */
  280.     function readChar()
  281.     {
  282.         return $this->notify(__FUNCTION__, $this->read(1));
  283.     }
  284.     
  285.     /**
  286.     * Read byte
  287.     * 
  288.     * Reads a byte from the socket and returns its integer representation.
  289.     * 
  290.     * @access   public
  291.     * @return   int
  292.     */
  293.     function readByte()
  294.     {
  295.         return $this->notify(__FUNCTION__, ord($this->readChar()));
  296.     }
  297.     
  298.     /**
  299.     * Read until delimiter
  300.     * 
  301.     * Reads from the socket until the first occurence of the delimiter $delim.
  302.     * Be aware that the delimiter is part of the returned string.
  303.     * 
  304.     * @access   public
  305.     * @return   string
  306.     * @param    string  $delim  1 character string
  307.     * @param    string  $escape escape character
  308.     */
  309.     function readDelim($delim, $escape = '\\')
  310.     {
  311.         $escape = $escape{0};
  312.         $delim  = $delim{0};
  313.         $buffer = '';
  314.         $char   = null;
  315.  
  316.         do {
  317.             $last = $char;
  318.             $char = $this->readChar();
  319.             $buffer .= $char;
  320.         } while ($delim !== $char && $last !== $escape);
  321.         
  322.         return $this->notify(__FUNCTION__, $buffer, $delim);
  323.     }
  324.     
  325.     /**
  326.     * Read line
  327.     * 
  328.     * Be aware, that the linefeed character doesn't get truncated.
  329.     * 
  330.     * @access   public
  331.     * @return   string
  332.     * @param    string  $LF
  333.     */
  334.     function readLine($LF = "\n")
  335.     {
  336.         return $this->notify(__FUNCTION__, $this->readDelim($LF));
  337.     }
  338.     
  339.     /**
  340.     * Read integer
  341.     * 
  342.     * Read a 32 bit network byte order integer (4 bytes).
  343.     * 
  344.     * @access   public
  345.     * @return   int
  346.     */
  347.     function readInt()
  348.     {
  349.         return $this->notify(__FUNCTION__, 
  350.             array_shift(unpack('V', $this->read(4))));
  351.     }
  352.     
  353.     /**
  354.     * Read NUL terminated string
  355.     * 
  356.     * Reads a string terminated wit a NUL byte from the socket resource.
  357.     * Be aware, that the NUL byte will *NOT* be truncated.
  358.     * 
  359.     * @access   public
  360.     * @return   string
  361.     */
  362.     function readString()
  363.     {
  364.         return $this->notify(__FUNCTION__, $this->readDelim("\0"));
  365.     }
  366. }
  367. ?>