4 * brisk - test/nonblocking.php
6 * Copyright (C) 2014 Matteo Nastasi
7 * mailto: nastasi@alternativeoutput.it
8 * matteo.nastasi@milug.org
9 * web: http://www.alternativeoutput.it
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details. You should have received a
20 * copy of the GNU General Public License along with this program; if
21 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
22 * Suite 330, Boston, MA 02111-1307, USA.
26 $blocking_mode = 0; // 0 is non-blocking
28 function cmd_serialize($attrs)
33 foreach ($attrs as $key => $value) {
34 $ret .= $sep . $key . '=' . urlencode($value);
40 function cmd_deserialize($cmd)
43 $a = explode('&', $cmd);
45 while ($i < count($a)) {
46 $b = split('=', $a[$i]);
47 $ret[urldecode($b[0])] = urldecode($b[1]);
55 function sock_mgmt($sock)
58 $output = "bib bob bub beb";
63 printf("\n LOOP SOCK BEGIN\n");
64 $buf = fread($sock, 4096);
66 printf(" BUF == FALSE\n");
69 printf(" BUF === FALSE\n");
71 if (!($buf == FALSE) && !($buf === FALSE)) {
72 if (strlen($buf) > 0) {
73 printf(" REC: [%s]\n", $buf);
75 if (substr(trim($buf_all), -13) == "&the_end=true") {
81 printf(" LEN(BUF) == 0\n");
85 printf(" FEOF RETURN %d\n", feof($sock));
93 fwrite($sock, substr($output, $ct, 9), 9);
103 GLOBAL $blocking_mode;
105 // $cmd1 = array ("pippo" => "pl'&uto", "minnie" => 'mic"=key', "the_end" => "true" );
106 $cmd1 = array ("cmd" => "userauth", "login" => 'mop', 'private' => 'it_must_be_correct',
107 'the_end' => 'true' );
109 $ret1 = cmd_serialize($cmd1);
110 $ret2 = cmd_deserialize($ret1);
113 printf("SER: %s\n", $ret1);
117 $file_socket = "/tmp/brisk.sock";
118 $unix_socket = "unix://$file_socket";
120 if (file_exists($file_socket)) {
121 unlink($file_socket);
124 $old_umask = umask(0);
125 if (($list = stream_socket_server($unix_socket, $err, $errs)) === FALSE) {
129 printf("Blocking mode (listen): %d\n", $blocking_mode);
130 stream_set_blocking($list, $blocking_mode); // Set the stream to non-blocking
133 printf("\nLOOP BEGIN\n");
134 if (($new_unix = stream_socket_accept($list)) == FALSE) {
135 printf("SOCKET_ACCEPT FAILED\n");
140 stream_set_blocking($new_unix, $blocking_mode); // Set the stream to non-blocking
141 sock_mgmt($new_unix);