From 525cfe7d868bfaefdde9f769f52a50bcb6f1e9e9 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Wed, 18 Jul 2012 07:21:12 +0200 Subject: [PATCH] add index_rd_ifra user data server side. rd keepalive and endtime management --- web/Obj/brisk.phh | 53 +++++++++++++++++++++++++++++++-------- web/index_rd_ifra.php | 13 +++++++++- web/index_wr.php | 6 ++--- web/spush/brisk-spush.php | 32 +++++++++++++++++------ 4 files changed, 82 insertions(+), 22 deletions(-) diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh index 231788e..95ec8ab 100644 --- a/web/Obj/brisk.phh +++ b/web/Obj/brisk.phh @@ -690,6 +690,8 @@ define('USER_FLAG_TY_SUPER', 0x020000); // done define('USER_FLAG_TY_SUSPEND', 0x400000); // done define('USER_FLAG_TY_DISABLE', 0x800000); // done +define('RD_STREAM_TIMEOUT', 4); + class User { var $idx; // index in the users array when you are in game var $idx_orig; // index in the users array when you aren't in game @@ -712,6 +714,8 @@ class User { var $rd_step; // actual step of push stream var $rd_from; // referer var $rd_scristp; // current script step (for each session) + var $rd_tout; // if no message are sent after RD_STREAM_TIMEOUT secs we send a keepalive from server + var $comm; // commands array // var $asta_card; // // var $asta_pnt; // @@ -763,6 +767,7 @@ class User { $thiz->rd_step = -1; $thiz->rd_from = ""; $thiz->rd_scristp = -1; + $thiz->rd_tout = -1; $thiz->asta_card = -2; $thiz->asta_pnt = -1; @@ -896,6 +901,17 @@ class User { return ($thiz); } + function rd_data_set($curtime, $stat, $subst, $step, $from) + { + $this->rd_endtime = $curtime + STREAM_TIMEOUT; + $this->rd_stat = $stat; + $this->rd_subst = $subst; + $this->rd_step = $step; + $this->rd_from = $from; + $this->rd_scristp = 0; + $this->rd_tout = $curtime + RD_STREAM_TIMEOUT; + } + function rd_socket_get() { return ($this->rd_socket); } @@ -904,6 +920,33 @@ class User { $this->rd_socket = $sock; } + function rd_tout_get() + { + return ($this->rd_tout); + } + + function rd_tout_set($tm) + { + $this->rd_tout = $tm; + } + + function rd_tout_is_expired($tm) + { + // printf("rd_tout %d tm %d\n", $this->rd_tout, $tm); + return ($this->rd_tout < $tm); + } + + function rd_endtime_is_expired($tm) + { + // printf("rd_endtime %d tm %d\n", $this->rd_tout, $tm); + return ($this->rd_endtime < $tm); + } + + function rd_tout_reset($tm) + { + $this->rd_tout = $tm + RD_STREAM_TIMEOUT; + } + function idx_get() { return ($this->idx); } @@ -942,16 +985,6 @@ class User { return TRUE; } - function rd_data_set($endtime, $stat, $subst, $step, $from) - { - $this->rd_endtime = $endtime; - $this->rd_stat = $stat; - $this->rd_subst = $subst; - $this->rd_step = $step; - $this->rd_from = $from; - $this->rd_scristp = 0; - } - function save_step() { diff --git a/web/index_rd_ifra.php b/web/index_rd_ifra.php index 7d4f493..2f2d393 100644 --- a/web/index_rd_ifra.php +++ b/web/index_rd_ifra.php @@ -505,6 +505,8 @@ function index_rd_ifra_init(&$room, &$user, &$header_out, &$body, $get, $post, $ { GLOBAL $G_four_rnd_string; + $curtime = time(); + $is_page_streaming = FALSE; // (webservers_exceeded() || stristr($HTTP_USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0)") || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE); $header_out['Cache-Control'] = 'no-cache, must-revalidate'; // HTTP/1.1 @@ -522,7 +524,7 @@ function index_rd_ifra_init(&$room, &$user, &$header_out, &$body, $get, $post, $ if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE) unset($step); - $user->rd_data_set(time() + STREAM_TIMEOUT, $stat, $subst, $step, $from); + $user->rd_data_set($curtime, $stat, $subst, $step, $from); $body .= sprintf(" @@ -614,4 +616,13 @@ push(\"%s\"); return TRUE; } + +function index_rd_ifra_keepalive(&$user) +{ + return (sprintf("", $user->rd_scristp++)); + +} ?> diff --git a/web/index_wr.php b/web/index_wr.php index 8b23bd9..aef7392 100644 --- a/web/index_wr.php +++ b/web/index_wr.php @@ -96,11 +96,11 @@ function index_wr_main(&$room, $remote_addr, $get, $post, $cookie) echo "Debugging time!"; return (FALSE); } - + /* * MAIN */ - + /* if the IP is banned, exit without do nothing */ if (array_search($remote_addr, $G_black_list) !== FALSE) { sleep(5); @@ -117,7 +117,7 @@ function index_wr_main(&$room, $remote_addr, $get, $post, $cookie) if (($user = $room->get_user($sess, &$idx)) == FALSE) { $argz = explode('|', xcapemesg($mesg)); - + if ($argz[0] == 'getchallenge') { if (isset($get['cli_name'])) $cli_name = $get['cli_name']; diff --git a/web/spush/brisk-spush.php b/web/spush/brisk-spush.php index 3750ba9..1f3d7e4 100755 --- a/web/spush/brisk-spush.php +++ b/web/spush/brisk-spush.php @@ -229,7 +229,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"); @@ -409,19 +409,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); + } } } } -- 2.17.1