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 / Creator.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.3 KB  |  229 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: Creator                                  |
  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: Creator.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::Creator
  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 SYSTEM_SOCKET_ROOT . '/Socket.php';
  28.  
  29. /** 
  30. * System_Socket_Creator
  31. *
  32. * @author   Michael Wallner <mike@php.net>
  33. * @version  $Revision: 1.1 $
  34. * @access   public
  35. */
  36. class System_Socket_Creator
  37. {
  38.     /**
  39.     * Create a System_Socket_Connection object
  40.     *
  41.     * @access   public
  42.     * @return   object  System_Socket_Connection or PEAR_Error on failure
  43.     * @param    array   $socketParams   associative array of System_Socket options
  44.     */
  45.     function &createConnection($socketParams = array())
  46.     {
  47.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Connection.php';
  48.  
  49.         $sock = &new System_Socket;
  50.         foreach ($socketParams as $property => $value) {
  51.             $sock->$property = $value;
  52.         }
  53.  
  54.         if ($sock->create() && $sock->connect()) {
  55.             return new System_Socket_Connection($sock);
  56.         }
  57.  
  58.         return System_Socket::lastError();
  59.     }
  60.  
  61.     /**
  62.     * Create a System_Socket_Listener object
  63.     *
  64.     * @access   public
  65.     * @return   object  System_Socket_Listener or PEAR_Error on failure
  66.     * @param    array   $socketParams   associative array of System_Socket options
  67.     */
  68.     function &createListener($socketParams = array())
  69.     {
  70.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Listener.php';
  71.         
  72.         $sock = &new System_Socket;
  73.         foreach ($socketParams as $property => $value) {
  74.             $sock->$property = $value;
  75.         }
  76.         
  77.         if ($sock->create() && $sock->bind() && $sock->listen()) {
  78.             return new System_Socket_Listener($sock);
  79.         }
  80.         
  81.         return System_Socket::lastError();
  82.     }
  83.     
  84.     /** 
  85.     * Create a unix socket listener
  86.     * 
  87.     * @access   public
  88.     * @return   object
  89.     * @param    string  path to unix socket
  90.     */
  91.     function &createUnixListener($path)
  92.     {
  93.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Listener.php';
  94.         
  95.         $sock = &new System_Socket;
  96.         $sock->domain  = AF_UNIX;
  97.         $sock->proto   = SOL_SOCKET;
  98.         $sock->port    = 0;
  99.         $sock->address = $path;
  100.         
  101.         if ($sock->create() && $sock->bind() && $sock->listen()) {
  102.             return new System_Socket_Listener($sock);
  103.         }
  104.         
  105.         return System_Socket::lastError();
  106.     }
  107.     
  108.     /** 
  109.     * Create a unix socket connection
  110.     * 
  111.     * @access   public
  112.     * @return   object
  113.     * @param    string  $path   path to unix socket
  114.     */
  115.     function &createUnixConnection($path)
  116.     {
  117.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Connection.php';
  118.  
  119.         $sock = &new System_Socket;
  120.         $sock->domain  = AF_UNIX;
  121.         $sock->proto   = SOL_SOCKET;
  122.         $sock->port    = 0;
  123.         $sock->address = $path;
  124.  
  125.         if ($sock->create() && $sock->connect()) {
  126.             return new System_Socket_Connection($sock);
  127.         }
  128.  
  129.         return System_Socket::lastError();
  130.     }
  131.     
  132.     /** 
  133.     * Create a TCP listener
  134.     * 
  135.     * @access   public
  136.     * @return   object
  137.     * @param    string  $address
  138.     * @param    int     $port
  139.     */
  140.     function &createTcpListener($address, $port)
  141.     {
  142.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Listener.php';
  143.         
  144.         $sock = &new System_Socket;
  145.         $sock->address = $address;
  146.         $sock->port    = $port;
  147.         
  148.         if ($sock->create() && $sock->bind() && $sock->listen()) {
  149.             return new System_Socket_Listener($sock);
  150.         }
  151.         
  152.         return System_Socket::lastError();
  153.     }
  154.     
  155.     /** 
  156.     * Create a TCP connection
  157.     * 
  158.     * @access   public
  159.     * @return   object
  160.     * @param    string  $address
  161.     * @param    int     $port
  162.     */
  163.     function &createTcpConnection($address, $port)
  164.     {
  165.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Connection.php';
  166.  
  167.         $sock = &new System_Socket;
  168.         $sock->address = $address;
  169.         $sock->port    = $port;
  170.  
  171.         if ($sock->create() && $sock->connect()) {
  172.             return new System_Socket_Connection($sock);
  173.         }
  174.  
  175.         return System_Socket::lastError();
  176.     }
  177.     
  178.     /** 
  179.     * Create an UDP listener
  180.     * 
  181.     * @access   public
  182.     * @return   object
  183.     * @param    string  $address
  184.     * @param    int     $port
  185.     */
  186.     function &createUdpListener($address, $port)
  187.     {
  188.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Listener.php';
  189.         
  190.         $sock = &new System_Socket;
  191.         $sock->proto   = SOL_UDP;
  192.         $sock->type    = SOCK_DGRAM;
  193.         $sock->address = $address;
  194.         $sock->port    = $port;
  195.         
  196.         if ($sock->create() && $sock->bind() && $sock->listen()) {
  197.             return new System_Socket_Listener($sock);
  198.         }
  199.         
  200.         return System_Socket::lastError();
  201.     }
  202.     
  203.     /** 
  204.     * Create an UDP connection
  205.     * 
  206.     * @access   public
  207.     * @return   object
  208.     * @param    string  $address
  209.     * @param    int     $port
  210.     */
  211.     function &createUdpConnection($address, $port)
  212.     {
  213.         require_once SYSTEM_SOCKET_ROOT . '/Socket/Connection.php';
  214.  
  215.         $sock = &new System_Socket;
  216.         $sock->type    = SOCK_DGRAM;
  217.         $sock->proto   = SOL_UDP;
  218.         $sock->address = $address;
  219.         $sock->port    = $port;
  220.  
  221.         if ($sock->create() && $sock->connect()) {
  222.             return new System_Socket_Connection($sock);
  223.         }
  224.  
  225.         return System_Socket::lastError();
  226.     }
  227. }
  228. ?>