chunked output added
[brisk.git] / web / spush / brisk-spush.phh
1 <?php
2 /*
3  *  brisk - spush/brisk-spush.phh
4  *
5  *  Copyright (C) 2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 $DOCUMENT_ROOT="";
26 $HTTP_HOST="dodo.birds.lan";
27
28 class PageFlush {
29
30   var $socket; // socket handler of page stream
31   var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
32   var $msg;    // place where store failed fwrite data
33   var $msg_sz; // size of content
34
35   function PageFlush($socket, $curtime, $kalive, $header_out, $body)
36   {
37       // $body_sz = mb_strlen($body, "ASCII");
38       // add length to header_out 
39       $hea = headers_render($header_out, 0);
40
41       $this->socket = $socket;
42       $this->kalive = $curtime + $kalive;
43       $this->msg    = $hea.$body;
44       $this->msg_sz = mb_strlen($this->msg, "ASCII");
45   }
46
47   /* return TRUE if is removable from it's list */
48   function try_flush($curtime)
49   {
50       if ($this->kalive < $curtime) {
51           fclose($this->socket);
52           return TRUE;
53       }   
54
55       $wret = @fwrite($this->socket, $this->msg);
56       if ($wret == FALSE && $wret !== FALSE) {
57           printf("PageFlush::try_flush: wret 0 but not FALSE\n");
58       }
59       if ($wret == $this->msg_sz) {
60           fclose($this->socket);
61           return TRUE;
62       }
63       $this->msg_sz -= $wret;
64       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
65
66       return FALSE;
67   }
68
69 }
70
71 ?>