modification to fix explorer bug.
[brisk.git] / web / Obj / sac-a-push.phh
index 746f7ec..fa78539 100644 (file)
@@ -105,9 +105,13 @@ function gpcs_var($name, $get, $post, $cookie)
 
 function headers_render($header, $len)
 {
-    
     $s = "";
-    $s .= "HTTP/1.1 200 OK\r\n";
+    if (isset($header['Location'])) {
+        return sprintf("HTTP/1.1 302 OK\r\nLocation: %s\r\n\r\n", $header['Location']);
+    }
+    else {
+        $s .= "HTTP/1.1 200 OK\r\n";
+    }
     if (!isset($header['Date']))
         $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
     if (!isset($header['Connection']))
@@ -258,6 +262,23 @@ class Sac_a_push {
         array_push($this->pages_flush, $pgflush);
     }
 
+    function garbage_manager($force)
+    {
+        $this->app->garbage_manager($force);
+
+        foreach ($this->socks as $k => $sock) {
+            if ($this->s2u[intval($sock)]->sess == '') {
+                if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
+                    $this->s2u[intval($sock)]->rd_socket_set(NULL);
+                }
+                unset($this->socks[intval($sock)]);
+                unset($this->s2u[intval($sock)]);
+                fclose($sock);
+                printf("CLOSE ON GARBAGE MANAGER\n");
+            }
+        }
+    }
+
     function run()
     {
         if ($this->main_loop) {
@@ -284,7 +305,7 @@ class Sac_a_push {
             }
             $write  = NULL;
             $except = NULL;
-            $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
+            $num_changed_sockets = stream_select($read, $write, $except, 0, 100000);
         
             if ($num_changed_sockets == 0) {
                 printf(" no data in 5 secs ");
@@ -346,7 +367,6 @@ class Sac_a_push {
                     else {
                         if (($buf = fread($sock, 512)) === FALSE) {
                             printf("error read\n");
-                            exit(123);
                         }
                         else if (strlen($buf) === 0) {
                             if ($sock === $this->list) {
@@ -371,7 +391,7 @@ class Sac_a_push {
                             }
                         }
                         else {
-                            if ($debug > 1) {
+                            if ($this->debug > 1) {
                                 print_r($read);
                             }
                             if ($sock === $this->list) {
@@ -388,10 +408,8 @@ class Sac_a_push {
                     }
                 }
             }
-            
-
-            $this->app->garbage_manager(FALSE);
 
+            $this->garbage_manager(FALSE);
 
             /* manage unfinished pages */
             foreach ($this->pages_flush as $k => $pgflush) {
@@ -400,17 +418,35 @@ class Sac_a_push {
                 }
             }
             
+            /*
+               $response:                        raw stream data not sent
+               $content:                         html consistent data (<script bla bla>)
+               $user->stream_keepalive($w_ping)  ping srv->cli OR srv->cli + cli->srv if $w_ping == TRUE
+            */
+
             /* manage open streaming */
             foreach ($this->socks as $k => $sock) {
                 if (isset($this->s2u[intval($sock)])) {
                     $user = $this->s2u[intval($sock)];
                     $response = $user->rd_cache_get();
+                    $do_ping = FALSE;
+                    if (($this->curtime - $user->lacc) > (EXPIRE_TIME_RD / 3)) {
+                        $do_ping = TRUE;
+                    }
+                    else {
+                        $user->ping_req = FALSE;
+                    }
+
                     if ($response == "") {
                         $content = "";
                         $user->stream_main($content, $get, $post, $cookie);
-                        
-                        if ($content == "" && $user->rd_kalive_is_expired($this->curtime)) {
-                            $content = $user->stream_keepalive();
+                        printf("[%s] [%d] [%d]\n", $user->name, $user->lacc, $this->curtime);
+                        if ($do_ping && $user->ping_req == FALSE) {
+                            $content .= $user->stream_keepalive(TRUE);
+                            $user->ping_req = TRUE;
+                        }
+                        else if ($content == "" && $user->rd_kalive_is_expired($this->curtime)) {
+                            $content = $user->stream_keepalive(FALSE);
                         }
                         if ($content != "") {
                             $response = chunked_content($content);
@@ -418,7 +454,8 @@ class Sac_a_push {
                     }
                     
                     if ($response != "") {
-                        echo "SPIA: [".substr($response, 0, 60)."...]\n";
+                        // echo "SPIA: [".substr($response, 0, 60)."...]\n";
+                        echo "SPIA: [".$response."]\n";
                         $response_l = mb_strlen($response, "ASCII");
                         $wret = @fwrite($sock, $response);
                         if ($wret < $response_l) {
@@ -449,4 +486,4 @@ class Sac_a_push {
     }  // function run(...
 }
 
-?>
\ No newline at end of file
+?>