Ejemplo n.º 1
0
 /**
  * Starts a socket server at 127.0.0.1:port
  *
  * startHTTPd converts Jaxl instance into a Jaxl socket server (may or may not be a HTTP server)
  * Same Jaxl socket server instance <b>SHOULD NOT</b> be used for XMPP communications.
  * Instead separate "new Jaxl();" instances should be created for such XMPP communications.
  *
  * @param integer $port Port at which to start the socket server
  * @param integer $maxq JAXLHTTPd socket server max queue
  */
 function startHTTPd($port, $maxq)
 {
     JAXLHTTPd::start(array('port' => $port, 'maxq' => $maxq));
 }
Ejemplo n.º 2
0
 public static function start($options)
 {
     self::reset($options);
     pcntl_signal(SIGTERM, array("JAXLHTTPd", "shutdown"));
     pcntl_signal(SIGINT, array("JAXLHTTPd", "shutdown"));
     $options = getopt("p:b:");
     foreach ($options as $opt => $val) {
         switch ($opt) {
             case 'p':
                 self::$settings['port'] = $val;
                 break;
             case 'b':
                 self::$settings['maxq'] = $val;
             default:
                 break;
         }
     }
     self::$httpd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     socket_set_option(self::$httpd, SOL_SOCKET, SO_REUSEADDR, 1);
     socket_bind(self::$httpd, 0, self::$settings['port']);
     socket_listen(self::$httpd, self::$settings['maxq']);
     self::$id = self::getResourceID(self::$httpd);
     self::$clients = array("0#" . self::$settings['port'] => self::$httpd);
     echo "JAXLHTTPd listening on port " . self::$settings['port'] . PHP_EOL;
     while (true) {
         $read = self::$clients;
         $ns = @socket_select($read, $write = null, $except = null, JAXL_HTTPd_SELECT_TIMEOUT);
         if ($ns) {
             foreach ($read as $read_socket) {
                 $accept_id = self::getResourceID($read_socket);
                 if (self::$id == $accept_id) {
                     $sock = socket_accept($read_socket);
                     socket_getpeername($sock, $ip, $port);
                     self::$clients[$ip . "#" . $port] = $sock;
                     //echo "Accepted new connection from ".$ip."#".$port.PHP_EOL;
                     continue;
                 } else {
                     socket_getpeername($read_socket, $ip, $port);
                     $data = trim(socket_read($read_socket, 1024));
                     if ($data == "") {
                         self::close($ip, $port);
                     } else {
                         //echo "Recv data from ".$ip."#".$port.PHP_EOL;
                         $request = self::parseRequest($data, array('ip' => $ip, 'port' => $port));
                         if ($request['meta']['protocol'] == 'HTTP') {
                             JAXLPlugin::execute('jaxl_httpd_get_http_request', $request);
                         } else {
                             JAXLPlugin::execute('jaxl_httpd_get_sock_request', $request);
                         }
                     }
                 }
             }
         }
         JAXLPlugin::execute('jaxl_httpd_post_read');
     }
 }