Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
authorDan
Wed, 26 Mar 2008 10:24:20 -0400
changeset 12 b3fcc21e557f
parent 11 0faea3a6c881
child 13 b5db2345c397
Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
greyhound.php
webserver.php
--- a/greyhound.php	Wed Mar 26 09:57:42 2008 -0400
+++ b/greyhound.php	Wed Mar 26 10:24:20 2008 -0400
@@ -12,12 +12,18 @@
  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  */
 
-// required for signal handling to work
-declare(ticks=1);
-
-// trap SIGTERM
-pcntl_signal(SIGTERM, 'sigterm');
-pcntl_signal(SIGINT,  'sigterm');
+// Try to trap termination signals to cleanly close the socket when needed
+// AmaroK sends a SIGTERM when it is shut down or the user requests to stop
+// the script
+if ( function_exists('pcntl_signal') )
+{
+  // required for signal handling to work
+  declare(ticks=1);
+  
+  // trap SIGTERM
+  pcntl_signal(SIGTERM, 'sigterm');
+  pcntl_signal(SIGINT,  'sigterm');
+}
 
 $public = true;
 $allowcontrol = true;
--- a/webserver.php	Wed Mar 26 09:57:42 2008 -0400
+++ b/webserver.php	Wed Mar 26 10:24:20 2008 -0400
@@ -137,11 +137,27 @@
     while ( true )
     {
       // wait for connection...
-      $remote = socket_accept($this->sock);
+      // trick from http://us.php.net/manual/en/function.socket-accept.php
+      $remote = false;
+      switch(@socket_select($r = array($this->sock), $w = array($this->sock), $e = array($this->sock), 5)) {
+        case 2:
+          break;
+        case 1:
+          $remote = @socket_accept($this->sock);
+          break;
+        case 0:
+          break;
+      }
+         
+      if ( !$remote )
+      {
+        continue;
+      }
+      
       // read request
       $last_line = '';
       $client_headers = '';
-      while ( $line = socket_read($remote, 1024, PHP_NORMAL_READ) )
+      while ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) )
       {
         $line = str_replace("\r", "", $line);
         if ( empty($line) )
@@ -286,15 +302,15 @@
     $headers = str_replace("\n", "\r\n", $headers);
     $headers = preg_replace("#[\r\n]+$#", '', $headers);
     
-    socket_write($socket, "HTTP/1.1 $http_code $reason_code\r\n");
-    socket_write($socket, "Server: $this->server_string");
-    socket_write($socket, "Connection: close\r\n");
-    socket_write($socket, "Content-Type: $contenttype\r\n");
+    @socket_write($socket, "HTTP/1.1 $http_code $reason_code\r\n");
+    @socket_write($socket, "Server: $this->server_string");
+    @socket_write($socket, "Connection: close\r\n");
+    @socket_write($socket, "Content-Type: $contenttype\r\n");
     if ( !empty($headers) )
     {
-      socket_write($socket, "$headers\r\n");
+      @socket_write($socket, "$headers\r\n");
     }
-    socket_write($socket, "\r\n");
+    @socket_write($socket, "\r\n");
   }
   
   /**
@@ -377,7 +393,7 @@
             $sz = strlen($contents);
             $this->send_client_headers($socket, 200, 'text/html', "Content-length: $sz\r\n");
             
-            socket_write($socket, $contents);
+            @socket_write($socket, $contents);
             
             return true;
           }
@@ -404,7 +420,7 @@
           // send body
           while ( $blk = @fread($fh, 768000) )
           {
-            socket_write($socket, $blk);
+            @socket_write($socket, $blk);
           }
           fclose($fh);
           return true;
@@ -455,7 +471,7 @@
           // send body
           while ( $blk = @fread($fh, 768000) )
           {
-            socket_write($socket, $blk);
+            @socket_write($socket, $blk);
           }
           fclose($fh);
           return true;
@@ -502,7 +518,7 @@
         $this->send_client_headers($socket, $this->response_code, $this->content_type, $headers);
         
         // write body
-        socket_write($socket, $output);
+        @socket_write($socket, $output);
         
         break;
     }
@@ -554,7 +570,7 @@
   </body>
 </html>
 EOF;
-    socket_write($socket, $html);
+    @socket_write($socket, $html);
     @socket_close($socket);
   }