function MCVE_uwait($usec) { return M_uwait($usec); }
function M_Monitor_read(&$conn, $timeout_ms = -1) { if (!$conn['fd']) { return false; } M_socket_set_timeout($conn, $timeout_ms); /* NOTE: stream_set_timeout() doesn't appear to actually work on SSL, * lots of bugs reported on this */ if ($conn['method'] == M_CONN_SSL && $timeout_ms != -1) { stream_set_blocking($conn['fd'], FALSE); } /* Read Data */ $data_read = false; while (1) { $buf = fread($conn['fd'], 8192); $info = stream_get_meta_data($conn['fd']); if ($buf === false || $info['eof']) { fclose($conn['fd']); $conn['fd'] = false; $conn['conn_error'] = "fread failure"; return false; } if ($info['timed_out'] || strlen($buf) == 0) { /* No data, read. We're non-blocking, remember? */ break; } $conn['readbuf'] .= $buf; $data_read = true; /* Only loop if buffer was full on last read */ if (strlen($buf) < 8192) { break; } /* Make timeout 0 (or non-blocking for SSL) incase there are no more bytes */ M_socket_set_timeout($conn, 0); if ($conn['method'] == M_CONN_SSL) { stream_set_blocking($conn['fd'], FALSE); } } if ($conn['method'] == M_CONN_SSL) { stream_set_blocking($conn['fd'], TRUE); /* With SSL, let's not loop too fast since we're using * non-blocking reads */ if (!$data_read && $timeout_ms != 0) { M_uwait(20000); } } return true; }