From 5886386f05c5a37d8091e14934eb42b8a5c22063 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Mon, 14 Apr 2014 13:25:17 +0200 Subject: [PATCH] non blocking test added --- test/nonblocking.php | 148 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100755 test/nonblocking.php diff --git a/test/nonblocking.php b/test/nonblocking.php new file mode 100755 index 0000000..4125c86 --- /dev/null +++ b/test/nonblocking.php @@ -0,0 +1,148 @@ +#!/usr/bin/php + $value) { + $ret .= $sep . $key . '=' . urlencode($value); + $sep = "&"; + } + return $ret; +} + +function cmd_deserialize($cmd) +{ + $ret = array(); + $a = explode('&', $cmd); + $i = 0; + while ($i < count($a)) { + $b = split('=', $a[$i]); + $ret[urldecode($b[0])] = urldecode($b[1]); + $i++; + } + + return $ret; +} + + +function sock_mgmt($sock) +{ + $buf_all = ""; + $output = "bib bob bub beb"; + + $st = 1; + while(1) { + if ($st == 1) { + printf("\n LOOP SOCK BEGIN\n"); + $buf = fread($sock, 4096); + if ($buf == FALSE) { + printf(" BUF == FALSE\n"); + } + if ($buf === FALSE) { + printf(" BUF === FALSE\n"); + } + if (!($buf == FALSE) && !($buf === FALSE)) { + if (strlen($buf) > 0) { + printf(" REC: [%s]\n", $buf); + $buf_all .= $buf; + if (substr(trim($buf_all), -13) == "&the_end=true") { + $st = 2; + $ct = 0; + } + } + else { + printf(" LEN(BUF) == 0\n"); + } + } + else { + printf(" FEOF RETURN %d\n", feof($sock)); + if (feof($sock)) { + fclose($sock); + return(TRUE); + } + } + } + else if ($st == 2) { + fwrite($sock, substr($output, $ct, 9), 9); + fflush($sock); + $ct += 9; + } + usleep(500000); + } +} + +function main() +{ + GLOBAL $blocking_mode; + + // $cmd1 = array ("pippo" => "pl'&uto", "minnie" => 'mic"=key', "the_end" => "true" ); + $cmd1 = array ("cmd" => "userauth", "login" => 'mop', 'private' => 'it_must_be_correct', + 'the_end' => 'true' ); + + $ret1 = cmd_serialize($cmd1); + $ret2 = cmd_deserialize($ret1); + + print_r($cmd1); + printf("SER: %s\n", $ret1); + printf("DESER\n"); + print_r($ret2); + + $file_socket = "/tmp/brisk.sock"; + $unix_socket = "unix://$file_socket"; + + if (file_exists($file_socket)) { + unlink($file_socket); + } + + $old_umask = umask(0); + if (($list = stream_socket_server($unix_socket, $err, $errs)) === FALSE) { + return (FALSE); + } + umask($old_umask); + printf("Blocking mode (listen): %d\n", $blocking_mode); + stream_set_blocking($list, $blocking_mode); // Set the stream to non-blocking + + while(1) { + printf("\nLOOP BEGIN\n"); + if (($new_unix = stream_socket_accept($list)) == FALSE) { + printf("SOCKET_ACCEPT FAILED\n"); + usleep(500000); + continue; + } + else { + stream_set_blocking($new_unix, $blocking_mode); // Set the stream to non-blocking + sock_mgmt($new_unix); + } + usleep(500000); + } +} + +main(); +?> \ No newline at end of file -- 2.17.1