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.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  8.5 KB  |  370 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket                                             |
  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: Socket.php,v 1.2 2004/04/29 16:07:03 mike Exp $
  15.  
  16. /**
  17. * System::Socket
  18. * @author       Michael Wallner <mike@php.net>
  19. * @package      System_Socket
  20. * @category     System
  21. */
  22.  
  23. if (!defined('SYSTEM_SOCKET_ROOT')) {
  24.     define('SYSTEM_SOCKET_ROOT', dirname(__FILE__));
  25. }
  26.  
  27. require_once 'PEAR.php';
  28.  
  29. /** 
  30. * System_Socket
  31. *
  32. * The System_Socket class represents a socket resource and aims to provide a
  33. * thight ad robust API for handling those.  Beside that it implements a creator
  34. * for System_Socket_Connection and System_Socket_Listener.  You'd most probably
  35. * never use this class or an object of it directly, but the creator pattern.
  36. * However this doesn't mean that you do not need to know about its socket 
  37. * configuration options.
  38. * Example: Simple HTTP request to localhost
  39. * <code>
  40. *   require_once 'System/Socket/Creator.php';
  41. *   $conn = &System_Socket_Creator::createConnection(
  42. *       array('address' => '127.0.0.1')
  43. *   );
  44. *   if (PEAR::isError($conn)) {
  45. *       die($conn->getMessage());
  46. *   }
  47. *   $conn->writeLine('GET / HTTP/1.0');
  48. *   $conn->writeLine('Connection: close');
  49. *   $conn->writeLine();
  50. *   while ($data = $conn->read()) {
  51. *       echo $data;
  52. *   }
  53. * </code>
  54. * @author   Michael Wallner <mike@php.net>
  55. * @version  $Revision: 1.2 $
  56. * @access   public
  57. */
  58. class System_Socket
  59. {
  60.     /**
  61.     * Socket resource
  62.     * @access private
  63.     */
  64.     var $_rsrc = null;
  65.     
  66.     /**
  67.     * Blocking mode
  68.     * @access private
  69.     */
  70.     var $_blocking = true;
  71.     
  72.     /**
  73.     * Socket domain/family
  74.     * 
  75.     * AF_INET | AF_UNIX
  76.     * 
  77.     * @access   public
  78.     * @var      int
  79.     */
  80.     var $domain = AF_INET;
  81.     
  82.     /**
  83.     * Socket type
  84.     * 
  85.     * SOCK_DGRAM | SOCK_RAW | SOCK_RDM | SOCK_SEQPACKET | SOCK_STREAM
  86.     * 
  87.     * @access   public
  88.     * @var      int
  89.     */
  90.     var $type = SOCK_STREAM;
  91.     
  92.     /**
  93.     * Socket layer
  94.     * 
  95.     * SOL_SOCKET | SOL_TCP | SOL_UDP
  96.     * 
  97.     * @access   public
  98.     * @var      int
  99.     */
  100.     var $proto = SOL_TCP;
  101.     
  102.     /**
  103.     * Network address (IP) or file path for unix sockets
  104.     * 
  105.     * @access   public
  106.     * @var      string
  107.     */
  108.     var $address = '127.0.0.1';
  109.     
  110.     /**
  111.     * Port
  112.     * 
  113.     * @access   public
  114.     * @var      int
  115.     */
  116.     var $port = 80;
  117.     
  118.     /**
  119.     * Queuelength
  120.     * 
  121.     * @access   public
  122.     * @var      int
  123.     */
  124.     var $queueLength = SOMAXCONN;
  125.     
  126.     /**
  127.     * Constructor
  128.     * 
  129.     * @access   public
  130.     * @return   object  System_Socket
  131.     * @param    int     $resource       socket resource handle
  132.     */
  133.     function System_Socket($resource = null)
  134.     {
  135.         System_Socket::__construct($resource);
  136.     }
  137.     
  138.     /**
  139.     * ZE2 Constructor
  140.     * @ignore
  141.     */
  142.     function __construct($resource = null)
  143.     {
  144.         if (System_Socket::isResource($resource)) {
  145.             $this->_rsrc = $resource;
  146.             socket_getSockname($resource, $this->address, $this->port);
  147.         }
  148.     }
  149.     
  150.     /**
  151.     * Retrieve last socket error
  152.     * 
  153.     * See include/WinSock.h (Win32) and include/sys/errno.h (Un*x) for the 
  154.     * values of the error codes.  See also (win32|unix)_socket_constants.h 
  155.     * located in ext/sockets for all available SOCKET_E* constants.
  156.     *
  157.     * @static
  158.     * @access   public
  159.     * @return   object  PEAR_Error
  160.     */
  161.     function &lastError()
  162.     {
  163.         $error_code = socket_last_error();
  164.         return PEAR::raiseError(socket_strerror($error_code), $error_code);
  165.     }
  166.     
  167.     /**
  168.     * Get socket resource handle
  169.     *
  170.     * @access public
  171.     * @return int        resource handle
  172.     */
  173.     function getResource()
  174.     {
  175.         return $this->_rsrc;
  176.     }
  177.  
  178.     /**
  179.     * Create a socket resource
  180.     *
  181.     * Create the socket resource with the objects properties as parameters.
  182.     *
  183.     * @access public
  184.     * @return bool
  185.     */
  186.     function create()
  187.     {
  188.         if (!System_Socket::isResource($this->_rsrc)) {
  189.             if (!System_Socket::isResource(   
  190.                 $this->_rsrc = @socket_create(
  191.                     $this->domain, $this->type, $this->proto))) {
  192.                 //
  193.                 return false;
  194.             }
  195.         }
  196.         return true;
  197.     }
  198.     
  199.     /**
  200.     * Bind the socket resource
  201.     *
  202.     * Bind the socket resource to System_Socket::address at System_Socket::port.
  203.     *
  204.     * @access public
  205.     * @return bool
  206.     */
  207.     function bind()
  208.     {
  209.         return (bool) @socket_bind($this->_rsrc, $this->address, $this->port);
  210.     }
  211.     
  212.     /**
  213.     * Close the socket
  214.     *
  215.     * Close the socket resource handle.
  216.     *
  217.     * @access public
  218.     * @return void
  219.     */
  220.     function close()
  221.     {
  222.         @socket_close($this->_rsrc);
  223.         $this->_rsrc = null;
  224.     }
  225.     
  226.     /**
  227.     * @access public
  228.     */
  229.     function listen()
  230.     {
  231.         return @socket_listen($this->_rsrc, $this->queueLength);
  232.     }
  233.     
  234.     /**
  235.     * Accept a socket connection
  236.     *
  237.     * Accept a socket connection from a prior created socket resource handle.
  238.     *
  239.     * @access public
  240.     * @return mixed     new socket resource handle or false
  241.     */
  242.     function accept()
  243.     {
  244.         return @socket_accept($this->_rsrc);
  245.     }
  246.     
  247.     /**
  248.     * Connect the socket
  249.     *
  250.     * Connect the socket resource handle to System_Socket::address 
  251.     * at System_Socket::port.
  252.     *
  253.     * @access public
  254.     * @return bool
  255.     */
  256.     function connect()
  257.     {
  258.         return (bool) @socket_connect($this->_rsrc, $this->address, $this->port);
  259.     }
  260.     
  261.     /**
  262.     * Set socket option
  263.     *
  264.     * Set a socket option after you have created a socket resource handle.
  265.     *
  266.     * @access public
  267.     * @return bool
  268.     * @param  string $option
  269.     * @param  mixed  $value
  270.     * @param  int    $level
  271.     */
  272.     function setOption($option, $value, $level = SOL_SOCKET)
  273.     {
  274.         return socket_set_option($this->_rsrc, $level, $option, $value);
  275.     }
  276.     
  277.     /**
  278.     * Get socket option
  279.     *
  280.     * Retrieve a socket option after you have created the socket resource handle.
  281.     * 
  282.     * @access public
  283.     * @return mixed
  284.     * @param  string  $option
  285.     * @param  int     $level
  286.     */
  287.     function getOption($option, $level = SOL_SOCKET)
  288.     {
  289.         return socket_get_option($this->_rsrc, $level, $option);
  290.     }
  291.     
  292.     /**
  293.     * Get socket status
  294.     * 
  295.     * @access   public
  296.     * @return   mixed
  297.     * @param    string  $part
  298.     */
  299.     function getStatus($part = null)
  300.     {
  301.         $status = socket_get_status($this->_rsrc);
  302.         return isset($part) ? @$status[$part] : $status;
  303.     }
  304.     
  305.     /**
  306.     * Set socket blocking
  307.     * 
  308.     * @access   public
  309.     * @return   bool
  310.     * @param    bool    $blocking
  311.     */
  312.     function setBlocking($blocking)
  313.     {
  314.         if ($blocking) {
  315.             $rs = socket_set_block($this->_rsrc);
  316.             $this->_blocking = $rs;
  317.         } else {
  318.             $rs = socket_set_nonblock($this->_rsrc);
  319.             $this->_blocking = !$rs;
  320.         }
  321.         return $rs;
  322.     }
  323.     
  324.     /**
  325.     * Get socket blocking
  326.     * 
  327.     * @access public
  328.     * @return bool
  329.     */
  330.     function getBlocking()
  331.     {
  332.         return $this->_blocking;
  333.     }
  334.     
  335.     /**
  336.     * Check if a resource is still valid
  337.     * 
  338.     * Due to a nasty bug in PHP < 4.3.6 and 5.0.0 is_resource() would return
  339.     * true for a already closed socket, too.  So we check the resource type
  340.     * additionally to be really sure we have still a valid socket resource.
  341.     *
  342.     * @static
  343.     * @access   public
  344.     * @return   bool
  345.     * @param    resource    $rsrc
  346.     */
  347.     function isResource($rsrc)
  348.     {
  349.         return (is_resource($rsrc) && get_resource_type($rsrc) == 'Socket');
  350.     }
  351.     
  352.     /**
  353.     * Check if the we have still a valid socket resource
  354.     *
  355.     * @see      System_Socket::isResource()
  356.     * @access   public
  357.     * @return   bool
  358.     */
  359.     function hasResource()
  360.     {
  361.         return System_Socket::isResource($this->_rsrc);
  362.     }
  363. }
  364. ?>