centralized get curtime var
[brisk.git] / web / spush / brisk-spush.php
index 3750ba9..3e3f601 100755 (executable)
  * Suite 330, Boston, MA 02111-1307, USA.
  *
  * TODO
- *   problema con getpeer (HOSTADDR)
+ *   index_wr.php::chat
+ *   index_wr.php::reload
+ *   index_wr.php::exit
+ *
  *   setcookie (for tables only)
  *   keepalive
  *   chunked 
- *   index_rd.php porting
- *   index.php auth part
- *   generic var management from internet
+ *   BUG - after restart index_rd.php receive from prev clients a lot of req
+ *   DONE/FROZEN - problema con getpeer (HOSTADDR)
+ *
+ *   DONE - index_rd.php porting
+ *   DONE - generic var management from internet
+ *   DONE - index.php auth part
  */
 
 $G_base = "../";
@@ -216,7 +222,9 @@ function main()
     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
 
     while ($main_loop) {
-        echo "IN LOOP\n";
+        $curtime = time();
+        printf("IN LOOP: Current opened: %d\n", count($socks));
+
         /* Prepare the read array */
         if ($shutdown) 
             $read   = array_merge(array("$in" => $in), $socks);
@@ -229,7 +237,7 @@ function main()
         }
         $write  = NULL;
         $except = NULL;
-        $num_changed_sockets = stream_select($read, $write, $except, 5);
+        $num_changed_sockets = stream_select($read, $write, $except, 1); // 0, 250000);
         
         if ($num_changed_sockets === FALSE) {
             printf("No data in 5 secs");
@@ -262,12 +270,14 @@ function main()
                         printf("COOKIE:\n");
                         print_r($cookie);
 
+                        $addr = stream_socket_get_name($new_socket, TRUE);
+
                         switch ($path) {
                         case SITE_PREFIX:
                         case SITE_PREFIX."index.php":
                             $header_out = array();
                             ob_start();
-                            index_main($room, $header_out, $get, $post, $cookie);
+                            index_main($room, $header_out, $addr, $get, $post, $cookie);
                             $content = ob_get_contents();
                             ob_end_clean();
                             // printf("OUT: [%s]\n", $G_content);
@@ -275,10 +285,10 @@ function main()
                             fclose($new_socket);
                             break;
                         case SITE_PREFIX."index_wr.php":
-                            $G_headers = "";
+                            $header_out = array();
                             $addr = "";
-                            $ret = socket_getpeername($new_socket, $addr);
-                            // printf("RET: %d\n", $ret);
+                            // $ret = socket_getpeername($new_socket, $addr);
+                            printf("RET: %s\n", $addr);
                             // exit(123);
                             ob_start();
                             index_wr_main($room, $addr, $get, $post, $cookie);
@@ -409,19 +419,35 @@ function main()
 
 
 
-
-
         foreach ($socks as $k => $sock) {
             if (isset($s2u[intval($sock)])) {
                 $body = "";
                 
 
-                $header_out = array();
                 $body = "";
-                index_rd_ifra_main($room, $room->user[$s2u[intval($sock)]], $body);
-                echo "SPIA: [".substr($body, 0, 60)."...]\n";
-                fwrite($sock, headers_render($header_out).$body);
-                fflush($sock);
+                $user = $room->user[$s2u[intval($sock)]];
+                index_rd_ifra_main($room, $user, $body);
+                if ($body == "" && $user->rd_tout_is_expired($curtime)) {
+                    $body = index_rd_ifra_keepalive($user);
+                }
+
+                if ($body != "") {
+                    echo "SPIA: [".substr($body, 0, 60)."...]\n";
+                    fwrite($sock, $body);
+                    fflush($sock);
+                    $user->rd_tout_reset($curtime);
+                }
+
+                // close socket after a while to prevent client memory consumption
+                if ($user->rd_endtime_is_expired($curtime)) {
+                    // $user_a[$s2u[intval($sock)]]->disable();
+                    if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
+                        $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
+                    }
+                    unset($socks[intval($sock)]);
+                    unset($s2u[intval($sock)]);
+                    fclose($sock);
+                }
             }
         }
     }